mcp: add ListResourcesHandler for dynamic resources/list
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
resourceswhen 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/listin middleware. - Preserves existing static
AddResourcebehavior 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
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.
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.
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!
Closing in favor of #1030, which supersedes this PR — same changes, rebased onto the latest main. Please review #1030 instead.