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
- Two ways to run the application
- Set up once
- VS Code
- CLion
- What a session looks like
- Things that go wrong
- What is tested
The loop
What we want the agent to be able to do on its own, as often as it needs:
- Build the application, and read the errors if the build fails.
- Start it, replacing the copy it started last time, and wait until Sonde is listening.
- Reproduce the problem with the tools, or exercise the new feature.
- Change the code, then go back to step 1.
- 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:
- the IDE templates below stop the agent’s copy before you launch yours;
sonderefuses to start while another copy is listening.
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-runandmcpcall.pyare now one command,sonde, and a project’s files live in.sonde/(design 0028). Runsonde migratein your project to move.qtmcp-run.json,.qtmcp-run.local.jsonand.qtmcp/qml/there (withgit mv, keeping your trust grants), then changeqtmcp-runtosondein your IDE configurations and.mcp.json.
Set up before 0.8?
sondeis now a compiled program, not the Python scripttools/sonde(design 0030). Changepython3 third_party/sonde/tools/sondetobuild/sonde(with your build directory) in your IDE configurations,.mcp.jsonand agent instructions.tools/sonderan 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:
- 0: success;
- 1: the application didn’t start or become ready;
- 2: the build failed;
- 3: the configuration is wrong.
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:
- A request the application didn’t get to answer gets a JSON-RPC error saying the application closed the connection. The agent sees that and tries again.
- Requests sent while the application is away are held for up to 15
seconds (
--hold SECONDSto change it), then answered with an error saying the application isn’t running. So a call made just aftersonde restartsimply waits until the new copy is up. - When the application is back, the bridge connects again. It replays
the client’s
initialize,notifications/initialized, log level and resource subscriptions, and swallows the answers, so the new copy has a session as if nothing happened. It then sendstools/list_changedandresources/list_changed, since a rebuild may have changed the tools.
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.
- It survives restarts: when the application restarts,
sondenotices the old session is gone and starts a new one. - It needs nothing in the agent’s MCP configuration.
- It uses HTTP on localhost. That is the agent’s shell making the request, so it works even with agents whose MCP client refuses HTTP servers.
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:
- how to restart the application, talk to it and read its log;
- how to check a change: reproduce first, then repeat the same steps after the fix, and check state and signals, not just screenshots;
- not to touch a copy you are running in the debugger.
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.
- tasks.json adds three tasks, run with Terminal → Run Task:
- Sonde: restart app for agent;
- Sonde: stop app for agent;
- Sonde: show app log.
The restart task’s build errors appear in the Problems panel.
- launch.json adds two debug configurations that start your application
with
SONDE_LOCALandSONDE_HTTPset:- one for the C/C++ extension (
cppdbg); - one for CodeLLDB (
lldb).
Both run Sonde: stop app for agent first, so your copy can take the socket name. The program comes from the CMake Tools extension (
${command:cmake.launchTargetPath}); without that extension, put the path in instead. - one for the C/C++ extension (
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:
- myapp (Sonde on) runs or debugs your CMake target with
SONDE_LOCALandSONDE_HTTPset. Replacemyappwith your CMake project and target names (thePROJECT_NAME,TARGET_NAMEandRUN_TARGET_*attributes). To have it stop the agent’s copy first, add Restart app for agent’s command, withstopin place ofrestart, under Before launch. - Restart app for agent is a Shell Script configuration that runs
./cmake-build-debug/sonde restartin the project directory. Changecmake-build-debugif your CMake profile builds elsewhere.
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:
sonde restart, which printsready.- Reproduce with the tools: import a file, then call
signal_watchonsaveButton’sclicked, theninput_clickonsaveButton. The click reply’s delivery report shows the press went to an overlay, and thesignalsevents showclicked()never fired.layout_explainon the button names the overlay in itssiblingsAbove. object_treeandobject_inspecton the overlay: a progress widget from the import, left visible but fully transparent.- Fix the code that should hide it, then
sonde restartagain. The build fails; it reads the error, fixes it, and restarts again. - Repeat step 2: the press now reaches the button, and
clicked(false)appears. - 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:
- The instance key is three words, such as
amber-falcon-tide, from a hash of the application’s executable path. Each checkout builds its own executable, so each has its own key. The application computes it (Sonde::instanceKey()), and so dosondeand itsbridge, so they all arrive at the same endpoint without being told. SONDE_LOCAL=myapp-mcp-{instance}becomesmyapp-mcp-amber-falcon-tide.SONDE_HTTP={instance}becomes a port from 28600 to 29599 chosen by the key, and{instance:9000-9099}one from that range. A host can come first:0.0.0.0:{instance}. If the port is taken, the application takes the next free one in the range, andsondeasks it which, soendpoint.jsonhas the port it really got.app_infogives theexecutable, theworkingDirectory, theinstanceand theendpoints, so an agent can check that it is talking to its own checkout’s copy.
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:
- this checkout’s build: usually one you started from the IDE. Stop it, or have the agent connect to it instead (way B). Agents should ask before stopping it; the instructions template says so.
- another build: another checkout’s copy holds the endpoint. With fixed
names, use
{instance}(Several checkouts). With{instance}, a taken port isn’t a problem: the application takes the next one.
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
sonde-bridge --reconnectis covered by the end-to-end test. It kills and restarts the application under one bridge and checks that:- the request in flight gets an error;
- a request sent meanwhile is answered by the new copy;
- the client gets
list_changed, but not the replayed handshake’s answers; --holdexpires held requests;- closing stdin while reconnecting answers what is held, then exits.
sondeis covered by the end-to-end test (tests/smoke_test.py). The test checks:- start;
sondeacross a restart;- status;
- refusing to start while another copy holds the name;
- stop.
It runs on Linux, macOS and Windows in CI.
- The templates are checked to be valid JSON and XML, but have not been tried in VS Code or CLion. If something in them doesn’t work, please fix it and say what you changed.