defineTool() for your application logic, or MCP to connect a group of tools through a server.
Define and run a tool
Use the TypeScript example setup and add Zod:maxTurns large enough for the intended loop.
Names and inputs
Direct tools supplied throughAgent({ tools: [...] }) are model-visible as sdk_<name>. Characters outside letters, digits, _, and - become _. For example, lookup-order becomes sdk_lookup-order. Names must remain unique after sanitization; use the model-visible name in tool policies and permission callbacks.
inputSchema accepts Zod or JSON Schema. inputJsonSchema accepts a JSON Schema directly. A schema describes the input expected by the model; v2 does not automatically parse custom callback input with Zod or validate arbitrary JSON Schema before calling execute. Validate in your callback before side effects, including when hooks or permission callbacks can replace the input. Keep transformations and application-specific refinements in that explicit validation step.
The callback may be synchronous or asynchronous. Return strings or plain serializable data. An exception becomes a tool failure that the model can receive; the overall run may still recover and succeed. Check the terminal result separately.
Cancellation and application boundaries
execute(input, context) receives context.sessionId, context.signal, and an optional context.toolCallId. Pass the signal to cancellable I/O, and check it before expensive work or side effects. The SDK cannot undo an external action your callback already committed.
Callbacks execute in your application process, including when the runtime uses isolated state or a remote server. They have your application’s access to databases, files, and services. Validate identifiers and enforce application authorization inside the callback when those checks must apply to every invocation.
Tool annotations such as read-only, destructive, idempotent, or open-world describe behavior to consumers. They do not enforce authorization.
Choose which tools the model can use
Supplying either list replaces the high-level default hidden-tool profile. For a narrow task, prefer a nonempty allowlist. A disallowed match wins over an allowed match. Scoped entries such as
bash(npm:*) match operations inside a tool; see permissions for the full distinction between availability, rules, modes, and callbacks.
For structured output, include StructuredOutput in the allowlist together with any application tools the task needs. Its case-sensitive name is an exception to the lower-case built-in tool names.
Discover a large tool collection
enableToolSearch: "auto" can defer tool schemas when there are many tools. "always" or true enables search; "never" or false disables it. Leaving the option unset disables deferred search. In auto mode, the runtime enables it at 20 total candidate tools or 10 MCP tools.
metadata.searchHint adds search terms, and metadata.alwaysLoad keeps a tool schema loaded. MCP tools can be deferred when search is enabled; other tool definitions must be marked deferred. The model uses tool_search to reveal relevant schemas for later steps. Discovery does not grant permission to execute a denied tool.