PrefectHQ/fastmcp · error · RuntimeError

No active context found.

Error message

No active context found.

What it means

get_context() returns the Context bound to the currently executing request via the _current_context context variable. This RuntimeError is thrown when the function is called outside any request — there is no active tool/resource/prompt invocation, so no Context exists. FastMCP throws it rather than returning None so callers fail loudly.

Source

Thrown at fastmcp_slim/fastmcp/server/dependencies.py:453

    code sees the updated signature.
    """
    from uncalled_for.introspection import _parameter_cache, _signature_cache

    _signature_cache.pop(fn, None)
    _parameter_cache.pop(fn, None)

    if inspect.ismethod(fn):
        _signature_cache.pop(fn.__func__, None)
        _parameter_cache.pop(fn.__func__, None)


def get_context() -> Context:
    """Get the current FastMCP Context instance directly."""
    from fastmcp.server.context import _current_context

    context = _current_context.get()
    if context is None:
        raise RuntimeError("No active context found.")
    return context


def get_server() -> FastMCP:
    """Get the current FastMCP server instance directly.

    In a background-task worker the tasks extension's resolver is consulted
    first, so a mounted-child task resolves to the child server rather than the
    root that started the worker (#3571).

    Returns:
        The active FastMCP server

    Raises:
        RuntimeError: If no server in context
    """
    resolver = _worker_server_resolver
    if resolver is not None:

View on GitHub (pinned to 1f02114297)

Solutions

  1. Only call get_context() inside a request-scoped handler (tool, resource, or prompt function) executed by the server.
  2. In tests, use the in-memory Client against the server (Client(server)) so a real request context is established.
  3. Pass the Context explicitly as a function parameter instead of looking it up globally when calling from non-request code.

Example fix

// before (module-level)
ctx = get_context()
// after (inside tool)
@mcp.tool
async def my_tool(ctx: Context) -> str: ...
Defensive patterns

Strategy: try-catch

Try / catch

try:
    ctx = get_context()
except RuntimeError as e:
    if "No active context found" in str(e):
        ctx = None  # not inside a request; take alternate path
    else:
        raise

Prevention

When it happens

Trigger: Calling get_context() from module top-level code, server startup/shutdown hooks, background threads or tasks spawned outside the request context, or plain unit tests that invoke handlers directly without running a request.

Common situations: Tests calling a tool function directly instead of via client.call_tool; background workers started with asyncio.create_task without propagating context; code executed during server lifespan.

Related errors


AI-assisted analysis of PrefectHQ/fastmcp@1f02114297 (2026-08-29). Data as JSON: /api/errors/f8062b9eca42021d. Report an issue: GitHub.