modelcontextprotocol / modelcontextprotocol/python-sdk

Expose `schema_generator` on `FastMCP` for tool params schema generation

Open
#2,582 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement needs decision P3
Dominant language
Python
Stars
24.3k
Forks
4k
Avg merge
1d 1h
Merged PRs (30d)
31

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/mcp/server/fastmcp/tools/base.py and utilities/func_metadata.py, then trace how FastMCP.init values reach the two model_json_schema calls. The change is done when an optional schema_generator reaches both paths, defaults preserve current behavior, and the strict-schema tradeoff is documented or covered as proposed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.