Skip to content

Commit 07577fc

Browse files
committed
feat:toolsbar add array support
1 parent e28a64c commit 07577fc

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

client/src/components/DynamicJsonForm.tsx

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -215,23 +215,48 @@ const DynamicJsonForm = ({
215215
return;
216216
}
217217

218-
const newValue = {
219-
...(typeof value === "object" && value !== null && !Array.isArray(value)
220-
? value
221-
: {}),
222-
} as JsonObject;
223-
let current: JsonObject = newValue;
218+
const updateArray = (array: JsonValue[], path: string[], value: JsonValue): JsonValue[] => {
219+
const [index, ...restPath] = path;
220+
const arrayIndex = Number(index);
221+
const newArray = [...array];
222+
223+
if (restPath.length === 0) {
224+
newArray[arrayIndex] = value;
225+
} else {
226+
newArray[arrayIndex] = updateValue(newArray[arrayIndex], restPath, value);
227+
}
228+
return newArray;
229+
};
224230

225-
for (let i = 0; i < path.length - 1; i++) {
226-
const key = path[i];
227-
if (!(key in current)) {
228-
current[key] = {};
231+
const updateObject = (obj: JsonObject, path: string[], value: JsonValue): JsonObject => {
232+
const [key, ...restPath] = path;
233+
const newObj = { ...obj };
234+
235+
if (restPath.length === 0) {
236+
newObj[key] = value;
237+
} else {
238+
newObj[key] = updateValue(newObj[key], restPath, value);
229239
}
230-
current = current[key] as JsonObject;
231-
}
240+
return newObj;
241+
};
242+
243+
const updateValue = (current: JsonValue, path: string[], value: JsonValue): JsonValue => {
244+
if (path.length === 0) return value;
245+
246+
if (!current) {
247+
current = !isNaN(Number(path[0])) ? [] : {};
248+
}
249+
250+
if (Array.isArray(current)) {
251+
return updateArray(current, path, value);
252+
} else if (typeof current === 'object' && current !== null) {
253+
return updateObject(current, path, value);
254+
}
255+
256+
return current;
257+
};
232258

233-
current[path[path.length - 1]] = fieldValue;
234-
onChange(newValue);
259+
onChange(updateValue(value, path, fieldValue));
235260
};
236261

237262
return (

client/src/components/ToolsTab.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ import ListPane from "./ListPane";
1717

1818
import { CompatibilityCallToolResult } from "@modelcontextprotocol/sdk/types.js";
1919

20-
type SchemaProperty = {
21-
type: string;
22-
description?: string;
23-
properties?: Record<string, SchemaProperty>;
24-
};
2520

2621
const ToolsTab = ({
2722
tools,
@@ -168,7 +163,7 @@ const ToolsTab = ({
168163
</p>
169164
{Object.entries(selectedTool.inputSchema.properties ?? []).map(
170165
([key, value]) => {
171-
const prop = value as SchemaProperty;
166+
const prop = value as JsonSchemaType;
172167
return (
173168
<div key={key}>
174169
<Label
@@ -211,15 +206,16 @@ const ToolsTab = ({
211206
}
212207
className="mt-1"
213208
/>
214-
) : prop.type === "object" ? (
209+
) : prop.type === "object"|| prop.type === "array" ? (
215210
<div className="mt-1">
216211
<DynamicJsonForm
217212
schema={
218213
{
219-
type: "object",
214+
type: prop.type,
220215
properties: prop.properties,
221216
description: prop.description,
222-
} as JsonSchemaType
217+
items:prop.items
218+
}
223219
}
224220
value={(params[key] as JsonValue) ?? {}}
225221
onChange={(newValue: JsonValue) => {

0 commit comments

Comments
 (0)