#!/usr/bin/env python3 """Apply the upstream Hermes API-server MCP refresh workaround. Hermes 0.20.5 creates an API-server AIAgent without rebuilding its tool snapshot from the live MCP registry. Healthy MCP servers that finish discovery after process startup are therefore invisible through the OpenAI compatible API even though ``hermes mcp test`` can reach them. This startup patch is deliberately small and fail-closed: it accepts either the pristine upstream source or an already patched source, and refuses to modify an unknown future implementation. """ from pathlib import Path TARGET = Path("/opt/hermes/gateway/platforms/api_server.py") ANCHOR = " agent = AIAgent(**agent_kwargs)\n" PATCHED = """ # Athena workaround for upstream issue #69746: the API platform # otherwise snapshots tools before the live MCP registry is attached. agent = AIAgent(**agent_kwargs) try: from tools.mcp_tool import refresh_agent_mcp_tools refresh_agent_mcp_tools(agent, quiet_mode=True) except Exception: logger.warning( "api_server failed to refresh the live MCP tool registry", exc_info=True, ) """ source = TARGET.read_text(encoding="utf-8") if PATCHED in source: print("Hermes API MCP refresh patch already present") elif source.count(ANCHOR) == 1: TARGET.write_text(source.replace(ANCHOR, PATCHED, 1), encoding="utf-8") print("Applied Hermes API MCP refresh patch") else: raise SystemExit( "HERMES_START_REFUSED: upstream api_server.py changed; " "review the MCP refresh patch before upgrading" )