Patterns
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
Important Notes
- HTTP requests are only available when FastMCP is running as part of a web application
- Accessing the HTTP request outside of a web request context will raise a
RuntimeError
- The
get_http_request()
function returns a standard Starlette Request object