mirror of
https://github.com/mcp-use/mcp-use.git
synced 2026-09-14 18:06:01 +08:00
be9942a4d4
* feat: server first implementation * docs: docs for server and images * chore: locked langchain dependencies in pyproject.toml (#321) * python/release/1.3.13 * catch uvicorn logs && log session id * fix debug logic * docs: logging update * fix: icon of elicitation * fix installation paths for local clones * docs: minor restructuring * docs: reorganization, killed non existing pages * docs: beta tag on server * chore: example servers * feat: inspector autopopulated and client only * feat: enhance MCP logging system with customizable formats and improved middleware * feat: implement weather alert and forecast tools in FastMCP example server * mcp path configurable * fix inspector * export as FastMCP too * chore: update fastmcp migration example * lock inspector version * type runner * unhide docs and remove dangers * migration diff * docs and context example * export context * logging improvement * pretty JSON RPC * autoConnect url param in inspecotr * move logging * type check * Server Router * show response logs * fallback tool name to function name in router registration * compatibility export * Introduced ty and prek as type checker and precommit engine respectively - moved ruff config in pyproject toml * add ty check * ty runs only on staged files * ty configuration fix * move startup info in logging * remove bug from action * update claude rules * fix resource serialization * comments on inspector errors * abstracted runner transports * remove redunant call for network * fix ip getter * add inspector mcp path * response limit logging * disable ty check in action * moved transport type to types * support sse * show transport type in logs * change default transport * refactored tests to use mcp server * removed fastmcp * file restructuring * test table * sampling, elicitation, tool disabling enabling * deprecte sse server side * simplified report * fix action * remove sse deprecated test * move tests files to allow for server integration tests * docs * reorg integration test files * api-reference-doxs * fix broken links * telemetry * remove feature useage from scarf
67 lines
2.2 KiB
Plaintext
67 lines
2.2 KiB
Plaintext
|
|
# FastAPI Mounting Guide
|
|
|
|
You can mount an `MCPServer` (or `FastMCP`) instance within an existing FastAPI application. This allows you to serve your MCP tools alongside your existing API endpoints.
|
|
|
|
## Basic Integration
|
|
|
|
When mounting `MCPServer`, we need to ensure its internal task group (used for managing SSE streams) is properly initialized. We do this using FastAPI's lifespan events.
|
|
|
|
```python
|
|
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI
|
|
from mcp_use.server import MCPServer
|
|
import uvicorn
|
|
import anyio
|
|
|
|
# 1. Create your MCP Server
|
|
mcp = MCPServer("my-agent-server")
|
|
|
|
@mcp.tool()
|
|
def add(a: int, b: int) -> int:
|
|
return a + b
|
|
|
|
# 2. Define lifespan to manage MCP server startup/shutdown
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# Initialize the SSE task group
|
|
async with anyio.create_task_group() as tg:
|
|
# Inject the task group into the session manager
|
|
mcp.session_manager._task_group = tg
|
|
yield
|
|
|
|
# 3. Create your existing FastAPI app with lifespan
|
|
app = FastAPI(title="My Existing App", lifespan=lifespan)
|
|
|
|
@app.get("/")
|
|
def read_root():
|
|
return {"message": "Hello from the main app"}
|
|
|
|
# 4. Mount the MCP Server
|
|
mount_path = "/agent"
|
|
app.mount(mount_path, mcp.app)
|
|
|
|
if __name__ == "__main__":
|
|
print(f"Serving MCP at http://localhost:8000{mount_path}/mcp")
|
|
uvicorn.run(app, port=8000)
|
|
```
|
|
|
|
## Important Considerations
|
|
|
|
1. **Lifespan Management:** The critical part is wrapping the application lifespan with an `anyio.create_task_group()` and assigning it to `mcp.session_manager._task_group`. This works around `FastMCP`'s assumption that it controls the main loop.
|
|
2. **Transport:** Mounting implies using the **Streamable HTTP** transport pattern.
|
|
3. **Paths:** The `MCPServer` has internal routes like `/mcp` (for SSE/POST) and `/docs`. When mounted at `/agent`, these become `/agent/mcp` and `/agent/docs`.
|
|
|
|
## Example with Shared State
|
|
|
|
If you want your MCP tools to access the main application's state (like a database connection), you can pass that context into the tool or use a closure.
|
|
|
|
```python
|
|
fake_db = {"items": []}
|
|
|
|
@mcp.tool()
|
|
def add_item(name: str):
|
|
fake_db["items"].append(name)
|
|
return f"Added {name}"
|
|
```
|