HTTP Requests
Accessing and using HTTP requests in FastMCP servers
New in version: 2.2.11
Overview
When running FastMCP as a web server, your MCP tools, resources, and prompts might need to access the underlying HTTP request information, such as headers, client IP, or query parameters.
FastMCP provides a clean way to access HTTP request information through a dependency function.
Accessing HTTP Requests
The recommended way to access the current HTTP request is through the get_http_request()
dependency function:
This approach works anywhere within a request’s execution flow, not just within your MCP function. It’s useful when:
- You need access to HTTP information in helper functions
- You’re calling nested functions that need HTTP request data
- You’re working with middleware or other request processing code
Accessing HTTP Headers Only
If you only need request headers and want to avoid potential errors, you can use the get_http_headers()
helper:
By default, get_http_headers()
excludes problematic headers like host
and content-length
. To include all headers, use get_http_headers(include_all=True)
.
Important Notes
- HTTP requests are only available when FastMCP is running as part of a web application
- Accessing the HTTP request with
get_http_request()
outside of a web request context will raise aRuntimeError
- The
get_http_headers()
function never raises errors - it returns an empty dict when no request context is available - The
get_http_request()
function returns a standard Starlette Request object