modelcontextprotocol / modelcontextprotocol/python-sdk
Expose `schema_generator` on `FastMCP` for tool params schema generation
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 24.3k
- Forks
- 4k
- Ø Merge
- 1 T. 1 Std.
- Gemergte PRs (30 T.)
- 31
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit src/mcp/server/fastmcp/tools/base.py und utilities/func_metadata.py und verfolge dann, wie die Werte von FastMCP.init zu den beiden Aufrufen von model_json_schema gelangen. Die Änderung ist abgeschlossen, wenn ein optionaler schema_generator beide Pfade erreicht, die Standardwerte das aktuelle Verhalten beibehalten und der strict-schema-Kompromiss wie vorgeschlagen dokumentiert oder abgedeckt ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- backend-api-design
- Issue-Typ
- Feature
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 55/100