Type Safety
mcp-lite provides automatic type inference when you use Standard Schema validators like Zod, Valibot, Effect Schema, or ArkType.
Automatic Type Inference
Section titled “Automatic Type Inference”Standard Schema validators provide automatic type inference for tool handlers:
import { z } from "zod";
const SearchSchema = z.object({ query: z.string(), limit: z.number().optional(), filters: z.array(z.string()).optional()});
server.tool("search", { inputSchema: SearchSchema, handler: (args) => { // args is automatically typed as: // { query: string, limit?: number, filters?: string[] }
args.query.toLowerCase() args.limit ?? 10 args.filters?.map(f => f.trim())
return { content: [{ type: "text", text: "..." }] } }})Structured Outputs with Validation
Section titled “Structured Outputs with Validation”Tools can return both human-readable content and machine-readable structured data. Use outputSchema to define the shape and get runtime validation:
const WeatherOutputSchema = z.object({ temperature: z.number(), conditions: z.string(),});
server.tool("getWeather", { inputSchema: z.object({ location: z.string() }), outputSchema: WeatherOutputSchema, handler: (args) => ({ content: [{ type: "text", text: `Weather in ${args.location}: 22°C, sunny` }], // structuredContent is typed and validated at runtime structuredContent: { temperature: 22, conditions: "sunny", } })})See the MCP structured content spec for protocol details.
Supported Validators
Section titled “Supported Validators”mcp-lite works with any Standard Schema validator:
import { z } from "zod";
const schema = z.object({ name: z.string(), age: z.number().optional()});Valibot
Section titled “Valibot”import * as v from "valibot";
const schema = v.object({ name: v.string(), age: v.optional(v.number())});Effect Schema
Section titled “Effect Schema”import * as S from "@effect/schema/Schema";
const schema = S.struct({ name: S.string, age: S.optional(S.number)});ArkType
Section titled “ArkType”import { type } from "arktype";
const schema = type({ name: "string", "age?": "number"});Context Validation
Section titled “Context Validation”The handler context provides a validate() helper for validating data at runtime:
handler: (args, ctx) => { const validated = ctx.validate(MySchema, someData); // validated is now typed according to MySchema}Examples
Section titled “Examples”Check out the examples directory for complete implementations:
examples/validation-zod- Validation with Zodexamples/validation-valibot- Validation using Valibotexamples/validation-effectschema- Validation with Effect Schemaexamples/validation-arktype- Standard Schema via ArkType
Next Steps
Section titled “Next Steps”- Tools - Learn about tool patterns
- Error Handling - Handle validation errors