Skip to content

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

Python

fix(mcp): a 200 with a JSON-RPC error is not proof of a stateless server

Rach Pradhan205a419292a4
diff --git a/src/mcp_protocol.zig b/src/mcp_protocol.zig
index 1e07d64e..0d58c592 100644
--- a/src/mcp_protocol.zig
+++ b/src/mcp_protocol.zig
@@ -392,6 +392,24 @@ test "classifyProbe: the real TS-SDK legacy rejection classifies as legacy" {
try std.testing.expectEqual(Probe.legacy, probe);
}
+test "a 200 carrying a JSON-RPC error is NOT proof of a modern server" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ // JSON-RPC puts application errors in a 200 body, so status alone cannot
+ // decide the era. A legacy server enforcing "initialize first" answers the
+ // stateless probe with exactly this, and an earlier version read the 2xx
+ // as "modern", skipped the fallback, and broke every such server.
+ const rejection = "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32000,\"message\":\"Server not initialized\"},\"id\":1}";
+ const parsed = try std.json.parseFromSliceLeaky(Value, a, rejection, .{});
+ // The rule the caller applies: a `result` member, not the status, is what
+ // proves the server understood a request sent with no handshake.
+ try std.testing.expect(parsed == .object and parsed.object.get("result") == null);
+ // And a real modern reply does carry one.
+ const ok = try std.json.parseFromSliceLeaky(Value, a, "{\"jsonrpc\":\"2.0\",\"result\":{\"tools\":[]},\"id\":1}", .{});
+ try std.testing.expect(ok == .object and ok.object.get("result") != null);
+}
+
test "classifyProbe: never crashes on empty, HTML, or truncated bodies" {
var arena_state = testArena();
defer arena_state.deinit();
diff --git a/src/mcp_rpc.zig b/src/mcp_rpc.zig
index ba0dd0b9..74b0502b 100644
--- a/src/mcp_rpc.zig
+++ b/src/mcp_rpc.zig
@@ -276,8 +276,15 @@ fn connectHttpAttempt(server: *Server, a: Allocator, session_alloc: Allocator, r
});
defer if (reply.body) |b| http.client.allocator.free(b);
- if (reply.status >= 200 and reply.status < 300) {
- const parsed = (if (reply.body) |b| mcp_http.parseHttpResponse(a, b, probe_id) else null) orelse return error.BadMcpResponse;
+ // A 2xx alone does NOT mean modern. JSON-RPC carries application errors in
+ // a 200 body, and a legacy server that enforces "initialize first" answers
+ // this probe with exactly that: 200 plus an error object. Treating it as
+ // modern skipped the fallback and broke every such server. Only a real
+ // `result` proves the server understood a request sent with no handshake.
+ if (reply.status >= 200 and reply.status < 300) modern: {
+ const body = reply.body orelse break :modern;
+ const parsed = mcp_http.parseHttpResponse(a, body, probe_id) orelse break :modern;
+ if (parsed != .object or parsed.object.get("result") == null) break :modern;
server.era = .modern;
server.protocol_version = try session_alloc.dupe(u8, modern_protocol);
server.initialized = true;