{"record":{"id":"6abc0e2dcda56ead","repo":"PrefectHQ/fastmcp","slug":"no-fastmcp-server-instance-in-context","errorCode":null,"errorMessage":"No FastMCP server instance in context","messagePattern":"No FastMCP server instance in context","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/server/dependencies.py","lineNumber":478,"sourceCode":"    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:\n        worker_server = resolver()\n        if worker_server is not None:\n            return worker_server\n\n    server_ref = _current_server.get()\n    if server_ref is None:\n        raise RuntimeError(\"No FastMCP server instance in context\")\n    server = server_ref()\n    if server is None:\n        raise RuntimeError(\"FastMCP server instance is no longer available\")\n    return server\n\n\nasync def get_session(session_id: str) -> Session:\n    \"\"\"Resolve and validate a `Session` for an explicit `session_id`.\n\n    Pair with a `session_id: SessionId` tool argument (the agent obtains an id\n    from `create_session` and passes it back). For a single per-user bucket with\n    nothing for the agent to pass, inject `session: UserSession` instead.\n\n    State is keyed by `(principal, session_id)`: the authenticated principal is\n    the isolation wall and `session_id` organizes sessions within it. The id must\n    have been minted by `create_session` under the current principal; an id that\n    was never created, or created under a different principal, raises\n    `InvalidSession` rather than resolving to a fresh empty bucket (the specific","sourceCodeStart":460,"sourceCodeEnd":496,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/server/dependencies.py#L460-L496","documentation":"get_server() resolves the current FastMCP instance from the _current_server context variable (a weak reference) after first checking any configured task-worker resolver. This RuntimeError is thrown when no server reference has been set for the current execution context. FastMCP throws instead of returning None to make misused APIs fail fast.","triggerScenarios":"Calling get_server() outside any server-managed execution context — e.g. in plain scripts, tests without a server contextvar, or code running before the server sets _current_server.","commonSituations":"Calling server-dependent helpers during module import; running tool helpers in an ad-hoc asyncio script; tests invoking dependency functions directly without Client(server).","solutions":["Call get_server() only from within request handlers or other code executed under a running FastMCP server.","In tests, establish the context with an in-memory Client(server) or set the server contextvar in a fixture.","Pass the FastMCP instance explicitly to helpers that need it instead of relying on the contextvar."],"exampleFix":"// before\nserver = get_server()  # at import time\n// after\ndef build(server: FastMCP) -> None: ...  # explicit injection","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    server = get_server()\nexcept RuntimeError as e:\n    if \"No FastMCP server instance in context\" in str(e):\n        server = None\n    else:\n        raise","preventionTips":["Only resolve the server inside request-managed code","Keep the FastMCP instance at module/app scope","Inject the server explicitly in scripts and startup hooks"],"tags":["context","server","lifecycle"],"backgroundTag":"no-active-context","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}