diff --git a/core/tools/callTool.ts b/core/tools/callTool.ts index dc1f16ade..22e83bb46 100644 --- a/core/tools/callTool.ts +++ b/core/tools/callTool.ts @@ -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 }, diff --git a/core/tools/parseArgs.ts b/core/tools/parseArgs.ts index 79f504d56..64a4f67d0 100644 --- a/core/tools/parseArgs.ts +++ b/core/tools/parseArgs.ts @@ -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, + schema?: Record, +): Record { + 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, diff --git a/core/tools/parseArgs.vitest.ts b/core/tools/parseArgs.vitest.ts index ed0463259..e21254b68 100644 --- a/core/tools/parseArgs.vitest.ts +++ b/core/tools/parseArgs.vitest.ts @@ -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 };