Generate and run tests from an agent
## What it is for
What it is for
A coding agent can have HeyBug write a test plan, run the tests and read the results, without you opening the dashboard. The agent needs three things: a way to start a run, a way to poll it until it finishes, and a way to cancel it if it is no longer needed.
Every MCP call on the remote server (/api/mcp) stops waiting after 50 seconds. The run keeps going on the server, so a slow call still gives the agent a run id to poll. For the general pattern, see long-running calls.
The 45 second rule
- Ask for at most 45 seconds per poll.
heybug_get_runacceptswaitfrom 0 to 45. One poll that waits 45 seconds finishes before the 50 second limit. - Poll in a loop. Call again while the status is
queuedorrunning. Stop atpassed,failed,errororcanceled. - Silence is not failure. "Stopped waiting" and "Still running" mean the work continues. Keep the id and poll.
The steps
-
Plan (optional). Call
heybug_planwithprojectIdandtype(frontendorbackend). It explores the app and proposes cases. The reply gives the pending card id. -
Start. Pick one:
heybug_generate_and_runwithprojectIdapplies the pending plan and runs it. PasstestIdsto run only some cases.heybug_runwithurlstarts a quick run of a page. Send"wait": falseto get the run id at once.heybug_run_testswithprojectIdandcaseKeysruns only the named tests.
-
Get the run id. The reply has the id. If the call stopped at 50 seconds, call
heybug_list_runswithprojectId. The newest run is listed first, with itsid=. -
Poll. Call
heybug_get_runwithrunIdandwait: 45. Repeat until the status is final. -
Cancel. The MCP tools in this build have no cancel call. Use the CLI (below) or the Cancel run button on the run page,
/p/{project}/runs/{run}. Both callPOST /api/runs/{runId}/cancel.heybug runs cancel run_123
Example
Start a run of the pending plan:
curl -s https://opentest.nagara.cloud/api/mcp \
-H "Authorization: Bearer $HEYBUG_TOKEN" \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"heybug_generate_and_run","arguments":{"projectId":"p_1"}}}'If the run is not finished at 50 seconds, the reply reads:
Stopped waiting after 50s; the work continues on the server. Call heybug_get_run with the run id to get the result.Find the id, then list the newest runs of the project:
3 runs, newest first:
- #12 running (0 passed, 0 failed, 0 errored of 8) id=run_123Poll with the id and wait 45 seconds:
curl -s https://opentest.nagara.cloud/api/mcp \
-H "Authorization: Bearer $HEYBUG_TOKEN" \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"heybug_get_run","arguments":{"runId":"run_123","wait":45}}}'While the run is going, the reply ends with:
Run #12 is running (0 passed, 0 failed, 0 errored of 8). https://opentest.nagara.cloud/p/p_1/runs/run_123
Still running; call heybug_get_run again with wait.When it finishes, the reply names the verdict and counts, then lists each failed case with its failing step, Expected, Actual and a link to the case. The numbers and ids above are examples.
From a terminal, the same run can be started and followed with the CLI. This prints a JSON receipt with the run ids:
heybug runs start --project p_1 --no-wait --output json
heybug runs wait run_123 --timeout 20m --output json
heybug runs cancel run_123When it fails
- "Stopped waiting" or "Still running". Not a failure. Poll with the run id.
- "Lost contact while waiting for the run". The connection dropped. The run continues, so poll again with the same id.
- "No runs yet. Use heybug_generate_and_run to start one." The start call did not create a run. Call
heybug_planfirst if there is no pending plan, then start again. - "runId is required" or "projectId is required". Pass the id from the reply or from the dashboard URL (
/p/<projectId>). - The id starts with
exp_. That id belongs to an exploration, not a run. Open the link in the reply instead. - Status
error. An infrastructure problem, not a test verdict. Start a new run. - Status
canceled. The run was stopped. Its results are incomplete. Start a new run if you still need them. heybug runs cancelexits 4. The run was not found. Check the id withheybug runs list --project <projectId>. See CLI exit codes.- 401 or 403. The key or the
Originheader is wrong. See MCP troubleshooting. - A tool is missing. Restart the client and compare with
GET /api/mcp/tools.