modelcontextprotocol / modelcontextprotocol/python-sdk
Refactor func_metadata() into smaller components for schema & metadata generation
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 24.3k
- Fork
- 4k
- Merge trung bình
- 1 ngày 1 giờ
- Pull request đã merge (30 ngày)
- 31
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu trong src/mcp/server/fastmcp/utilities/func_metadata.py bằng cách lần theo func_metadata() cùng với các đường dẫn introspection chữ ký, phân tích kiểu, tạo schema và chuyển đổi của nó. Sử dụng các bài kiểm thử unit và integration hiện có làm baseline, sau đó bổ sung coverage tập trung cho các helper được đề xuất và duy trì hành vi metadata hiện có, đồng thời cung cấp một cơ chế mở rộng rõ ràng cho các họ kiểu mới.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- backend
- Loại issue
- Tái cấu trúc
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 42/100