Fix: Return empty Arrays for List methods when no handlers registered (#929) #986
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):