Validates tool input against a Zod schema.
This function should be called at the start of every tool handler to validate incoming arguments before processing.
The expected type after validation
The Zod schema to validate against
The unknown input to validate
Result containing validated data or a ValidationError
const InputSchema = z.object({
task: z.string().min(1),
context: z.record(z.string(), z.unknown()).optional(),
});
server.tool('my_tool', InputSchema.shape, async (args) => {
const result = validateToolInput(InputSchema, args);
if (!result.ok) {
return toolStructuredError({ errorCategory: 'validation', message: result.error.message });
}
const { task, context } = result.value;
// Process validated input...
});
MCP exports - MCP server implementation Split from index.ts for file size compliance (Issue #285) Updated Issue #538: Added missing tool registration exports