go-sdk icon indicating copy to clipboard operation
go-sdk copied to clipboard

mcp: add ListResourcesHandler for dynamic resources/list

Open piyushbag opened this issue 5 months ago • 1 comments

Problem

resources/list is backed only by a static in-memory set populated via AddResource. Gateway servers that proxy upstream catalogs and template-served skill assets cannot enumerate resources on demand: reads work via ResourceHandler, but listing does not.

Approach

Add ListResourcesHandler on ServerOptions, analogous to ResourceHandler for reads:

  • Handler only (no static resources): client pagination cursors pass through unchanged. Suitable for gateway/proxy servers.
  • Handler + static resources: SDK paginates registered resources first, then invokes the handler for subsequent pages using an internal composite cursor.
  • Capability: advertise resources when the handler is set, even if no resources were added statically.

resources/directory/read backing is intentionally deferred until custom-method support (#956) lands.

Why this helps

  • First-class, typed registration instead of intercepting resources/list in middleware.
  • Preserves existing static AddResource behavior when no handler is configured.
  • Composes static and dynamic catalogs for template-backed servers that also register fixed entries.

Test plan

  • [x] go test ./mcp/ -run TestListResourcesHandler
  • [x] go test ./...
  • [x] go test -race ./mcp/ -run TestListResourcesHandler

Fixes #998

piyushbag avatar Jun 10 '26 04:06 piyushbag

Local verification on fix-998-list-resources-handler (855cf72):

go test ./... -count=1
ok  github.com/modelcontextprotocol/go-sdk/mcp
ok  github.com/modelcontextprotocol/go-sdk/oauthex

go test -race ./mcp/... -run TestListResourcesHandler -count=1
ok  github.com/modelcontextprotocol/go-sdk/mcp

I kept resources/directory/read out of scope per the PR note until custom methods (#956) land. Open to splitting static+dynamic pagination into a follow-up if maintainers want a smaller first merge.

piyushbag avatar Jun 11 '26 02:06 piyushbag

Rebased onto current main (includes #1000) and force-pushed 6eb52d4.

Local gate:

go test ./... -count=1
go test -race ./mcp/... -run TestListResourcesHandler -count=1
ok  github.com/modelcontextprotocol/go-sdk/mcp

GitHub CI on this push: lint, test 1.25/1.26, race, client/server conformance, CodeQL — all green.

Scope unchanged: handler-only pagination with cursor passthrough for gateway/proxy cases; resources/directory/read still deferred until #956. Open to splitting static+dynamic pagination if maintainers want a smaller first merge.

piyushbag avatar Jun 16 '26 10:06 piyushbag

Friendly ping @findleyr — I've rebased this onto the latest main (resolved a conflict in mcp/server.go so resources/list results still get setDefaultCacheableValues()). Local go test ./..., go test -race ./mcp/..., go vet ./..., and staticcheck ./... are all green. Would appreciate a review when you have a moment. Thanks!

piyushbag avatar Jun 27 '26 17:06 piyushbag

Closing in favor of #1030, which supersedes this PR — same changes, rebased onto the latest main. Please review #1030 instead.

piyushbag avatar Jun 27 '26 17:06 piyushbag