Overview
defineTool creates a SharedToolDefinition that you can register with createMCPServer. It wraps your handler so that plain object return values are automatically converted to the ToolResult format the MCP protocol expects.
Parameters
Unique identifier for the tool. Use lowercase letters and underscores (e.g.
get_profile, search_documents). This name is what LLMs see when deciding
which tool to call.Human-readable description shown to the LLM. Write this as a clear, precise
statement of what the tool does and when to use it.
Zod object schema that validates and types the tool’s input arguments. The
inferred type is passed to your
handler as the first argument.handler
(args: z.infer<TSchema>, context: ToolContext) => Promise<ToolResult | Record<string, unknown>>
required
Async function that executes the tool. Receives validated
args and a
ToolContext object.Return either a full ToolResult or a plain object. Plain objects are
automatically wrapped — see Return values.Human-readable display name shown in client UIs. Separate from
name, which
is the machine identifier.Zod schema for the tool’s structured output. Accepts either a raw shape
(
{ field: z.string() }) or a ZodObject. Normalized internally to
ZodRawShape. Used by MCP clients that support structured content.When
true, the dispatcher automatically rejects calls where
context.providerToken is absent, returning an authentication error before
your handler runs. Defaults to false.Behavioral hints for MCP clients. These are informational — they are not
enforced by the SDK, but well-behaved clients may use them to surface
warnings or adjust behavior.
Version metadata automatically injected into every handler result as
tool_version and tool_last_update fields. Useful for tracking which
version of a tool produced a given output.Return values
Your handler can return either a fullToolResult or a plain object.
Plain object — defineTool wraps it automatically:
ToolResult — passed through unchanged:
Helpers
toolFail
Creates a typed error factory with preset default fields. Call the returned function with an error message to produce a consistent error object.
assertProviderToken
Narrows context so that context.providerToken is typed as string (not string | undefined). Throws "Authentication required" if the token is absent.
Use this in tools where you need a guaranteed token but prefer not to set requiresAuth: true on the definition itself.