{"record":{"id":"4f42bb10547f09fa","repo":"PrefectHQ/fastmcp","slug":"request-id-is-not-available-because-the-mcp-sessio","errorCode":null,"errorMessage":"request_id is not available because the MCP session has not been established yet. Check `context.request_context` for None before accessing this attribute.","messagePattern":"request_id is not available because the MCP session has not been established yet\\. Check `context\\.request_context` for None before accessing this attribute\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/server/context.py","lineNumber":702,"sourceCode":"            return False\n        return client_supports_extension(session, extension_id)\n\n    @property\n    def client_id(self) -> str | None:\n        \"\"\"Get the client ID if available.\"\"\"\n        rc = self.request_context\n        return (\n            rc.meta.get(\"client_id\") if rc is not None and rc.meta is not None else None\n        )\n\n    @property\n    def request_id(self) -> str:\n        \"\"\"Get the unique ID for this request.\n\n        Raises RuntimeError if MCP request context is not available.\n        \"\"\"\n        if self.request_context is None:\n            raise RuntimeError(\n                \"request_id is not available because the MCP session has not been established yet. \"\n                \"Check `context.request_context` for None before accessing this attribute.\"\n            )\n        return str(self.request_context.request_id)\n\n    @property\n    def session_id(self) -> str:\n        \"\"\"Get the MCP session ID for ALL transports.\n\n        Returns the session ID that can be used as a key for session-based\n        data storage (e.g., Redis) to share data between tool calls within\n        the same client session.\n\n        Returns:\n            The session ID for StreamableHTTP transports, or a generated ID\n            for other transports.\n\n        Raises:","sourceCodeStart":684,"sourceCodeEnd":720,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/server/context.py#L684-L720","documentation":"Context.request_id returns the unique MCP request ID from the request_context. When the MCP session has not been established (request_context is None — e.g. the Context is not attached to an in-flight request), there is no request ID to report, so a RuntimeError is raised and the message tells you to check request_context for None first.","triggerScenarios":"Accessing ctx.request_id inside code that runs outside a request lifecycle: at import/setup time, inside on_initialize before the first request, in background tasks, or in tests that build a Context manually.","commonSituations":"Logging middleware hooks that fire before a request exists; startup code calling request_id to correlate logs; unit tests exercising tools without a client session.","solutions":["Gate access on the context: only read ctx.request_id when ctx.request_context is not None.","Move code that needs request_id into the request path (tool/resource/prompt execution, request-scoped middleware).","Use a fallback identifier (e.g. generated uuid) when request_context is None.","In background tasks, capture request_id before spawning the task."],"exampleFix":"// before\nasync def tool(x: int, ctx: Context):\n    log.info(\"rid=%s\", ctx.request_id)  # may raise\n// after\nasync def tool(x: int, ctx: Context):\n    rid = ctx.request_id if ctx.request_context is not None else \"no-request\"\n    log.info(\"rid=%s\", rid)","handlingStrategy":"type-guard","validationCode":"rid = str(ctx.request_context.request_id) if ctx.request_context is not None else None","typeGuard":"def has_request_context(ctx) -> bool:\n    return ctx.request_context is not None","tryCatchPattern":"try:\n    rid = ctx.request_id\nexcept RuntimeError:\n    rid = str(uuid.uuid4())  # no active request","preventionTips":["Only read request-scoped properties inside tool/resource/prompt handlers or request middleware.","Check ctx.request_context for None before any request_id/session access.","Capture request-scoped values before spawning background tasks."],"tags":["python","context","request-id","runtimeerror","session-lifecycle"],"backgroundTag":"no-active-request-context","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}