Skip to content

Commit 3fa704d

Browse files
improve stdio server logging
1 parent 6ef20bc commit 3fa704d

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

mcp_bridge/mcp_clients/AbstractClient.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ def __init__(self, name: str) -> None:
2929
self.session = None
3030
self.name = name
3131

32+
logger.debug(f"initializing client class for {name}")
33+
3234
@abstractmethod
3335
async def _maintain_session(self):
3436
pass
@@ -39,7 +41,9 @@ async def _session_maintainer(self):
3941
await self._maintain_session()
4042
except Exception as e:
4143
logger.trace(f"failed to maintain session for {self.name}: {e}")
42-
await asyncio.sleep(0.5)
44+
45+
logger.debug(f"restarting session for {self.name}")
46+
await asyncio.sleep(0.5)
4347

4448
async def start(self):
4549
asyncio.create_task(self._session_maintainer())
@@ -122,7 +126,7 @@ async def list_prompts(self) -> ListPromptsResult:
122126
logger.error(f"error listing prompts: {e}")
123127
return ListPromptsResult(prompts=[])
124128

125-
async def _wait_for_session(self, timeout: int = 10, http_error: bool = True):
129+
async def _wait_for_session(self, timeout: int = 5, http_error: bool = True):
126130
try:
127131
async with asyncio.timeout(timeout):
128132
while self.session is None:
@@ -132,10 +136,10 @@ async def _wait_for_session(self, timeout: int = 10, http_error: bool = True):
132136
except asyncio.TimeoutError:
133137
if http_error:
134138
raise HTTPException(
135-
status_code=500, detail="Could not connect to MCP server."
139+
status_code=500, detail=f"Could not connect to MCP server \"{self.name}\"."
136140
)
137141

138-
raise TimeoutError("Session initialization timed out.")
142+
raise TimeoutError(f"Could not connect to MCP server \"{self.name}\"." )
139143

140144
assert self.session is not None, "Session is None"
141145

mcp_bridge/mcp_clients/StdioClient.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, name: str, config: StdioServerParameters) -> None:
2424
if not any(keyword in key for keyword in venv_keywords)
2525
}
2626

27-
logger.debug(f"env: {env}")
27+
# logger.debug(f"env: {env}")
2828

2929
if config.env is not None:
3030
env.update(config.env)
@@ -43,8 +43,13 @@ def __init__(self, name: str, config: StdioServerParameters) -> None:
4343
self.config = own_config
4444

4545
async def _maintain_session(self):
46+
logger.debug(f"starting maintain session for {self.name}")
4647
async with stdio_client(self.config) as client:
48+
logger.debug(f"entered stdio_client context manager for {self.name}")
49+
assert client[0] is not None, f"missing read stream for {self.name}"
50+
assert client[1] is not None, f"missing write stream for {self.name}"
4751
async with ClientSession(*client) as session:
52+
logger.debug(f"entered client session context manager for {self.name}")
4853
await session.initialize()
4954
logger.debug(f"finished initialise session for {self.name}")
5055
self.session = session

0 commit comments

Comments
 (0)