modelcontextprotocol / modelcontextprotocol/python-sdk

find_context_parameter treats a Context return annotation as a parameter

Open Beginner friendly
#3,298 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

P3 v1 v2
Dominant language
Python
Stars
24.3k
Forks
4k
Avg merge
1d 1h
Merged PRs (30d)
31

Description

Initial checks
  • I tested the current main branch (6e30452).
  • I searched existing issues and pull requests for this behavior.
Description

find_context_parameter() iterates over every entry returned by typing.get_type_hints(). That mapping includes the function return annotation under the special key "return", even though it is not a parameter.

As a result, a tool, prompt, or resource function whose return annotation is Context (or a union containing Context) is incorrectly recorded as having a context parameter named "return". The SDK later attempts to inject context as a keyword argument with that name, which can produce an unexpected-keyword error when the callable is invoked.

Expected: context detection only examines actual function parameters and ignores the return annotation.

Example code
from mcp.server.mcpserver import Context
from mcp.server.mcpserver.utilities.context_injection import find_context_parameter

def fn(value: int) -> Context:
    ...

print(find_context_parameter(fn))  # current: "return"; expected: None
Environment
  • Python 3.12
  • MCP Python SDK main at 6e30452

I used AI assistance while investigating this issue and reviewed and reproduced the behavior locally.

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 mcp/server/mcpserver/utilities/context_injection.py at find_context_parameter() and reproduce the example with a Context return annotation. Confirm that detection examines only actual parameters, ignores the "return" annotation, and no longer reports "return" as the context parameter.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.