modelcontextprotocol / modelcontextprotocol/python-sdk

Refactor func_metadata() into smaller components for schema & metadata generation

Open
#1,700 1 comment 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

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

  1. Decompose into smaller functions

    For example:

    • extract_parameters(fn) -> list[Parameter]
    • build_arg_model(params) -> PydanticModel | None
    • build_output_model(return_type) -> PydanticModel | None
    • create_converters(...) -> Callable
  2. 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.
  3. 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

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 in src/mcp/server/fastmcp/utilities/func_metadata.py by tracing func_metadata() and its signature introspection, type analysis, schema generation, and converter paths. Use the existing unit and integration tests as a baseline, then add focused coverage for the proposed helpers and preserve existing metadata behavior while providing a clear extension mechanism for new type families.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.