The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(ui): reseed the MCP tool test form when the schema changes
Yuneng Jiangc2e0daa50a97
diff --git a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/ToolArgumentsForm.tsx b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/ToolArgumentsForm.tsxindex 0a212a75345..9238d604246 100644--- a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/ToolArgumentsForm.tsx+++ b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/ToolArgumentsForm.tsx@@ -10,107 +10,14 @@ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/compimport { FieldGroup } from "@/components/shared/form/field";import { FormField, type FormFieldControlProps } from "@/components/shared/form/FormField";import { UiLoadingSpinner } from "@/components/ui/ui-loading-spinner";-import { InputSchemaProperty } from "@/components/mcp_tools/types";import {ToolArgumentField,ToolArgumentsFormValues,buildToolCallArguments,+ initialArgumentValues,toolArgumentsResolver,} from "./toolCallArguments";-const isPlainObject = (value: unknown): value is Record<string, any> =>- typeof value === "object" && value !== null && !Array.isArray(value);--function buildArrayItems(items?: InputSchemaProperty | InputSchemaProperty[]): any[] {- if (!items) {- return [];- }-- if (Array.isArray(items)) {- return items.map((item) => buildDefaultValue(item)).filter((value) => value !== undefined);- }-- const itemDefault = buildDefaultValue(items);- if (itemDefault === undefined) {- return [];- }-- return [itemDefault];-}--function buildDefaultValue(prop?: InputSchemaProperty, overrideDefault?: any): any {- if (!prop) {- return undefined;- }-- const effectiveDefault = overrideDefault !== undefined ? overrideDefault : prop.default;-- if (prop.type === "object") {- const base = isPlainObject(effectiveDefault) ? { ...effectiveDefault } : {};-- if (prop.properties) {- Object.entries(prop.properties).forEach(([childKey, childProp]) => {- base[childKey] = buildDefaultValue(childProp, base[childKey]);- });- }-- return base;- }-- if (prop.type === "array") {- if (Array.isArray(effectiveDefault)) {- const itemSchema = prop.items;- if (!itemSchema) {- return effectiveDefault;- }-- if (effectiveDefault.length === 0) {- const sample = buildArrayItems(itemSchema);- return sample.length ? sample : effectiveDefault;- }-- if (Array.isArray(itemSchema)) {- return effectiveDefault.map((value, index) => {- const schema = itemSchema[index] ?? itemSchema[itemSchema.length - 1];- return buildDefaultValue(schema, value);- });- }-- return effectiveDefault.map((value) => buildDefaultValue(itemSchema, value));- }-- if (effectiveDefault !== undefined) {- return effectiveDefault;- }-- return buildArrayItems(prop.items);- }-- if (effectiveDefault !== undefined) {- return effectiveDefault;- }-- switch (prop.type) {- case "integer":- case "number":- return 0;- case "boolean":- return false;- case "string":- default:- return "";- }-}--const getInitialValueForField = (prop: InputSchemaProperty): any => {- const defaultValue = buildDefaultValue(prop);- if (prop.type === "object" || prop.type === "array") {- const fallback = prop.type === "array" ? [] : {};- return JSON.stringify(defaultValue ?? fallback, null, 2);- }- return defaultValue;-};-const argumentLabel = (field: ToolArgumentField): React.ReactNode => (<span className="flex items-center">{field.key}@@ -301,7 +208,7 @@ export const ToolArgumentsForm: React.FC<{onRun: (args: Record<string, unknown>) => void;}> = ({ fields, singleInputFallback, isLoading, hasRun, onRun }) => {const form = useForm<ToolArgumentsFormValues>({- defaultValues: { args: fields.map((field) => getInitialValueForField(field.prop)) },+ defaultValues: { args: initialArgumentValues(fields) },resolver: toolArgumentsResolver(fields),});diff --git a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/ToolTestPanel.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/ToolTestPanel.test.tsxindex 009c261cb79..3fb5d602402 100644--- a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/ToolTestPanel.test.tsx+++ b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/ToolTestPanel.test.tsx@@ -339,3 +339,53 @@ describe("ToolTestPanel argument payload", () => {expect(onSubmit).toHaveBeenCalledWith({ ratio: 0.4, active: true, label: "seeded" });});});++describe("ToolTestPanel schema changes under a stable tool name", () => {+ const renderWith = (schema: InputSchema, onSubmit: ReturnType<typeof vi.fn>) => (+ <ToolTestPanel+ tool={buildTool(schema)}+ onSubmit={onSubmit}+ isLoading={false}+ result={null}+ error={null}+ onClose={vi.fn()}+ />+ );++ it("reseeds the fields when the same-named tool's schema changes", async () => {+ const onSubmit = vi.fn();+ const before: InputSchema = { type: "object", properties: { message: { type: "string" } } };+ const after: InputSchema = {+ type: "object",+ properties: { query: { type: "string" }, limit: { type: "integer", default: 5 } },+ };++ const { rerender } = render(renderWith(before, onSubmit));+ const user = userEvent.setup();+ await user.type(screen.getByLabelText("message"), "stale value");++ rerender(renderWith(after, onSubmit));++ expect(screen.queryByLabelText("message")).not.toBeInTheDocument();+ expect(screen.getByLabelText("query")).toHaveValue("");+ expect(screen.getByLabelText("limit")).toHaveValue(5);++ await user.type(screen.getByLabelText("query"), "widgets");+ await user.click(screen.getByRole("button", { name: "Call Tool" }));++ expect(onSubmit).toHaveBeenCalledWith({ query: "widgets", limit: 5 });+ });++ it("keeps what the user typed when the schema is rebuilt with identical content", async () => {+ const onSubmit = vi.fn();+ const schema = (): InputSchema => ({ type: "object", properties: { message: { type: "string" } } });++ const { rerender } = render(renderWith(schema(), onSubmit));+ const user = userEvent.setup();+ await user.type(screen.getByLabelText("message"), "typed by hand");++ rerender(renderWith(schema(), onSubmit));++ expect(screen.getByLabelText("message")).toHaveValue("typed by hand");+ });+});diff --git a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/ToolTestPanel.tsx b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/ToolTestPanel.tsxindex a8ecfc39ed5..84e23fec2f3 100644--- a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/ToolTestPanel.tsx+++ b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/ToolTestPanel.tsx@@ -7,7 +7,7 @@ import { MCPTool, InputSchema } from "@/components/mcp_tools/types";import { resolveLogoSrc } from "@/lib/assetPaths";import { toast } from "@/lib/toast";import { ToolArgumentsForm } from "./ToolArgumentsForm";-import { ToolArgumentField, hasNestedParamsSchema, toolArgumentFields } from "./toolCallArguments";+import { ToolArgumentField, argumentsFormKey, hasNestedParamsSchema, toolArgumentFields } from "./toolCallArguments";export function ToolTestPanel({tool,@@ -69,6 +69,7 @@ export function ToolTestPanel({[actualSchema],);const wrapInParams = React.useMemo(() => hasNestedParamsSchema(schema), [schema]);+ const formKey = React.useMemo(() => `${tool.name}:${argumentsFormKey(actualSchema)}`, [tool.name, actualSchema]);const runToolCall = (args: Record<string, unknown>) => {setStartTime(Date.now());@@ -149,13 +150,13 @@ export function ToolTestPanel({<div className="flex items-center space-x-2 mb-1"><h2 className="text-lg font-semibold text-foreground">Test Tool:</h2><div- className="group inline-flex items-center space-x-1 bg-slate-50 hover:bg-slate-100 px-3 py-1 rounded-md cursor-pointer transition-colors border border-slate-200"+ className="group inline-flex items-center space-x-1 bg-muted hover:bg-accent px-3 py-1 rounded-md cursor-pointer transition-colors border border-border"onClick={handleCopyToolName}title="Click to copy tool name">- <span className="font-mono text-slate-700 font-medium text-sm">{tool.name}</span>+ <span className="font-mono text-foreground font-medium text-sm">{tool.name}</span><svg- className="w-3 h-3 text-slate-400 group-hover:text-slate-600 transition-colors"+ className="w-3 h-3 text-muted-foreground group-hover:text-foreground transition-colors"fill="none"stroke="currentColor"viewBox="0 0 24 24"@@ -204,7 +205,7 @@ export function ToolTestPanel({<div className="p-4"><ToolArgumentsForm- key={tool.name}+ key={formKey}fields={argumentFields}singleInputFallback={typeof tool.inputSchema === "string"}isLoading={isLoading}diff --git a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/toolCallArguments.test.ts b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/toolCallArguments.test.tsindex 552668ab806..80157a5bc99 100644--- a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/toolCallArguments.test.ts+++ b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/toolCallArguments.test.ts@@ -3,8 +3,10 @@ import { describe, expect, it } from "vitest";import { InputSchema } from "@/components/mcp_tools/types";import {ToolArgumentField,+ argumentsFormKey,buildToolCallArguments,hasNestedParamsSchema,+ initialArgumentValues,toolArgumentFields,toolArgumentsResolver,validateToolArgument,@@ -186,3 +188,147 @@ describe("hasNestedParamsSchema", () => {expect(hasNestedParamsSchema({ type: "object", properties: { q: { type: "string" } } })).toBe(false);});});++describe("initialArgumentValues", () => {+ const seed = (schema: InputSchema): unknown[] => initialArgumentValues(toolArgumentFields(schema));++ it("seeds each primitive type with its empty value when the schema declares no default", () => {+ expect(+ seed({+ type: "object",+ properties: {+ message: { type: "string" },+ attempts: { type: "integer" },+ ratio: { type: "number" },+ active: { type: "boolean" },+ },+ }),+ ).toEqual(["", 0, 0, false]);+ });++ it("prefers a declared default over the empty value", () => {+ expect(+ seed({+ type: "object",+ properties: {+ label: { type: "string", default: "seeded" },+ ratio: { type: "number", default: 0.4 },+ active: { type: "boolean", default: true },+ },+ }),+ ).toEqual(["seeded", 0.4, true]);+ });++ it("seeds a false boolean default rather than falling back to the empty value", () => {+ expect(seed({ type: "object", properties: { active: { type: "boolean", default: false } } })).toEqual([false]);+ });++ it("renders an object field as pretty JSON built from its nested property defaults", () => {+ const [payload] = seed({+ type: "object",+ properties: {+ payload: {+ type: "object",+ properties: {+ id: { type: "string" },+ count: { type: "integer", default: 3 },+ },+ },+ },+ });++ expect(payload).toBe(JSON.stringify({ id: "", count: 3 }, null, 2));+ });++ it("keeps keys a declared object default carries that the schema does not describe", () => {+ const [payload] = seed({+ type: "object",+ properties: {+ payload: {+ type: "object",+ properties: { id: { type: "string" } },+ default: { id: "abc", extra: true },+ },+ },+ });++ expect(payload).toBe(JSON.stringify({ id: "abc", extra: true }, null, 2));+ });++ it("renders an array field as pretty JSON holding one sample item built from its item schema", () => {+ const [tags] = seed({+ type: "object",+ properties: { tags: { type: "array", items: { type: "string" } } },+ });++ expect(tags).toBe(JSON.stringify([""], null, 2));+ });++ it("rebuilds each entry of a declared array default against the item schema", () => {+ const [rows] = seed({+ type: "object",+ properties: {+ rows: {+ type: "array",+ items: { type: "object", properties: { id: { type: "string" }, n: { type: "integer" } } },+ default: [{ id: "a" }],+ },+ },+ });++ expect(rows).toBe(JSON.stringify([{ id: "a", n: 0 }], null, 2));+ });++ it("does not mutate the schema it seeds from", () => {+ const schema: InputSchema = {+ type: "object",+ properties: {+ payload: { type: "object", properties: { id: { type: "string" } }, default: { other: 1 } },+ },+ };+ const before = JSON.stringify(schema);++ seed(schema);++ expect(JSON.stringify(schema)).toBe(before);+ });++ it("seeds positionally, so a dotted key is just another index", () => {+ expect(+ seed({+ type: "object",+ properties: { "filter.name": { type: "string", default: "acme" }, plain: { type: "string" } },+ }),+ ).toEqual(["acme", ""]);+ });+});++describe("argumentsFormKey", () => {+ it("changes when a property's type changes under the same property names", () => {+ const before: InputSchema = { type: "object", properties: { value: { type: "string" } } };+ const after: InputSchema = { type: "object", properties: { value: { type: "integer" } } };++ expect(argumentsFormKey(after)).not.toBe(argumentsFormKey(before));+ });++ it("changes when a property's default changes", () => {+ const before: InputSchema = { type: "object", properties: { value: { type: "string", default: "a" } } };+ const after: InputSchema = { type: "object", properties: { value: { type: "string", default: "b" } } };++ expect(argumentsFormKey(after)).not.toBe(argumentsFormKey(before));+ });++ it("changes when a property is added", () => {+ const before: InputSchema = { type: "object", properties: { a: { type: "string" } } };+ const after: InputSchema = { type: "object", properties: { a: { type: "string" }, b: { type: "string" } } };++ expect(argumentsFormKey(after)).not.toBe(argumentsFormKey(before));+ });++ it("is stable across separately built but identical schemas", () => {+ const one: InputSchema = { type: "object", properties: { a: { type: "string", default: "x" } } };+ const two: InputSchema = { type: "object", properties: { a: { type: "string", default: "x" } } };++ expect(argumentsFormKey(one)).toBe(argumentsFormKey(two));+ });+});diff --git a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/toolCallArguments.ts b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/toolCallArguments.tsindex c907f60251e..5fdeccd41bc 100644--- a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/toolCallArguments.ts+++ b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/toolCallArguments.ts@@ -119,3 +119,75 @@ export const hasNestedParamsSchema = (schema: InputSchema): boolean => {if (params === undefined) return false;return params.type === "object" && params.properties !== undefined;};++function buildArrayItems(items: InputSchemaProperty | InputSchemaProperty[] | undefined): unknown[] {+ if (!items) return [];+ if (Array.isArray(items)) {+ return items.map((item) => buildDefaultValue(item)).filter((value) => value !== undefined);+ }+ const itemDefault = buildDefaultValue(items);+ return itemDefault === undefined ? [] : [itemDefault];+}++function buildObjectDefault(prop: InputSchemaProperty, effectiveDefault: unknown): Record<string, unknown> {+ const base: Record<string, unknown> = isPlainObject(effectiveDefault) ? effectiveDefault : {};+ if (!prop.properties) return { ...base };+ return {+ ...base,+ ...Object.fromEntries(+ Object.entries(prop.properties).map(([childKey, childProp]) => [+ childKey,+ buildDefaultValue(childProp, base[childKey]),+ ]),+ ),+ };+}++function buildArrayDefault(prop: InputSchemaProperty, effectiveDefault: unknown): unknown {+ if (Array.isArray(effectiveDefault)) {+ const itemSchema = prop.items;+ if (!itemSchema) return effectiveDefault;+ if (effectiveDefault.length === 0) {+ const sample = buildArrayItems(itemSchema);+ return sample.length > 0 ? sample : effectiveDefault;+ }+ if (Array.isArray(itemSchema)) {+ return effectiveDefault.map((value: unknown, index: number) =>+ buildDefaultValue(itemSchema[index] ?? itemSchema[itemSchema.length - 1], value),+ );+ }+ return effectiveDefault.map((value: unknown) => buildDefaultValue(itemSchema, value));+ }+ if (effectiveDefault !== undefined) return effectiveDefault;+ return buildArrayItems(prop.items);+}++function buildDefaultValue(prop: InputSchemaProperty | undefined, overrideDefault?: unknown): unknown {+ if (!prop) return undefined;+ const effectiveDefault: unknown = overrideDefault !== undefined ? overrideDefault : prop.default;++ if (prop.type === "object") return buildObjectDefault(prop, effectiveDefault);+ if (prop.type === "array") return buildArrayDefault(prop, effectiveDefault);+ if (effectiveDefault !== undefined) return effectiveDefault;++ switch (prop.type) {+ case "integer":+ case "number":+ return 0;+ case "boolean":+ return false;+ default:+ return "";+ }+}++export const initialArgumentValues = (fields: readonly ToolArgumentField[]): unknown[] =>+ fields.map(({ prop }) => {+ const defaultValue = buildDefaultValue(prop);+ if (isJsonField(prop)) {+ return JSON.stringify(defaultValue ?? (prop.type === "array" ? [] : {}), null, 2);+ }+ return defaultValue;+ });++export const argumentsFormKey = (schema: InputSchema): string => JSON.stringify(schema);