Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ function dereferenceSpec(spec: McpSpec): McpSpec {

const defs = spec.$defs;

function decodeJsonPointerToken(token: string): string {
return token.replace(/~1/g, "/").replace(/~0/g, "~");
}

function resolveRefs(obj: unknown): unknown {
if (typeof obj !== "object" || obj === null) return obj;
if (Array.isArray(obj)) return obj.map(resolveRefs);
Expand All @@ -93,8 +97,11 @@ function dereferenceSpec(spec: McpSpec): McpSpec {
if (typeof record.$ref === "string") {
const refPath = record.$ref;
const match = refPath.match(/^#\/\$defs\/(.+)$/);
if (match && defs[match[1]]) {
return resolveRefs(structuredClone(defs[match[1]]));
if (match) {
const defName = decodeJsonPointerToken(match[1]);
if (defs[defName]) {
return resolveRefs(structuredClone(defs[defName]));
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,35 @@ describe("$ref dereferencing", () => {
const fooProp = spec.tools![0].inputSchema.properties!.foo;
expect(fooProp).toHaveProperty("$ref", "#/$defs/Missing");
});

it("resolves escaped JSON Pointer tokens in $defs refs", () => {
const spec = parseString(
JSON.stringify({
mcpSpec: "0.1.0",
server: { name: "test", version: "1.0.0" },
$defs: {
"Address/Primary": { type: "string" },
"Name~Display": { type: "string" },
},
tools: [
{
name: "profile",
inputSchema: {
type: "object",
properties: {
address: { $ref: "#/$defs/Address~1Primary" },
displayName: { $ref: "#/$defs/Name~0Display" },
},
},
},
],
}),
);

const properties = spec.tools![0].inputSchema.properties!;
expect(properties.address).not.toHaveProperty("$ref");
expect(properties.address).toHaveProperty("type", "string");
expect(properties.displayName).not.toHaveProperty("$ref");
expect(properties.displayName).toHaveProperty("type", "string");
});
});