fix: skip schema fetch when no coercion candidates exist; handle union string types

Addresses PR review feedback:
- enforceSchemaStringTypes() now early-returns when no number
  values are present, avoiding an unconditional loadToolMetadata
  call before every tool invocation
- String type check now handles union schemas like
  type: ["string", "null"] in addition to type: "string"
This commit is contained in:
Ali Hamza 2026-04-18 13:40:16 +05:00 committed by Peter Steinberger
parent 4d78444003
commit 2a57d48d43

View File

@ -331,7 +331,13 @@ async function enforceSchemaStringTypes(
tool: string,
args: Record<string, unknown>,
): Promise<Record<string, unknown>> {
// Load tool schema to check declared types for each argument.
const hasCoercedNumbers = Object.values(args).some(
(v) => typeof v === "number",
);
if (!hasCoercedNumbers) {
return args;
}
const tools = await loadToolMetadata(runtime, server, {
includeSchema: true,
}).catch(() => undefined);
@ -343,16 +349,23 @@ async function enforceSchemaStringTypes(
return args;
}
const schema = toolInfo.tool.inputSchema as {
properties?: Record<string, { type?: string }>;
properties?: Record<string, { type?: string | string[] }>;
};
if (!schema.properties) {
return args;
}
const corrected: Record<string, unknown> = { ...args };
for (const [key, value] of Object.entries(corrected)) {
if (typeof value !== "number") {
continue;
}
const declared = schema.properties[key];
// If schema says this field is a string but we have a number, convert it back.
if (declared?.type === "string" && typeof value === "number") {
const declaredType = declared?.type;
const isStringType =
declaredType === "string" ||
(Array.isArray(declaredType) && declaredType.includes("string"));
if (isStringType) {
corrected[key] = String(value);
}
}