Compare commits

..

6 Commits

Author SHA1 Message Date
gitprov 07329d8672 Switch Agent0 from MCP to REST API (/api/api_message)
MCP endpoint returns 403 (wrong token) and the mcp library's TaskGroup
error is opaque. The REST API (X-API-KEY header, /api/api_message) is
already validated in connectivity tests and returns proper responses.

mcp_key field is reused as the X-API-KEY value.
context_id replaces chat_id for conversation continuity.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-02 19:44:11 +02:00
gitprov 1006d4490a Clear Hermes session cache on token rejection retry
When the dashboard token is rejected and re-fetched, the stale
session_id was being reused, causing prompt.submit to go to a
non-existent session. Now clears both caches so the retry creates
a fresh session.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-02 19:04:36 +02:00
gitprov 6a259e1ef7 Add full traceback logging to Hermes route for diagnostics
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-26 20:09:09 +02:00
gitprov bb2a4e2293 Strip whitespace from endpoint URLs before use
Leading/trailing spaces in the endpoint field cause httpx to receive
a URL without a recognisable scheme, producing UnsupportedProtocol.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-26 20:06:35 +02:00
gitprov d74d1a74f8 Add debug logging to /v1/agent/chat route handler
Log agent_type, full models payload and selected model on every request
to diagnose endpoint routing and empty-endpoint issues.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-26 20:00:39 +02:00
gitprov 8704404c40 Fix agent0 connectivity test to use health + key check
Use GET /api/health (instant, no LLM call) for reachability and
POST /api/api_reset_chat to verify the API key is accepted.
Sending a real message blocks for the full LLM round-trip (>30s).

