modelcontextprotocol / modelcontextprotocol/python-sdk
Refactor func_metadata() into smaller components for schema & metadata generation
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 24.3k
- Fork
- 4k
- Merge medio
- 1g 1h
- PR unite (30g)
- 31
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia in src/mcp/server/fastmcp/utilities/func_metadata.py tracciando func_metadata() e i relativi percorsi di introspezione della firma, analisi dei tipi, generazione dello schema e conversione. Usa gli unit test e i test di integrazione esistenti come baseline, quindi aggiungi una copertura mirata per gli helper proposti e preserva il comportamento esistente dei metadati, fornendo al contempo un meccanismo di estensione chiaro per nuove famiglie di tipi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- backend
- Tipo di issue
- Refactoring
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100