Skip to content

Commit bda5288

Browse files
authored
fix(gateway): preserve MCP literal union schemas (#108054)
1 parent d5ece3b commit bda5288

2 files changed

Lines changed: 191 additions & 13 deletions

File tree

‎src/gateway/mcp-http.schema.ts‎

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// MCP loopback tool schema projection.
22
// Converts gateway-scoped tools into MCP tools/list-compatible schemas.
3+
import { isDeepStrictEqual } from "node:util";
34
import { isRecord } from "@openclaw/normalization-core/record-coerce";
4-
import { uniqueValues } from "@openclaw/normalization-core/string-normalization";
55
import { logWarn } from "../logger.js";
66
import { resolveGatewayScopedTools } from "./tool-resolution.js";
77

@@ -57,6 +57,69 @@ function readLoopbackToolParameters(tool: McpLoopbackTool): Record<string, unkno
5757
}
5858
}
5959

60+
function readLiteralSchemaValues(schema: Record<string, unknown>): unknown[] | undefined {
61+
const enumValues = Array.isArray(schema.enum) ? schema.enum : undefined;
62+
if (Object.hasOwn(schema, "const")) {
63+
if (!enumValues) {
64+
return [schema.const];
65+
}
66+
return enumValues.some((value) => isDeepStrictEqual(value, schema.const)) ? [schema.const] : [];
67+
}
68+
return enumValues;
69+
}
70+
71+
function uniqueLiteralValues(values: unknown[]): unknown[] {
72+
return values.filter(
73+
(value, index) =>
74+
values.findIndex((candidate) => isDeepStrictEqual(candidate, value)) === index,
75+
);
76+
}
77+
78+
const SCHEMA_ANNOTATION_KEYS = new Set([
79+
"$comment",
80+
"default",
81+
"deprecated",
82+
"description",
83+
"example",
84+
"examples",
85+
"readOnly",
86+
"title",
87+
"writeOnly",
88+
]);
89+
90+
function readLiteralValidationConstraints(
91+
schema: Record<string, unknown>,
92+
): Record<string, unknown> {
93+
return Object.fromEntries(
94+
Object.entries(schema).filter(
95+
([key]) => key !== "const" && key !== "enum" && !SCHEMA_ANNOTATION_KEYS.has(key),
96+
),
97+
);
98+
}
99+
100+
function mergeLiteralSchemas(
101+
existing: Record<string, unknown>,
102+
incoming: Record<string, unknown>,
103+
): Record<string, unknown> | undefined {
104+
const existingValues = readLiteralSchemaValues(existing);
105+
const incomingValues = readLiteralSchemaValues(incoming);
106+
if (existingValues === undefined || incomingValues === undefined) {
107+
return undefined;
108+
}
109+
const existingConstraints = readLiteralValidationConstraints(existing);
110+
const incomingConstraints = readLiteralValidationConstraints(incoming);
111+
if (!isDeepStrictEqual(existingConstraints, incomingConstraints)) {
112+
return undefined;
113+
}
114+
const values = uniqueLiteralValues([...existingValues, ...incomingValues]);
115+
if (values.length === 0) {
116+
return undefined;
117+
}
118+
const merged: Record<string, unknown> = { ...existing, enum: values };
119+
delete merged.const;
120+
return merged;
121+
}
122+
60123
function flattenUnionSchema(
61124
raw: Record<string, unknown>,
62125
toolName: string,
@@ -111,20 +174,14 @@ function flattenUnionSchema(
111174
}
112175
continue;
113176
}
114-
if (Array.isArray(existing.enum) && Array.isArray(incoming.enum)) {
115-
mergedProps[key] = {
116-
...existing,
117-
enum: uniqueValues([...(existing.enum as unknown[]), ...(incoming.enum as unknown[])]),
118-
};
177+
if (isDeepStrictEqual(existing, incoming)) {
119178
continue;
120179
}
121-
if ("const" in existing && "const" in incoming && existing.const !== incoming.const) {
122-
const merged: Record<string, unknown> = {
123-
...existing,
124-
enum: [existing.const, incoming.const],
125-
};
126-
delete merged.const;
127-
mergedProps[key] = merged;
180+
// A prior const merge becomes an enum. Treat both as one literal family
181+
// so later union variants cannot silently disappear based on ordering.
182+
const mergedLiterals = mergeLiteralSchemas(existing, incoming);
183+
if (mergedLiterals) {
184+
mergedProps[key] = mergedLiterals;
128185
continue;
129186
}
130187
warnSchemaOnce(

‎src/gateway/mcp-http.test.ts‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,127 @@ describe("buildMcpToolSchema", () => {
775775
}
776776
});
777777

778+
it("preserves every literal across const and enum union variants", () => {
779+
const tool = makeMockTool({
780+
name: "codex_threads_literal_union",
781+
parameters: {
782+
anyOf: [
783+
{
784+
type: "object",
785+
properties: {
786+
action: {
787+
type: "string",
788+
const: "list",
789+
enum: ["list", "ignored"],
790+
description: "List threads",
791+
},
792+
thread_id: { type: "string" },
793+
},
794+
required: ["action"],
795+
},
796+
{
797+
type: "object",
798+
properties: {
799+
action: { type: "string", const: "fork", description: "Fork a thread" },
800+
thread_id: { type: "string" },
801+
},
802+
required: ["action"],
803+
},
804+
{
805+
type: "object",
806+
properties: {
807+
action: { type: "string", const: "rename" },
808+
thread_id: { type: "string" },
809+
},
810+
required: ["action"],
811+
},
812+
{
813+
type: "object",
814+
properties: {
815+
action: { type: "string", enum: ["archive", "unarchive"] },
816+
thread_id: { type: "string" },
817+
},
818+
required: ["action"],
819+
},
820+
],
821+
},
822+
});
823+
824+
expect(buildMockMcpToolSchema([tool])[0]?.inputSchema).toEqual({
825+
type: "object",
826+
properties: {
827+
action: {
828+
type: "string",
829+
enum: ["list", "fork", "rename", "archive", "unarchive"],
830+
description: "List threads",
831+
},
832+
thread_id: { type: "string" },
833+
},
834+
required: ["action"],
835+
});
836+
expect(logWarnMock).not.toHaveBeenCalled();
837+
});
838+
839+
it("keeps the first literal schema when validation constraints differ", () => {
840+
const tool = makeMockTool({
841+
name: "codex_threads_constrained_literals",
842+
parameters: {
843+
anyOf: [
844+
{
845+
type: "object",
846+
properties: {
847+
action: { type: "string", const: "list", pattern: "^list$" },
848+
},
849+
},
850+
{
851+
type: "object",
852+
properties: {
853+
action: { type: "string", const: "fork", pattern: "^fork$" },
854+
},
855+
},
856+
],
857+
},
858+
});
859+
860+
expect(buildMockMcpToolSchema([tool])[0]?.inputSchema).toMatchObject({
861+
properties: {
862+
action: { type: "string", const: "list", pattern: "^list$" },
863+
},
864+
});
865+
expect(logWarnMock.mock.calls.map(([message]) => message)).toEqual([
866+
'mcp loopback: conflicting schema definitions for "codex_threads_constrained_literals.action", keeping the first variant',
867+
]);
868+
});
869+
870+
it("does not warn for structurally identical property schemas", () => {
871+
const tool = makeMockTool({
872+
name: "codex_threads_identical_properties",
873+
parameters: {
874+
oneOf: [
875+
{
876+
type: "object",
877+
properties: {
878+
thread_id: { type: "string", description: "Thread identifier" },
879+
},
880+
},
881+
{
882+
type: "object",
883+
properties: {
884+
thread_id: { type: "string", description: "Thread identifier" },
885+
},
886+
},
887+
],
888+
},
889+
});
890+
891+
expect(buildMockMcpToolSchema([tool])[0]?.inputSchema).toMatchObject({
892+
properties: {
893+
thread_id: { type: "string", description: "Thread identifier" },
894+
},
895+
});
896+
expect(logWarnMock).not.toHaveBeenCalled();
897+
});
898+
778899
it("warns once for repeated union schema conflicts across loopback schema rebuilds", () => {
779900
const tool = makeMockTool({
780901
name: "mcp_message_send_rebuild",

0 commit comments

Comments
 (0)