Running Agents
The run loop
POST /api/agents/:id/run { "message": "How many open P1 tickets does Acme Corp have?" }
Step 1: Load agent config → model: claude-sonnet-4-6 → mcpServerIds: ["mcp_builtin"] → systemPrompt: "You are a PSA assistant..."
Step 2: Discover tools from each MCP server → POST http://mcp-server:4000/mcp { method: "tools/list" } → Tools: [ping, list_products, list_subscriptions, ..., connectwise_psa_get_tickets, ...]
Step 3: Send to Claude → messages: [system prompt, user message] → tools: [all 16 tool definitions]
Step 4: Claude responds with tool_use → tool: connectwise_psa_get_tickets → input: { company: "Acme Corp", status: "open", limit: 50 }
Step 5: Execute tool on MCP server → POST /mcp { method: "tools/call", params: { name, arguments } } → result: "[{id:1001, priority:1, summary:'Server down'}, {id:1002,...}]"
Step 6: Send tool result back to Claude → Claude produces final text response
Step 7: Return to caller { result: "Acme Corp has 2 open Priority 1 tickets: ...", toolCalls: [{ tool, input, result }], iterations: 2 }Iteration count
Each time Claude calls a tool and gets a result back, that’s one iteration. The loop continues until:
- Claude produces a response with no tool calls (normal completion)
- 10 iterations are reached (safety cap — returns last response)
Most queries complete in 1–3 iterations. Complex multi-step tasks (e.g. “get all open tickets for Acme, check storage for their backup logs, and summarize”) may need 4–6 iterations.
Interpreting tool calls
The toolCalls array in the response shows everything Claude did:
{ "toolCalls": [ { "tool": "connectwise_psa_get_tickets", "input": { "company": "Acme Corp", "status": "open", "limit": 50 }, "result": "[{\"id\":1001,\"priority\":1,\"summary\":\"Server down\"}]" }, { "tool": "get_download_url", "input": { "bucket": "backups", "key": "acme/latest.tar.gz", "expiresIn": 3600 }, "result": "{\"url\":\"http://...\"}" } ], "iterations": 3}Use this for debugging — if an agent gives unexpected results, check what tools it called and what they returned.
Running via API
curl -X POST http://localhost:3000/api/agents/agent_abc/run \ -H "Content-Type: application/json" \ -d '{ "message": "List all open tickets for Acme Corp" }'Running from a scheduled job
{ "action": "agent_run", "config": { "agentId": "agent_abc", "message": "Summarize all open P1 and P2 tickets opened in the last 24 hours. Format as a bullet list with ticket ID, company, and priority." }}The job run history stores the full agent response as the output field.
Error handling
| Error | Cause | Fix |
|---|---|---|
"Agent is disabled" |
status !== 'active' |
Set agent status to active |
"ANTHROPIC_API_KEY not configured" |
Missing env var | Add key to .env and restart |
"MCP server unreachable" |
Server down | Check MCP server container logs |
"Max iterations reached" |
Agent loop too long | Improve system prompt to be more decisive |