modelcontextprotocol / modelcontextprotocol/python-sdk
Add `ClientSession.register_tool_schema()` for dynamic tool discovery patterns
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 24.3k
- 分支
- 4k
- 平均合併
- 1 天 1 小時
- 30 天內合併 PR
- 31
描述
Description
Problem
ClientSession.validate_tool_result() validates tool results against cached output schemas in self._tool_output_schemas, which is populated exclusively by list_tools(). When a tool is invoked that wasn't returned by list_tools(), two things happen:
- A warning is logged:
"Tool {name} not listed by server, cannot validate any structured content" - If the tool returns
structuredContentwith anoutputSchema, validation is skipped entirely — a correctness gap.
This affects MCP servers that use dynamic tool discovery, where list_tools() intentionally returns only a small subset of available tools (e.g., meta-tools like a search tool) and actual tools are discovered at runtime via semantic search or catalog queries.
Use case
AWS Bedrock AgentCore Gateway is an MCP-compatible server that can host hundreds or thousands of tools. Rather than returning all tools in list_tools() (which would overwhelm LLM context windows), it exposes a built-in tool called x_amz_bedrock_agentcore_search. Clients call this tool with a natural language query, and the gateway returns matching tool definitions — including names, descriptions, input schemas, and output schemas. The client then invokes these discovered tools by name via call_tool().
At that point, the SDK doesn't recognize these tools because they never appeared in a list_tools() response. This triggers the warning and skips output validation. The only workaround today is accessing the private _tool_output_schemas dict directly.
AWS Prescriptive Guidance documents this "search function" approach as one of three canonical tool discovery strategies for MCP (alongside static definition and dynamic discovery via list_tools()).
Proposed API
A single public method on ClientSession:
def register_tool_schema(
self, name: str, output_schema: dict[str, Any] | None = None
) -> None:
"""Register a tool's output schema for result validation.
Use this when tools are discovered dynamically (e.g., via a catalog
search API) and won't appear in list_tools() responses.
"""
self._tool_output_schemas[name] = output_schema
Usage:
# Client discovers tools via semantic search (not list_tools)
search_results = await session.call_tool("x_amz_bedrock_agentcore_search", {"query": "data analysis"})
# Register discovered tools for proper validation
for tool in parse_discovered_tools(search_results):
session.register_tool_schema(tool.name, tool.output_schema)
# Now call_tool validates structuredContent correctly, no warning logged
result = await session.call_tool("data_analysis_v2", {"input": "..."})
This is purely additive — no changes to existing behavior, no new data structures. It writes to the same dict that _absorb_tool_listing() already populates.
References
- Search for tools in your AgentCore gateway with a natural language query — Official docs for the
x_amz_bedrock_agentcore_searchtool - Tool discovery - AWS Prescriptive Guidance — Documents "search function" as a canonical MCP tool discovery strategy
- aws-samples/sample-aws-ops-agentcore-gateway-search — Working example with 268+ tools behind semantic search, reduced to 10-15 per query
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 ClientSession.validate_tool_result() 以及 list_tools() 和 _absorb_tool_listing() 中現有的 _tool_output_schemas 填充邏輯開始。新增 issue 中描述的公開註冊入口,然後驗證動態探索到的工具可以被註冊,且其 structuredContent 能通過驗證,而不會觸發缺少工具的警告。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- api, backend
- Issue 類型
- 功能
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 活躍度
- 冷清
- 描述清晰度
- 描述清楚
- 新手友好度
- 72/100