modelcontextprotocol / modelcontextprotocol/python-sdk

Refactor func_metadata() into smaller components for schema & metadata generation

未关闭
#1,700 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 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

  1. Decompose into smaller functions

    For example:

    • extract_parameters(fn) -> list[Parameter]
    • build_arg_model(params) -> PydanticModel | None
    • build_output_model(return_type) -> PydanticModel | None
    • create_converters(...) -> Callable
  2. 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.
  3. 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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 src/mcp/server/fastmcp/utilities/func_metadata.py 开始,跟踪 func_metadata() 及其签名内省、类型分析、模式生成和转换器路径。以现有的单元测试和集成测试为基线,然后为提议的辅助函数添加有针对性的覆盖,同时保持现有的元数据行为,并为新的类型族提供清晰的扩展机制。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
重构
难度
5/5
预计耗时
一周以上
活跃度
冷清
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。