Skip to content

Commit 3e35004

Browse files
committed
Launch mcp at application startup
1 parent aed02d9 commit 3e35004

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

backend/src/neuroagent/app/main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from starlette.responses import JSONResponse
2424

2525
from neuroagent import __version__
26+
from neuroagent.mcp import MCPClient
2627
from neuroagent.app.app_utils import get_semantic_router, setup_engine
2728
from neuroagent.app.config import Settings
2829
from neuroagent.app.dependencies import (
@@ -113,6 +114,14 @@ async def lifespan(fastapi_app: FastAPI) -> AsyncContextManager[None]: # type:
113114
semantic_router = get_semantic_router(settings=app_settings)
114115
app.state.semantic_router = semantic_router
115116

117+
if app_settings.mcp.config_path is not None:
118+
mcp_client: MCPClient | None = MCPClient(
119+
config_path=app_settings.mcp.config_path,
120+
)
121+
await mcp_client.connect_to_servers()
122+
else:
123+
mcp_client = None
124+
116125
async with aclosing(
117126
AsyncAccountingSessionFactory(
118127
base_url=app_settings.accounting.base_url,
@@ -130,6 +139,9 @@ async def lifespan(fastapi_app: FastAPI) -> AsyncContextManager[None]: # type:
130139
if fastapi_app.state.redis_client is not None:
131140
await fastapi_app.state.redis_client.aclose()
132141

142+
if mcp_client is not None:
143+
await mcp_client.cleanup()
144+
133145

134146
app = FastAPI(
135147
title="Agents",

backend/src/neuroagent/mcp.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import asyncio
44
import json
5+
import logging
56
from contextlib import AsyncExitStack
67
from pathlib import Path
78

89
from mcp import ClientSession, StdioServerParameters
910
from mcp.client.stdio import stdio_client
1011
from pydantic import BaseModel
1112

13+
logger = logging.getLogger(__name__)
14+
1215

1316
class MCPServerConfig(BaseModel):
1417
command: str
@@ -45,7 +48,7 @@ async def connect_to_servers(self) -> None:
4548
server_script_path: Path to the server script (.py or .js)
4649
"""
4750
for name, server_config in self.config.servers.items():
48-
print(f"Connecting to server: {name}")
51+
logger.info(f"Connecting to server: {name}")
4952
server_params = StdioServerParameters(
5053
command=server_config.command,
5154
args=server_config.args or [],
@@ -64,15 +67,15 @@ async def connect_to_servers(self) -> None:
6467
# List available tools
6568
response = await self.sessions[name].list_tools()
6669
tools = response.tools
67-
print("\nConnected to server with tools:", [tool.name for tool in tools])
6870

69-
async def cleanup(self):
71+
async def cleanup(self) -> None:
7072
"""Clean up resources"""
71-
# await self.exit_stack.aclose()
7273

73-
for name, stack in self.exit_stack.items():
74-
print(f"Cleaning up server: {name}")
75-
await stack.aclose()
74+
# Unfortunately, the below code seems to have an issue
75+
76+
# for name, stack in self.exit_stack.items():
77+
# logger.info(f"Cleaning up server: {name}")
78+
# await stack.aclose()
7679

7780

7881
async def main():

0 commit comments

Comments
 (0)