-
Notifications
You must be signed in to change notification settings - Fork 799
fix: route MCP server log notifications to loguru instead of TUI #1637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -269,10 +269,17 @@ async def load_mcp_tools( | |
| connected. | ||
| """ | ||
| import fastmcp | ||
| from fastmcp.client.logging import LogMessage | ||
| from fastmcp.mcp_config import MCPConfig, RemoteMCPServer | ||
|
|
||
| from kimi_cli.ui.shell.prompt import toast | ||
|
|
||
| async def _mcp_log_handler(message: LogMessage) -> None: | ||
| """Route MCP server log notifications to loguru instead of rich stderr.""" | ||
| data = message.data | ||
| msg = data.get("message") or data.get("msg") or str(data) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new Useful? React with 👍 / 👎. |
||
| logger.debug("MCP server log: {msg}", msg=msg) | ||
|
|
||
| async def _check_oauth_tokens(server_url: str) -> bool: | ||
| """Check if OAuth tokens exist for the server.""" | ||
| try: | ||
|
|
@@ -372,7 +379,10 @@ async def _connect(): | |
| if isinstance(server_config, RemoteMCPServer) and server_config.auth == "oauth": | ||
| oauth_servers[server_name] = server_config.url | ||
|
|
||
| client = fastmcp.Client(MCPConfig(mcpServers={server_name: server_config})) | ||
| client = fastmcp.Client( | ||
| MCPConfig(mcpServers={server_name: server_config}), | ||
| log_handler=_mcp_log_handler, | ||
| ) | ||
| self._mcp_servers[server_name] = MCPServerInfo( | ||
| status="pending", client=client, tools=[] | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 AttributeError when MCP server sends non-dict log data
The
_mcp_log_handlercallsdata.get("message")onmessage.data, but per the MCP spec,datais typed asAnyand can be any JSON-serializable value — including a plain string, number, or list. If an MCP server sends a log notification with a stringdata(e.g."Server started"), calling.get()on it raisesAttributeError: 'str' object has no attribute 'get'. This exception propagates up frommcp.client.session:_received_notification(line 431 ofmcp/client/session.py) and can crash the MCP session, potentially disconnecting the server.Suggested fix
Check if
datais a dict before calling.get():Was this helpful? React with 👍 or 👎 to provide feedback.