Skip to main content

fastmcp.server.http

Functions

set_http_request

create_base_app

Create a base Starlette app with common middleware and routes. Args:
  • routes: List of routes to include in the app
  • middleware: List of middleware to include in the app
  • debug: Whether to enable debug mode
  • lifespan: Optional lifespan manager for the app
Returns:
  • A Starlette application

create_sse_app

Return an instance of the SSE server app. Args:
  • server: The FastMCP server instance
  • message_path: Path for SSE messages
  • sse_path: Path for SSE connections
  • auth: Optional authentication provider (AuthProvider)
  • debug: Whether to enable debug mode
  • routes: Optional list of custom routes
  • middleware: Optional list of middleware
Returns: A Starlette application with RequestContextMiddleware

create_streamable_http_app

Return an instance of the StreamableHTTP server app. Args:
  • server: The FastMCP server instance
  • streamable_http_path: Path for StreamableHTTP connections
  • event_store: Optional event store for SSE polling/resumability
  • retry_interval: Optional retry interval in milliseconds for SSE polling. Controls how quickly clients should reconnect after server-initiated disconnections. Requires event_store to be set. Defaults to SDK default.
  • auth: Optional authentication provider (AuthProvider)
  • json_response: Whether to use JSON response format
  • stateless_http: Whether to use stateless mode (new transport per request)
  • debug: Whether to enable debug mode
  • routes: Optional list of custom routes
  • middleware: Optional list of middleware
  • host_origin_protection: Whether to validate Host and Origin headers before requests reach the MCP endpoint. Defaults to False for compatibility. “auto” protects localhost-bound servers and explicit host/origin allowlists.
  • allowed_hosts: Additional hostnames that may appear in the Host header.
  • allowed_origins: Additional browser origins trusted by the request guard. Configure CORS separately when browser JavaScript must read cross-origin responses.
  • session_idle_timeout: Maximum time in seconds a session may remain idle before it is terminated. The deadline is pushed forward on every request. When None, sessions never expire from inactivity. Not supported in stateless mode.
Returns:
  • A Starlette application with StreamableHTTP support

Classes

FastMCPStreamableHTTPSessionManager

Session manager that scopes resumability storage per transport session. Methods:

event_store

event_store

StreamableHTTPASGIApp

ASGI application wrapper for Streamable HTTP server transport.

HostOriginGuardMiddleware

Validate Host and Origin headers before requests reach MCP sessions.

StarletteWithLifespan

Methods:

lifespan

RequestContextMiddleware

Middleware that stores each request in a ContextVar and sets transport type.