Skip to content

Commit bc8fa74

Browse files
committed
use stateful http transport session manager instead
1 parent ab110bc commit bc8fa74

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

fastapi_mcp/server.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from fastapi_mcp.openapi.convert import convert_openapi_to_mcp_tools
1212
from fastapi_mcp.transport.sse import FastApiSseTransport
13-
from fastapi_mcp.transport.http import FastApiStreamableHttpTransport
13+
from fastapi_mcp.transport.http import FastApiHttpSessionManager
1414
from fastapi_mcp.types import HTTPRequestInfo, AuthConfig
1515

1616
import logging
@@ -119,6 +119,7 @@ def __init__(
119119
)
120120

121121
self._forward_headers = {h.lower() for h in headers}
122+
self._http_transport: FastApiHttpSessionManager | None = None # Store reference to HTTP transport for cleanup
122123

123124
self.setup_server()
124125

@@ -230,7 +231,7 @@ def _register_mcp_endpoints_sse(
230231
def _register_mcp_http_endpoint(
231232
self,
232233
router: FastAPI | APIRouter,
233-
transport: FastApiStreamableHttpTransport,
234+
transport: FastApiHttpSessionManager,
234235
mount_path: str,
235236
dependencies: Optional[Sequence[params.Depends]],
236237
):
@@ -247,7 +248,7 @@ async def handle_mcp_streamable_http(request: Request):
247248
def _register_mcp_endpoints_http(
248249
self,
249250
router: FastAPI | APIRouter,
250-
transport: FastApiStreamableHttpTransport,
251+
transport: FastApiHttpSessionManager,
251252
mount_path: str,
252253
dependencies: Optional[Sequence[params.Depends]],
253254
):
@@ -347,11 +348,12 @@ def mount_http(
347348

348349
assert isinstance(router, (FastAPI, APIRouter)), f"Invalid router type: {type(router)}"
349350

350-
http_transport = FastApiStreamableHttpTransport(mcp_server=self.server)
351+
http_transport = FastApiHttpSessionManager(mcp_server=self.server)
351352
dependencies = self._auth_config.dependencies if self._auth_config else None
352353

353354
self._register_mcp_endpoints_http(router, http_transport, mount_path, dependencies)
354355
self._setup_auth()
356+
self._http_transport = http_transport # Store reference
355357

356358
# HACK: If we got a router and not a FastAPI instance, we need to re-include the router so that
357359
# FastAPI will pick up the new routes we added. The problem with this approach is that we assume

0 commit comments

Comments
 (0)