Also switch from MCP streamable-http to REST API (X-API-KEY header).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-26 19:09:02 +02:00
2 changed files with 84 additions and 58 deletions
+51 -45
View File
@@ -268,60 +268,50 @@ async def handle_agent0_mcp(
valid_poses: list[str],
) -> ChatResponse:
"""
Send a message to a live Agent Zero instance via its MCP streamable-http
server and return the response as a ChatResponse.
Send a message to a live Agent Zero instance via its REST API
(POST /api/api_message, X-API-KEY header).
MCP URL format: {endpoint}/mcp/t-{mcp_key}/http
Tool called: send_message (built into every Agent Zero instance)
Conversation continuity is maintained via Agent Zero's chat_id, which maps
to a gnommoweb conversation_id in _a0_sessions (in-memory).
Conversation continuity is maintained via Agent Zero's context_id, which
maps to a gnommoweb conversation_id in _a0_sessions (in-memory).
"""
from mcp.client.streamable_http import streamablehttp_client
from mcp import ClientSession
endpoint = (model.endpoint or "").strip().rstrip("/")
api_key = model.mcp_key or "" # stored in mcp_key field; used as X-API-KEY
api_url = f"{endpoint}/api/api_message"
endpoint = (model.endpoint or "").rstrip("/")
mcp_key = model.mcp_key or ""
mcp_url = f"{endpoint}/mcp/t-{mcp_key}/http"
# Retrieve existing Agent0 chat_id for this conversation (if any)
a0_chat_id = _a0_sessions.get(req.conversation_id) if req.conversation_id else None
# Retrieve existing Agent0 context_id for this conversation (if any)
a0_context_id = _a0_sessions.get(req.conversation_id) if req.conversation_id else None
log.info(
f"[{agent.name}] → Agent0 MCP url={mcp_url} "
f"conv={req.conversation_id} a0_chat={a0_chat_id or 'new'} "
f"[{agent.name}] → Agent0 REST url={api_url} "
f"conv={req.conversation_id} a0_ctx={a0_context_id or 'new'} "
f"msg='{req.message[:60]}'"
)
try:
async with streamablehttp_client(mcp_url) as (read, write, _):
async with ClientSession(read, write) as session:
await session.initialize()
payload: dict = {
"message": req.message,
"lifetime_hours": 24,
}
if a0_context_id:
payload["context_id"] = a0_context_id
tool_args: dict = {
"message": req.message,
"persistent_chat": True,
}
if a0_chat_id:
tool_args["chat_id"] = a0_chat_id
async with httpx.AsyncClient(timeout=120.0) as client:
r = await client.post(
api_url,
headers={"X-API-KEY": api_key, "Content-Type": "application/json"},
json=payload,
)
r.raise_for_status()
result = await session.call_tool("send_message", tool_args)
data = r.json()
log.info(f"[{agent.name}] ← Agent0 REST: {str(data)[:300]}")
# The tool returns a JSON-serialised ToolResponse / ToolError
raw = result.content[0].text if result.content else "{}"
log.info(f"[{agent.name}] ← Agent0 MCP raw: {raw[:300]}")
response_text = data.get("response", "")
new_context_id = data.get("context_id", "")
parsed = json.loads(raw)
if parsed.get("status") == "error":
raise RuntimeError(parsed.get("error", "Unknown Agent0 error"))
response_text = parsed.get("response", "")
new_chat_id = parsed.get("chat_id", "")
# Persist the Agent0 chat_id so the next turn continues the same context
if new_chat_id and req.conversation_id is not None:
_a0_sessions[req.conversation_id] = new_chat_id
# Persist context_id for conversation continuity
if new_context_id and req.conversation_id is not None:
_a0_sessions[req.conversation_id] = new_context_id
pose = pick_pose(response_text, valid_poses)
@@ -334,7 +324,7 @@ async def handle_agent0_mcp(
)
except Exception as e:
log.error(f"[{agent.name}] Agent0 MCP error: {e}")
log.error(f"[{agent.name}] Agent0 REST error: {e}")
fallback_pose = next(
(p for p in ("sorry", "annoyed", "neutral") if p in valid_poses),
valid_poses[0],
@@ -362,8 +352,10 @@ async def _fetch_hermes_token(endpoint: str) -> str:
if cached:
return cached
url = f"{endpoint}/"
log.info(f"[hermes] fetching token — GET {url!r} (len={len(url)}, bytes={url.encode()!r})")
async with httpx.AsyncClient(timeout=10.0) as client:
resp = await client.get(f"{endpoint}/")
resp = await client.get(url)
resp.raise_for_status()
html = resp.text
@@ -405,7 +397,7 @@ async def handle_hermes(
except ImportError:
from websockets.client import connect as ws_connect # type: ignore
endpoint = (model.endpoint or "").rstrip("/")
endpoint = (model.endpoint or "").strip().rstrip("/")
hermes_session_id = _hermes_sessions.get(req.conversation_id) if req.conversation_id else None
token = await _fetch_hermes_token(endpoint)
@@ -509,6 +501,9 @@ async def handle_hermes(
if "401" in err_str or "403" in err_str or "Unauthorized" in err_str.lower():
log.warning(f"[{agent.name}] Hermes token rejected, re-fetching…")
_hermes_token_cache.pop(endpoint, None)
if req.conversation_id is not None:
_hermes_sessions.pop(req.conversation_id, None)
hermes_session_id = None # force session.create on retry
try:
token = await _fetch_hermes_token(endpoint)
return await _do_chat(token)
@@ -558,6 +553,12 @@ async def agent_chat(req: ChatRequest, authorization: str = Header(default="")):
valid_poses = agent.poses if agent.poses else ["neutral"]
model = select_model(req.models)
log.info(
f"[route] agent={agent.name!r} agent_type={agent.agent_type!r} "
f"models_payload={req.models} "
f"selected_model={model}"
)
# ── Route: Hermes Agent ───────────────────────────────────────────────────
if agent.agent_type == "hermes":
if not model or not model.endpoint:
@@ -569,7 +570,12 @@ async def agent_chat(req: ChatRequest, authorization: str = Header(default="")):
pose=valid_poses[0],
conversation_id=req.conversation_id,
)
return await handle_hermes(req, agent, model, valid_poses)
try:
return await handle_hermes(req, agent, model, valid_poses)
except Exception as e:
import traceback
log.error(f"[hermes] unhandled exception:\n{traceback.format_exc()}")
raise
# ── Route: Agent0 MCP ─────────────────────────────────────────────────────
if agent.agent_type == "agent0" or (model and model.type == "agent0"):
+33 -13
View File
@@ -21,8 +21,8 @@ Configuration via environment variables (or a local .env file):
LM_STUDIO_URL — LM Studio base URL (default: http://localhost:1234)
LM_STUDIO_MODEL — model to use (default: first available)
HERMES_URL — Hermes dashboard URL (default: http://localhost:50007)
AGENT0_URL — Agent Zero base URL
AGENT0_MCP_KEY — Agent Zero MCP token
AGENT0_URL — Agent Zero base URL (e.g. http://localhost:50003)
AGENT0_API_KEY — Agent Zero X-API-KEY from the agent's dashboard
Usage:
python test_connectivity.py
@@ -287,27 +287,47 @@ async def test_hermes():
# ── Agent Zero ────────────────────────────────────────────────────────────────
async def test_agent0():
section("Agent Zero (MCP)")
section("Agent Zero (REST)")
base_url = os.environ.get("AGENT0_URL", "")
mcp_key = os.environ.get("AGENT0_MCP_KEY", "")
api_key = os.environ.get("AGENT0_API_KEY", "")
if not base_url or not mcp_key:
skip("MCP endpoint", "AGENT0_URL and AGENT0_MCP_KEY not set")
if not base_url or not api_key:
skip("api_message endpoint", "AGENT0_URL and AGENT0_API_KEY not set")
return
mcp_url = f"{base_url.rstrip('/')}/mcp/t-{mcp_key}/http"
url = f"{base_url.rstrip('/')}/api/api_message"
health_url = f"{base_url.rstrip('/')}/api/health"
import httpx
try:
# Step 1: health check (no auth needed, fast)
async with httpx.AsyncClient(timeout=10.0) as client:
r = await client.get(mcp_url, headers={"Accept": "application/json"})
rh = await client.get(health_url)
if r.status_code in (200, 405):
ok("MCP endpoint reachable", f"HTTP {r.status_code} at {mcp_url}")
elif r.status_code == 401:
fail("Bad MCP key", f"HTTP {r.status_code}")
if rh.status_code != 200:
fail("Health endpoint", f"HTTP {rh.status_code}")
return
info = rh.json()
version = info.get("gitinfo", {}).get("version", "unknown")
ok(f"Reachable ({version})", health_url)
# Step 2: verify API key is accepted via api_reset_chat (fast, no LLM call)
reset_url = f"{base_url.rstrip('/')}/api/api_reset_chat"
async with httpx.AsyncClient(timeout=10.0) as client:
rk = await client.post(
reset_url,
headers={"X-API-KEY": api_key, "Content-Type": "application/json"},
json={},
)
if rk.status_code == 200:
ok("API key accepted", f"HTTP {rk.status_code}")
elif rk.status_code in (401, 403):
fail("Bad API key", f"HTTP {rk.status_code}")
else:
ok("MCP endpoint reachable", f"HTTP {r.status_code}")
ok("API key accepted", f"HTTP {rk.status_code}")
except httpx.ConnectError as e:
fail("Not reachable", str(e)[:120])