modelcontextprotocol / modelcontextprotocol/python-sdk
Type the MCPServer handler pipeline: tool/resource/prompt return types
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 24.3k
- 分支
- 4k
- 平均合併
- 1 天 1 小時
- 30 天內合併 PR
- 31
描述
The chain from user @tool()-decorated functions through to CallToolResult is typed as Any end-to-end, hiding a wrong public return type and dead code.
Tool call chain
FuncMetadata.convert_result() returns exactly three shapes:
CallToolResult(when the user returned one directly)Sequence[ContentBlock](unstructured)tuple[Sequence[ContentBlock], dict[str, Any]](unstructured + structured)
But every step of the chain is -> Any:
FuncMetadata.convert_result()—-> AnyTool.run()—-> AnyToolManager.call_tool()—-> AnyMCPServer.call_tool()— declared-> Sequence[ContentBlock] | dict[str, Any], wrong — never returnsdict
Root cause: the convert_result: bool flag on Tool.run() / ToolManager.call_tool() toggles the return type, which is unrepresentable. The False path isn't used in production (only server.py:405, always True).
Symptoms:
- Dead code + TODO acknowledging the wrong type at server.py:317-324
# type: ignore[arg-type]shoving tuple elements intoCallToolResultat server.py:314-315
Prompt / Resource
PromptResultalready includesAwaitable, butPrompt.fnis typedCallable[..., PromptResult | Awaitable[PromptResult]]— doubly wrappedResourceTemplate.matches()returnsdict[str, Any] | Nonebut values are alwaysstr(regex groupdict + unquote)
User-facing return types
What you can return from @tool() is undocumented. A strict bound is impossible (_convert_to_content() ends with pydantic_core.to_json(fallback=str) — everything works), but the docstrings should enumerate what gets first-class handling (str/primitives/BaseModel/ContentBlock/Image/Audio/CallToolResult/list/tuple) vs what gets JSON-dumped.
Related
- #1207 — excessive generics (reason to not introduce
Tool[ReturnT]; aTypeAliasfor the converted result is sufficient) - #2153 — handler error semantics
- #2201 — Tasks support (adds
CreateTaskResultto the converted-result union)
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 FuncMetadata.convert_result()、Tool.run()、ToolManager.call_tool() 和 MCPServer.call_tool() 開始,追蹤工具回傳鏈和 convert_result 旗標。接著檢查 Prompt.fn、PromptResult 和 ResourceTemplate.matches(),確認所述的註解不一致問題。完成的標準是:handler pipeline 具有準確的公開型別,已處理無效或錯誤的型別路徑,並記錄面向使用者的回傳值處理方式。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend-api-design, documentation
- Issue 類型
- 重構
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 48/100