Skip to content

Commit 40045b3

Browse files
authored
Merge pull request #889 from yanOO1497/fix_inspector_
fix: resolve $ref references in tool input schema properties
2 parents c7683b4 + fb7fae1 commit 40045b3

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,
@@ -90,14 +91,21 @@ const ToolsTab = ({
9091
useEffect(() => {
9192
const params = Object.entries(
9293
selectedTool?.inputSchema.properties ?? [],
93-
).map(([key, value]) => [
94-
key,
95-
generateDefaultValue(
94+
).map(([key, value]) => {
95+
// First resolve any $ref references
96+
const resolvedValue = resolveRef(
9697
value as JsonSchemaType,
97-
key,
9898
selectedTool?.inputSchema as JsonSchemaType,
99-
),
100-
]);
99+
);
100+
return [
101+
key,
102+
generateDefaultValue(
103+
resolvedValue,
104+
key,
105+
selectedTool?.inputSchema as JsonSchemaType,
106+
),
107+
];
108+
});
101109
setParams(Object.fromEntries(params));
102110

103111
// Reset validation errors when switching tools
@@ -154,7 +162,12 @@ const ToolsTab = ({
154162
</p>
155163
{Object.entries(selectedTool.inputSchema.properties ?? []).map(
156164
([key, value]) => {
157-
const prop = normalizeUnionType(value as JsonSchemaType);
165+
// First resolve any $ref references
166+
const resolvedValue = resolveRef(
167+
value as JsonSchemaType,
168+
selectedTool.inputSchema as JsonSchemaType,
169+
);
170+
const prop = normalizeUnionType(resolvedValue);
158171
const inputSchema =
159172
selectedTool.inputSchema as JsonSchemaType;
160173
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)