Skip to content

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

Python

mcp: send one _meta on computer-use tool calls (#1511)

justrach193b3d69e1ed
diff --git a/src/mcp_notify.zig b/src/mcp_notify.zig
index 3894a3d0..fb22afc0 100644
--- a/src/mcp_notify.zig
+++ b/src/mcp_notify.zig
@@ -98,8 +98,11 @@ pub fn withProgressToken(a: Allocator, line: []const u8, id: i64) ![]const u8 {
const at = std.mem.indexOf(u8, line, key) orelse return line;
const after = at + key.len;
const rest = line[after..];
- if (std.mem.startsWith(u8, rest, "\"_meta\":{"))
- return std.fmt.allocPrint(a, "{s}\"_meta\":{{\"progressToken\":{d},{s}", .{ line[0..after], id, rest["\"_meta\":{".len..] });
+ if (std.mem.startsWith(u8, rest, "\"_meta\":{")) {
+ const meta_rest = rest["\"_meta\":{".len..];
+ const meta_sep: []const u8 = if (std.mem.startsWith(u8, meta_rest, "}")) "" else ",";
+ return std.fmt.allocPrint(a, "{s}\"_meta\":{{\"progressToken\":{d}{s}{s}", .{ line[0..after], id, meta_sep, meta_rest });
+ }
const sep: []const u8 = if (std.mem.startsWith(u8, rest, "}")) "" else ",";
return std.fmt.allocPrint(a, "{s}\"_meta\":{{\"progressToken\":{d}}}{s}{s}", .{ line[0..after], id, sep, rest });
}
diff --git a/src/mcp_protocol.zig b/src/mcp_protocol.zig
index 80d2dc8b..7257e931 100644
--- a/src/mcp_protocol.zig
+++ b/src/mcp_protocol.zig
@@ -95,6 +95,18 @@ pub fn buildRequest(a: Allocator, id: i64, method: []const u8, params: []const u
\\{{"jsonrpc":"2.0","id":{d},"method":"{s}","params":{{{s}}}}}
, .{ id, method, modern_meta });
}
+ // #1511: params that already lead with `_meta` (computer-use turn
+ // context) get the envelope merged in; a second `_meta` key is a
+ // duplicate a strict server rejects.
+ const meta_open = "\"_meta\":{";
+ if (std.mem.startsWith(u8, inner, meta_open)) {
+ const envelope = modern_meta[meta_open.len .. modern_meta.len - 1];
+ const rest = inner[meta_open.len..];
+ const sep: []const u8 = if (std.mem.startsWith(u8, rest, "}")) "" else ",";
+ return std.fmt.allocPrint(a,
+ \\{{"jsonrpc":"2.0","id":{d},"method":"{s}","params":{{{s}{s}{s}{s}}}}}
+ , .{ id, method, meta_open, envelope, sep, rest });
+ }
return std.fmt.allocPrint(a,
\\{{"jsonrpc":"2.0","id":{d},"method":"{s}","params":{{{s},{s}}}}}
, .{ id, method, modern_meta, inner });
diff --git a/src/mcp_turn_context.zig b/src/mcp_turn_context.zig
index 534b4e7f..e74b7d75 100644
--- a/src/mcp_turn_context.zig
+++ b/src/mcp_turn_context.zig
@@ -45,10 +45,9 @@ pub fn params(a: Allocator, server: []const u8, name: []const u8, input: std.jso
errdefer out.deinit();
var s: std.json.Stringify = .{ .writer = &out.writer };
try s.beginObject();
- try s.objectField("name");
- try s.write(name);
- try s.objectField("arguments");
- try s.write(input);
+ // #1511: `_meta` leads, so the protocol envelope and the progress token
+ // merge into this one object (mcp_protocol.buildRequest,
+ // mcp_notify.withProgressToken) instead of adding a second `_meta` key.
if (std.mem.eql(u8, server, "cua_repl") or std.mem.eql(u8, server, "node_repl")) {
if (context) |ctx| {
try s.objectField("_meta");
@@ -58,6 +57,10 @@ pub fn params(a: Allocator, server: []const u8, name: []const u8, input: std.jso
try s.endObject();
}
}
+ try s.objectField("name");
+ try s.write(name);
+ try s.objectField("arguments");
+ try s.write(input);
try s.endObject();
return out.toOwnedSlice();
}
@@ -114,3 +117,26 @@ test "computer adapters still get turn metadata when the caller omitted it" {
try std.testing.expect(meta.get("turn_id").?.string.len == 36);
try std.testing.expect(effective(null, std.testing.io, "ordinary") == null);
}
+
+test "#1511: a computer-use tools/call carries one _meta with the turn context and progress token" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ var state: State = .{};
+ state.begin(std.testing.io);
+ const input = try std.json.parseFromSliceLeaky(std.json.Value, a, "{\"code\":\"await createBrowserTab()\"}", .{});
+ const p = try params(a, "cua_repl", "js", input, state.value);
+ for ([_]bool{ false, true }) |modern| {
+ // The same steps as mcp_rpc.request for a tools/call.
+ const built = try @import("mcp_protocol.zig").buildRequest(a, 9, "tools/call", p, modern);
+ const line = try @import("mcp_notify.zig").withProgressToken(a, built, 9);
+ // A duplicate key is a parse error here, as it is for a strict server.
+ const v = try std.json.parseFromSliceLeaky(std.json.Value, a, line, .{ .duplicate_field_behavior = .@"error" });
+ const params_obj = v.object.get("params").?.object;
+ try std.testing.expectEqualStrings("js", params_obj.get("name").?.string);
+ const meta = params_obj.get("_meta").?.object;
+ try std.testing.expectEqual(@as(i64, 9), meta.get("progressToken").?.integer);
+ try std.testing.expectEqualStrings(&state.value.?.turn, meta.get("x-codex-turn-metadata").?.object.get("turn_id").?.string);
+ try std.testing.expectEqual(modern, meta.get("io.modelcontextprotocol/protocolVersion") != null);
+ }
+}