modelcontextprotocol / modelcontextprotocol/python-sdk
Type the MCPServer handler pipeline: tool/resource/prompt return types
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 24.3k
- フォーク
- 4k
- 平均マージ
- 1日 1時間
- マージ済み PR(30日)
- 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 にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
FuncMetadata.convert_result()、Tool.run()、ToolManager.call_tool()、MCPServer.call_tool()から始めて、ツールの戻り値チェーンとconvert_resultフラグを追跡します。次に、Prompt.fn、PromptResult、ResourceTemplate.matches()を調べ、記載されているアノテーションの不一致を確認します。ハンドラーパイプラインの公開型が正確になり、不要または誤った型付けの経路に対処し、ユーザー向けの戻り値処理が文書化されていれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- backend-api-design, documentation
- issue の種類
- リファクタリング
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 48/100