Skip to content

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

Python

fix(mcp): lower top-level oneOf/allOf/anyOf so one MCP tool cannot break every Anthropic turn (#352)

justrachec19f224816c
diff --git a/src/mcp.zig b/src/mcp.zig
index fdec3ff9..dc128c05 100644
--- a/src/mcp.zig
+++ b/src/mcp.zig
@@ -446,6 +446,10 @@ pub const Registry = struct {
const desc = if (t.object.get("description")) |d| (if (d == .string) d.string else "") else if (t.object.get("title")) |ti| (if (ti == .string) ti.string else "") else "";
var schema = t.object.get("inputSchema") orelse Value{ .object = .empty };
try rewriteOneOf(a, &schema);
+ // ...then lower any TOP-LEVEL combinator: Anthropic rejects the
+ // whole request over one, so a single server advertising it would
+ // break every turn (codedbpro's `replace`, "path or paths").
+ try mcp_protocol.flattenTopLevel(a, &schema);
try tools.append(a, .{
.server_index = server_index,
.original_name = orig,
diff --git a/src/mcp_protocol.zig b/src/mcp_protocol.zig
index 0d58c592..6cb35d30 100644
--- a/src/mcp_protocol.zig
+++ b/src/mcp_protocol.zig
@@ -1,30 +1,20 @@
-//! MCP protocol negotiation and tool JSON Schema normalization.
+//! MCP protocol negotiation. Tool JSON Schema normalization lives in the sibling mcp_schema.zig.
const std = @import("std");
const Value = std.json.Value;
const Allocator = std.mem.Allocator;
-/// Recursively rewrite the JSON Schema keyword `oneOf` to `anyOf` (graff's
-/// rewrite_one_of_to_any_of). OpenAI's tool-schema validator — including the
-/// chatgpt.com /codex/responses endpoint — rejects `oneOf` outright with
-/// "'oneOf' is not permitted"; `anyOf` is accepted by both OpenAI and
-/// Anthropic and is equivalent for the discriminated unions MCP servers emit
-/// in practice. When both keywords are present (rare, ambiguous to merge),
-/// the existing `anyOf` wins and `oneOf` is dropped. Runs once per tool at
-/// discovery, so the rendered tools JSON stays KV-cache-stable.
-pub fn rewriteOneOf(a: Allocator, v: *Value) Allocator.Error!void {
- switch (v.*) {
- .object => |*obj| {
- if (obj.get("oneOf")) |branches| {
- if (obj.get("anyOf") == null) try obj.put(a, "anyOf", branches);
- _ = obj.swapRemove("oneOf");
- }
- var it = obj.iterator();
- while (it.next()) |e| try rewriteOneOf(a, e.value_ptr);
- },
- .array => |*arr| for (arr.items) |*item| try rewriteOneOf(a, item),
- else => {},
- }
+/// Tool-schema normalization (rewriteOneOf, flattenTopLevel) moved to
+/// mcp_schema.zig at the 600-line ceiling; re-exported so every existing
+/// call site keeps working.
+const mcp_schema = @import("mcp_schema.zig");
+pub const rewriteOneOf = mcp_schema.rewriteOneOf;
+pub const combinators = mcp_schema.combinators;
+pub const topLevelCombinator = mcp_schema.topLevelCombinator;
+pub const flattenTopLevel = mcp_schema.flattenTopLevel;
+
+test { // the moved tests still have to run from a referenced module
+ _ = mcp_schema;
}
/// The one modern (stateless, per-request `_meta`) revision graff speaks.
@@ -219,47 +209,6 @@ pub fn resultIsComplete(result: std.json.ObjectMap) bool {
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();
- const a = arena_state.allocator();
- var v = try std.json.parseFromSliceLeaky(Value, a,
- \\{"oneOf":[{"type":"string"}],"properties":{"x":{"oneOf":[{"type":"number"},{"type":"null"}]}}}
- , .{});
- try rewriteOneOf(a, &v);
- try std.testing.expect(v.object.get("oneOf") == null);
- try std.testing.expectEqual(@as(usize, 1), v.object.get("anyOf").?.array.items.len);
- const x = v.object.get("properties").?.object.get("x").?;
- try std.testing.expect(x.object.get("oneOf") == null);
- try std.testing.expectEqual(@as(usize, 2), x.object.get("anyOf").?.array.items.len);
-}
-
-test "rewriteOneOf: existing anyOf wins when both are present" {
- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
- defer arena_state.deinit();
- const a = arena_state.allocator();
- var v = try std.json.parseFromSliceLeaky(Value, a,
- \\{"anyOf":[{"type":"string"}],"oneOf":[{"type":"number"},{"type":"boolean"}]}
- , .{});
- try rewriteOneOf(a, &v);
- try std.testing.expect(v.object.get("oneOf") == null);
- // the pre-existing single-branch anyOf survives, the oneOf is dropped
- try std.testing.expectEqual(@as(usize, 1), v.object.get("anyOf").?.array.items.len);
-}
-
-test "rewriteOneOf: arrays and scalars pass through untouched" {
- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
- defer arena_state.deinit();
- const a = arena_state.allocator();
- var v = try std.json.parseFromSliceLeaky(Value, a,
- \\{"items":[{"oneOf":[1,2]},"plain",42]}
- , .{});
- try rewriteOneOf(a, &v);
- const first = v.object.get("items").?.array.items[0];
- try std.testing.expect(first.object.get("oneOf") == null);
- try std.testing.expect(first.object.get("anyOf") != null);
-}
-
test "initialize negotiation accepts transport-compatible protocol versions" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
diff --git a/src/mcp_schema.zig b/src/mcp_schema.zig
new file mode 100644
index 00000000..29ed48d5
--- /dev/null
+++ b/src/mcp_schema.zig
@@ -0,0 +1,180 @@
+//! MCP tool JSON Schema normalization: making a server-supplied
+//! `inputSchema` acceptable to every provider graff can talk to.
+//!
+//! Split out of mcp_protocol.zig (600-line ceiling) when the Anthropic
+//! top-level-combinator fix landed; that file keeps protocol negotiation and
+//! re-exports these so existing call sites are unchanged.
+
+const std = @import("std");
+const Value = std.json.Value;
+const Allocator = std.mem.Allocator;
+
+/// Recursively rewrite the JSON Schema keyword `oneOf` to `anyOf` (graff's
+/// rewrite_one_of_to_any_of). OpenAI's tool-schema validator — including the
+/// chatgpt.com /codex/responses endpoint — rejects `oneOf` outright with
+/// "'oneOf' is not permitted"; `anyOf` is accepted by both OpenAI and
+/// Anthropic and is equivalent for the discriminated unions MCP servers emit
+/// in practice. When both keywords are present (rare, ambiguous to merge),
+/// the existing `anyOf` wins and `oneOf` is dropped. Runs once per tool at
+/// discovery, so the rendered tools JSON stays KV-cache-stable.
+pub fn rewriteOneOf(a: Allocator, v: *Value) Allocator.Error!void {
+ switch (v.*) {
+ .object => |*obj| {
+ if (obj.get("oneOf")) |branches| {
+ if (obj.get("anyOf") == null) try obj.put(a, "anyOf", branches);
+ _ = obj.swapRemove("oneOf");
+ }
+ var it = obj.iterator();
+ while (it.next()) |e| try rewriteOneOf(a, e.value_ptr);
+ },
+ .array => |*arr| for (arr.items) |*item| try rewriteOneOf(a, item),
+ else => {},
+ }
+}
+
+/// The JSON Schema combinators no provider agrees on at the top level of a
+/// tool schema.
+pub const combinators = [_][]const u8{ "allOf", "anyOf", "oneOf" };
+
+/// The first combinator present at the TOP level of `schema`, or null. Nested
+/// combinators (inside a property) are legal everywhere and are not reported.
+pub fn topLevelCombinator(schema: Value) ?[]const u8 {
+ if (schema != .object) return null;
+ for (combinators) |key| {
+ if (schema.object.get(key) != null) return key;
+ }
+ return null;
+}
+
+/// Lower a top-level `allOf`/`anyOf`/`oneOf` into a plain object schema.
+///
+/// The Anthropic Messages API rejects any tool whose input_schema carries one
+/// of these at the top level — "input_schema does not support oneOf, allOf, or
+/// anyOf at the top level" — and it rejects the whole REQUEST, so a single
+/// MCP server advertising one takes down every turn, not just calls to that
+/// tool. `rewriteOneOf` above does not help: it rewrites `oneOf` to `anyOf`,
+/// which Anthropic rejects just as hard. Real example that caused this
+/// (codedbpro's `replace`): `{type, properties, required,
+/// anyOf:[{required:["path"]},{required:["paths"]}]}` — "path or paths".
+///
+/// Lowering rather than deleting, so nothing the model can pass is lost:
+/// * `allOf` branches all hold, so their `properties` AND `required` merge in;
+/// * `anyOf`/`oneOf` branches are alternatives, so only their `properties`
+/// merge — hoisting their `required` would demand every alternative at
+/// once. The requirement instead becomes a sentence in the schema's
+/// description, so the model still knows one of them is needed and the
+/// server keeps enforcing it.
+/// Existing top-level keys always win; nested combinators are left alone.
+pub fn flattenTopLevel(a: Allocator, v: *Value) Allocator.Error!void {
+ if (v.* != .object) return;
+ if (topLevelCombinator(v.*) == null) return; // the overwhelmingly common case
+ var alternatives: std.ArrayList([]const u8) = .empty;
+ for (combinators) |key| {
+ const branches = v.object.get(key) orelse continue;
+ _ = v.object.swapRemove(key);
+ if (branches != .array) continue;
+ const all = std.mem.eql(u8, key, "allOf");
+ for (branches.array.items) |branch| {
+ if (branch != .object) continue;
+ try mergeProperties(a, &v.object, branch.object);
+ if (all) try mergeRequired(a, &v.object, branch.object) else try noteRequired(a, &alternatives, branch.object);
+ }
+ }
+ // A flattened schema must still be a valid object schema to Anthropic.
+ if (v.object.get("type") == null) try v.object.put(a, "type", .{ .string = "object" });
+ if (alternatives.items.len > 1) {
+ const joined = try std.mem.join(a, " or ", alternatives.items);
+ const existing = if (v.object.get("description")) |d| (if (d == .string) d.string else "") else "";
+ const note = try std.fmt.allocPrint(a, "{s}{s}Requires one of: {s}.", .{ existing, if (existing.len > 0) " " else "", joined });
+ try v.object.put(a, "description", .{ .string = note });
+ }
+}
+
+fn mergeProperties(a: Allocator, top: *std.json.ObjectMap, branch: std.json.ObjectMap) Allocator.Error!void {
+ const from = branch.get("properties") orelse return;
+ if (from != .object) return;
+ const existing = top.get("properties");
+ if (existing == null or existing.? != .object) {
+ try top.put(a, "properties", from);
+ return;
+ }
+ var target = existing.?.object;
+ var it = from.object.iterator();
+ while (it.next()) |e| {
+ if (target.get(e.key_ptr.*) != null) continue; // the top level wins
+ try target.put(a, e.key_ptr.*, e.value_ptr.*);
+ }
+ try top.put(a, "properties", .{ .object = target });
+}
+
+fn mergeRequired(a: Allocator, top: *std.json.ObjectMap, branch: std.json.ObjectMap) Allocator.Error!void {
+ const from = branch.get("required") orelse return;
+ if (from != .array) return;
+ var list: std.json.Array = if (top.get("required")) |r|
+ (if (r == .array) r.array else std.json.Array.init(a))
+ else
+ std.json.Array.init(a);
+ for (from.array.items) |name| {
+ if (name != .string) continue;
+ var seen = false;
+ for (list.items) |have| {
+ if (have == .string and std.mem.eql(u8, have.string, name.string)) seen = true;
+ }
+ if (!seen) try list.append(name);
+ }
+ try top.put(a, "required", .{ .array = list });
+}
+
+/// Record one alternative branch's required names ("path", or "a + b") so the
+/// dropped constraint survives as prose.
+fn noteRequired(a: Allocator, out: *std.ArrayList([]const u8), branch: std.json.ObjectMap) Allocator.Error!void {
+ const from = branch.get("required") orelse return;
+ if (from != .array or from.array.items.len == 0) return;
+ var names: std.ArrayList([]const u8) = .empty;
+ for (from.array.items) |name| {
+ if (name == .string) try names.append(a, name.string);
+ }
+ if (names.items.len == 0) return;
+ try out.append(a, try std.mem.join(a, " + ", names.items));
+}
+
+test "rewriteOneOf: converts oneOf to anyOf, recursively" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var v = try std.json.parseFromSliceLeaky(Value, a,
+ \\{"oneOf":[{"type":"string"}],"properties":{"x":{"oneOf":[{"type":"number"},{"type":"null"}]}}}
+ , .{});
+ try rewriteOneOf(a, &v);
+ try std.testing.expect(v.object.get("oneOf") == null);
+ try std.testing.expectEqual(@as(usize, 1), v.object.get("anyOf").?.array.items.len);
+ const x = v.object.get("properties").?.object.get("x").?;
+ try std.testing.expect(x.object.get("oneOf") == null);
+ try std.testing.expectEqual(@as(usize, 2), x.object.get("anyOf").?.array.items.len);
+}
+
+test "rewriteOneOf: existing anyOf wins when both are present" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var v = try std.json.parseFromSliceLeaky(Value, a,
+ \\{"anyOf":[{"type":"string"}],"oneOf":[{"type":"number"},{"type":"boolean"}]}
+ , .{});
+ try rewriteOneOf(a, &v);
+ try std.testing.expect(v.object.get("oneOf") == null);
+ // the pre-existing single-branch anyOf survives, the oneOf is dropped
+ try std.testing.expectEqual(@as(usize, 1), v.object.get("anyOf").?.array.items.len);
+}
+
+test "rewriteOneOf: arrays and scalars pass through untouched" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var v = try std.json.parseFromSliceLeaky(Value, a,
+ \\{"items":[{"oneOf":[1,2]},"plain",42]}
+ , .{});
+ try rewriteOneOf(a, &v);
+ const first = v.object.get("items").?.array.items[0];
+ try std.testing.expect(first.object.get("oneOf") == null);
+ try std.testing.expect(first.object.get("anyOf") != null);
+}
diff --git a/src/smolify_manifest.zig b/src/smolify_manifest.zig
index 29c89251..11b46180 100644
--- a/src/smolify_manifest.zig
+++ b/src/smolify_manifest.zig
@@ -63,6 +63,8 @@ pub fn appendTools(
"";
var schema = tool.object.get("inputSchema") orelse Value{ .object = .empty };
try mcp_protocol.rewriteOneOf(a, &schema);
+ try mcp_protocol.flattenTopLevel(a, &schema); // Anthropic rejects a top-level combinator outright
+
try out.append(a, .{
.server_index = server_index,
.original_name = try a.dupe(u8, name_v.string),
diff --git a/src/tool_gates.zig b/src/tool_gates.zig
index 74fea8b4..a8ff8772 100644
--- a/src/tool_gates.zig
+++ b/src/tool_gates.zig
@@ -91,6 +91,10 @@ pub fn withAvailable(comptime Spec: type, arena: Allocator, base: []const Spec,
return buf[0..i];
}
+test { // the served catalogs' wire-compatibility guard (an unreferenced module's tests never run)
+ _ = @import("tool_schema_tests.zig");
+}
+
test "#352: an optional tool is advertised only while its flag is set; other names are unaffected" {
const saved = imagegen.available;
defer imagegen.available = saved;
diff --git a/src/tool_schema_tests.zig b/src/tool_schema_tests.zig
new file mode 100644
index 00000000..c1e02883
--- /dev/null
+++ b/src/tool_schema_tests.zig
@@ -0,0 +1,194 @@
+//! Wire-compatibility guard for every tool schema graff serves.
+//!
+//! The Anthropic Messages API rejects a tool whose `input_schema` carries
+//! `oneOf`, `allOf` or `anyOf` at the TOP level — and it rejects the entire
+//! REQUEST, not just that tool, so one bad schema takes down every turn before
+//! the model is ever reached:
+//!
+//! api error: tools.27.custom.input_schema: input_schema does not support
+//! oneOf, allOf, or anyOf at the top level
+//!
+//! That was a live failure on a real Anthropic run. It survived the earlier
+//! checks because those exercised the codex/Responses wire and `--schema`,
+//! neither of which validates this. The offender was an MCP tool
+//! (codedbpro's `replace`, expressing "path or paths"), which is why the guard
+//! covers BOTH sources: the built-in catalogs are asserted clean at every
+//! nesting level, and MCP-sourced schemas — where nested combinators are legal
+//! and common — are asserted clean at the top level after normalization.
+//!
+//! Pulled in from tool_gates.zig's test block; an unreferenced module's tests
+//! silently compile to nothing.
+
+const std = @import("std");
+const Value = std.json.Value;
+const Allocator = std.mem.Allocator;
+
+const schema = @import("schema.zig");
+const mcp = @import("mcp.zig");
+const mcp_protocol = @import("mcp_protocol.zig");
+const imagegen = @import("imagegen.zig");
+const no_local_tools = @import("no_local_tools.zig");
+const Provider = @import("provider.zig").Provider;
+
+const testing = std.testing;
+
+/// Any combinator at any depth, for schemas we author ourselves.
+fn combinatorAnywhere(v: Value) ?[]const u8 {
+ switch (v) {
+ .object => |obj| {
+ if (mcp_protocol.topLevelCombinator(v)) |k| return k;
+ var it = obj.iterator();
+ while (it.next()) |e| {
+ if (combinatorAnywhere(e.value_ptr.*)) |k| return k;
+ }
+ },
+ .array => |arr| for (arr.items) |item| {
+ if (combinatorAnywhere(item)) |k| return k;
+ },
+ else => {},
+ }
+ return null;
+}
+
+/// Every rendered catalog, parsed, with each tool's schema handed to `check`.
+fn forEachServedSchema(arena: Allocator, catalog: []const u8, check: *const fn (name: []const u8, sch: Value) anyerror!void) !void {
+ const parsed = try std.json.parseFromSliceLeaky(Value, arena, catalog, .{ .allocate = .alloc_always });
+ for (parsed.array.items) |tool| {
+ const obj = tool.object;
+ // anthropic: {name, input_schema}; openai: {function:{name,parameters}};
+ // responses: {name, parameters}.
+ const fn_obj = if (obj.get("function")) |f| f.object else obj;
+ const name = fn_obj.get("name").?.string;
+ const sch = fn_obj.get("input_schema") orelse fn_obj.get("parameters").?;
+ try check(name, sch);
+ }
+}
+
+fn assertClean(name: []const u8, sch: Value) anyerror!void {
+ if (combinatorAnywhere(sch)) |key| {
+ std.debug.print("\ntool '{s}' has a JSON Schema '{s}' — Anthropic rejects the whole request over a top-level one\n", .{ name, key });
+ return error.ToolSchemaHasCombinator;
+ }
+}
+
+test "no built-in tool schema carries oneOf/allOf/anyOf — the wire rejection that killed a live Anthropic run" {
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+
+ const saved_gate = no_local_tools.enabled;
+ const saved_imagegen = imagegen.available;
+ defer {
+ no_local_tools.enabled = saved_gate;
+ imagegen.available = saved_imagegen;
+ }
+
+ // Every root catalog variant, in every wire format, with the gated tool
+ // both present and absent — the served set is what has to be valid.
+ for ([_]bool{ false, true }) |gated| {
+ no_local_tools.enabled = gated;
+ for ([_]bool{ false, true }) |optional| {
+ imagegen.available = optional;
+ const specs = try schema.effectiveRootSpecs(arena);
+ for ([_]Provider.Kind{ .anthropic, .openai, .responses }) |kind| {
+ const catalog = try schema.renderRootTools(arena, kind, specs, &.{});
+ try forEachServedSchema(arena, catalog, assertClean);
+ // ...and the subagent catalogs, which are comptime constants
+ // rather than assembled here.
+ try forEachServedSchema(arena, schema.subToolsJson(kind, gated), assertClean);
+ }
+ }
+ }
+
+ // The guard has to be able to fail, or it proves nothing.
+ const bad = try std.json.parseFromSliceLeaky(Value, arena,
+ \\{"type":"object","properties":{},"anyOf":[{"required":["a"]},{"required":["b"]}]}
+ , .{});
+ try testing.expectError(error.ToolSchemaHasCombinator, assertClean("fake", bad));
+}
+
+test "an MCP tool's top-level combinator is lowered at discovery, so one server cannot break every turn" {
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+
+ // The exact shape that failed live: codedbpro's `replace`.
+ var replace = try std.json.parseFromSliceLeaky(Value, arena,
+ \\{"type":"object","properties":{"pattern":{"type":"string"},"path":{"type":"string"},"paths":{"type":"array"}},"required":["pattern"],"anyOf":[{"required":["path"]},{"required":["paths"]}]}
+ , .{});
+ try mcp_protocol.rewriteOneOf(arena, &replace);
+ try mcp_protocol.flattenTopLevel(arena, &replace);
+
+ try testing.expect(mcp_protocol.topLevelCombinator(replace) == null);
+ // Nothing callable was lost, and the dropped constraint survives as prose.
+ try testing.expectEqualStrings("object", replace.object.get("type").?.string);
+ for ([_][]const u8{ "pattern", "path", "paths" }) |p|
+ try testing.expect(replace.object.get("properties").?.object.get(p) != null);
+ try testing.expectEqual(@as(usize, 1), replace.object.get("required").?.array.items.len); // alternatives NOT force-merged
+ try testing.expect(std.mem.indexOf(u8, replace.object.get("description").?.string, "Requires one of: path or paths.") != null);
+
+ // A server that sends oneOf gets the same treatment (rewriteOneOf makes it
+ // anyOf first, which Anthropic rejects just as hard).
+ var one_of = try std.json.parseFromSliceLeaky(Value, arena,
+ \\{"type":"object","oneOf":[{"required":["a"]},{"required":["b","c"]}]}
+ , .{});
+ try mcp_protocol.rewriteOneOf(arena, &one_of);
+ try mcp_protocol.flattenTopLevel(arena, &one_of);
+ try testing.expect(mcp_protocol.topLevelCombinator(one_of) == null);
+ try testing.expect(std.mem.indexOf(u8, one_of.object.get("description").?.string, "Requires one of: a or b + c.") != null);
+
+ // allOf DOES conjoin: every branch holds, so properties and required merge.
+ var all_of = try std.json.parseFromSliceLeaky(Value, arena,
+ \\{"allOf":[{"properties":{"x":{"type":"string"}},"required":["x"]},{"properties":{"y":{"type":"number"}},"required":["y"]}]}
+ , .{});
+ try mcp_protocol.flattenTopLevel(arena, &all_of);
+ try testing.expect(mcp_protocol.topLevelCombinator(all_of) == null);
+ try testing.expectEqualStrings("object", all_of.object.get("type").?.string); // added, since the source had none
+ try testing.expectEqual(@as(usize, 2), all_of.object.get("required").?.array.items.len);
+ try testing.expect(all_of.object.get("properties").?.object.get("y") != null);
+
+ // A NESTED combinator is legal on every provider and must survive.
+ var nested = try std.json.parseFromSliceLeaky(Value, arena,
+ \\{"type":"object","properties":{"v":{"anyOf":[{"type":"string"},{"type":"number"}]}}}
+ , .{});
+ try mcp_protocol.flattenTopLevel(arena, &nested);
+ try testing.expect(mcp_protocol.topLevelCombinator(nested) == null);
+ try testing.expect(nested.object.get("properties").?.object.get("v").?.object.get("anyOf") != null);
+
+ // A schema with no combinator is returned byte-identical.
+ var plain = try std.json.parseFromSliceLeaky(Value, arena, "{\"type\":\"object\",\"properties\":{}}", .{});
+ try mcp_protocol.flattenTopLevel(arena, &plain);
+ try testing.expectEqual(@as(usize, 2), plain.object.count());
+}
+
+test "the discovery path itself flattens, so nothing reaches a provider unlowered" {
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+
+ // Render an MCP tool through the same writer the root catalog uses, with
+ // a schema that has already been through discovery's normalization.
+ var sch = try std.json.parseFromSliceLeaky(Value, arena,
+ \\{"type":"object","properties":{"a":{"type":"string"}},"oneOf":[{"required":["a"]},{"required":["b"]}]}
+ , .{});
+ try mcp_protocol.rewriteOneOf(arena, &sch);
+ try mcp_protocol.flattenTopLevel(arena, &sch);
+ const tools = [_]mcp.Tool{.{
+ .server_index = 0,
+ .original_name = "replace",
+ .qualified_name = "mcp__srv__replace",
+ .description = "d",
+ .input_schema = sch,
+ }};
+ for ([_]Provider.Kind{ .anthropic, .openai, .responses }) |kind| {
+ const catalog = try schema.renderRootTools(arena, kind, &.{}, &tools);
+ try forEachServedSchema(arena, catalog, struct {
+ fn check(name: []const u8, s: Value) anyerror!void {
+ if (mcp_protocol.topLevelCombinator(s)) |key| {
+ std.debug.print("\nMCP tool '{s}' still has top-level '{s}'\n", .{ name, key });
+ return error.ToolSchemaHasCombinator;
+ }
+ }
+ }.check);
+ }
+}