context_injection
Context injection utilities for MCPServer.
find_context_parameter
Find the parameter that should receive the Context object.
Searches through the function's signature to find a parameter with a Context type annotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[..., Any]
|
The function to inspect |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The name of the context parameter, or None if not found |
Source code in src/mcp/server/mcpserver/utilities/context_injection.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
inject_context
inject_context(
fn: Callable[..., Any],
kwargs: dict[str, Any],
context: Any | None,
context_kwarg: str | None,
) -> dict[str, Any]
Inject context into function kwargs if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[..., Any]
|
The function that will be called |
required |
kwargs
|
dict[str, Any]
|
The current keyword arguments |
required |
context
|
Any | None
|
The context object to inject (if any) |
required |
context_kwarg
|
str | None
|
The name of the parameter to inject into |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Updated kwargs with context injected if applicable |
Source code in src/mcp/server/mcpserver/utilities/context_injection.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |