API Reference

This section provides a detailed reference for the main classes and models in the fastapi-mcp library.

FastApiMCP Class

The main class for creating and managing an MCP server from a FastAPI application.

from fastapi_mcp import FastApiMCP

__init__(...)

Initializes the MCP server instance.

Parameters:

  • fastapi (FastAPI): The FastAPI application to create an MCP server from.
  • name (Optional[str]): Name for the MCP server (defaults to app.title).
  • description (Optional[str]): Description for the MCP server (defaults to app.description).
  • describe_all_responses (bool): Whether to include all possible response schemas in tool descriptions. Defaults to False.
  • describe_full_response_schema (bool): Whether to include full JSON schema for responses in tool descriptions. Defaults to False.
  • http_client (Optional[httpx.AsyncClient]): Optional custom HTTP client to use for API calls to the FastAPI app.
  • include_operations (Optional[List[str]]): List of operation IDs to include as MCP tools. Cannot be used with exclude_operations.
  • exclude_operations (Optional[List[str]]): List of operation IDs to exclude from MCP tools. Cannot be used with include_operations.
  • include_tags (Optional[List[str]]): List of tags to include as MCP tools. Cannot be used with exclude_tags.
  • exclude_tags (Optional[List[str]]): List of tags to exclude from MCP tools. Cannot be used with include_tags.
  • auth_config (Optional[AuthConfig]): Configuration for MCP authentication.
  • headers (List[str]): List of HTTP header names to forward from the incoming MCP request. Defaults to ['authorization'].

mount_http(...)

Mounts the MCP server using the recommended Streamable HTTP transport.

Parameters:

  • router (Optional[FastAPI | APIRouter]): The FastAPI app or APIRouter to mount to. Defaults to the app provided in the constructor.
  • mount_path (str): The path to mount the MCP server on. Defaults to "/mcp".

mount_sse(...)

Mounts the MCP server using Server-Sent Events (SSE) for backwards compatibility.

Parameters:

  • router (Optional[FastAPI | APIRouter]): The FastAPI app or APIRouter to mount to. Defaults to the app provided in the constructor.
  • mount_path (str): The path to mount the MCP server on. Defaults to "/sse".

setup_server()

Refreshes the MCP server's tool list. This is useful if you add FastAPI routes dynamically after FastApiMCP has been initialized.

AuthConfig Model

A Pydantic model for configuring MCP-compliant authentication.

from fastapi_mcp import AuthConfig

Fields:

  • version (Literal["2025-03-26"]): The MCP spec version for authorization. Defaults to "2025-03-26".
  • dependencies (Optional[Sequence[params.Depends]]): A sequence of FastAPI Depends() that will be applied to the MCP endpoint to trigger authentication checks.
  • issuer (Optional[str]): The issuer URL of the OAuth 2.0 server (e.g., your Auth0 domain).
  • oauth_metadata_url (Optional[StrHttpUrl]): The full URL to the OAuth provider's metadata endpoint (.well-known/openid-configuration).
  • authorize_url (Optional[StrHttpUrl]): The URL of the OAuth provider's authorization endpoint.
  • audience (Optional[str]): A default audience to inject into the authorization flow if the client does not provide one.
  • default_scope (str): A default scope to request if the client does not specify one. Defaults to "openid profile email".
  • client_id (Optional[str]): Your application's client ID from the OAuth provider.
  • client_secret (Optional[str]): Your application's client secret.
  • custom_oauth_metadata (Optional[OAuthMetadataDict]): Allows you to provide a complete, custom OAuth metadata dictionary directly.
  • setup_proxies (bool): If True, sets up proxy endpoints to make a non-compliant OAuth provider compatible with MCP. Defaults to False.
  • setup_fake_dynamic_registration (bool): If True (and setup_proxies is true), creates a fake dynamic client registration endpoint required by some MCP clients. Defaults to True.
  • metadata_path (str): The path where the OAuth metadata will be served. Defaults to "/.well-known/oauth-authorization-server".

See the Authentication Guide for detailed usage.