Fix: Return empty Arrays for List methods when no handlers registered (#929)#986
Closed
alantriesagain wants to merge 1 commit intomodelcontextprotocol:mainfrom
Closed
Conversation
88f9723 to
3cf1c07
Compare
46181b8 to
2732797
Compare
2732797 to
4f6eced
Compare
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
4f6eced to
d4713dc
Compare
Author
|
@felixweinberger just saw that this PR needed some updates, pushed a rebase. fixes #929 |
d4713dc to
a48c5cd
Compare
a48c5cd to
9698d16
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR resolves issue #929 by implementing a default fallback handler that automatically returns empty arrays for standard MCP list methods when no specific handlers are registered, preventing "Method not found" errors.
Fixes #929
Problem
When a server advertises a capability (prompts, resources, tools) but doesn't register handlers for the corresponding list methods, clients receive
-32601: Method not founderrors instead of empty arrays.Reproduction scenario:
This affects all four list methods:
prompts/list→ should return{ prompts: [] }resources/list→ should return{ resources: [] }resources/templates/list→ should return{ resourceTemplates: [] }tools/list→ should return{ tools: [] }Root cause: The protocol layer had no default handling for list methods, so servers with capabilities but no registered items would fail with "Method not found" instead of gracefully returning empty collections.
Solution
Added a
_defaultFallbackRequestHandlermethod to the Protocol class that:fallbackRequestHandlerwhen providedImplementation approach:
Handler resolution order:
fallbackRequestHandler(if set)Changes
Core Protocol Changes:
src/shared/protocol.ts:_defaultFallbackRequestHandlermethod for automatic empty array responses_onrequestto use layered fallback logicTest Coverage:
src/client/index.test.ts:Testing
Unit Tests (6 new tests):
prompts/listreturns empty array when no handler registeredresources/listreturns empty array when no handler registeredresources/templates/listreturns empty array when no handler registeredtools/listreturns empty array when no handler registeredTest Results:
Validation scenarios:
[][](unchanged)fallbackRequestHandler→ uses custom logic (unchanged)Breaking Changes
None. This is a backward-compatible enhancement that:
fallbackRequestHandlerwhen presentTypes of Changes
Checklist
Impact
This fix resolves a critical UX issue affecting MCP server development. Developers can now:
Before (Broken):
After (Fixed):