Skip to content

Commit eceece2

Browse files
committed
feat(mcp): dynamically manage mcp context managers
1 parent 4c8afef commit eceece2

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

backend/api/core/dependencies.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from contextlib import asynccontextmanager
1+
from contextlib import AsyncExitStack, asynccontextmanager
22
from typing import Annotated, AsyncGenerator
33

44
from fastapi import Depends
@@ -41,12 +41,19 @@ async def setup_graph() -> AsyncGenerator[Resource]:
4141
async with checkpointer_context(
4242
settings.checkpoint_conn_str
4343
) as checkpointer:
44-
async with mcp_sse_client() as session:
45-
tools = await load_mcp_tools(session)
44+
tools = []
45+
sessions = []
46+
async with AsyncExitStack() as stack:
47+
for hostname in settings.mcp_hostnames:
48+
session = await stack.enter_async_context(
49+
mcp_sse_client(hostname)
50+
)
51+
tools += await load_mcp_tools(session)
52+
sessions.append(session)
4653
yield Resource(
4754
checkpointer=checkpointer,
4855
tools=tools,
49-
session=session,
56+
sessions=sessions,
5057
)
5158

5259

backend/api/core/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Resource(BaseModel):
88
checkpointer: AsyncPostgresSaver
99
tools: list[StructuredTool]
10-
session: ClientSession
10+
sessions: list[ClientSession]
1111

1212
class Config:
1313
arbitrary_types_allowed = True

0 commit comments

Comments
 (0)