Skip to content

The agent that grows with you

fix: release ACP transport without client MCP discovery

Teknium92d97ba8a444
diff --git a/acp_adapter/entry.py b/acp_adapter/entry.py
index 13dcbcbcc46..9f6ec9ebe87 100644
--- a/acp_adapter/entry.py
+++ b/acp_adapter/entry.py
@@ -182,23 +182,18 @@ def main(argv: list[str] | None = None) -> None:
import acp
from .server import HermesACPAgent
- # MCP discovery from config.yaml runs in a background daemon thread so the ACP server is
- # responsive immediately (blocking here cost 2-5 s); per-session MCP servers registered via
- # asyncio.to_thread are unaffected. Metadata-only hosts can opt out of the global startup.
- # Previously this blocked asyncio.run() for 2-5 s. (ACP also registers per-session MCP servers
- # dynamically via asyncio.to_thread inside the event loop; that path is unaffected.) Moved from
- # model_tools.py module scope to avoid freezing the gateway's loop on lazy import (#16856).
- if os.environ.get("HERMES_ACP_SKIP_CONFIGURED_MCP", "").strip() != "1":
- try:
- from hermes_cli.mcp_startup import start_background_mcp_discovery
+ agent = HermesACPAgent()
- start_background_mcp_discovery(logger=logger, thread_name="acp-mcp-discovery")
- except Exception:
- logger.debug("MCP tool discovery failed at ACP startup", exc_info=True)
+ async def serve():
+ # MCP and execution belong to the daemon. Close only this viewer, while
+ # its event loop is still alive, including on protocol/transport errors.
+ try:
+ await acp.run_agent(agent, use_unstable_protocol=True)
+ finally:
+ await agent.aclose()
- agent = HermesACPAgent()
try:
- asyncio.run(acp.run_agent(agent, use_unstable_protocol=True))
+ asyncio.run(serve())
except KeyboardInterrupt:
logger.info("Shutting down (KeyboardInterrupt)")
except Exception:
diff --git a/tests/acp/test_gateway_entry_lifecycle.py b/tests/acp/test_gateway_entry_lifecycle.py
new file mode 100644
index 00000000000..0433d47476c
--- /dev/null
+++ b/tests/acp/test_gateway_entry_lifecycle.py
@@ -0,0 +1,39 @@
+"""The ACP transport owns neither MCP discovery nor a surviving client socket."""
+import asyncio
+
+import acp
+import pytest
+
+from acp_adapter import entry
+from acp_adapter.gateway_server import GatewayACPAgent
+
+
[email protected]("crash", [False, True])
+def test_entry_closes_transport_without_discovering_mcp(monkeypatch, crash):
+ calls = []
+ agent = GatewayACPAgent()
+
+ async def close():
+ calls.append(("close", asyncio.get_running_loop()))
+
+ async def serve(instance, **kwargs):
+ assert instance is agent
+ calls.append(("serve", asyncio.get_running_loop()))
+ if crash:
+ raise RuntimeError("owned transport failure")
+
+ monkeypatch.setattr(entry, "_setup_logging", lambda: None)
+ monkeypatch.setattr(entry, "_load_env", lambda: None)
+ monkeypatch.setattr("acp_adapter.server.HermesACPAgent", lambda: agent)
+ monkeypatch.setattr(agent, "aclose", close)
+ monkeypatch.setattr(acp, "run_agent", serve)
+ monkeypatch.setattr("hermes_cli.mcp_startup.start_background_mcp_discovery",
+ lambda **kw: calls.append(("mcp", None)))
+ monkeypatch.delenv("HERMES_ACP_SKIP_CONFIGURED_MCP", raising=False)
+ if crash:
+ with pytest.raises(SystemExit):
+ entry.main([])
+ else:
+ entry.main([])
+ assert [name for name, loop in calls] == ["serve", "close"]
+ assert calls[0][1] is calls[1][1]