{"record":{"id":"f8062b9eca42021d","repo":"PrefectHQ/fastmcp","slug":"no-active-context-found","errorCode":null,"errorMessage":"No active context found.","messagePattern":"No active context found\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/server/dependencies.py","lineNumber":453,"sourceCode":"    code sees the updated signature.\n    \"\"\"\n    from uncalled_for.introspection import _parameter_cache, _signature_cache\n\n    _signature_cache.pop(fn, None)\n    _parameter_cache.pop(fn, None)\n\n    if inspect.ismethod(fn):\n        _signature_cache.pop(fn.__func__, None)\n        _parameter_cache.pop(fn.__func__, None)\n\n\ndef get_context() -> Context:\n    \"\"\"Get the current FastMCP Context instance directly.\"\"\"\n    from fastmcp.server.context import _current_context\n\n    context = _current_context.get()\n    if context is None:\n        raise RuntimeError(\"No active context found.\")\n    return context\n\n\ndef get_server() -> FastMCP:\n    \"\"\"Get the current FastMCP server instance directly.\n\n    In a background-task worker the tasks extension's resolver is consulted\n    first, so a mounted-child task resolves to the child server rather than the\n    root that started the worker (#3571).\n\n    Returns:\n        The active FastMCP server\n\n    Raises:\n        RuntimeError: If no server in context\n    \"\"\"\n    resolver = _worker_server_resolver\n    if resolver is not None:","sourceCodeStart":435,"sourceCodeEnd":471,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/server/dependencies.py#L435-L471","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before (module-level)\nctx = get_context()\n// after (inside tool)\n@mcp.tool\nasync def my_tool(ctx: Context) -> str: ...","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    ctx = get_context()\nexcept RuntimeError as e:\n    if \"No active context found\" in str(e):\n        ctx = None  # not inside a request; take alternate path\n    else:\n        raise","preventionTips":["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"],"tags":["context","lifecycle","request-scope"],"backgroundTag":"no-active-context","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}