Skip to content

context_injection

Context injection utilities for MCPServer.

find_context_parameter

find_context_parameter(
    fn: Callable[..., Any],
) -> str | None

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
def find_context_parameter(fn: Callable[..., Any]) -> str | None:
    """Find the parameter that should receive the Context object.

    Searches through the function's signature to find a parameter
    with a Context type annotation.

    Args:
        fn: The function to inspect

    Returns:
        The name of the context parameter, or None if not found
    """
    # Get type hints to properly resolve string annotations
    try:
        hints = typing.get_type_hints(fn)
    except Exception:  # pragma: lax no cover
        # If we can't resolve type hints, we can't find the context parameter
        return None

    # Check each parameter's type hint
    for param_name, annotation in hints.items():
        # Handle direct Context type
        if inspect.isclass(annotation) and issubclass(annotation, Context):
            return param_name

        # Handle generic types like Optional[Context]
        origin = typing.get_origin(annotation)
        if origin is not None:
            args = typing.get_args(annotation)
            for arg in args:
                if inspect.isclass(arg) and issubclass(arg, Context):
                    return param_name

    return None

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
def 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.

    Args:
        fn: The function that will be called
        kwargs: The current keyword arguments
        context: The context object to inject (if any)
        context_kwarg: The name of the parameter to inject into

    Returns:
        Updated kwargs with context injected if applicable
    """
    if context_kwarg is not None and context is not None:
        return {**kwargs, context_kwarg: context}
    return kwargs