fix: coerce MCP tool args to match schema string types (#11852)
* fix: coerce MCP tool args to match schema string types When tool call arguments are parsed via JSON.parse(), string-typed parameters containing valid JSON (e.g. file content for .json files) get deeply parsed into objects. Built-in tools handle this via getStringArg(), but MCP tools with dynamic schemas had no equivalent fix, causing write_file and similar tools to receive objects instead of strings. Add coerceArgsToSchema() that checks the tool's inputSchema and re-stringifies any values that should be strings before passing args to the MCP client. Fixes #8510 * fix: narrow coercion to only objects/arrays, not numbers/booleans Only re-stringify values that are objects or arrays (the actual deep-parsing issue). Numbers and booleans passed to a string-typed parameter are left as-is so they surface as type errors rather than being silently converted.
This commit is contained in:
parent
b7fead504e
commit
ff89a88299
3 changed files with 168 additions and 2 deletions
|
|
@ -23,7 +23,7 @@ import { searchWebImpl } from "./implementations/searchWeb";
|
|||
import { viewDiffImpl } from "./implementations/viewDiff";
|
||||
import { viewRepoMapImpl } from "./implementations/viewRepoMap";
|
||||
import { viewSubdirectoryImpl } from "./implementations/viewSubdirectory";
|
||||
import { safeParseToolCallArgs } from "./parseArgs";
|
||||
import { coerceArgsToSchema, safeParseToolCallArgs } from "./parseArgs";
|
||||
|
||||
async function callHttpTool(
|
||||
url: string,
|
||||
|
|
@ -95,10 +95,14 @@ async function callToolFromUri(
|
|||
if (!client) {
|
||||
throw new Error("MCP connection not found");
|
||||
}
|
||||
const coercedArgs = coerceArgsToSchema(
|
||||
args,
|
||||
extras.tool?.function?.parameters,
|
||||
);
|
||||
const response = await client.client.callTool(
|
||||
{
|
||||
name: toolName,
|
||||
arguments: args,
|
||||
arguments: coercedArgs,
|
||||
},
|
||||
CallToolResultSchema,
|
||||
{ timeout: client.options.timeout },
|
||||
|
|
|
|||
|
|
@ -24,6 +24,44 @@ export function safeParseToolCallArgs(
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Coerce parsed args to match the tool's input schema types.
|
||||
* JSON.parse() deeply parses all values, so string-typed parameters
|
||||
* that contain valid JSON (e.g. file content for a .json file) get
|
||||
* converted to objects. This checks the schema and re-stringifies
|
||||
* any values that should be strings.
|
||||
*/
|
||||
export function coerceArgsToSchema(
|
||||
args: Record<string, any>,
|
||||
schema?: Record<string, any>,
|
||||
): Record<string, any> {
|
||||
if (!schema?.properties) {
|
||||
return args;
|
||||
}
|
||||
|
||||
const coerced = { ...args };
|
||||
for (const [key, value] of Object.entries(coerced)) {
|
||||
const propSchema = schema.properties[key];
|
||||
if (!propSchema) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
propSchema.type === "string" &&
|
||||
typeof value === "object" &&
|
||||
value !== null
|
||||
) {
|
||||
try {
|
||||
coerced[key] = JSON.stringify(value);
|
||||
} catch {
|
||||
// leave as-is if stringify fails
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return coerced;
|
||||
}
|
||||
|
||||
export function getStringArg(
|
||||
args: any,
|
||||
argName: string,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { describe, expect, it } from "vitest";
|
||||
import { ToolCallDelta } from "..";
|
||||
import {
|
||||
coerceArgsToSchema,
|
||||
getBooleanArg,
|
||||
getNumberArg,
|
||||
getOptionalStringArg,
|
||||
|
|
@ -292,6 +293,129 @@ describe("getBooleanArg", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("coerceArgsToSchema", () => {
|
||||
it("should stringify object values when schema expects string", () => {
|
||||
const args = { content: { key: "value", number: 123 } };
|
||||
const schema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
content: { type: "string" },
|
||||
},
|
||||
};
|
||||
const result = coerceArgsToSchema(args, schema);
|
||||
expect(result.content).toBe('{"key":"value","number":123}');
|
||||
});
|
||||
|
||||
it("should stringify array values when schema expects string", () => {
|
||||
const args = { content: ["item1", "item2"] };
|
||||
const schema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
content: { type: "string" },
|
||||
},
|
||||
};
|
||||
const result = coerceArgsToSchema(args, schema);
|
||||
expect(result.content).toBe('["item1","item2"]');
|
||||
});
|
||||
|
||||
it("should leave string values as-is", () => {
|
||||
const args = { content: "hello world" };
|
||||
const schema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
content: { type: "string" },
|
||||
},
|
||||
};
|
||||
const result = coerceArgsToSchema(args, schema);
|
||||
expect(result.content).toBe("hello world");
|
||||
});
|
||||
|
||||
it("should leave non-string schema types as-is", () => {
|
||||
const args = { count: 42, data: { nested: true } };
|
||||
const schema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
count: { type: "number" },
|
||||
data: { type: "object" },
|
||||
},
|
||||
};
|
||||
const result = coerceArgsToSchema(args, schema);
|
||||
expect(result.count).toBe(42);
|
||||
expect(result.data).toEqual({ nested: true });
|
||||
});
|
||||
|
||||
it("should return args unchanged when no schema provided", () => {
|
||||
const args = { content: { key: "value" } };
|
||||
const result = coerceArgsToSchema(args, undefined);
|
||||
expect(result).toBe(args);
|
||||
});
|
||||
|
||||
it("should return args unchanged when schema has no properties", () => {
|
||||
const args = { content: { key: "value" } };
|
||||
const result = coerceArgsToSchema(args, { type: "object" });
|
||||
expect(result).toBe(args);
|
||||
});
|
||||
|
||||
it("should handle mixed args with some needing coercion", () => {
|
||||
const args = {
|
||||
filePath: "/path/to/file.json",
|
||||
content: { key: "value" },
|
||||
overwrite: true,
|
||||
};
|
||||
const schema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
filePath: { type: "string" },
|
||||
content: { type: "string" },
|
||||
overwrite: { type: "boolean" },
|
||||
},
|
||||
};
|
||||
const result = coerceArgsToSchema(args, schema);
|
||||
expect(result.filePath).toBe("/path/to/file.json");
|
||||
expect(result.content).toBe('{"key":"value"}');
|
||||
expect(result.overwrite).toBe(true);
|
||||
});
|
||||
|
||||
it("should skip args not in schema", () => {
|
||||
const args = { unknown: { key: "value" }, known: "hello" };
|
||||
const schema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
known: { type: "string" },
|
||||
},
|
||||
};
|
||||
const result = coerceArgsToSchema(args, schema);
|
||||
expect(result.unknown).toEqual({ key: "value" });
|
||||
expect(result.known).toBe("hello");
|
||||
});
|
||||
|
||||
it("should not coerce numbers or booleans to strings", () => {
|
||||
const args = { content: 42, flag: true };
|
||||
const schema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
content: { type: "string" },
|
||||
flag: { type: "string" },
|
||||
},
|
||||
};
|
||||
const result = coerceArgsToSchema(args, schema);
|
||||
expect(result.content).toBe(42);
|
||||
expect(result.flag).toBe(true);
|
||||
});
|
||||
|
||||
it("should not mutate the original args object", () => {
|
||||
const args = { content: { key: "value" } };
|
||||
const schema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
content: { type: "string" },
|
||||
},
|
||||
};
|
||||
coerceArgsToSchema(args, schema);
|
||||
expect(args.content).toEqual({ key: "value" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("getNumberArg", () => {
|
||||
it("should return number argument when valid", () => {
|
||||
const args = { count: 42 };
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue