modelcontextprotocol / modelcontextprotocol/python-sdk
Expose `schema_generator` on `FastMCP` for tool params schema 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
Context
Tool.from_function (in src/mcp/server/fastmcp/tools/base.py) generates the
tool parameters JSON schema via:
parameters = func_arg_metadata.arg_model.model_json_schema(by_alias=True)
This is hardcoded — no way for users to customize the generation. Pydantic 2.11+
supports union_format='primitive_type_array' which produces compact
LLM-friendly schemas like {"type": ["integer", "string"]} instead of
{"anyOf": [...]}. There's no path to enable it without monkey-patching or
post-processing.
Proposal
Add schema_generator: type[GenerateJsonSchema] | None = None to FastMCP.__init__.
When provided, plumb it to both model_json_schema calls:
# tools/base.py
parameters = func_arg_metadata.arg_model.model_json_schema(
by_alias=True,
schema_generator=self.schema_generator or GenerateJsonSchema,
)
# utilities/func_metadata.py
schema = model.model_json_schema(
schema_generator=self.schema_generator or StrictJsonSchema,
)
Use case
from pydantic.json_schema import GenerateJsonSchema
class CompactUnionGen(GenerateJsonSchema):
def __init__(self, by_alias=True, ref_template='#/$defs/{model}'):
super().__init__(
by_alias=by_alias,
ref_template=ref_template,
union_format='primitive_type_array',
)
mcp = FastMCP("srv", schema_generator=CompactUnionGen)
Tradeoff to discuss
func_metadata.py currently uses StrictJsonSchema (warnings → errors). With a
user-supplied generator, strict semantics are lost unless the user extends
StrictJsonSchema themselves. Proposed resolution: document that custom
generators should extend StrictJsonSchema if strict output validation is
desired.
Backward compat
Default None preserves current behavior exactly.
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 với src/mcp/server/fastmcp/tools/base.py và utilities/func_metadata.py, sau đó lần theo cách các giá trị của FastMCP.init đi tới hai lệnh gọi model_json_schema. Thay đổi được hoàn tất khi một schema_generator tùy chọn đi tới cả hai nhánh, các giá trị mặc định vẫn giữ nguyên hành vi hiện tại và đánh đổi của strict-schema được ghi lại hoặc được bao phủ như đề xuất.
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-api-design
- Loại issue
- Tính năng
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- 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
- 55/100