fix: redirect console logging to stderr for MCP stdio transport#6
Open
Jasper-1222 wants to merge 1 commit into
Open
fix: redirect console logging to stderr for MCP stdio transport#6Jasper-1222 wants to merge 1 commit into
Jasper-1222 wants to merge 1 commit into
Conversation
The setup_logging function configured StreamHandler(sys.stdout), which interleaves log lines with MCP JSON-RPC protocol messages on stdout. This violates the MCP stdio transport specification, which requires uncontaminated stdout for JSON-RPC wire format frames. Changed to sys.stderr so structured logs go to stderr while stdout remains clean for the MCP protocol. This is the Python convention: stdout for program data, stderr for diagnostics. The file handler continues to write to logs/mcp2anp.log as before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
修复 stdio 模式下日志输出污染 MCP 协议通道的问题。
现象
启动 mcp2anp stdio 模式后,
list_tools可以正常工作,但call_tool偶尔失败。错误原因是 structlog 的 ConsoleRenderer 将彩色格式的日志行输出到了 stdout,与 MCP JSON-RPC 协议帧混合,导致客户端解析 JSON-RPC 消息失败。根因
setup_logging函数在mcp2anp/utils/logging.py第 33 行配置了StreamHandler(sys.stdout)。MCP stdio 传输协议要求 stdout 只传输 JSON-RPC 协议帧,所有诊断输出必须走 stderr。这是 Python 社区的标准约定。修复
将
StreamHandler(sys.stdout)改为StreamHandler(sys.stderr)。logs/mcp2anp.log(不受影响)Verification
本地测试通过,8/8 MCP bridge tests pass。
Risk
低风险。仅影响日志输出流,不涉及业务逻辑。