Skip to content

fix: preserve parameter types in tool() and resource() decorators with ParamSpec#2964

Open
Robin1987China wants to merge 1 commit into
modelcontextprotocol:mainfrom
Robin1987China:fix/decorator-paramspec-preserve-types
Open

fix: preserve parameter types in tool() and resource() decorators with ParamSpec#2964
Robin1987China wants to merge 1 commit into
modelcontextprotocol:mainfrom
Robin1987China:fix/decorator-paramspec-preserve-types

Conversation

@Robin1987China

Copy link
Copy Markdown

Motivation

Fixes #1822

MCPServer decorators (@server.tool(), @server.resource()) use _CallableT = TypeVar("_CallableT", bound=Callable[..., Any]) which erases parameter type information. This forces users to add # type: ignore and prevents type checkers from validating decorated function signatures.

Changes

  • Replace _CallableT with ParamSpec for tool() and resource() decorators (pure pass-through, full benefit)
  • Keep explicit Callable[..., Any] for completion() and prompt() — these feed into downstream type-constrained APIs where ParamSpec creates false type errors

Verification

  • pyright: 0 errors, 0 warnings
  • pytest: 2601 passed (1 pre-existing flaky test unrelated)

AI assistance

Fix implemented with AI assistance (opencode). I have reviewed every changed line and validated with pyright + pytest.

…h ParamSpec (modelcontextprotocol#1822)

Replace the type-erasing _CallableT with ParamSpec for the tool()
and resource() decorators, which are pure pass-through and benefit
fully from parameter type preservation. This allows type checkers to
validate decorated function signatures without # type: ignore.

completion() and prompt() keep explicit Callable[..., Any] because
they feed into downstream type-constrained APIs where ParamSpec
would create false type errors.

AI assistance: Fix implemented with AI assistance (opencode). I've
reviewed every changed line and validated with pyright + pytest.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decorator type signatures lose parameter types (use ParamSpec)

1 participant