Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

fix(mcp): probe stdio servers by default, and prove the probe ran without a stopwatch

Rach Pradhan4f340129fcb4
diff --git a/scripts/test-mcp-stdio-probe.py b/scripts/test-mcp-stdio-probe.py
index 363c1b1c..6e4939ea 100644
--- a/scripts/test-mcp-stdio-probe.py
+++ b/scripts/test-mcp-stdio-probe.py
@@ -44,9 +44,13 @@ FIXTURE_SOURCE = textwrap.dedent(
method = msg.get("method")
mid = msg.get("id")
if method == "server/discover":
- # Deliberately silent: the worst legacy case (unrecognized
- # method, no reply at all) that the probe's bounded read must
- # survive.
+ # Record that the probe reached us, so the test can assert the
+ # probe RAN without timing it (the bound is a tunable constant).
+ with open(__file__ + ".saw-discover", "w") as fh:
+ fh.write("1")
+ # Then stay deliberately silent: the worst legacy case, an
+ # unrecognized method with no reply at all, which the probe's
+ # bounded read must survive.
continue
if method == "initialize":
send({
@@ -124,6 +128,11 @@ EXITING_FIXTURE_SOURCE = textwrap.dedent(
).strip()
+def fixture_saw_discover(fixture: Path) -> bool:
+ """Did the fixture record a server/discover? Behavioral proof the probe ran."""
+ return Path(str(fixture) + ".saw-discover").exists()
+
+
def run_graff(graff: Path, fixture: Path, probe: bool) -> subprocess.CompletedProcess[str]:
with tempfile.TemporaryDirectory(prefix="graff-mcp-stdio-") as temp:
workspace = Path(temp)
@@ -191,10 +200,11 @@ def run(graff: Path) -> None:
), f"[gate off] expected a normal legacy connect:\n{completed.stderr}"
print("gate off: byte-identical legacy connect, no server/discover — OK")
- # Scenario 2: gate on, server silently drops server/discover. Must
- # time out (~3s) and fall back to the legacy handshake on the same
- # process, completing well inside the outer 20s subprocess timeout
- # (which would fire and fail this script if the probe hung).
+ # Scenario 2: gate on, server silently drops server/discover. Must time
+ # out (the probe bound, see mcp_rpc.stdio_probe_timeout) and fall back
+ # to the legacy handshake on the same process, completing well inside
+ # the outer 20s subprocess timeout (which would fire and fail this
+ # script if the probe hung).
start = time.monotonic()
completed = run_graff(graff, fixture, probe=True)
elapsed = time.monotonic() - start
@@ -206,12 +216,15 @@ def run(graff: Path) -> None:
assert (
"connected (mcp 2025-06-18)" in completed.stderr
), f"[gate on] expected the legacy fallback to still connect:\n{completed.stderr}"
- # The probe's 3s bound should dominate; a hang would have hit the
- # 20s subprocess.run timeout above and raised instead of getting
- # here, but a slack upper bound catches a "cancel doesn't actually
- # cancel, it just silently proceeds without waiting" regression too.
+ # That the probe RAN is asserted on the fixture's own record, not on a
+ # stopwatch: the timing assertion here used to hard-code the 3s bound
+ # and went red the moment that constant was retuned, which says nothing
+ # about correctness. The upper bound stays, because it catches a real
+ # regression ("cancel doesn't actually cancel, it silently proceeds").
+ assert fixture_saw_discover(fixture), (
+ f"[gate on] the server never received server/discover, so the probe did not run:\n{completed.stderr}"
+ )
assert elapsed < 15, f"[gate on] took {elapsed:.1f}s — the probe bound is not working"
- assert elapsed >= 2.5, f"[gate on] took only {elapsed:.1f}s — the probe may not have run at all"
print(f"gate on: timed-out probe -> legacy fallback in {elapsed:.1f}s — OK")
# Scenario 3: gate on, server exits on server/discover (the other
diff --git a/src/mcp.zig b/src/mcp.zig
index c008e814..59a298db 100644
--- a/src/mcp.zig
+++ b/src/mcp.zig
@@ -47,12 +47,19 @@ pub const Registry = struct {
mutex: Io.Mutex = .init,
servers: []*Server = &.{},
tools: []Tool = &.{},
- /// `GRAFF_MCP_PROBE=1`, default off (see mcp_rpc.probeStdio's doc comment
- /// for why this cannot be turned on unconditionally today). Only
- /// `Registry.init` reads the environment; servers added later via
+ /// The stdio spec's backward-compatibility SHOULD: a dual-era client
+ /// probes `server/discover` before assuming legacy. ON by default;
+ /// `GRAFF_MCP_PROBE=0` opts out.
+ /// The cost was measured against the stdio server this repo actually
+ /// ships with, timing to the "connected" line rather than to process
+ /// exit: probe on and off are within run-to-run noise, because a legacy
+ /// server either answers the unknown method with an error at once or
+ /// closes stdout and is respawned. Only a server that SILENTLY IGNORES it
+ /// pays the timeout above, and it pays it once per process.
+ /// Only `Registry.init` reads the environment; servers added later via
/// `addServer`/`trustWorkspace` on a `Registry.empty*`-constructed
/// registry inherit `false` — a documented gap, never a safety issue.
- stdio_probe: bool = false,
+ stdio_probe: bool = true,
pub fn arena(self: *Registry) Allocator {
return self.arena_state.allocator();
@@ -74,7 +81,7 @@ pub const Registry = struct {
.io = io,
.home = home,
.arena_state = std.heap.ArenaAllocator.init(gpa),
- .stdio_probe = if (environ_map.get("GRAFF_MCP_PROBE")) |v| std.mem.eql(u8, v, "1") else false,
+ .stdio_probe = if (environ_map.get("GRAFF_MCP_PROBE")) |v| !std.mem.eql(u8, v, "0") else true,
};
errdefer reg.deinit();
const a = reg.arena();
diff --git a/src/mcp_rpc.zig b/src/mcp_rpc.zig
index 74b0502b..ad812a93 100644
--- a/src/mcp_rpc.zig
+++ b/src/mcp_rpc.zig
@@ -147,7 +147,14 @@ fn connectLegacy(server: *Server, a: Allocator, session_alloc: Allocator) !Value
return request(server, a, "{}", "tools/list", null);
}
-const stdio_probe_timeout: Io.Duration = .fromSeconds(3);
+/// A stdio server is a LOCAL process, so a reply is a pipe write away: 3s was
+/// a network-shaped budget and made the spec's SHOULD-probe unaffordable to
+/// run by default. A server that has not answered `server/discover` in this
+/// long is treated as legacy, which is exactly the behavior graff had before
+/// the probe existed - so the cost of guessing wrong is latency, never
+/// function (stdio spec: fall back on "no response within a reasonable
+/// timeout", never keyed to a specific error code).
+const stdio_probe_timeout: Io.Duration = .fromMilliseconds(600);
fn stdioProbeReadTask(server: *Server, response_alloc: Allocator, id: i64) anyerror!Value {
const stdio = &server.transport.stdio;