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
9 changes: 0 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
PromptMessageSchema,
CallToolResultSchema,
CompleteRequestSchema,
ToolSchema,
ToolUseContentSchema,
ToolResultContentSchema,
ToolChoiceSchema,
Expand Down Expand Up @@ -319,6 +320,132 @@ describe('Types', () => {
});
});

describe('ToolSchema - JSON Schema 2020-12 support', () => {
test('should accept inputSchema with $schema field', () => {
const tool = {
name: 'test',
inputSchema: {
$schema: 'https://json-schema.org/draft/2020-12/schema',
type: 'object',
properties: { name: { type: 'string' } }
}
};
const result = ToolSchema.safeParse(tool);
expect(result.success).toBe(true);
});

test('should accept inputSchema with additionalProperties', () => {
const tool = {
name: 'test',
inputSchema: {
type: 'object',
properties: { name: { type: 'string' } },
additionalProperties: false
}
};
const result = ToolSchema.safeParse(tool);
expect(result.success).toBe(true);
});

test('should accept inputSchema with composition keywords', () => {
const tool = {
name: 'test',
inputSchema: {
type: 'object',
allOf: [{ properties: { a: { type: 'string' } } }, { properties: { b: { type: 'number' } } }]
}
};
const result = ToolSchema.safeParse(tool);
expect(result.success).toBe(true);
});

test('should accept inputSchema with $ref and $defs', () => {
const tool = {
name: 'test',
inputSchema: {
type: 'object',
properties: { user: { $ref: '#/$defs/User' } },
$defs: {
User: { type: 'object', properties: { name: { type: 'string' } } }
}
}
};
const result = ToolSchema.safeParse(tool);
expect(result.success).toBe(true);
});

test('should accept inputSchema with metadata keywords', () => {
const tool = {
name: 'test',
inputSchema: {
type: 'object',
title: 'User Input',
description: 'Input parameters for user creation',
deprecated: false,
examples: [{ name: 'John' }],
properties: { name: { type: 'string' } }
}
};
const result = ToolSchema.safeParse(tool);
expect(result.success).toBe(true);
});

test('should accept outputSchema with full JSON Schema features', () => {
const tool = {
name: 'test',
inputSchema: { type: 'object' },
outputSchema: {
type: 'object',
properties: {
id: { type: 'string' },
tags: { type: 'array' }
},
required: ['id'],
additionalProperties: false,
minProperties: 1
}
};
const result = ToolSchema.safeParse(tool);
expect(result.success).toBe(true);
});

test('should still require type: object at root for inputSchema', () => {
const tool = {
name: 'test',
inputSchema: {
type: 'string'
}
};
const result = ToolSchema.safeParse(tool);
expect(result.success).toBe(false);
});

test('should still require type: object at root for outputSchema', () => {
const tool = {
name: 'test',
inputSchema: { type: 'object' },
outputSchema: {
type: 'array'
}
};
const result = ToolSchema.safeParse(tool);
expect(result.success).toBe(false);
});

test('should accept simple minimal schema (backward compatibility)', () => {
const tool = {
name: 'test',
inputSchema: {
type: 'object',
properties: { name: { type: 'string' } },
required: ['name']
}
};
const result = ToolSchema.safeParse(tool);
expect(result.success).toBe(true);
});
});

describe('ToolUseContent', () => {
test('should validate a tool call content', () => {
const toolCall = {
Expand Down
24 changes: 14 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -999,24 +999,28 @@ export const ToolSchema = z.object({
*/
description: z.string().optional(),
/**
* A JSON Schema object defining the expected parameters for the tool.
* A JSON Schema 2020-12 object defining the expected parameters for the tool.
* Must have type: 'object' at the root level per MCP spec.
*/
inputSchema: z.object({
type: z.literal('object'),
properties: z.record(z.string(), AssertObjectSchema).optional(),
required: z.optional(z.array(z.string()))
}),
inputSchema: z
.object({
type: z.literal('object'),
properties: z.record(z.string(), AssertObjectSchema).optional(),
required: z.array(z.string()).optional()
})
.catchall(z.unknown()),
/**
* An optional JSON Schema object defining the structure of the tool's output returned in
* the structuredContent field of a CallToolResult.
* An optional JSON Schema 2020-12 object defining the structure of the tool's output
* returned in the structuredContent field of a CallToolResult.
* Must have type: 'object' at the root level per MCP spec.
*/
outputSchema: z
.object({
type: z.literal('object'),
properties: z.record(z.string(), AssertObjectSchema).optional(),
required: z.optional(z.array(z.string())),
additionalProperties: z.optional(z.boolean())
required: z.array(z.string()).optional()
})
.catchall(z.unknown())
.optional(),
/**
* Optional additional tool information.
Expand Down
1 change: 0 additions & 1 deletion src/validation/cfworker-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* This provider uses @cfworker/json-schema for validation without code generation,
* making it compatible with edge runtimes like Cloudflare Workers that restrict
* eval and new Function.
*
*/

import { type Schema, Validator } from '@cfworker/json-schema';
Expand Down
Loading