{{ message }}
fix(mcpserver): advertise capabilities only for registered primitives#2499
Open
Zelys-DFKH wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix(mcpserver): advertise capabilities only for registered primitives#2499Zelys-DFKH wants to merge 1 commit intomodelcontextprotocol:mainfrom
Zelys-DFKH wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
MCPServer unconditionally passed non-None list handlers to the lowlevel Server, which caused it to advertise tools/resources/prompts capabilities even when none of those primitives had been registered. Per the MCP schema spec, a capability entry should only appear when the server actually offers that primitive. Adds a `capability_filter` hook to the lowlevel Server that, if set, post-processes the computed ServerCapabilities before they are returned. MCPServer uses this to suppress tools/resources/prompts entries when the corresponding manager is empty at capability-computation time (i.e. when create_initialization_options() is called, after all decorators have been applied). Fixes modelcontextprotocol#2473. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

Summary
MCPServer.__init__unconditionally passes non-Nonelist handlers tothe lowlevel
Server, causingget_capabilities()to always advertisetools,resources, andprompts, even when none are registered.capability_filterparameter to the lowlevelServerthat post-processes the computed
ServerCapabilities.MCPServerpasses a filter that suppresses each entry when thecorresponding manager is empty at capability-computation time.
Motivation and Context
Per the MCP schema spec and lifecycle docs, a
ServerCapabilitiesentry should appear only when the server actually offers that primitive. Currently, an emptyMCPServeradvertises all three, and a server with only one tool still advertisesresourcesandpromptsit doesn't have.The filter runs when
create_initialization_options()is called at the start ofrun(), after all@mcp.tool()/@mcp.resource()/@mcp.prompt()decorators have been applied. Every transport path (stdio, SSE, streamable HTTP) callsserver.create_initialization_options()→get_capabilities(), so the filter applies without any per-transport changes.How Has This Been Tested?
Six new tests in
tests/server/mcpserver/test_server.pycover: empty server, tool-only, resource-only, resource-template-only, prompt-only, and all-registered cases.Full suite:
./scripts/test— 100% branch coverage.Breaking Changes
None. Servers that previously over-advertised will now send correct capability sets.
Types of changes
Checklist
Closes #2473