Letting an agent run, restart and check your application

An AI coding agent that can only edit code has to ask you whether its change worked. With Sonde it can look for itself, but only if it can also build, start and restart the application, and reconnect each time. This guide sets that up, with templates for VS Code and CLion.

Audience: application developers who use an AI coding agent (Claude Code or similar) on a Qt application that already embeds Sonde in debug builds. Read using.md first.

The loop

What we want the agent to be able to do on its own, as often as it needs:

  1. Build the application, and read the errors if the build fails.
  2. Start it, replacing the copy it started last time, and wait until Sonde is listening.
  3. Reproduce the problem with the tools, or exercise the new feature.
  4. Change the code, then go back to step 1.
  5. Stop when the problem no longer reproduces, and say what it checked.

Steps 1, 2 and 4 are where agents usually get stuck. They start the application in the foreground and block. They start a second copy while the first still runs. They lose their connection on every restart. Or they never notice that the build failed. sonde deals with all of that.

sonde is a small program, built with Sonde, with QtCore and QtNetwork only. It is at the top of your build directory: build/sonde, or build/sonde.exe on Windows. The paths below use build/; use your own build directory instead (CLion’s default is cmake-build-debug/). If you installed Sonde, it is in the install’s bin/, with sonde-bridge.

Two ways to run the application

A. The agent runs it with sonde. This is what you want for the agent’s own fix-and-check loop. It can restart the application as often as it likes, without asking you.

B. You run it from your IDE, often in the debugger, and the agent connects to that copy. This is what you want when you hit a bug and want help. The agent can investigate, but it must not restart your copy. It asks you to relaunch instead.

Both use the same socket name and port, so the agent’s connection settings work for either. Only one copy can listen at a time:

Another checkout, such as a second git worktree with an agent of its own, gets a socket and port of its own: see Several checkouts.

Set up once

Set up before 0.5? qtmcp-run and mcpcall.py are now one command, sonde, and a project’s files live in .sonde/ (design 0028). Run sonde migrate in your project to move .qtmcp-run.json, .qtmcp-run.local.json and .qtmcp/qml/ there (with git mv, keeping your trust grants), then change qtmcp-run to sonde in your IDE configurations and .mcp.json.

Set up before 0.8? sonde is now a compiled program, not the Python script tools/sonde (design 0030). Change python3 third_party/sonde/tools/sonde to build/sonde (with your build directory) in your IDE configurations, .mcp.json and agent instructions. tools/sonde ran the built program in 0.8, and is gone since 0.9.

1. Make debug builds listen when asked

The application must start Sonde from the environment, as in using.md, step 3:

Sonde::Server mcp;
Sonde::installWidgetsIntegration();
Sonde::installStandardTools(mcp);
mcp.listenFromEnvironment();   // SONDE_LOCAL / SONDE_HTTP

Nothing listens unless those variables are set, so this is safe to leave in every debug build.

2. Describe the application to sonde

Copy templates/sonde-config.json to .sonde/config.json at the root of your project, and edit it:

{
  "build": ["cmake", "--build", "build", "--target", "myapp"],
  "run": ["build/myapp"],
  "env": {},
  "local": "myapp-mcp-{instance}",
  "http": "{instance}",
  "readyTimeout": 30
}
Key Meaning
build Command that builds the application. Optional. Use the build directory your IDE uses: CLion’s default is cmake-build-debug, VS Code’s CMake Tools uses build.
run The program and its arguments. Paths are relative to the project’s root, the directory that holds .sonde/. On Windows the program may leave out .exe, so one configuration serves every platform.
env Extra environment variables, e.g. QT_LOGGING_RULES, or QT_QPA_PLATFORM: offscreen to keep windows off your screen.
local Local socket name; sets SONDE_LOCAL. {instance} in it is this checkout’s instance key (see Several checkouts).
http Port; sets SONDE_HTTP (localhost only). "{instance}" is a port chosen by the instance key, from 28600 to 29599, or the next free one; "{instance:9000-9099}" picks from another range.
executable The application’s path, if run starts a wrapper script rather than the application. Optional. Not needed for an .app bundle: sonde runs the binary inside it.
bridge A sonde-bridge program for sonde bridge to run. Optional: by default, sonde bridge runs the same bridge in its own process.
readyTimeout Seconds to wait for the listeners (default 30).

Add .sonde/state/ and .sonde/local.json to your .gitignore. The directory holds the process id, the logs and endpoint.json. The file is optional and holds this machine’s settings: its keys replace those of .sonde/config.json, and its env adds to the shared one. Use it for what differs per machine, such as QT_QPA_PLATFORM or another port range. A copy started from the IDE doesn’t read it.

Then try it. Building your application’s target doesn’t build sonde, so build it once (building everything does too):

cmake --build build --target sonde-cli
build/sonde restart
built in 4.2 s
started pid 63104
ready: local socket myapp-mcp-amber-falcon-tide, http://127.0.0.1:9318/mcp

The other commands are stop, status, log -n 100, endpoint (this checkout’s endpoints, with no application needed) and bridge (step 3). sonde only ever stops the process it started itself. It records the process’s start time next to its id, so an id that the system later gives to another process (after a crash or a reboot) is never mistaken for the application. Output is kept short for agents. The end of the build or application output is printed only when something fails, and the exit status says what happened:

3. Choose how the agent connects

There are two ways. Use the bridge unless the agent can’t add an MCP server. Both survive restarts.

As an MCP server through sonde-bridge --reconnect (recommended). The tools appear among the agent’s own tools, with their descriptions and argument schemas. That matters more than it seems: an agent picks the tools it can see. With sonde, it only learns what there is if it runs sonde describe and reads the output. Without that, it tends to stick to the few tools it already knows, and to ask you to check by hand what a tool could have checked. The bridge also needs no HTTP. Put this in .mcp.json:

