Skip to content

When generating tools, function parameters should allow annotations(Annotated args) to propagate to the inputSchema of each tool. #566

Open
@DmitriyLeybel

Description

@DmitriyLeybel

Is your feature request related to a problem? Please describe.
Currently, the parameters are typically best described within the docstring. This is a subpar approach and can be further refined to create a better inputSchema when the tool list is emitted.

e.g.

@mcp.tool()
def generate_random_number(min_value: int = 1, max_value: int = 100) -> int:
    """
    Generate a random number between min_value and max_value (inclusive).
    
    Args:
        min_value: Minimum value (default: 1)
        max_value: Maximum value (default: 100)
        
    Returns:
        A random integer
    """
    return random.randint(min_value, max_value)

Describe the solution you'd like
Instead, we can use the Annotated feature of Python and pass in the appropriate metadata into the function signature.

@mcp.tool()
def generate_random_number(
    min_value: Annotated[int, {"description": "The inclusive minimum value for the random number",
                                               "minimum": 0}] = 1,
    max_value: Annotated[int, {"description": "The inclusive maximum value for the random number",
                                               "maximum": 1000000000}] = 100,
) -> int:
    """
    Generate a random number between min_value and max_value (inclusive).
    """
    if min_value > max_value:
        raise ValueError("min_value must be less than or equal to max_value")
    return random.randint(min_value, max_value)

Describe alternatives you've considered
Considered Pydantic fields, but that's unnecessary overhead for simple cases.


Seems like a fairly simple addition to the func_metadata method.
https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/server/fastmcp/utilities/func_metadata.py#L105

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions