Description
Summary
FastMCP currently uses introspection to automatically inject a Context parameter into tools/resources when it detects a parameter with the right name/type. This is convenient but “magic”:
- it relies on parameter naming/type hints,
- it’s invisible in function signatures (from a caller’s perspective),
- it can break silently after refactors.
This issue proposes making context injection more explicit and type-safe, while preserving ergonomics.
Problems
- Brittle conventions: Renaming the parameter or changing the import path of
Context can silently break injection.
- Type checker mismatch: Type hints show a required parameter, but at runtime the SDK auto-populates it, which can confuse static analysis and IDEs.
- New user confusion: It’s not obvious from examples why a parameter is populated or how to opt into/opt out of it.
Proposal
-
Introduce explicit opt-in for context injection
For example:
-
Document a single, clear convention
- If conventions are used (e.g.,
_mcp_context parameter name), make them explicit and heavily documented:
- “If you define a parameter named
_mcp_context with type Context, FastMCP will inject it.”
-
Migration path
- Support both the old introspection behavior and the new explicit pattern for at least one minor release.
- Add warnings or docs guiding users towards the explicit pattern.
Why this matters
- Predictability: Less “magic” makes it easier to reason about what the SDK is doing.
- Tooling: IDEs and static analyzers can more accurately represent what parameters are expected.
- Refactoring safety: Users are less likely to accidentally break context injection when renaming parameters or types.
Acceptance criteria
References
No response
Description
Summary
FastMCP currently uses introspection to automatically inject a
Contextparameter into tools/resources when it detects a parameter with the right name/type. This is convenient but “magic”:This issue proposes making context injection more explicit and type-safe, while preserving ergonomics.
Problems
Contextcan silently break injection.Proposal
Introduce explicit opt-in for context injection
For example:
Via decorator argument:
Or via a dedicated “injected” marker type, e.g.
ctx: Injected[Context].Document a single, clear convention
_mcp_contextparameter name), make them explicit and heavily documented:_mcp_contextwith typeContext, FastMCP will inject it.”Migration path
Why this matters
Acceptance criteria
References
No response