1010
1111from fastapi_mcp .openapi .convert import convert_openapi_to_mcp_tools
1212from fastapi_mcp .transport .sse import FastApiSseTransport
13- from fastapi_mcp .transport .http import FastApiStreamableHttpTransport
13+ from fastapi_mcp .transport .http import FastApiHttpSessionManager
1414from fastapi_mcp .types import HTTPRequestInfo , AuthConfig
1515
1616import 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