Skip to main content
The tool() function creates tool definitions for SDK-embedded MCP servers with automatic TypeScript type inference from Zod schemas.

Import

Signature

Parameters

string
required
Tool name (1-64 characters). Must start with a letter and contain only letters, numbers, and underscores.
string
required
Human-readable description of what the tool does. This helps the AI understand when to use the tool.
Write clear, concise descriptions. The AI uses these to decide when to invoke the tool.
ZodRawShape
required
Zod schema object defining the tool’s input parameters. The schema is used for:
  • Type inference (TypeScript types for handler args)
  • Runtime validation (invalid inputs are rejected)
  • AI guidance (schema sent to AI as JSON Schema)
function
required
Async function that executes the tool and returns the result.Signature:
  • args: Tool input, typed according to inputSchema
  • extra: Reserved for future use (currently unused)
  • Returns: CallToolResult with content blocks

Return Value

object
A tool definition object that can be passed to createSdkMcpServer().

CallToolResult Format

The handler must return a CallToolResult object:

Text Content

Most common response type:

Image Content

Return base64-encoded images:

Multiple Content Blocks

Combine text and images:

Error Response

Indicate errors with isError: true:

Examples

Simple Calculator

File Operations

API Integration

Database Query

Complex Schema with Nested Objects

Tool with No Input

Schema Validation

Supported Zod Types

The tool function supports all Zod schema types:

Descriptions

Add descriptions to help the AI understand parameters:

Validation Rules

Tool Name Validation

The tool name must:
  • Be 1-64 characters long
  • Start with a letter (a-z, A-Z)
  • Contain only letters, numbers, and underscores

Input Validation

Zod validates inputs at runtime:

Error Handling

Validation Errors

Invalid inputs are automatically rejected:

Handler Errors

Catch and format errors in your handler:

Timeout Errors

Tools that take too long will timeout:

Type Safety

The tool function provides full type inference:

Best Practices

1. Write Clear Descriptions

2. Validate and Constrain Inputs

3. Use Optional and Default Values

4. Handle Errors Gracefully

5. Return Structured Data

See Also