Skip to content

Commit aae8a17

Browse files
feat(mcp): enhance MCP server integration
- Add streamable HTTP and SSE transport apps for MCP server - Update lifespan management to support both app types - Modify mounting logic for improved compatibility
1 parent 43e7efd commit aae8a17

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

.mcp.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"mcpServers": {
3+
"pyplots": {
4+
"command": "npx",
5+
"args": ["-y", "mcp-remote", "https://api.pyplots.ai/sse/"]
6+
}
7+
}
8+
}

api/main.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@
4343
logger = logging.getLogger(__name__)
4444

4545

46-
# Create MCP HTTP app (needed for lifespan integration)
47-
mcp_http_app = mcp_server.http_app(path="/mcp")
46+
# Create MCP HTTP apps (needed for lifespan integration)
47+
# Streamable HTTP (modern, recommended) - used by newer clients
48+
mcp_http_app = mcp_server.http_app(path="/")
49+
# SSE transport (legacy, wider compatibility) - used by mcp-remote, older clients
50+
mcp_sse_app = mcp_server.http_app(path="/", transport="sse")
4851

4952

5053
@asynccontextmanager
@@ -60,10 +63,11 @@ async def lifespan(app: FastAPI):
6063
except Exception as e:
6164
logger.error(f"Failed to initialize database: {e}")
6265

63-
# Initialize MCP server lifespan (required for session management)
66+
# Initialize MCP server lifespans (required for session management)
6467
async with mcp_http_app.lifespan(app):
65-
logger.info("MCP server initialized")
66-
yield
68+
async with mcp_sse_app.lifespan(app):
69+
logger.info("MCP server initialized (HTTP + SSE)")
70+
yield
6771

6872
# Cleanup database connection
6973
logger.info("Shutting down pyplots API...")
@@ -132,9 +136,10 @@ async def add_cache_headers(request: Request, call_next):
132136
return response
133137

134138

135-
# Mount MCP server for AI assistant integration
136-
# Note: mcp_http_app is created earlier with lifespan integration
137-
app.mount("/mcp", mcp_http_app)
139+
# Mount MCP servers for AI assistant integration
140+
# Note: Apps are created earlier with lifespan integration
141+
app.mount("/mcp", mcp_http_app) # Streamable HTTP at /mcp
142+
app.mount("/sse", mcp_sse_app) # SSE transport at /sse
138143

139144
# Register routers
140145
app.include_router(health_router)

0 commit comments

Comments
 (0)