Skip to content

find_context_parameter() fails to detect Context parameter in callable class instances #1974

Open

Description

@mjahr

Initial Checks

Description

When registering a callable class instance as an MCP tool via FastMCP.add_tool(), the ctx: Context parameter is incorrectly exposed as an externally visible tool parameter instead of being injected by the framework. This is because find_context_parameter() uses typing.get_type_hints() which doesn't introspect the call method of callable class instances.

Suggested Fix
Update find_context_parameter() to handle callable class instances:

def find_context_parameter(fn: Callable[..., Any]) -> str | None:
    from mcp.server.fastmcp.server import Context

    # Handle callable class instances by using __call__ method
    target = fn
    if not (inspect.isfunction(fn) or inspect.ismethod(fn)):
        if callable(fn) and hasattr(fn, "__call__"):
            target = fn.__call__

    try:
        hints = typing.get_type_hints(target)
    except Exception:
        return None

    # ... rest of function unchanged

Example Code

from typing import Annotated, Any
from mcp.server.fastmcp import Context, FastMCP
from mcp.server.session import ServerSession
from starlette.requests import Request
import pydantic

class MyTool:
    def __init__(self, name: str):
        self.__name__ = name

    async def __call__(
        self,
        query: Annotated[str, pydantic.Field(description="Search query")],
        ctx: Context[ServerSession, Any, Request],
    ) -> str:
        """Performs a search."""
        return f"Results for: {query}"

mcp = FastMCP(name="Test Server")
mcp.add_tool(MyTool(name="my_search"), name="my_search", description="Search tool")

# When listing tools, 'ctx' appears as a required parameter in the schema

Python & MCP Python SDK

1.26.0

Activity

  1. added
    bugSomething isn't working
    P3Nice to haves, rare edge cases
    v2Affects the v2 line (2.x on main)
    on Feb 10, 2026
  2. maxisbey commented on Feb 10, 2026

    @maxisbey
    Contributor

    This is part of the broader context handling refactor planned for V2 — see #1684 for tracking.

    AI Disclaimer

  3. added a commit that references this issue on Feb 12, 2026
    2ed8c3f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Nice to haves, rare edge casesbugSomething isn't workingv2Affects the v2 line (2.x on main)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions