find_context_parameter treats a Context return annotation as a parameter

Offen Anfängerfreundlich
#3,298 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
78/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python
Bereich
api, backend

Rechercherichtung

Beginnen Sie in mcp/server/mcpserver/utilities/context_injection.py bei find_context_parameter() und reproduzieren Sie das Beispiel mit einer Context-Rückgabeannotation. Bestätigen Sie, dass die Erkennung nur tatsächliche Parameter untersucht, die "return"-Annotation ignoriert und "return" nicht mehr als Kontextparameter meldet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

P3 v1 v2
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.

Vorherrschende Sprache
Python
Sterne
24.3k
Forks
4k
Ø Merge
1 T. 19 Min.
Gemergte PRs (30 T.)
29

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus modelcontextprotocol/python-sdk

Alle Issues in modelcontextprotocol/python-sdk

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.