defineTool to create tools, then pass them to createMCPServer.
Defining a tool
Tool definition fields
| Field | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Unique tool identifier. Use lowercase with underscores. |
description | string | Yes | Description shown to the LLM to decide when to call this tool. |
inputSchema | ZodObject | Yes | Zod schema for input validation. Arguments are type-inferred from this. |
outputSchema | ZodRawShape | ZodObject | No | Zod schema for structured output. Normalized to ZodRawShape internally. |
handler | function | Yes | (args, context) => Promise<ToolResult | Record<string, unknown>> |
requiresAuth | boolean | No | When true, the dispatcher automatically rejects calls without a provider token. |
title | string | No | Human-readable display title shown in client UIs. |
annotations | object | No | MCP behavioral hints for clients (see Annotations). |
meta | { version?, last_update? } | No | Auto-injected into every result as tool_version and tool_last_update. |
Handler return values
Handlers can return either a plain object or a fullToolResult. Plain objects are automatically wrapped into structured content — you don’t need to construct the content array yourself in the common case.
ToolResult has the following shape:
Authenticated tools
SetrequiresAuth: true to have the framework automatically reject calls that arrive without a valid provider token. Inside the handler, use context.resolvedHeaders to forward authentication to external APIs without constructing the header yourself.
providerToken is present, use assertProviderToken:
Tool context
Every handler receives acontext object as its second argument:
| Property | Type | Description |
|---|---|---|
sessionId | string | Current MCP session ID. |
providerToken | string | undefined | Access token for external API calls. Present for OAuth, bearer, and API key strategies. |
resolvedHeaders | Record<string, string> | undefined | Ready-to-use auth headers for fetch. Strategy-aware — use this instead of building headers manually. |
authStrategy | AuthStrategy | undefined | Active strategy: oauth, bearer, api_key, custom, or none. |
provider | ProviderInfo | undefined | Provider info object (OAuth only). |
signal | AbortSignal | undefined | Abort signal for request cancellation. |
Annotations
Annotations are behavioral hints for MCP clients. They are not enforced by the framework — they help clients display accurate UI and make safe decisions about when to invoke a tool automatically.| Annotation | Type | Default | Description |
|---|---|---|---|
readOnlyHint | boolean | false | Tool does not modify any environment or state. |
destructiveHint | boolean | true | Tool may delete or overwrite data. |
idempotentHint | boolean | false | Repeated calls with identical arguments have no additional effect. |
openWorldHint | boolean | true | Tool interacts with external entities outside the server. |
Error responses with toolFail
Use toolFail to create a typed error factory that merges a message into a preset shape. This keeps error responses structurally consistent with success responses.
Tool versioning with meta
Supply a meta object to have tool_version and tool_last_update automatically injected into every handler result, including error paths.
Built-in tools
@phake/mcp ships two built-in tools you can use for testing and diagnostics.
echo
Echoes a message back, optionally uppercased. Useful for verifying connectivity.Input:
{ message: string, uppercase?: boolean }Output: { echoed: string, length: number }health
Reports server status, runtime, and optional uptime details.Input:
{ verbose?: boolean }Output: { status: string, timestamp: number, runtime: string, uptime?: number }