{
  "mcpServers": {
    "myapp": {
      "command": "build/sonde",
      "args": ["bridge"]
    }
  }
}

sonde bridge runs the bridge, as sonde-bridge --reconnect would, on this checkout’s socket name, from .sonde/config.json, so the same .mcp.json works in every checkout. (With a fixed name, you can run the bridge directly instead: "command": "build/third_party/sonde/tools/bridge/sonde-bridge", "args": ["--reconnect", "myapp-mcp"].)

Claude Code reads .mcp.json when a session starts, from the folder it starts in. A worktree on a branch that doesn’t have the file yet has no myapp tools until it is merged in; sonde works there meanwhile.

--reconnect keeps the bridge running when the application exits, so the agent’s MCP connection survives restarts:

Without --reconnect (just --wait), the bridge exits when the application does, and someone has to reconnect the MCP server (in Claude Code, /mcp). Agents usually can’t do that themselves.

From its shell with sonde, for agents that can’t add an MCP server, or whose MCP client refuses it. The agent runs sonde like any other command:

build/sonde input_click '{"object": "saveButton"}'

It finds the application by itself: sonde writes this checkout’s endpoints to .sonde/state/endpoint.json, and sonde reads the nearest one. SONDE_URL or --url still take precedence.

Either route works for both ways of running the application. You can set up both: the template in the next step tells the agent to use the MCP tools and to fall back on sonde only when they aren’t connected.

4. Tell the agent

Paste templates/agent-instructions.md into your project’s CLAUDE.md or AGENTS.md, adjusting paths and names. It tells the agent:

Agents follow concrete instructions like these much more reliably than general advice.

VS Code

Copy templates/vscode/ into your project’s .vscode/ folder, merging with any files already there.

Change build/ (where sonde is) and myapp-mcp to match your project. With the CMake Tools extension, ${command:cmake.buildDirectory}/sonde follows its build directory setting. The {instance} placeholders stay: they give each checkout the same socket and port as sonde does. On Windows the tasks run sonde.exe.

CLion

Copy the templates/clion/.run/ folder into your project. CLion picks up run configurations stored in .run/, and they appear under Run → Edit Configurations:

If CLion doesn’t load a file, set the configuration up by hand: in your existing configuration, set Environment variables to SONDE_LOCAL=myapp-mcp-{instance};SONDE_HTTP={instance}. Then add a Shell Script configuration that runs ./cmake-build-debug/sonde restart in the project directory.

What a session looks like

You ask the agent to fix “the Save button does nothing after an import”. With the setup above, it can work through it like this, without you:

  1. sonde restart, which prints ready.
  2. Reproduce with the tools: import a file, then call signal_watch on saveButton’s clicked, then input_click on saveButton. The click reply’s delivery report shows the press went to an overlay, and the signals events show clicked() never fired. layout_explain on the button names the overlay in its siblingsAbove.
  3. object_tree and object_inspect on the overlay: a progress widget from the import, left visible but fully transparent.
  4. Fix the code that should hide it, then sonde restart again. The build fails; it reads the error, fixes it, and restarts again.
  5. Repeat step 2: the press now reaches the button, and clicked(false) appears.
  6. Report what it changed and what it verified.

The walkthrough shows this kind of investigation in full, with real output.

Several checkouts

If you work in several checkouts of the project at once, such as git worktrees with an agent in each, each one needs a socket and port of its own. Otherwise one agent finds another’s copy listening, or worse, drives it without noticing. {instance} does this with one configuration, committed once:

Socket names won’t collide in practice. Ports can, about one time in a hundred with five checkouts, and then the second copy simply takes the next port (design 0019). sonde checks, when it starts a session, that the copy at the URL in endpoint.json is this checkout’s build, and says so if it isn’t: a copy started from the IDE may since have taken another port. sonde endpoint then finds the right one.

Spelling and bundles. The key comes from the executable’s path as the system reports it, so sonde uses the same: on macOS, the spelling the disk has, whatever capitals .sonde/config.json uses, and for an .app bundle, the binary inside it, which it then runs directly so that it gets the environment. Run the bundle, not open, which starts it through Launch Services.

For development builds only. {instance} belongs to listenFromEnvironment(), which a release build never calls: see release.md. Nothing here changes that. Design 0018 has the details.

Things that go wrong

“something else is already listening”. Another copy holds the socket name or port, and sonde says which, from that copy’s app_info:

Tool calls time out. The application is probably paused in the debugger, at a breakpoint or after a crash. Its event loop isn’t running, so Sonde can’t answer. Continue it in the IDE.

“not listening after 30 s”. The application started but Sonde didn’t listen. Check that it calls listenFromEnvironment(), and look at sonde log for Sonde warnings. If a listener can’t start, sonde prints Sonde’s warning about it.

The agent’s MCP server disappeared. The application restarted and the bridge exited, because it was started without --reconnect (see step 3). Add --reconnect, then reconnect the server once (in Claude Code, /mcp).

A tool call failed with “The application closed the connection before answering”. The application exited or restarted while the call was in progress. That’s expected after a restart; send it again. If it happens without a restart, the application crashed: check sonde log.

Windows pop up on your screen while the agent works. Set "env": {"QT_QPA_PLATFORM": "offscreen"} in .sonde/config.json. Screenshots still work, because Sonde renders them itself. Leave it out when you want to watch what the agent does.

The application runs in a container. Use containers.md instead. sonde starts local processes only.

What is tested