{"record":{"id":"a834d362194626e5","repo":"PrefectHQ/fastmcp","slug":"no-active-http-request-found","errorCode":null,"errorMessage":"No active HTTP request found.","messagePattern":"No active HTTP request found\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/server/dependencies.py","lineNumber":537,"sourceCode":"\ndef get_http_request() -> Request:\n    \"\"\"Get the current HTTP request.\n\n    Tries MCP SDK's request_ctx first, then falls back to FastMCP's HTTP context.\n    \"\"\"\n    # Try FastMCP's request context first (set during normal MCP request handling)\n    request = None\n    fastmcp_ctx = fastmcp_request_ctx.get()\n    if fastmcp_ctx is not None:\n        request = fastmcp_ctx.request\n\n    # Fallback to FastMCP's HTTP context variable\n    # This is needed during `on_initialize` middleware where request_ctx isn't set yet\n    if request is None:\n        request = _current_http_request.get()\n\n    if request is None:\n        raise RuntimeError(\"No active HTTP request found.\")\n    return request\n\n\ndef get_http_headers(\n    include_all: bool = False,\n    include: set[str] | None = None,\n) -> dict[str, str]:\n    \"\"\"Extract headers from the current HTTP request if available.\n\n    Never raises an exception, even if there is no active HTTP request (in which case\n    an empty dict is returned).\n\n    By default, strips problematic headers like `content-length`, and credential\n    headers like `authorization` and `cookie`, that cause issues if forwarded to\n    downstream services. If `include_all` is True, all headers are returned.\n\n    The `include` parameter allows specific headers to be included even if they would\n    normally be excluded. This is useful for proxy transports that need to forward","sourceCodeStart":519,"sourceCodeEnd":555,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/server/dependencies.py#L519-L555","documentation":"get_http_request() returns the Starlette Request for the current call, first from the MCP SDK request context and then from FastMCP's _current_http_request contextvar. This RuntimeError is thrown when neither is populated — the code is running where no HTTP request is in scope (e.g. stdio transport, background workers, or before initialization middleware completes).","triggerScenarios":"Calling get_http_request() (directly or via get_http_headers/get_access_token) under stdio transport; in background task workers without HTTP context propagation; in on_initialize middleware before request_ctx is set; in non-HTTP tests.","commonSituations":"The same server run over both stdio and HTTP, with code that unconditionally reads HTTP headers; tasks scheduled with asyncio.create_task losing the contextvar; unit tests exercising tools without an HTTP transport.","solutions":["Guard with try/except RuntimeError and use a fallback when no HTTP request exists (the library itself does this pattern).","Only use HTTP-dependent helpers (headers, access token) in code paths reachable exclusively over HTTP transport.","For background tasks, capture the needed request data up front instead of re-resolving the request inside the task."],"exampleFix":"// before\nheaders = get_http_headers()\n// after\ntry:\n    headers = get_http_headers()\nexcept RuntimeError:\n    headers = {}  # non-HTTP transport (e.g. stdio)","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    request = get_http_request()\nexcept RuntimeError:\n    request = None  # stdio or background context; use defaults\nheaders = dict(request.headers) if request else {}","preventionTips":["Gate HTTP-specific code on the transport actually in use","Capture request data before spawning background tasks","Test the same server over stdio to catch these paths"],"tags":["http","context","transport"],"backgroundTag":"no-active-context","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}