Skip to content

Commit ccfc45e

Browse files
shoummu1madhav165
andauthored
Fix: Correctly parse array inputs in Test Tool UI (#641)
* fix for tool input issue Signed-off-by: Shoumi <[email protected]> * lint-web fixes Signed-off-by: Madhav Kandukuri <[email protected]> --------- Signed-off-by: Shoumi <[email protected]> Signed-off-by: Madhav Kandukuri <[email protected]> Co-authored-by: Madhav Kandukuri <[email protected]>
1 parent 13e447a commit ccfc45e

File tree

1 file changed

+71
-12
lines changed

1 file changed

+71
-12
lines changed

mcpgateway/static/admin.js

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,19 +3507,78 @@ async function runToolTest() {
35073507
}
35083508
let value;
35093509
if (prop.type === "array") {
3510-
value = formData.getAll(key);
3511-
if (prop.items && prop.items.type === "number") {
3512-
value = value.map((v) => (v === "" ? null : Number(v)));
3513-
} else if (prop.items && prop.items.type === "boolean") {
3514-
value = value.map((v) => v === "true" || v === true);
3515-
}
3516-
if (
3517-
value.length === 0 ||
3518-
(value.length === 1 && value[0] === "")
3519-
) {
3520-
continue;
3510+
const inputValues = formData.getAll(key);
3511+
try {
3512+
// Convert values based on the items schema type
3513+
if (prop.items && prop.items.type) {
3514+
switch (prop.items.type) {
3515+
case "object":
3516+
value = inputValues.map((v) => {
3517+
try {
3518+
const parsed = JSON.parse(v);
3519+
if (
3520+
typeof parsed !== "object" ||
3521+
Array.isArray(parsed)
3522+
) {
3523+
throw new Error(
3524+
`Value must be an object, got ${typeof parsed}`,
3525+
);
3526+
}
3527+
return parsed;
3528+
} catch (e) {
3529+
console.error(
3530+
`Error parsing object for ${key}:`,
3531+
e,
3532+
);
3533+
throw new Error(
3534+
`Invalid object format for ${key}. Each item must be a valid JSON object.`,
3535+
);
3536+
}
3537+
});
3538+
break;
3539+
case "number":
3540+
value = inputValues.map((v) =>
3541+
v === "" ? null : Number(v),
3542+
);
3543+
break;
3544+
case "boolean":
3545+
value = inputValues.map(
3546+
(v) => v === "true" || v === true,
3547+
);
3548+
break;
3549+
default:
3550+
// For other types (like strings), use raw values
3551+
value = inputValues;
3552+
}
3553+
} else {
3554+
// If no items type specified, use raw values
3555+
value = inputValues;
3556+
}
3557+
3558+
// Handle empty values
3559+
if (
3560+
value.length === 0 ||
3561+
(value.length === 1 && value[0] === "")
3562+
) {
3563+
if (
3564+
schema.required &&
3565+
schema.required.includes(key)
3566+
) {
3567+
params[keyValidation.value] = [];
3568+
}
3569+
continue;
3570+
}
3571+
params[keyValidation.value] = value;
3572+
} catch (error) {
3573+
console.error(
3574+
`Error parsing array values for ${key}:`,
3575+
error,
3576+
);
3577+
showErrorMessage(
3578+
`Invalid input format for ${key}. Please check the values are in correct format.`,
3579+
);
3580+
throw error;
35213581
}
3522-
params[keyValidation.value] = value;
35233582
} else {
35243583
value = formData.get(key);
35253584
if (value === null || value === undefined || value === "") {

0 commit comments

Comments
 (0)