### Install FastAPI-MCP with uv Source: https://fastapi-mcp.tadata.com/getting-started/installation Use this command to add FastAPI-MCP to your project with uv. ```bash uv add fastapi-mcp ``` -------------------------------- ### Install FastAPI-MCP with uv pip or pip Source: https://fastapi-mcp.tadata.com/getting-started/installation Install FastAPI-MCP using either uv pip or the standard pip package installer. ```bash uv pip install fastapi-mcp ``` ```bash pip install fastapi-mcp ``` -------------------------------- ### Run MCP Inspector Source: https://fastapi-mcp.tadata.com/getting-started/FAQ Use this command to start the MCP Inspector in a new terminal. Connect to your MCP server by entering the mount path URL. ```bash npx @modelcontextprotocol/inspector ``` -------------------------------- ### Mount HTTP MCP Server with FastAPI Source: https://fastapi-mcp.tadata.com/ Use this snippet to quickly set up a secured MCP server for your FastAPI application. It requires minimal setup and automatically exposes your API at the /mcp endpoint. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() mcp = FastApiMCP(app) mcp.mount_http() ``` -------------------------------- ### Combine Operation and Tag Filtering Source: https://fastapi-mcp.tadata.com/configurations/customization Use both operation IDs and tags to precisely control which endpoints are exposed. This example includes a specific operation only if it also has a specific tag. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() mcp = FastApiMCP( app, include_operations=["user_login"], include_tags=["public"] ) mcp.mount_http() ``` -------------------------------- ### Configure mcp-remote Client Connection to MCP Server Source: https://fastapi-mcp.tadata.com/getting-started/quickstart Configuration for using `mcp-remote` as a bridge for MCP clients that do not support SSE or require authentication. This setup allows for more advanced connection options. ```json { "mcpServers": { "fastapi-mcp": { "command": "npx", "args": [ "mcp-remote", "http://localhost:8000/mcp", "8080" // Optional port number. Necessary if you want your OAuth to work and you don't have dynamic client registration. ] } } } ``` -------------------------------- ### Create a Basic MCP Server with FastAPI Source: https://fastapi-mcp.tadata.com/getting-started/quickstart Import FastAPI and FastApiMCP, then wrap your FastAPI app with FastApiMCP and mount the HTTP server. This is the foundational step for creating an MCP server. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP # Create (or import) a FastAPI app app = FastAPI() # Create an MCP server based on this app mcp = FastApiMCP(app) # Mount the MCP server directly to your app mcp.mount_http() ``` -------------------------------- ### FastAPI MCP AuthConfig with setup_proxies=True Source: https://fastapi-mcp.tadata.com/advanced/auth Configure FastAPI MCP with Auth0 details, enabling `setup_proxies=True` to ensure compatibility with MCP clients by creating proxy endpoints. ```python mcp = FastApiMCP( app, auth_config=AuthConfig( # Your OAuth provider information issuer="https://auth.example.com", authorize_url="https://auth.example.com/authorize", oauth_metadata_url="https://auth.example.com/.well-known/oauth-authorization-server", # Credentials registered with your OAuth provider client_id="your-client-id", client_secret="your-client-secret", # Recommended, since some clients don't specify them audience="your-api-audience", default_scope="openid profile email", # Your auth checking dependency dependencies=[Depends(verify_auth)], # Create compatibility proxies - usually needed! setup_proxies=True, ), ) ``` -------------------------------- ### Configure FastApiMCP for Full OAuth 2 Flow Source: https://fastapi-mcp.tadata.com/advanced/auth Set up FastApiMCP to support the full OAuth 2 flow by providing necessary authentication server details. This configuration requires a `verify_auth` dependency. ```python from fastapi import Depends from fastapi_mcp import FastApiMCP, AuthConfig mcp = FastApiMCP( app, name="MCP With OAuth", auth_config=AuthConfig( issuer=f"https://auth.example.com/", authorize_url=f"https://auth.example.com/authorize", oauth_metadata_url=f"https://auth.example.com/.well-known/oauth-authorization-server", audience="my-audience", client_id="my-client-id", client_secret="my-client-secret", dependencies=[Depends(verify_auth)], setup_proxies=True, ), ) mcp.mount_http() ``` -------------------------------- ### Define Server Metadata Source: https://fastapi-mcp.tadata.com/configurations/customization Set the server name and description when initializing FastApiMCP. This metadata is used to identify your API. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() mcp = FastApiMCP( app, name="My API MCP", description="Very cool MCP server", ) mcp.mount_http() ``` -------------------------------- ### Configure Custom HTTP Client for FastAPI-MCP Source: https://fastapi-mcp.tadata.com/advanced/asgi Provide a custom httpx.AsyncClient to FastApiMCP for custom base URLs or timeouts. This bypasses the default ASGI transport. ```python import httpx from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() custom_client = httpx.AsyncClient( base_url="https://api.example.com", timeout=30.0 ) mcp = FastApiMCP( app, http_client=custom_client ) mcp.mount() ``` -------------------------------- ### Run FastAPI MCP Server with Uvicorn Source: https://fastapi-mcp.tadata.com/getting-started/quickstart Integrate uvicorn to run your FastAPI application, which includes the mounted MCP server. This allows you to serve the MCP at a specified host and port. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() mcp = FastApiMCP(app) mcp.mount_http() if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000) ``` -------------------------------- ### Control Tool Description Verbosity Source: https://fastapi-mcp.tadata.com/configurations/customization Configure FastApiMCP to include all response schemas or full JSON schemas in tool descriptions. This is useful for providing more detailed information about API responses. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() mcp = FastApiMCP( app, name="My API MCP", description="Very cool MCP server", describe_all_responses=True, describe_full_response_schema=True ) mcp.mount_http() ``` -------------------------------- ### Configure FastApiMCP with Custom OAuth Metadata Source: https://fastapi-mcp.tadata.com/advanced/auth Provide custom OAuth metadata directly to FastApiMCP for full control over the authentication flow. This is useful for custom OAuth servers or specialized implementations and requires a `verify_auth` dependency. ```python from fastapi import Depends from fastapi_mcp import FastApiMCP, AuthConfig mcp = FastApiMCP( app, name="MCP With Custom OAuth", auth_config=AuthConfig( # Provide your own complete OAuth metadata custom_oauth_metadata={ "issuer": "https://auth.example.com", "authorization_endpoint": "https://auth.example.com/authorize", "token_endpoint": "https://auth.example.com/token", "registration_endpoint": "https://auth.example.com/register", "scopes_supported": ["openid", "profile", "email"], "response_types_supported": ["code"], "grant_types_supported": ["authorization_code"], "token_endpoint_auth_methods_supported": ["none"], "code_challenge_methods_supported": ["S256"] }, # Your auth checking dependency dependencies=[Depends(verify_auth)], ), ) mcp.mount_http() ``` -------------------------------- ### Mount MCP to a Separate FastAPI App Source: https://fastapi-mcp.tadata.com/advanced/deploy Create an MCP server from an existing FastAPI app and mount it to a separate FastAPI application. This allows for independent deployment of the MCP server. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP # Your API app api_app = FastAPI() # ... define your API endpoints on api_app ... # A separate app for the MCP server mcp_app = FastAPI() # Create MCP server from the API app mcp = FastApiMCP(api_app) # Mount the MCP server to the separate app mcp.mount_http(mcp_app) ``` -------------------------------- ### Mount Custom Path with HTTP and SSE Transport Source: https://fastapi-mcp.tadata.com/advanced/transport Configure custom routing and mount MCP with both HTTP and SSE transports to a specific path using an APIRouter. ```python from fastapi import FastAPI, APIRouter from fastapi_mcp import FastApiMCP app = FastAPI() router = APIRouter(prefix="/api/v1") mcp = FastApiMCP(app) # Mount to custom path with HTTP transport mcp.mount_http(router, mount_path="/my-http") # Or with SSE transport mcp.mount_sse(router, mount_path="/my-sse") ``` -------------------------------- ### Run FastAPI Apps Separately with Uvicorn Source: https://fastapi-mcp.tadata.com/advanced/deploy Run the main API application and the MCP server application independently using Uvicorn. Ensure to specify the correct host and port for each application. ```bash uvicorn main:api_app --host api-host --port 8001 uvicorn main:mcp_app --host mcp-host --port 8000 ``` -------------------------------- ### Auth0 Environment Variables Configuration Source: https://fastapi-mcp.tadata.com/advanced/auth Required environment variables for Auth0 integration. Ensure these are set in your `.env` file. ```dotenv AUTH0_DOMAIN=your-tenant.auth0.com AUTH0_AUDIENCE=https://your-tenant.auth0.com/api/v2/ AUTH0_CLIENT_ID=your-client-id AUTH0_CLIENT_SECRET=your-client-secret ``` -------------------------------- ### Add New Endpoint and Refresh Server Source: https://fastapi-mcp.tadata.com/advanced/refresh Add new endpoints to your FastAPI application and then refresh the MCP server to recognize them. Ensure `FastAPI` and `FastApiMCP` are imported. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() mcp = FastApiMCP(app) mcp.mount_http() # Add new endpoints after MCP server creation @app.get("/new/endpoint/", operation_id="new_endpoint") async def new_endpoint(): return {"message": "Hello, world!"} # Refresh the MCP server to include the new endpoint mcp.setup_server() ``` -------------------------------- ### Include Specific Operations Source: https://fastapi-mcp.tadata.com/configurations/customization Expose only a predefined list of operations (endpoints) as MCP tools. This is useful for limiting the MCP server's functionality to specific actions. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() mcp = FastApiMCP( app, include_operations=["get_user", "create_user"] ) mcp.mount_http() ``` -------------------------------- ### Configure mcp-remote for Basic Token Passthrough Source: https://fastapi-mcp.tadata.com/advanced/auth Configure your mcp-remote client to pass a valid authorization header to your FastAPI endpoints. Ensure your token is correctly formatted. ```json { "mcpServers": { "remote-example": { "command": "npx", "args": [ "mcp-remote", "http://localhost:8000/mcp", "--header", "Authorization:${AUTH_HEADER}" ] }, "env": { "AUTH_HEADER": "Bearer " } } } ``` -------------------------------- ### Include Specific Tags Source: https://fastapi-mcp.tadata.com/configurations/customization Expose only operations tagged with specific values. This allows grouping and exposing related endpoints. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() mcp = FastApiMCP( app, include_tags=["users", "public"] ) mcp.mount_http() ``` -------------------------------- ### Mount SSE Transport Source: https://fastapi-mcp.tadata.com/advanced/transport Use this to mount the FastAPI application with SSE transport for backward compatibility with older MCP implementations. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() mcp = FastApiMCP(app) # Mount using SSE transport (backwards compatibility) mcp.mount_sse() ``` -------------------------------- ### Add FastAPI Dependency for Auth Header Verification Source: https://fastapi-mcp.tadata.com/advanced/auth Optionally, add a FastAPI dependency to your FastApiMCP instance to reject requests that do not include an authorization header. This requires defining a `verify_auth` function. ```python from fastapi import Depends from fastapi_mcp import FastApiMCP, AuthConfig mcp = FastApiMCP( app, name="Protected MCP", auth_config=AuthConfig( dependencies=[Depends(verify_auth)], ), ) mcp.mount_http() ``` -------------------------------- ### SSE Transport Client Connection Configuration Source: https://fastapi-mcp.tadata.com/advanced/transport Client configuration for connecting to an MCP server using SSE transport. Specifies the server URL. ```json { "mcpServers": { "fastapi-mcp": { "url": "http://localhost:8000/sse" } } } ``` -------------------------------- ### HTTP Transport Client Connection Configuration Source: https://fastapi-mcp.tadata.com/advanced/transport Client configuration for connecting to an MCP server using HTTP transport. Specifies the server URL. ```json { "mcpServers": { "fastapi-mcp": { "url": "http://localhost:8000/mcp" } } } ``` -------------------------------- ### Mount HTTP Transport Source: https://fastapi-mcp.tadata.com/advanced/transport Use this to mount the FastAPI application with HTTP transport, the recommended method for better session management and connection handling. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() mcp = FastApiMCP(app) # Mount using HTTP transport (recommended) mcp.mount_http() ``` -------------------------------- ### mcp-remote Configuration for Fixed Port Source: https://fastapi-mcp.tadata.com/advanced/auth Configure `mcp-remote` to run on a fixed port (e.g., 8080) to allow proper configuration of OAuth provider callback URLs. ```json { "mcpServers": { "example": { "command": "npx", "args": [ "mcp-remote", "http://localhost:8000/mcp", "8080" ] } } } ``` -------------------------------- ### Define Explicit operation_id for Tool Naming Source: https://fastapi-mcp.tadata.com/configurations/tool-naming Use explicit `operation_id` in FastAPI route definitions for clearer tool names. Auto-generated IDs can be cryptic. ```python # Auto-generated operation_id (something like "read_user_users__user_id__get") @app.get("/users/{user_id}") async def read_user(user_id: int): return {"user_id": user_id} # Explicit operation_id (tool will be named "get_user_info") @app.get("/users/{user_id}", operation_id="get_user_info") async def read_user(user_id: int): return {"user_id": user_id} ``` -------------------------------- ### Exclude Specific Tags Source: https://fastapi-mcp.tadata.com/configurations/customization Prevent operations tagged with specific values from being exposed as MCP tools. This is useful for excluding administrative or internal endpoints. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() mcp = FastApiMCP( app, exclude_tags=["admin", "internal"] ) mcp.mount_http() ``` -------------------------------- ### Exclude Specific Operations Source: https://fastapi-mcp.tadata.com/configurations/customization Prevent specific operations (endpoints) from being exposed as MCP tools. This is useful for hiding sensitive or internal operations. ```python from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() mcp = FastApiMCP( app, exclude_operations=["delete_user"] ) mcp.mount_http() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.