Skip to content

Commit 3044d8f

Browse files
authored
Merge branch 'main' into meta_properies_in_tools
2 parents 1c1afc3 + 40045b3 commit 3044d8f

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

client/src/components/ToolsTab.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
generateDefaultValue,
1919
isPropertyRequired,
2020
normalizeUnionType,
21+
resolveRef,
2122
} from "@/utils/schemaUtils";
2223
import {
2324
CompatibilityCallToolResult,
@@ -97,14 +98,21 @@ const ToolsTab = ({
9798
useEffect(() => {
9899
const params = Object.entries(
99100
selectedTool?.inputSchema.properties ?? [],
100-
).map(([key, value]) => [
101-
key,
102-
generateDefaultValue(
101+
).map(([key, value]) => {
102+
// First resolve any $ref references
103+
const resolvedValue = resolveRef(
103104
value as JsonSchemaType,
104-
key,
105105
selectedTool?.inputSchema as JsonSchemaType,
106-
),
107-
]);
106+
);
107+
return [
108+
key,
109+
generateDefaultValue(
110+
resolvedValue,
111+
key,
112+
selectedTool?.inputSchema as JsonSchemaType,
113+
),
114+
];
115+
});
108116
setParams(Object.fromEntries(params));
109117

110118
// Reset validation errors when switching tools
@@ -161,7 +169,12 @@ const ToolsTab = ({
161169
</p>
162170
{Object.entries(selectedTool.inputSchema.properties ?? []).map(
163171
([key, value]) => {
164-
const prop = normalizeUnionType(value as JsonSchemaType);
172+
// First resolve any $ref references
173+
const resolvedValue = resolveRef(
174+
value as JsonSchemaType,
175+
selectedTool.inputSchema as JsonSchemaType,
176+
);
177+
const prop = normalizeUnionType(resolvedValue);
165178
const inputSchema =
166179
selectedTool.inputSchema as JsonSchemaType;
167180
const required = isPropertyRequired(key, inputSchema);

client/src/utils/jsonUtils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export type JsonSchemaType = {
4848
const?: JsonValue;
4949
oneOf?: (JsonSchemaType | JsonSchemaConst)[];
5050
anyOf?: (JsonSchemaType | JsonSchemaConst)[];
51+
$ref?: string;
5152
};
5253

5354
export type JsonObject = { [key: string]: JsonValue };

client/src/utils/schemaUtils.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,50 @@ export function isPropertyRequired(
145145
return schema.required?.includes(propertyName) ?? false;
146146
}
147147

148+
/**
149+
* Resolves $ref references in JSON schema
150+
* @param schema The schema that may contain $ref
151+
* @param rootSchema The root schema to resolve references against
152+
* @returns The resolved schema without $ref
153+
*/
154+
export function resolveRef(
155+
schema: JsonSchemaType,
156+
rootSchema: JsonSchemaType,
157+
): JsonSchemaType {
158+
if (!("$ref" in schema) || !schema.$ref) {
159+
return schema;
160+
}
161+
162+
const ref = schema.$ref;
163+
164+
// Handle simple #/properties/name references
165+
if (ref.startsWith("#/")) {
166+
const path = ref.substring(2).split("/");
167+
let current: unknown = rootSchema;
168+
169+
for (const segment of path) {
170+
if (
171+
current &&
172+
typeof current === "object" &&
173+
current !== null &&
174+
segment in current
175+
) {
176+
current = (current as Record<string, unknown>)[segment];
177+
} else {
178+
// If reference cannot be resolved, return the original schema
179+
console.warn(`Could not resolve $ref: ${ref}`);
180+
return schema;
181+
}
182+
}
183+
184+
return current as JsonSchemaType;
185+
}
186+
187+
// For other types of references, return the original schema
188+
console.warn(`Unsupported $ref format: ${ref}`);
189+
return schema;
190+
}
191+
148192
/**
149193
* Normalizes union types (like string|null from FastMCP) to simple types for form rendering
150194
* @param schema The JSON schema to normalize

0 commit comments

Comments
 (0)