mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-05-06 01:36:17 +00:00
fix(mcp): hint on stdio protocol pollution
This commit is contained in:
parent
3573109408
commit
92ef594b6a
@ -454,7 +454,23 @@ async def connect_mcp_servers(
|
|||||||
return name, server_stack
|
return name, server_stack
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("MCP server '{}': failed to connect: {}", name, e)
|
hint = ""
|
||||||
|
text = str(e).lower()
|
||||||
|
if any(
|
||||||
|
marker in text
|
||||||
|
for marker in (
|
||||||
|
"parse error",
|
||||||
|
"invalid json",
|
||||||
|
"unexpected token",
|
||||||
|
"jsonrpc",
|
||||||
|
"content-length",
|
||||||
|
)
|
||||||
|
):
|
||||||
|
hint = (
|
||||||
|
" Hint: this looks like stdio protocol pollution. Make sure the MCP server writes "
|
||||||
|
"only JSON-RPC to stdout and sends logs/debug output to stderr instead."
|
||||||
|
)
|
||||||
|
logger.error("MCP server '{}': failed to connect: {}{}", name, e, hint)
|
||||||
try:
|
try:
|
||||||
await server_stack.aclose()
|
await server_stack.aclose()
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
@ -356,6 +356,33 @@ async def test_connect_mcp_servers_enabled_tools_warns_on_unknown_entries(
|
|||||||
assert "Available wrapped names: mcp_test_demo" in warnings[-1]
|
assert "Available wrapped names: mcp_test_demo" in warnings[-1]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_connect_mcp_servers_logs_stdio_pollution_hint(
|
||||||
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
|
) -> None:
|
||||||
|
messages: list[str] = []
|
||||||
|
|
||||||
|
def _error(message: str, *args: object) -> None:
|
||||||
|
messages.append(message.format(*args))
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def _broken_stdio_client(_params: object):
|
||||||
|
raise RuntimeError("Parse error: Unexpected token 'INFO' before JSON-RPC headers")
|
||||||
|
yield # pragma: no cover
|
||||||
|
|
||||||
|
monkeypatch.setattr(sys.modules["mcp.client.stdio"], "stdio_client", _broken_stdio_client)
|
||||||
|
monkeypatch.setattr("nanobot.agent.tools.mcp.logger.error", _error)
|
||||||
|
|
||||||
|
registry = ToolRegistry()
|
||||||
|
stacks = await connect_mcp_servers({"gh": MCPServerConfig(command="github-mcp")}, registry)
|
||||||
|
|
||||||
|
assert stacks == {}
|
||||||
|
assert messages
|
||||||
|
assert "stdio protocol pollution" in messages[-1]
|
||||||
|
assert "stdout" in messages[-1]
|
||||||
|
assert "stderr" in messages[-1]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_connect_mcp_servers_one_failure_does_not_block_others(
|
async def test_connect_mcp_servers_one_failure_does_not_block_others(
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user