modelcontextprotocol / modelcontextprotocol/python-sdk
Refactor func_metadata() into smaller components for schema & metadata generation
未關閉
還沒有人認領這個 Issue。
enhancement
needs decision
P3
- 主要語言
- Python
- 星號
- 24.3k
- 分支
- 4k
- 平均合併
- 1 天 1 小時
- 30 天內合併 PR
- 31
描述
Description
Summary
func_metadata() in src/mcp/server/fastmcp/utilities/func_metadata.py is a large function that handles:
- signature introspection,
- parameter/return type analysis,
- JSON schema generation,
- special handling for various container and model types.
This centralizes important logic, but its size and responsibility make it difficult to understand, test, and extend.
Problems
- Complexity: Many branching code paths for different types (Pydantic, dataclasses, TypedDict, primitives, etc.).
- Maintenance: Adding new type handling or fixing edge cases requires navigating a large function.
- Testing: Hard to write targeted tests for individual behaviors without exercising the entire function.
Proposal
-
Decompose into smaller functions
For example:
extract_parameters(fn) -> list[Parameter]build_arg_model(params) -> PydanticModel | Nonebuild_output_model(return_type) -> PydanticModel | Nonecreate_converters(...) -> Callable
-
Use a strategy/registry for type handling
- Allow registering handlers for specific type families (Pydantic, TypedDict, dataclasses, etc.).
- This reduces the need for a long series of
if isinstance(...)checks in one place.
-
Add targeted unit tests
- Test each helper independently, covering edge cases.
- Keep some higher-level tests that ensure the overall metadata behavior remains consistent.
Why this matters
- Extensibility: Easier to support new type patterns without making
func_metadata()even larger. - Reliability: Smaller, well-tested pieces reduce the risk of regressions when making changes.
- Readability: Contributors can understand and modify the code more easily.
Acceptance criteria
-
func_metadata()is decomposed into smaller, focused helpers. - There is a clear extension mechanism for adding support for new types.
- Unit tests cover individual behaviors; existing integration tests still pass.
References
No response
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 src/mcp/server/fastmcp/utilities/func_metadata.py 開始,追蹤 func_metadata() 及其簽章內省、型別分析、結構描述產生和轉換器路徑。以現有的單元測試和整合測試作為基準,接著為提議的輔助函式新增有針對性的涵蓋範圍,同時保留現有的中繼資料行為,並為新的型別家族提供清楚的擴充機制。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend
- Issue 類型
- 重構
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100