Skip to content

feat: add jsonSchemaAdapter for plain JSON Schema support — closes #54#244

Open
lucamorettibuilds wants to merge 5 commits intopunkpeye:mainfrom
lucamorettibuilds:feat/json-schema-support
Open

feat: add jsonSchemaAdapter for plain JSON Schema support — closes #54#244
lucamorettibuilds wants to merge 5 commits intopunkpeye:mainfrom
lucamorettibuilds:feat/json-schema-support

Conversation

@lucamorettibuilds
Copy link

Summary

Closes #54

Adds a jsonSchemaAdapter() utility that wraps plain JSON Schema objects with a Standard Schema v1 interface. This lets users define tool parameters using raw JSON Schema — no Zod, Yup, or other validation library required.

How it works

  1. jsonSchemaAdapter(schema) returns a StandardSchemaV1-compatible object
  2. Runtime validation uses AJV (lazy-loaded, optional peer dep)
  3. Fast-path: The adapter exposes __jsonSchema so FastMCP can skip the toJsonSchema() conversion and use the raw schema directly — zero overhead
  4. ajv-formats is optionally detected for format validation (e.g. email, uri)

Usage

import { FastMCP, jsonSchemaAdapter } from "fastmcp";

const server = new FastMCP({ name: "Example" });

server.addTool({
  name: "greet",
  description: "Greet a user",
  parameters: jsonSchemaAdapter({
    type: "object",
    properties: {
      name: { type: "string" },
    },
    required: ["name"],
  }),
  execute: async ({ name }) => `Hello, ${name}!`,
});

Changes

File What
src/jsonSchemaAdapter.ts The adapter function + JsonSchemaObject type
src/jsonSchemaAdapter.test.ts 5 tests: valid input, invalid input, __jsonSchema fast-path, vendor metadata, nested objects
src/FastMCP.ts Re-export from barrel + fast-path detection in inputSchema resolution

Design decisions

  • AJV is a peer dep, not a direct dep — keeps the fastmcp bundle lightweight. Users who want JSON Schema support install ajv themselves. The adapter throws a clear error message if AJV is missing.
  • __jsonSchema fast-path — avoids the toJsonSchema() roundtrip since we already have valid JSON Schema. FastMCP checks for this property before falling back to the standard conversion.
  • No changes to existing behavior — all existing Zod/Valibot/ArkType schemas continue to work exactly as before.

Related

Closes punkpeye#54

Adds a  function that wraps plain JSON Schema objects
with a StandardSchemaV1-compatible interface, enabling users to define
tool parameters using raw JSON Schema without a third-party validation
library like Zod.

**How it works:**
- Wraps a JSON Schema object to satisfy StandardSchemaV1
- Uses AJV for runtime validation (lazy-loaded, optional peer dep)
- Exposes a  fast-path so FastMCP can skip the
  toJsonSchema() conversion and use the raw schema directly
- Supports ajv-formats optionally for format validation

**Changes:**
- `src/jsonSchemaAdapter.ts` — the adapter function
- `src/jsonSchemaAdapter.test.ts` — comprehensive tests
- `src/FastMCP.ts` — re-export + fast-path detection in inputSchema
  resolution

**Usage:**
```ts
import { FastMCP, jsonSchemaAdapter } from 'fastmcp';

server.addTool({
  name: 'greet',
  description: 'Greet a user',
  parameters: jsonSchemaAdapter({
    type: 'object',
    properties: { name: { type: 'string' } },
    required: ['name'],
  }),
  execute: async ({ name }) => `Hello, ${name}!`,
});
```
@lucamorettibuilds
Copy link
Author

Hey @punkpeye — CI is blocked on first-time contributor approval (action_required). Could you approve the workflow run when you get a chance? The implementation wraps ajv in a Standard Schema–compatible adapter so existing Zod tools continue working while plain JSON Schema objects can be passed as schema directly. Happy to adjust anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for ajv as a validator implementation

1 participant