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
- Only call get_context() inside a request-scoped handler (tool, resource, or prompt function) executed by the server.
- In tests, use the in-memory Client against the server (Client(server)) so a real request context is established.
- 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
- Call get_context() only from tool/resource/prompt handlers
- Declare ctx: Context as a handler parameter instead
- In tests, use Client(server) to create a real request context
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
- FastMCP instance is no longer available
- No FastMCP server instance in context
- FastMCP server instance is no longer available
- Client is not connected. Use the 'async with client:' contex
- ClientGroup is already connected
AI-assisted analysis of PrefectHQ/fastmcp@1f02114297 (2026-08-29).
Data as JSON: /api/errors/f8062b9eca42021d.
Report an issue: GitHub.