Skip to content

Commit 7573b91

Browse files
authored
fix(code-mode): preserve MCP array union declarations (#131528)
Render MCP array item types with Array<...> so API.read and MCP.$api preserve enum and union grouping. Keep advertised schemas and runtime validation unchanged. Fixes #131476. Release context: Code Mode MCP declarations now correctly describe mixed enum and union arrays, including nested arrays.
1 parent 2058fd7 commit 7573b91

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

‎src/agents/code-mode-mcp-api.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function schemaType(schema: unknown): string {
140140
case "boolean":
141141
return "boolean";
142142
case "array":
143-
return `${schemaType(schema.items)}[]`;
143+
return `Array<${schemaType(schema.items)}>`;
144144
case "object":
145145
return renderInlineObjectType(schema);
146146
case "null":

‎src/agents/code-mode-namespaces.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,69 @@ describe("Code Mode MCP namespace model", () => {
7474
);
7575
});
7676

77+
it.each([
78+
{
79+
name: "enum",
80+
items: { type: "string", enum: ["red", "blue"] },
81+
declaration: 'Array<"red" | "blue">',
82+
},
83+
{
84+
name: "anyOf",
85+
items: { anyOf: [{ type: "string" }, { type: "number" }] },
86+
declaration: "Array<string | number>",
87+
},
88+
{
89+
name: "oneOf",
90+
items: { oneOf: [{ type: "boolean" }, { type: "null" }] },
91+
declaration: "Array<boolean | null>",
92+
},
93+
{
94+
name: "multiple types",
95+
items: { type: ["string", "null"] },
96+
declaration: "Array<string | null>",
97+
},
98+
{
99+
name: "nested array",
100+
items: { type: "array", items: { type: "string", enum: ["red", "blue"] } },
101+
declaration: 'Array<Array<"red" | "blue">>',
102+
},
103+
{
104+
name: "simple array",
105+
items: { type: "string" },
106+
declaration: "Array<string>",
107+
},
108+
{
109+
name: "object",
110+
items: { type: "object", properties: { id: { type: "string" } }, required: ["id"] },
111+
declaration: "Array<{ id: string }>",
112+
},
113+
])("preserves $name item grouping in MCP API declarations", async ({ items, declaration }) => {
114+
const parameters = {
115+
type: "object",
116+
properties: { values: { type: "array", items } },
117+
required: ["values"],
118+
};
119+
const runtime = createCodeModeNamespaceRuntime([
120+
mcpCatalogEntry({ id: "github__read_file", parameters }),
121+
]);
122+
const executeTool = vi.fn();
123+
const api = await runtime.invoke(
124+
"mcp",
125+
["github", "$api"],
126+
["readFile", { schema: true }],
127+
executeTool,
128+
);
129+
130+
expect(runtime.apiFiles.find((file) => file.path === "mcp/github.d.ts")?.content).toContain(
131+
`values: ${declaration};`,
132+
);
133+
expect(api).toMatchObject({
134+
header: expect.stringContaining(`values: ${declaration};`),
135+
schemas: { readFile: parameters },
136+
});
137+
expect(executeTool).not.toHaveBeenCalled();
138+
});
139+
77140
it.each(["constructor", "toString", "__proto__"])(
78141
"does not satisfy required MCP argument %s from Object.prototype",
79142
async (key) => {

‎src/agents/code-mode.mcp.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe("Code Mode MCP namespace", () => {
5353
repo: { type: "string", description: "Repository 名称" },
5454
title: { type: "string", description: "Issue title\nShown in tracker" },
5555
body: { type: "string", default: "" },
56+
labels: { type: "array", items: { type: "string", enum: ["red", "blue"] } },
5657
},
5758
required: ["owner", "repo", "title"],
5859
},
@@ -83,6 +84,7 @@ describe("Code Mode MCP namespace", () => {
8384
owner: "openclaw",
8485
repo: "openclaw",
8586
title: "Ship it",
87+
labels: ["red", "blue"],
8688
});
8789
const createdPayload = JSON.parse(created.content[0].text);
8890
const searchHits = await catalog.search("github create issue", { limit: 5 });
@@ -117,6 +119,7 @@ describe("Code Mode MCP namespace", () => {
117119
repo: "openclaw",
118120
title: "Ship it",
119121
body: "",
122+
labels: ["red", "blue"],
120123
},
121124
},
122125
leakedInternalDetails: false,
@@ -147,6 +150,8 @@ describe("Code Mode MCP namespace", () => {
147150
expect(value.apiHeader).toContain("@param title Issue title Shown in tracker");
148151
expect(value.apiHeader).not.toContain("@param title Issue title\n");
149152
expect(value.apiHeader).toContain("title: string;");
153+
expect(value.apiHeader).toContain('labels?: Array<"red" | "blue">;');
154+
expect(value.serverFileContent).toContain('labels?: Array<"red" | "blue">;');
150155
expect(githubCreate.execute).toHaveBeenCalledTimes(1);
151156
});
152157

0 commit comments

Comments
 (0)