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
42 changes: 37 additions & 5 deletions src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ describe("MCP server tools/list", () => {
const tools = await getToolList();
for (const tool of tools) {
const schema = tool.inputSchema as Record<string, unknown>;
expect(
schema.$schema,
`Tool "${tool.name}" is missing $schema or has wrong draft`,
).toBe("https://json-schema.org/draft/2020-12/schema");
expect(schema.$schema, `Tool "${tool.name}" is missing $schema or has wrong draft`).toBe(
"https://json-schema.org/draft/2020-12/schema",
);
}
});

Expand All @@ -60,7 +59,40 @@ describe("MCP server tools/list", () => {

for (const tool of tools) {
const found = findNestedSchemaKeys(tool.inputSchema);
expect(found, `Tool "${tool.name}" has nested $schema keys at: ${found.join(", ")}`).toHaveLength(0);
expect(
found,
`Tool "${tool.name}" has nested $schema keys at: ${found.join(", ")}`,
).toHaveLength(0);
}
});

it("no tool inputSchema exposes regex lookaround patterns", async () => {
const tools = await getToolList();

function findLookaroundPatterns(obj: unknown, path = ""): string[] {
if (obj === null || typeof obj !== "object") return [];
if (Array.isArray(obj)) {
return obj.flatMap((item, i) => findLookaroundPatterns(item, `${path}[${i}]`));
}

const record = obj as Record<string, unknown>;
const found: string[] = [];
for (const [key, value] of Object.entries(record)) {
const currentPath = path ? `${path}.${key}` : key;
if (key === "pattern" && typeof value === "string" && /\(\?<?[=!]/.test(value)) {
found.push(currentPath);
}
found.push(...findLookaroundPatterns(value, currentPath));
}
return found;
}

for (const tool of tools) {
const found = findLookaroundPatterns(tool.inputSchema);
expect(
found,
`Tool "${tool.name}" exposes provider-incompatible lookaround patterns at: ${found.join(", ")}`,
).toHaveLength(0);
}
});

Expand Down
22 changes: 22 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ function stripNestedSchemaKeys(value: unknown): void {
}
}

function usesUnsupportedRegexSyntax(pattern: unknown): boolean {
return typeof pattern === "string" && /\(\?<?[=!]/.test(pattern);
}

function stripUnsupportedRegexPatterns(value: unknown): void {
if (value === null || typeof value !== "object") return;
if (Array.isArray(value)) {
for (const item of value) stripUnsupportedRegexPatterns(item);
return;
}

const record = value as Record<string, unknown>;
if (usesUnsupportedRegexSyntax(record.pattern)) {
delete record.pattern;
}

for (const child of Object.values(record)) {
stripUnsupportedRegexPatterns(child);
}
}

// Claude's API requires JSON Schema draft 2020-12. The MCP SDK's built-in
// Zod→JSON Schema converter emits draft-07 by default, which causes a 400
// error on tools/list. We bypass the SDK's auto-generated handler by
Expand All @@ -63,6 +84,7 @@ function toDraft2020_12JsonSchema(schema: ZodObject<ZodRawShape>): Record<string
}) as Record<string, unknown>;

stripNestedSchemaKeys(result);
stripUnsupportedRegexPatterns(result);
result.$schema = JSON_SCHEMA_2020_12;
return result;
}
Expand Down