Skip to content

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

Python

feat(mcp): pure helpers for the 2026-07-28 stateless envelope

Rach Pradhan4b45e1102f34
diff --git a/src/mcp_http.zig b/src/mcp_http.zig
index ac86bd03..01fc8436 100644
--- a/src/mcp_http.zig
+++ b/src/mcp_http.zig
@@ -5,6 +5,7 @@ const Io = std.Io;
const Value = std.json.Value;
const Allocator = std.mem.Allocator;
const mcp_oauth = @import("mcp_oauth.zig");
+const mcp_protocol = @import("mcp_protocol.zig");
const max_http_response = 1 << 20;
@@ -31,6 +32,36 @@ pub fn validRemoteUrl(url: []const u8) bool {
return validRemoteUri(std.Uri.parse(url) catch return false);
}
+/// Per-request header inputs, modern vs legacy. `name` is the tool name for
+/// a `tools/call` (rendered as `Mcp-Name`); leave null for `tools/list` and
+/// `server/discover`, which have none.
+pub const RequestMeta = struct {
+ protocol_version: []const u8,
+ method: []const u8,
+ name: ?[]const u8 = null,
+ modern: bool,
+};
+
+/// Build the extra headers for one Streamable HTTP POST. Modern requests get
+/// `Mcp-Method`/`Mcp-Name` and never `Mcp-Session-Id` (no session is ever
+/// minted in the stateless wire format); legacy requests get the negotiated
+/// `Mcp-Session-Id` when one exists and never `Mcp-Method`/`Mcp-Name`, so a
+/// dual-era server can't mistake a legacy request for a modern one.
+pub fn buildHeaders(a: Allocator, http: *const HttpTransport, meta: RequestMeta, bearer: ?[]const u8) ![]std.http.Header {
+ var headers: std.ArrayList(std.http.Header) = .empty;
+ try headers.appendSlice(a, http.headers);
+ if (bearer) |token| try headers.append(a, .{ .name = "authorization", .value = try std.fmt.allocPrint(a, "Bearer {s}", .{token}) });
+ try headers.append(a, .{ .name = "accept", .value = "application/json, text/event-stream" });
+ try headers.append(a, .{ .name = "mcp-protocol-version", .value = meta.protocol_version });
+ if (meta.modern) {
+ try headers.append(a, .{ .name = "mcp-method", .value = meta.method });
+ if (meta.name) |name| try headers.append(a, .{ .name = "mcp-name", .value = try mcp_protocol.headerValue(a, name) });
+ } else if (http.session_id) |session_id| {
+ try headers.append(a, .{ .name = "mcp-session-id", .value = session_id });
+ }
+ return headers.toOwnedSlice(a);
+}
+
pub fn matchingResponse(a: Allocator, bytes: []const u8, id: i64) ?Value {
const trimmed = std.mem.trim(u8, bytes, " \t\r\n");
if (trimmed.len == 0) return null;
@@ -295,3 +326,64 @@ test "parseHttpResponse accepts JSON and Streamable HTTP SSE" {
try std.testing.expectEqual(@as(i64, 8), event.object.get("id").?.integer);
try std.testing.expect(parseHttpResponse(a, sse, 9) == null);
}
+
+fn findHeader(headers: []const std.http.Header, name: []const u8) ?[]const u8 {
+ for (headers) |h| if (std.ascii.eqlIgnoreCase(h.name, name)) return h.value;
+ return null;
+}
+
+test "buildHeaders: modern tools/call carries Mcp-Method/Mcp-Name, never a session id" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var http: HttpTransport = .{ .url = "https://example/mcp", .client = .{ .allocator = std.testing.allocator, .io = std.testing.io } };
+ const headers = try buildHeaders(a, &http, .{
+ .protocol_version = mcp_protocol.modern_protocol,
+ .method = "tools/call",
+ .name = "search",
+ .modern = true,
+ }, null);
+ try std.testing.expectEqualStrings("tools/call", findHeader(headers, "mcp-method").?);
+ try std.testing.expectEqualStrings("search", findHeader(headers, "mcp-name").?);
+ try std.testing.expectEqualStrings(mcp_protocol.modern_protocol, findHeader(headers, "mcp-protocol-version").?);
+ try std.testing.expectEqualStrings("application/json, text/event-stream", findHeader(headers, "accept").?);
+ try std.testing.expect(findHeader(headers, "mcp-session-id") == null);
+ // Structurally guaranteed to match modern_meta's embedded protocolVersion,
+ // asserted anyway per the migration spec's design note.
+ try std.testing.expect(std.mem.indexOf(u8, mcp_protocol.modern_meta, mcp_protocol.modern_protocol) != null);
+}
+
+test "buildHeaders: legacy carries the negotiated session id, never Mcp-Method/Mcp-Name" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var http: HttpTransport = .{
+ .url = "https://example/mcp",
+ .client = .{ .allocator = std.testing.allocator, .io = std.testing.io },
+ .session_id = "sess-123",
+ };
+ const headers = try buildHeaders(a, &http, .{
+ .protocol_version = "2025-06-18",
+ .method = "tools/call",
+ .name = "search",
+ .modern = false,
+ }, null);
+ try std.testing.expectEqualStrings("sess-123", findHeader(headers, "mcp-session-id").?);
+ try std.testing.expectEqualStrings("2025-06-18", findHeader(headers, "mcp-protocol-version").?);
+ try std.testing.expect(findHeader(headers, "mcp-method") == null);
+ try std.testing.expect(findHeader(headers, "mcp-name") == null);
+}
+
+test "buildHeaders: modern tools/list omits Mcp-Name" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var http: HttpTransport = .{ .url = "https://example/mcp", .client = .{ .allocator = std.testing.allocator, .io = std.testing.io } };
+ const headers = try buildHeaders(a, &http, .{
+ .protocol_version = mcp_protocol.modern_protocol,
+ .method = "tools/list",
+ .modern = true,
+ }, null);
+ try std.testing.expectEqualStrings("tools/list", findHeader(headers, "mcp-method").?);
+ try std.testing.expect(findHeader(headers, "mcp-name") == null);
+}
diff --git a/src/mcp_protocol.zig b/src/mcp_protocol.zig
index 1ce141be..1e07d64e 100644
--- a/src/mcp_protocol.zig
+++ b/src/mcp_protocol.zig
@@ -71,6 +71,154 @@ pub fn negotiatedProtocol(response: Value, transport: Transport) ![]const u8 {
return error.UnsupportedMcpProtocolVersion;
}
+// ── 2026-07-28 stateless envelope: pure helpers, not wired to any transport
+// yet (see mcp_http.zig's `probe`/`buildHeaders` and mcp_rpc.zig's stdio
+// probe for the callers). ─────────────────────────────────────────────────
+
+/// The three reserved `_meta` keys (basic/index § `_meta`), pre-rendered as a
+/// single JSON object member. graff supports exactly one modern version and
+/// declares no client capabilities, so this is a comptime constant rather
+/// than something built per request.
+pub const modern_meta =
+ \\"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientInfo":{"name":"codegraff","version":"1"},"io.modelcontextprotocol/clientCapabilities":{}}
+;
+
+/// Build a JSON-RPC request line. Modern era splices the `_meta` envelope
+/// into `params` (first, so the splice is a single concat with no
+/// trailing-comma case); legacy era emits `params` byte-for-byte unchanged.
+///
+/// LOAD-BEARING: when `modern` is false, this must produce exactly the bytes
+/// graff wrote before the 2026-07-28 migration — no `_meta`, nothing added.
+/// That byte-identity is the whole backward-compatibility guarantee for
+/// every server graff talks to today (codedbpro, smolify, every stdio
+/// server in a workspace `.mcp.json`). See the "legacy byte-identity" test.
+pub fn buildRequest(a: Allocator, id: i64, method: []const u8, params: []const u8, modern: bool) ![]u8 {
+ if (params.len < 2 or params[0] != '{' or params[params.len - 1] != '}') return error.BadMcpParams;
+ if (!modern) {
+ return std.fmt.allocPrint(a,
+ \\{{"jsonrpc":"2.0","id":{d},"method":"{s}","params":{s}}}
+ , .{ id, method, params });
+ }
+ const inner = std.mem.trim(u8, params[1 .. params.len - 1], " \t\r\n");
+ if (inner.len == 0) {
+ return std.fmt.allocPrint(a,
+ \\{{"jsonrpc":"2.0","id":{d},"method":"{s}","params":{{{s}}}}}
+ , .{ id, method, modern_meta });
+ }
+ return std.fmt.allocPrint(a,
+ \\{{"jsonrpc":"2.0","id":{d},"method":"{s}","params":{{{s},{s}}}}}
+ , .{ id, method, modern_meta, inner });
+}
+
+fn looksLikeSentinel(value: []const u8) bool {
+ return value.len >= 4 and std.mem.startsWith(u8, value, "=?") and std.mem.endsWith(u8, value, "?=");
+}
+
+fn isHeaderSafe(value: []const u8) bool {
+ if (value.len == 0) return true;
+ if (value[0] == ' ' or value[0] == '\t') return false;
+ if (value[value.len - 1] == ' ' or value[value.len - 1] == '\t') return false;
+ for (value) |c| {
+ if (!(c == 0x20 or c == 0x09 or (c >= 0x21 and c <= 0x7E))) return false;
+ }
+ return !looksLikeSentinel(value);
+}
+
+/// Encode an `Mcp-Name` (or any MCP header) value per the spec's encoding
+/// table: plain if every byte is space/tab/0x21-0x7E, no leading/trailing
+/// space or tab, and it does not already look like the sentinel below —
+/// otherwise the whole value is base64'd inside `=?base64?<b64>?=` so a
+/// non-ASCII or newline-bearing tool name can't corrupt an HTTP header line.
+pub fn headerValue(a: Allocator, value: []const u8) ![]const u8 {
+ if (isHeaderSafe(value)) return a.dupe(u8, value);
+ const encoder = std.base64.standard.Encoder;
+ const encoded = try a.alloc(u8, encoder.calcSize(value.len));
+ _ = encoder.encode(encoded, value);
+ return std.fmt.allocPrint(a, "=?base64?{s}?=", .{encoded});
+}
+
+/// The three renumbered error codes actually introduced by the 2026-07-28
+/// draft. `-32000..-32019` is the grandfathered implementation-defined
+/// sub-range: a real 2025-11-25 server may legitimately emit e.g. `-32003`
+/// for something unrelated, so only these three exact codes are ever read
+/// as "this server speaks the modern protocol".
+pub const ModernError = enum { header_mismatch, missing_capability, unsupported_version };
+
+pub fn modernErrorCode(code: i64) ?ModernError {
+ return switch (code) {
+ -32020 => .header_mismatch,
+ -32021 => .missing_capability,
+ -32022 => .unsupported_version,
+ else => null,
+ };
+}
+
+/// Result of inspecting a Streamable HTTP/stdio probe reply. `unsupported_version`
+/// carries the server's raw `data.supported` list (only when it overlaps
+/// something graff speaks — see `classifyProbe`); the caller picks `modern_protocol`
+/// if present, else the legacy `initialize` handshake, per versioning §
+/// Protocol Version Negotiation.
+pub const Probe = union(enum) {
+ modern,
+ legacy,
+ unsupported_version: []const Value,
+ incompatible,
+};
+
+fn versionOverlap(version: []const u8) bool {
+ if (std.mem.eql(u8, version, modern_protocol)) return true;
+ for (supported_protocols) |ours| if (std.mem.eql(u8, version, ours)) return true;
+ return false;
+}
+
+/// Classify a non-2xx Streamable HTTP body (or a stdio `server/discover`
+/// error) to decide modern-vs-legacy. Never crashes on empty/malformed
+/// input — an unparseable body is exactly what a legacy server (which does
+/// not know this probe exists) produces, so it reads as `.legacy`.
+pub fn classifyProbe(a: Allocator, raw_body: []const u8) Probe {
+ const trimmed = std.mem.trim(u8, raw_body, " \t\r\n");
+ if (trimmed.len == 0) return .legacy;
+ const parsed = std.json.parseFromSliceLeaky(Value, a, trimmed, .{ .allocate = .alloc_always }) catch return .legacy;
+ if (parsed != .object) return .legacy;
+ const err_v = parsed.object.get("error") orelse return .legacy;
+ if (err_v != .object) return .legacy;
+ const code_v = err_v.object.get("code") orelse return .legacy;
+ if (code_v != .integer) return .legacy;
+ const kind = modernErrorCode(code_v.integer) orelse return .legacy;
+ switch (kind) {
+ .header_mismatch, .missing_capability => return .modern,
+ .unsupported_version => {
+ const data_v = err_v.object.get("data") orelse return .incompatible;
+ if (data_v != .object) return .incompatible;
+ const supported_v = data_v.object.get("supported") orelse return .incompatible;
+ if (supported_v != .array) return .incompatible;
+ for (supported_v.array.items) |v| {
+ if (v == .string and versionOverlap(v.string)) return .{ .unsupported_version = supported_v.array.items };
+ }
+ return .incompatible;
+ },
+ }
+}
+
+/// Extract `result.supportedVersions` from a `server/discover` reply.
+pub fn discoverSupportedVersions(response: Value) ![]const Value {
+ if (response != .object) return error.BadMcpDiscoverResponse;
+ const result = response.object.get("result") orelse return error.BadMcpDiscoverResponse;
+ if (result != .object) return error.BadMcpDiscoverResponse;
+ const versions = result.object.get("supportedVersions") orelse return error.MissingMcpSupportedVersions;
+ if (versions != .array) return error.InvalidMcpSupportedVersions;
+ return versions.array.items;
+}
+
+/// A result's `resultType`. Absent MUST read as "complete" (the backward
+/// -compatibility rule for every server older than this field existing);
+/// anything other than the literal string "complete" is not complete —
+/// including a malformed non-string value, which never crashes here.
+pub fn resultIsComplete(result: std.json.ObjectMap) bool {
+ const v = result.get("resultType") orelse return true;
+ return v == .string and std.mem.eql(u8, v.string, "complete");
+}
+
test "rewriteOneOf: converts oneOf to anyOf, recursively" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
@@ -148,3 +296,197 @@ test "initialize negotiation rejects missing, non-string, and unsupported versio
try std.testing.expectError(case.expected, negotiatedProtocol(parsed.value, .streamable_http));
}
}
+
+fn testArena() std.heap.ArenaAllocator {
+ return std.heap.ArenaAllocator.init(std.testing.allocator);
+}
+
+test "buildRequest: modern with empty params splices the full _meta envelope" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const line = try buildRequest(a, 1, "tools/list", "{}", true);
+ try std.testing.expectEqualStrings(
+ "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\",\"params\":{" ++ modern_meta ++ "}}",
+ line,
+ );
+ var parsed = try std.json.parseFromSlice(Value, a, line, .{});
+ defer parsed.deinit();
+ const meta = parsed.value.object.get("params").?.object.get("_meta").?;
+ try std.testing.expect(meta.object.get("io.modelcontextprotocol/protocolVersion") != null);
+ try std.testing.expect(meta.object.get("io.modelcontextprotocol/clientCapabilities") != null);
+ try std.testing.expect(meta.object.get("io.modelcontextprotocol/clientInfo") != null);
+}
+
+test "buildRequest: modern with non-empty params preserves them and adds no stray comma" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const line = try buildRequest(a, 3, "tools/call", "{\"name\":\"t\",\"arguments\":{}}", true);
+ var parsed = try std.json.parseFromSlice(Value, a, line, .{});
+ defer parsed.deinit();
+ const params = parsed.value.object.get("params").?.object;
+ try std.testing.expect(params.get("_meta") != null);
+ try std.testing.expectEqualStrings("t", params.get("name").?.string);
+ try std.testing.expectEqual(@as(usize, 0), params.get("arguments").?.object.count());
+ // no double/trailing comma: exactly 3 top-level params members
+ try std.testing.expectEqual(@as(usize, 3), params.count());
+}
+
+test "buildRequest: rejects non-object params" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ try std.testing.expectError(error.BadMcpParams, buildRequest(a, 1, "tools/list", "[]", true));
+ try std.testing.expectError(error.BadMcpParams, buildRequest(a, 1, "tools/list", "", true));
+ try std.testing.expectError(error.BadMcpParams, buildRequest(a, 1, "tools/list", "null", true));
+}
+
+test "buildRequest: LEGACY BYTE-IDENTITY GUARD — modern=false reproduces the pre-migration wire format exactly" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const line = try buildRequest(a, 3, "tools/call", "{\"name\":\"t\",\"arguments\":{}}", false);
+ try std.testing.expectEqualStrings(
+ "{\"jsonrpc\":\"2.0\",\"id\":3,\"method\":\"tools/call\",\"params\":{\"name\":\"t\",\"arguments\":{}}}",
+ line,
+ );
+ try std.testing.expect(std.mem.indexOf(u8, line, "_meta") == null);
+}
+
+test "headerValue: matches the spec's encoding table verbatim" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ try std.testing.expectEqualStrings("get_weather", try headerValue(a, "get_weather"));
+ try std.testing.expectEqualStrings("=?base64?SGVsbG8sIOS4lueVjA==?=", try headerValue(a, "Hello, 世界"));
+ try std.testing.expectEqualStrings("=?base64?IHBhZGRlZCA=?=", try headerValue(a, " padded "));
+ try std.testing.expectEqualStrings("=?base64?bGluZTEKbGluZTI=?=", try headerValue(a, "line1\nline2"));
+ try std.testing.expectEqualStrings("=?base64?PT9iYXNlNjQ/bGl0ZXJhbD89?=", try headerValue(a, "=?base64?literal?="));
+}
+
+test "modernErrorCode: only the three spec-reserved codes map, draft codes never do" {
+ try std.testing.expectEqual(ModernError.header_mismatch, modernErrorCode(-32020).?);
+ try std.testing.expectEqual(ModernError.missing_capability, modernErrorCode(-32021).?);
+ try std.testing.expectEqual(ModernError.unsupported_version, modernErrorCode(-32022).?);
+ // The draft-code trap: -32001/-32003/-32004 never shipped in a released
+ // revision and live in the grandfathered -32000..-32019 range. Treating
+ // them as modern would skip the legacy fallback and break a real
+ // 2025-11-25 server that happens to emit one for something unrelated.
+ try std.testing.expect(modernErrorCode(-32001) == null);
+ try std.testing.expect(modernErrorCode(-32003) == null);
+ try std.testing.expect(modernErrorCode(-32004) == null);
+ try std.testing.expect(modernErrorCode(-32000) == null);
+ try std.testing.expect(modernErrorCode(-32601) == null);
+ try std.testing.expect(modernErrorCode(-32602) == null);
+ try std.testing.expect(modernErrorCode(-32700) == null);
+}
+
+test "classifyProbe: the real TS-SDK legacy rejection classifies as legacy" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ // id:null proves classification never routes through matchingResponse,
+ // which requires an integer id match and would return null here.
+ const probe = classifyProbe(a, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32000,\"message\":\"Bad Request: Server not initialized\"},\"id\":null}");
+ try std.testing.expectEqual(Probe.legacy, probe);
+}
+
+test "classifyProbe: never crashes on empty, HTML, or truncated bodies" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ try std.testing.expectEqual(Probe.legacy, classifyProbe(a, ""));
+ try std.testing.expectEqual(Probe.legacy, classifyProbe(a, "<html>404</html>"));
+ try std.testing.expectEqual(Probe.legacy, classifyProbe(a, "{"));
+}
+
+test "classifyProbe: -32022 with modern_protocol in data.supported — selection picks it" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const probe = classifyProbe(a, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32022,\"data\":{\"supported\":[\"2026-07-28\",\"2025-11-25\"]}},\"id\":null}");
+ const supported = switch (probe) {
+ .unsupported_version => |s| s,
+ else => return error.TestUnexpectedResult,
+ };
+ // the caller's selection step: modern_protocol present -> retry modern once
+ var picked: ?[]const u8 = null;
+ for (supported) |v| if (v == .string and std.mem.eql(u8, v.string, modern_protocol)) {
+ picked = v.string;
+ };
+ try std.testing.expectEqualStrings(modern_protocol, picked.?);
+}
+
+test "classifyProbe: -32022 with only a legacy overlap drives the legacy handshake" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const probe = classifyProbe(a, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32022,\"data\":{\"supported\":[\"2025-11-25\"]}},\"id\":null}");
+ const supported = switch (probe) {
+ .unsupported_version => |s| s,
+ else => return error.TestUnexpectedResult,
+ };
+ var has_modern = false;
+ var has_legacy = false;
+ for (supported) |v| if (v == .string) {
+ if (std.mem.eql(u8, v.string, modern_protocol)) has_modern = true;
+ for (supported_protocols) |ours| if (std.mem.eql(u8, v.string, ours)) {
+ has_legacy = true;
+ };
+ };
+ try std.testing.expect(!has_modern);
+ try std.testing.expect(has_legacy); // -> caller runs the legacy initialize handshake
+}
+
+test "classifyProbe: -32022 with no mutual version at all is incompatible" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const probe = classifyProbe(a, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32022,\"data\":{\"supported\":[\"1999-01-01\"]}},\"id\":null}");
+ try std.testing.expectEqual(Probe.incompatible, probe);
+}
+
+test "classifyProbe: -32020/-32021 mark the server modern, never fall back to legacy" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ try std.testing.expectEqual(Probe.modern, classifyProbe(a, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32020,\"message\":\"bad headers\"},\"id\":null}"));
+ try std.testing.expectEqual(Probe.modern, classifyProbe(a, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32021,\"message\":\"missing capability\"},\"id\":null}"));
+}
+
+test "discoverSupportedVersions: extracts the list, errors on missing or non-array" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const ok = try std.json.parseFromSliceLeaky(Value, a,
+ \\{"jsonrpc":"2.0","id":1,"result":{"supportedVersions":["2026-07-28"],"serverInfo":{"name":"fixture","version":"1"}}}
+ , .{});
+ const versions = try discoverSupportedVersions(ok);
+ try std.testing.expectEqual(@as(usize, 1), versions.len);
+ try std.testing.expectEqualStrings("2026-07-28", versions[0].string);
+
+ const missing = try std.json.parseFromSliceLeaky(Value, a,
+ \\{"jsonrpc":"2.0","id":1,"result":{"serverInfo":{"name":"fixture","version":"1"}}}
+ , .{});
+ try std.testing.expectError(error.MissingMcpSupportedVersions, discoverSupportedVersions(missing));
+
+ const not_array = try std.json.parseFromSliceLeaky(Value, a,
+ \\{"jsonrpc":"2.0","id":1,"result":{"supportedVersions":"2026-07-28"}}
+ , .{});
+ try std.testing.expectError(error.InvalidMcpSupportedVersions, discoverSupportedVersions(not_array));
+}
+
+test "resultIsComplete: absent is the MUST-complete default for older servers" {
+ var arena_state = testArena();
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const absent = (try std.json.parseFromSliceLeaky(Value, a, "{\"tools\":[]}", .{})).object;
+ try std.testing.expect(resultIsComplete(absent));
+ const complete = (try std.json.parseFromSliceLeaky(Value, a, "{\"resultType\":\"complete\"}", .{})).object;
+ try std.testing.expect(resultIsComplete(complete));
+ const input_required = (try std.json.parseFromSliceLeaky(Value, a, "{\"resultType\":\"input_required\"}", .{})).object;
+ try std.testing.expect(!resultIsComplete(input_required));
+ const malformed = (try std.json.parseFromSliceLeaky(Value, a, "{\"resultType\":42}", .{})).object;
+ try std.testing.expect(!resultIsComplete(malformed));
+}