refactor: make tool registration object-based#2415
Open
mplemay wants to merge 3 commits intomodelcontextprotocol:mainfrom
Open
refactor: make tool registration object-based#2415mplemay wants to merge 3 commits intomodelcontextprotocol:mainfrom
mplemay wants to merge 3 commits intomodelcontextprotocol:mainfrom
Conversation
BREAKING CHANGE: MCPServer.add_tool() and ToolManager.add_tool() now require prebuilt Tool objects. Use Tool.from_function() or the @mcp.tool() decorator to construct tools before registration.
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
Refactor tool registration to be object-first, matching resource registration, and fix structured-output inference for
list[Any]so mixed media tool results continue to serialize correctly.Motivation and Context
This makes
MCPServer.add_tool()consistent withadd_resource()andToolManager.add_tool(). It also preventslist[Any]return annotations from advertising structured output when they can includeImage/Audiohelpers that only serialize correctly through the unstructured conversion path.How Has This Been Tested?
UV_CACHE_DIR=/tmp/uv-cache uv run --frozen pytest tests/server/mcpserver/tools/test_base.py tests/server/mcpserver/test_tool_manager.py tests/server/mcpserver/test_server.pyUV_CACHE_DIR=/tmp/uv-cache uv run --frozen pytest tests/server/mcpserver/test_func_metadata.py tests/server/mcpserver/test_server.py -qUV_CACHE_DIR=/tmp/uv-cache uv run --frozen ruff check src/mcp/server/mcpserver/tools/base.py src/mcp/server/mcpserver/tools/tool_manager.py src/mcp/server/mcpserver/server.py src/mcp/server/mcpserver/utilities/func_metadata.py tests/server/mcpserver/test_tool_manager.py tests/server/mcpserver/test_server.py tests/server/mcpserver/test_func_metadata.pyUV_CACHE_DIR=/tmp/uv-cache uv run --frozen pyright src/mcp/server/mcpserver/tools/base.py src/mcp/server/mcpserver/tools/tool_manager.py src/mcp/server/mcpserver/server.py src/mcp/server/mcpserver/utilities/func_metadata.py tests/server/mcpserver/test_tool_manager.py tests/server/mcpserver/test_server.py tests/server/mcpserver/test_func_metadata.pyBreaking Changes
Yes.
MCPServer.add_tool()andToolManager.add_tool()now expect prebuiltToolinstances instead of callables. Callers should useTool.from_function(...)or@mcp.tool().Types of changes
Checklist
Additional context
Tool registration now follows the same object-first pattern as resources. The
list[Any]guard keeps mixed-content tool results unstructured soImageandAudiohelpers stay on the legacy content conversion path and CI no longer fails on JSON serialization.