Skip to content

wrapped tools using annotations fail #1391

@stevenh

Description

@stevenh

Initial Checks

Description

If you try to use a tool a which is wrapped and uses from __future__ import annotations setup fails with and InvalidSignature exception e.g.

mcp.server.fastmcp.exceptions.InvalidSignature: Unable to evaluate type annotation ForwardRef("Literal['literal'] | None")

The cause of this is that the global name space used for the lookup is the wrapper namespace and not the original function namespace.

Example Code

from __future__ import annotations

import logfire
from mcp.server.fastmcp import FastMCP

@logfire.instrument
def test_tool(location_type: Literal["literal"] | None = None) -> str:
    """Test tool."""
    return f"a is {location_type}"

mcp = FastMCP(
    "Wrap test",
    tools=[
        Tool.from_function(test_tool),
    ],
)

Python & MCP Python SDK

SDL: 1.14.1
Python: 3.13.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Moderate issues affecting some users, edge cases, potentially valuable featurebugSomething isn't workingready for workEnough information for someone to start working on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions