The official MCP TypeScript SDK requires modeling MCP Tool Schema using a type-system called [Zod](https://zod.dev/). `mcp-codegen` allows you to leverage plain-old-TypeScript-objects.
npm install mcp-codegenThe official MCP TypeScript SDK requires modeling MCP Tool Schema using a type-system called Zod. mcp-codegen allows you to leverage plain-old-TypeScript-objects.
``bash`
npm install mcp-codegen
This package is built using esbuild to transpile TypeScript to ES2015 (ES6) for broad compatibility. The build process generates both JavaScript and TypeScript declaration files.
`bash`
npm run build:all
The package exports the following components:
- McpRequired() - Decorator for required fieldsMcpOptional()
- - Decorator for optional fields Format(format: string)
- - Decorator for field format specificationSchemaBuilder
- - Function to generate Zod schema from decorated classesPrimitives
- - TypeScript type for primitive valuesZOD_METADATA
- , ZOD_PROPS - Internal constants
1. TypeScript experimental decorators (legacy only, TC39 coming soon)
1. Reflect and reflect-metadata
1. Zod to JSON Schema (Zod4 coming soon)
`ts
import { McpRequired, McpOptional, Format } from "mcp-codegen";
export class ToolCallInput {
im_just_a_prop: string;
@McpRequired()
query: string;
@McpRequired()
async: boolean;
@McpOptional()
max?: number;
@Format('bigint')
@McpOptional()
variance?: number;
@McpOptional()
queryId?: string;
@Format('date-time')
@McpOptional()
createdAt?: string;
}
`
ts
import { ToolCallInput } from "decorated-typescript-class";
import { SchemaBuilder } from "mcp-codegen";
import zodToJsonSchema from "zod-to-json-schema";const zodObject = SchemaBuilder(ToolCallInput);
const jsonSchema = zodToJsonSchema(z.object(zodObject));
`#### Generated JSON Schema
`json
{
"type": "object",
"properties": {
"query": {
"type": "string"
},
"async": {
"type": "boolean"
},
"max": {
"type": "number"
},
"variance": {
"type": "integer",
"format": "int64"
},
"queryId": {
"type": "string"
},
"createdAt": {
"type": "string",
"format": "date-time"
}
},
"required": [
"query",
"async"
],
"additionalProperties": false,
"$schema": "http://json-schema.org/draft-07/schema#"
}
`$3
`ts
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";new McpServer().registerTool(
"example-tool",
{
title: "Example Tool",
inputSchema: toolSchema // Output of SchemaBuilder
},
(args: ToolCallInput): Promise => {
return Promise.resolve({
content: [
{
type: "text",
text:
Results for query: ${args.query} // Required type
},
{
type: "text",
text: Results requested: ${args.max || 100} // Optional type
}
]
});
}
)
``