Give Claude custom tools
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -381,7 +381,7 @@
}
}
-To try the policy, replace the `options` in the Call a custom tool script with this example’s `options`, then run that script from your terminal. The script prints `Approval requested for mcp__weather__get_temperature`, followed by Claude’s answer about the temperature. For a complete script, see the [TypeScript reference](</docs/en/agent-sdk/typescript#mcpsdkserverconfigwithinstance>). If you use `auto` or `plan` [permission mode](</docs/en/agent-sdk/permissions#permission-modes>), set `destructiveHint: true` in the tool’s annotations so calls to the tool still reach your callback. Otherwise the [auto mode classifier](</docs/en/permission-modes#eliminate-prompts-with-auto-mode>) can approve some `always_ask` calls, under [conditions listed in the TypeScript reference](</docs/en/agent-sdk/typescript#permission-modes-that-can-approve-an-always_ask-call>), and a call it approves doesn’t reach your callback. Set the hint explicitly, because an unset `destructiveHint` counts as `false` for `always_ask` calls rather than defaulting to `true`.
+To try the policy, replace the `options` in the Call a custom tool script with this example’s `options`, then run that script from your terminal. The script prints `Approval requested for mcp__weather__get_temperature`, followed by Claude’s answer about the temperature. For a complete script, see the [TypeScript reference](</docs/en/agent-sdk/typescript#mcpsdkserverconfigwithinstance>). If you use `auto` or `plan` [permission mode](</docs/en/agent-sdk/permissions#permission-modes>), set `destructiveHint: true` in the tool’s annotations to keep the [auto mode classifier](</docs/en/permission-modes#eliminate-prompts-with-auto-mode>) from approving calls in place of your callback. Otherwise the classifier can approve some `always_ask` calls, under [conditions listed in the TypeScript reference](</docs/en/agent-sdk/typescript#permission-modes-that-can-approve-an-always_ask-call>). Set the hint explicitly, because an unset `destructiveHint` counts as `false` for `always_ask` calls rather than defaulting to `true`.
## Handle errors
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -362,7 +362,7 @@
### Require approval for specific tools
-Set a tool’s `permission_policy` to `always_ask`, and `allowedTools` stops approving that tool’s calls, so they can reach your [`canUseTool`](</docs/en/agent-sdk/user-input>) callback even though the tool is listed. Set `always_deny` instead, and [a call to the tool is denied](</docs/en/agent-sdk/permissions#how-permissions-are-evaluated>), but a [mod](</docs/en/plugins/mods/overview>) in a plugin you load [can still approve it](</docs/en/permissions#precedence-among-rules-hooks-and-mods>). The field is TypeScript-only and requires Agent SDK v0.3.301 or later. Put the policy entry in a `tools` array on the config object `createSdkMcpServer()` returns, not in the `tools` option you pass into that function. This example spreads `weatherServer` from the weather tool example and adds a `tools` array giving `get_temperature` the `always_ask` policy:
+Set a tool’s `permission_policy` to `always_ask`, and `allowedTools` stops approving that tool’s calls, so they can reach your [`canUseTool`](</docs/en/agent-sdk/user-input>) callback even though the tool is listed. Set `always_deny` instead to [block the tool](</docs/en/agent-sdk/typescript#permission-policy-values>). The field is TypeScript-only and requires Agent SDK v0.3.301 or later. Put the policy entry in a `tools` array on the config object `createSdkMcpServer()` returns, not in the `tools` option you pass into that function. This example spreads `weatherServer` from the weather tool example and adds a `tools` array giving `get_temperature` the `always_ask` policy:
TypeScript
Show diff (214 lines)
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -1,10 +1,6 @@
Custom tools extend the Agent SDK by letting you define your own functions that Claude can call during a conversation. Using the SDK’s in-process MCP server, you can give Claude access to databases, external APIs, domain-specific logic, or any other capability your application needs.
-##
-
-
-
-Quick reference
+## Quick reference
What you want to do| Do this
---|---
@@ -20,11 +16,7 @@
Return a machine-readable JSON result| Set `structuredContent` on the result. See Return structured data.
Scale to many tools| Use [tool search](</docs/en/agent-sdk/tool-search>) to load tools on demand.
-##
-
-
-
-Create a custom tool
+## Create a custom tool
A tool is defined by four parts, passed as arguments to the [`tool()`](</docs/en/agent-sdk/typescript#tool>) helper in TypeScript or the [`@tool`](</docs/en/agent-sdk/python#tool>) decorator in Python:
@@ -48,11 +40,7 @@
pip install httpx
-###
-
-
-
-Weather tool example
+### Weather tool example
This example defines a `get_temperature` tool and wraps it in an MCP server, without passing the server to `query`. To run the tool, see Call a custom tool.
@@ -137,11 +125,7 @@
See the [`tool()`](</docs/en/agent-sdk/typescript#tool>) TypeScript reference or the [`@tool`](</docs/en/agent-sdk/python#tool>) Python reference for full parameter details, including JSON Schema input formats and return value structure.
-###
-
-
-
-Make a parameter optional
+### Make a parameter optional
To make a parameter optional, declare it optional in the schema and apply the default in the handler:
@@ -150,11 +134,7 @@
The `get_precipitation_chance` tool shows both patterns.
-###
-
-
-
-Call a custom tool
+### Call a custom tool
To call a custom tool, follow these steps:
@@ -219,11 +199,7 @@
Claude calls `get_temperature` and the script prints a one-line answer with the current temperature in San Francisco.
-###
-
-
-
-Add more tools
+### Add more tools
A server holds as many tools as you list in its `tools` array. With more than one tool on a server, you can list each one in `allowedTools` individually or use the wildcard `mcp__weather__*` to cover every tool the server exposes. The example below defines a second tool, `get_precipitation_chance`, and replaces the `weatherServer` definition from the weather tool example with one that lists both tools in the array.
@@ -321,11 +297,7 @@
[Tool search](</docs/en/agent-sdk/tool-search>) is on by default and defers SDK MCP tools: Claude sees each tool’s name in a compact list and loads its full schema on demand. With tool search disabled, every tool in this array consumes context window space on every turn. In TypeScript, pass `alwaysLoad: true` in the `extras` argument of [`tool()`](</docs/en/agent-sdk/typescript#tool>) or in the options of [`createSdkMcpServer()`](</docs/en/agent-sdk/typescript#createsdkmcpserver>) to keep a tool’s full schema in the initial prompt.
-###
-
-
-
-Add tool annotations
+### Add tool annotations
[Tool annotations](<https://modelcontextprotocol.io/docs/concepts/tools#tool-annotations>) are optional metadata describing how a tool behaves. Pass them as the fifth argument to `tool()` helper in TypeScript or via the `annotations` keyword argument for the `@tool` decorator in Python. All hint fields are Booleans.
@@ -368,19 +340,11 @@
See `ToolAnnotations` in the [TypeScript](</docs/en/agent-sdk/typescript#toolannotations>) or [Python](</docs/en/agent-sdk/python#toolannotations>) reference.
-##
-
-
-
-Control tool access
+## Control tool access
The weather tool example registered a server and listed tools in `allowedTools`. This section covers how to scope access when you have multiple tools or want to restrict built-ins. For how tool names are constructed, see Call a custom tool.
-###
-
-
-
-Configure allowed tools
+### Configure allowed tools
You control two layers with the `tools`, `allowedTools`, and `disallowedTools` options:
@@ -396,11 +360,7 @@
To remove a built-in from Claude’s context, omit it from `tools` or list its bare name in `disallowedTools` (TypeScript) or `disallowed_tools` (Python). For the full evaluation order, see [Configure permissions](</docs/en/agent-sdk/permissions#how-permissions-are-evaluated>).
-###
-
-
-
-Require approval for specific tools
+### Require approval for specific tools
Set a tool’s `permission_policy` to `always_ask`, and `allowedTools` stops approving that tool’s calls, so they can reach your [`canUseTool`](</docs/en/agent-sdk/user-input>) callback even though the tool is listed. Set `always_deny` instead, and [a call to the tool is denied](</docs/en/agent-sdk/permissions#how-permissions-are-evaluated>), but a [mod](</docs/en/plugins/mods/overview>) in a plugin you load [can still approve it](</docs/en/permissions#precedence-among-rules-hooks-and-mods>). The field is TypeScript-only and requires Agent SDK v0.3.301 or later. Put the policy entry in a `tools` array on the config object `createSdkMcpServer()` returns, not in the `tools` option you pass into that function. This example spreads `weatherServer` from the weather tool example and adds a `tools` array giving `get_temperature` the `always_ask` policy:
@@ -423,11 +383,7 @@
To try the policy, replace the `options` in the Call a custom tool script with this example’s `options`, then run that script from your terminal. The script prints `Approval requested for mcp__weather__get_temperature`, followed by Claude’s answer about the temperature. For a complete script, see the [TypeScript reference](</docs/en/agent-sdk/typescript#mcpsdkserverconfigwithinstance>). If you use `auto` or `plan` [permission mode](</docs/en/agent-sdk/permissions#permission-modes>), set `destructiveHint: true` in the tool’s annotations so calls to the tool still reach your callback. Otherwise the [auto mode classifier](</docs/en/permission-modes#eliminate-prompts-with-auto-mode>) can approve some `always_ask` calls, under [conditions listed in the TypeScript reference](</docs/en/agent-sdk/typescript#permission-modes-that-can-approve-an-always_ask-call>), and a call it approves doesn’t reach your callback. Set the hint explicitly, because an unset `destructiveHint` counts as `false` for `always_ask` calls rather than defaulting to `true`.
-##
-
-
-
-Handle errors
+## Handle errors
A handler error doesn’t stop the agent loop. The SDK’s in-process MCP server catches uncaught exceptions and returns them as error results, so how you report an error determines what Claude reads, not whether the query fails:
@@ -531,19 +487,11 @@
}
);
-##
-
-
-
-Return images and resources
+## Return images and resources
The `content` array in a tool result accepts `text`, `image`, `audio`, `resource`, and `resource_link` blocks. You can mix them in the same response. In TypeScript, the SDK saves audio blocks to disk and Claude receives a text block with the saved file path; in Python, the SDK drops audio blocks from the tool result and logs a warning. Claude receives each resource link block as a text block containing the link’s name, URI, and description. In TypeScript, your application also receives the links themselves as [`resourceLinks`](</docs/en/agent-sdk/typescript#sdkmcpresourcelink>) on the user message’s `tool_use_result`; in Python, the SDK flattens them to text before the CLI sees the result, so the Python [`resourceLinks` key](</docs/en/agent-sdk/python#usermessage>) is never produced for in-process tools.
-###
-
-
-
-Images
+### Images
An image block carries the image bytes inline, encoded as base64. There is no URL field. To return an image that lives at a URL, fetch it in the handler, read the response bytes, and base64-encode them before returning. A PNG, JPEG, GIF, or WebP image reaches Claude as visual input; an image of any other type is saved to disk and Claude receives its file path as text instead.
@@ -607,11 +555,7 @@
}
);
-###
-
-
-
-Resources
+### Resources
A resource block embeds a piece of content identified by a URI. The actual content rides in the block’s `text` or `blob` field. Use this when your tool produces a generated file or a record from an external system.
@@ -657,11 +601,7 @@
These block shapes come from the MCP `CallToolResult` type. See the [MCP specification](<https://modelcontextprotocol.io/specification/2025-06-18/server/tools#tool-result>) for the full definition.
-##
-
-
-
-Return structured data
+## Return structured data
`structuredContent` is an optional JSON object on the result, separate from the `content` array. Use it to return raw values that Claude can read as exact fields instead of parsing them out of a text string or image. For Python handlers, see Structured content in Python. When a TypeScript `tool()` handler sets `structuredContent`, Claude receives the JSON plus the image and resource blocks in `content`, and the text blocks in `content` don’t reach Claude. The example below renders a chart as an image block and returns the data points behind it in `structuredContent` from the same handler. In the snippet, `chartPngBuffer` is a `Buffer` holding the rendered PNG bytes.
@@ -684,11 +624,7 @@
The Python `@tool` decorator forwards only `content` and `is_error` from the handler’s return dict. To return `structuredContent` from Python, run a [standalone MCP server](</docs/en/agent-sdk/mcp>) instead of an in-process SDK server.
-##
-
-
-
-Example: unit converter
+## Example: unit converter
This tool converts values between units of length, temperature, and weight. A user can ask “convert 100 kilometers to miles” or “what is 72°F in Celsius,” and Claude picks the right unit type and units from the request. It demonstrates two patterns:
@@ -927,11 +863,7 @@
}
}
-##
-
-
-
-Next steps
+## Next steps
You can mix the patterns on this page in the same server: a single server can hold a database tool, an API gateway tool, and an image renderer alongside each other. From here:
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -54,7 +54,7 @@
Weather tool example
-This example defines a `get_temperature` tool and wraps it in an MCP server, without passing the server to `query`. To run the tool, see Call a custom tool below.
+This example defines a `get_temperature` tool and wraps it in an MCP server, without passing the server to `query`. To run the tool, see Call a custom tool.
Python
@@ -148,7 +148,7 @@
* **TypeScript** : add `.optional()` to the Zod field.
* **Python** : the dict schema requires every key. Use the JSON Schema form, leave the parameter out of `required`, and read it with `args.get()`. For a typed schema with optional keys, see [TypedDict class](</docs/en/agent-sdk/python#input-schema-options>).
-The `get_precipitation_chance` tool below shows both patterns.
+The `get_precipitation_chance` tool shows both patterns.
###
@@ -156,7 +156,12 @@
Call a custom tool
-Pass the MCP server you created to `query` via the `mcpServers` option, and list each tool’s [fully qualified name](</docs/en/agent-sdk/mcp#tool-naming-convention>) in `allowedTools` so the tool runs without a permission prompt. That name is `mcp__{server_name}__{tool_name}`, where `{server_name}` is the key you give the server in `mcpServers`. If the key contains characters such as `.` or spaces, replace them with `_` when you write `{server_name}`. These snippets reuse the `weatherServer` from the weather tool example to ask Claude what the weather is in a specific location.
+To call a custom tool, follow these steps:
+
+ 1. Pass the MCP server you created to `query` using the `mcpServers` option.
+ 2. List the tool’s [fully qualified name](</docs/en/agent-sdk/mcp#tool-naming-convention>) in `allowedTools` to [pre-approve](</docs/en/agent-sdk/permissions#allow-and-deny-rules>) its calls. That name follows the convention `mcp__{server_name}__{tool_name}`, where `{server_name}` is the key you give the server in `mcpServers`. If the key contains characters such as `.` or spaces, replace them with `_` when you write `{server_name}`.
+
+These snippets reuse the `weatherServer` from the weather tool example to ask Claude what the weather is in a specific location.
Python
@@ -328,8 +333,8 @@
---|---|---
`readOnlyHint`| `false`| Tool does not modify its environment. Controls whether the tool can be called in parallel with other read-only tools.
`destructiveHint`| `true`| Tool may perform destructive updates. Set it to `true` explicitly on any tool you mark `always_ask`.
-`idempotentHint`| `false`| Repeated calls with the same arguments have no additional effect. Informational only.
-`openWorldHint`| `true`| Tool reaches systems outside your process. Informational only.
+`idempotentHint`| `false`| Repeated calls with the same arguments have no additional effect.
+`openWorldHint`| `true`| Tool reaches systems outside your process.
Annotations are metadata, not enforcement. A tool marked `readOnlyHint: true` can still write to disk if that’s what the handler does. Keep the annotation accurate to the handler. This example adds `readOnlyHint` to the `get_temperature` tool from the weather tool example.
@@ -377,16 +382,19 @@
Configure allowed tools
-The `tools` option and the allowed/disallowed lists affect two layers: availability, which controls whether a tool appears in Claude’s context, and permission, which controls whether a call is approved once Claude attempts it. `tools` and bare-name `disallowedTools` entries change availability. `allowedTools` and scoped `disallowedTools` rules change permission. If you name one of the [task-tracking tools](</docs/en/agent-sdk/todo-tracking#model-availability>) in `allowedTools`, Claude Code also opts the session in.
+You control two layers with the `tools`, `allowedTools`, and `disallowedTools` options:
+
+ * **Availability** : whether a tool appears in Claude’s context. To change it, use the `tools` option or list a bare built-in name such as `"Bash"` in `disallowedTools`.
+ * **Permission** : whether Claude Code approves a call when Claude makes it. To change it, use `allowedTools` or a scoped `disallowedTools` rule such as `"Bash(rm *)"`.
Option| Layer| Effect
---|---|---
`tools: ["Read", "Grep"]`| Availability| Only the listed built-ins are in Claude’s context. Unlisted built-ins are removed. MCP tools are unaffected.
`tools: []`| Availability| All built-ins are removed. Claude can only use your MCP tools.
-allowed tools| Permission| Listed tools run without a permission prompt. Other unlisted tools remain available; calls go through the [permission flow](</docs/en/agent-sdk/permissions>).
-disallowed tools| Both| A bare tool name such as `"Bash"` removes the tool from Claude’s context, the same as omitting it from `tools`. A scoped rule such as `"Bash(rm *)"` leaves the tool in context and denies only calls that match [as written](</docs/en/permissions#bash-rule-limits>).
-
-To remove a built-in entirely, omit it from `tools` or list its bare name in `disallowedTools` (Python: `disallowed_tools`); both keep the tool out of context so Claude never attempts it. A scoped `disallowedTools` rule blocks matching calls but leaves the tool visible, so Claude may waste a turn trying it. See [Configure permissions](</docs/en/agent-sdk/permissions>) for the full evaluation order.
+allowed tools| Permission| Listed tools are pre-approved, apart from the [calls that no allow rule approves](</docs/en/agent-sdk/permissions#calls-an-allow-rule-never-approves>). Calls to tools you leave out of the list go through the [permission flow](</docs/en/agent-sdk/permissions>).
+disallowed tools| Both| A bare built-in name such as `"Bash"` removes the tool from Claude’s context. A scoped rule such as `"Bash(rm *)"` leaves the tool in context and denies the calls that match it [as written](</docs/en/permissions#bash-rule-limits>).
+
+To remove a built-in from Claude’s context, omit it from `tools` or list its bare name in `disallowedTools` (TypeScript) or `disallowed_tools` (Python). For the full evaluation order, see [Configure permissions](</docs/en/agent-sdk/permissions#how-permissions-are-evaluated>).
###
@@ -655,7 +663,7 @@
Return structured data
-`structuredContent` is an optional JSON object on the result, separate from the `content` array. Use it to return raw values that Claude can read as exact fields instead of parsing them out of a text string or image. When `structuredContent` is set, Claude receives the JSON plus any image or resource blocks from `content`. Text blocks in `content` are not forwarded, since they are assumed to duplicate the structured data. The example below renders a chart as an image block and returns the data points behind it in `structuredContent` from the same handler. In the snippet, `chartPngBuffer` is a `Buffer` holding the rendered PNG bytes.
+`structuredContent` is an optional JSON object on the result, separate from the `content` array. Use it to return raw values that Claude can read as exact fields instead of parsing them out of a text string or image. For Python handlers, see Structured content in Python. When a TypeScript `tool()` handler sets `structuredContent`, Claude receives the JSON plus the image and resource blocks in `content`, and the text blocks in `content` don’t reach Claude. The example below renders a chart as an image block and returns the data points behind it in `structuredContent` from the same handler. In the snippet, `chartPngBuffer` is a `Buffer` holding the rendered PNG bytes.
TypeScript
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -12,6 +12,7 @@
Make a parameter optional| Declare it optional in the schema and apply the default in the handler. See Make a parameter optional.
Register a tool with Claude| Wrap in `create_sdk_mcp_server` / `createSdkMcpServer` and pass to `mcpServers` in `query()`. See Call a custom tool.
Pre-approve a tool| Add to your allowed tools. See Configure allowed tools.
+Require approval for calls to a tool, or block the tool| In the TypeScript SDK, add a `permission_policy` for the tool to the config object that `createSdkMcpServer()` returns. See Require approval for specific tools.
Remove a built-in tool from Claude’s context| Pass a `tools` array listing only the built-ins you want. See Configure allowed tools.
Let Claude call tools in parallel| Set `readOnlyHint: true` on tools with no side effects. See Add tool annotations.
Control the error message Claude reads| Return `isError: true` to compose the message instead of surfacing the raw exception. See Handle errors.
@@ -326,7 +327,7 @@
Field| Default| Meaning
---|---|---
`readOnlyHint`| `false`| Tool does not modify its environment. Controls whether the tool can be called in parallel with other read-only tools.
-`destructiveHint`| `true`| Tool may perform destructive updates. Informational only.
+`destructiveHint`| `true`| Tool may perform destructive updates. Set it to `true` explicitly on any tool you mark `always_ask`.
`idempotentHint`| `false`| Repeated calls with the same arguments have no additional effect. Informational only.
`openWorldHint`| `true`| Tool reaches systems outside your process. Informational only.
@@ -386,6 +387,33 @@
disallowed tools| Both| A bare tool name such as `"Bash"` removes the tool from Claude’s context, the same as omitting it from `tools`. A scoped rule such as `"Bash(rm *)"` leaves the tool in context and denies only calls that match [as written](</docs/en/permissions#bash-rule-limits>).
To remove a built-in entirely, omit it from `tools` or list its bare name in `disallowedTools` (Python: `disallowed_tools`); both keep the tool out of context so Claude never attempts it. A scoped `disallowedTools` rule blocks matching calls but leaves the tool visible, so Claude may waste a turn trying it. See [Configure permissions](</docs/en/agent-sdk/permissions>) for the full evaluation order.
+
+###
+
+
+
+Require approval for specific tools
+
+Set a tool’s `permission_policy` to `always_ask`, and `allowedTools` stops approving that tool’s calls, so they can reach your [`canUseTool`](</docs/en/agent-sdk/user-input>) callback even though the tool is listed. Set `always_deny` instead, and [a call to the tool is denied](</docs/en/agent-sdk/permissions#how-permissions-are-evaluated>), but a [mod](</docs/en/plugins/mods/overview>) in a plugin you load [can still approve it](</docs/en/permissions#precedence-among-rules-hooks-and-mods>). The field is TypeScript-only and requires Agent SDK v0.3.301 or later. Put the policy entry in a `tools` array on the config object `createSdkMcpServer()` returns, not in the `tools` option you pass into that function. This example spreads `weatherServer` from the weather tool example and adds a `tools` array giving `get_temperature` the `always_ask` policy:
+
+TypeScript
+
+ options: {
+ mcpServers: {
+ weather: {
+ ...weatherServer,
+ tools: [{ name: "get_temperature", permission_policy: "always_ask" as const }]
+ }
+ },
+ allowedTools: ["mcp__weather__get_temperature"],
+ permissionMode: "default",
+ canUseTool: async (toolName, input) => {
+ console.log(`Approval requested for ${toolName}`);
+ return { behavior: "allow", updatedInput: input };
+ }
+ }
+
+To try the policy, replace the `options` in the Call a custom tool script with this example’s `options`, then run that script from your terminal. The script prints `Approval requested for mcp__weather__get_temperature`, followed by Claude’s answer about the temperature. For a complete script, see the [TypeScript reference](</docs/en/agent-sdk/typescript#mcpsdkserverconfigwithinstance>). If you use `auto` or `plan` [permission mode](</docs/en/agent-sdk/permissions#permission-modes>), set `destructiveHint: true` in the tool’s annotations so calls to the tool still reach your callback. Otherwise the [auto mode classifier](</docs/en/permission-modes#eliminate-prompts-with-auto-mode>) can approve some `always_ask` calls, under [conditions listed in the TypeScript reference](</docs/en/agent-sdk/typescript#permission-modes-that-can-approve-an-always_ask-call>), and a call it approves doesn’t reach your callback. Set the hint explicitly, because an unset `destructiveHint` counts as `false` for `always_ask` calls rather than defaulting to `true`.
##
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -155,7 +155,7 @@
Call a custom tool
-Pass the MCP server you created to `query` via the `mcpServers` option. Each tool’s [fully qualified name](</docs/en/agent-sdk/mcp#tool-naming-convention>) is `mcp__{server_name}__{tool_name}`, where `{server_name}` is the key you give the server in `mcpServers`. If the key contains characters such as `.` or spaces, replace them with `_` when you write `{server_name}`. List the fully qualified name in `allowedTools` so the tool runs without a permission prompt. These snippets reuse the `weatherServer` from the weather tool example to ask Claude what the weather is in a specific location.
+Pass the MCP server you created to `query` via the `mcpServers` option, and list each tool’s [fully qualified name](</docs/en/agent-sdk/mcp#tool-naming-convention>) in `allowedTools` so the tool runs without a permission prompt. That name is `mcp__{server_name}__{tool_name}`, where `{server_name}` is the key you give the server in `mcpServers`. If the key contains characters such as `.` or spaces, replace them with `_` when you write `{server_name}`. These snippets reuse the `weatherServer` from the weather tool example to ask Claude what the weather is in a specific location.
Python
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -155,7 +155,7 @@
Call a custom tool
-Pass the MCP server you created to `query` via the `mcpServers` option. The key in `mcpServers` becomes the `{server_name}` segment in each tool’s fully qualified name: `mcp__{server_name}__{tool_name}`. List that name in `allowedTools` so the tool runs without a permission prompt. These snippets reuse the `weatherServer` from the weather tool example to ask Claude what the weather is in a specific location.
+Pass the MCP server you created to `query` via the `mcpServers` option. Each tool’s [fully qualified name](</docs/en/agent-sdk/mcp#tool-naming-convention>) is `mcp__{server_name}__{tool_name}`, where `{server_name}` is the key you give the server in `mcpServers`. If the key contains characters such as `.` or spaces, replace them with `_` when you write `{server_name}`. List the fully qualified name in `allowedTools` so the tool runs without a permission prompt. These snippets reuse the `weatherServer` from the weather tool example to ask Claude what the weather is in a specific location.
Python
Show diff (139 lines)
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -9,6 +9,7 @@
What you want to do| Do this
---|---
Define a tool| Use [`@tool`](</docs/en/agent-sdk/python#tool>) (Python) or [`tool()`](</docs/en/agent-sdk/typescript#tool>) (TypeScript) with a name, description, schema, and handler. See Create a custom tool.
+Make a parameter optional| Declare it optional in the schema and apply the default in the handler. See Make a parameter optional.
Register a tool with Claude| Wrap in `create_sdk_mcp_server` / `createSdkMcpServer` and pass to `mcpServers` in `query()`. See Call a custom tool.
Pre-approve a tool| Add to your allowed tools. See Configure allowed tools.
Remove a built-in tool from Claude’s context| Pass a `tools` array listing only the built-ins you want. See Configure allowed tools.
@@ -28,13 +29,23 @@
* **Name:** a unique identifier Claude uses to call the tool.
* **Description:** what the tool does. Claude reads this to decide when to call it.
- * **Input schema:** the arguments Claude must provide. In TypeScript this is always a [Zod schema](<https://zod.dev/>), and the handler’s `args` are typed from it automatically. In Python this is a dict mapping names to types, like `{"latitude": float}`, which the SDK converts to JSON Schema for you. The Python decorator also accepts a full [JSON Schema](<https://json-schema.org/understanding-json-schema/about>) dict directly when you need enums, ranges, optional fields, or nested objects.
+ * **Input schema:** the arguments the tool accepts, declared per language:
+ * **TypeScript** : a [Zod schema](<https://zod.dev/>). The handler’s `args` take their types from it. Call `.describe()` on a field to give it a description Claude sees.
+ * **Python** : a dict mapping names to types, like `{"latitude": float}`, which the SDK converts to JSON Schema for you. Wrap a type in `Annotated`, like `{"latitude": Annotated[float, "Latitude coordinate"]}`, to give the field a description Claude sees. The decorator also accepts a full [JSON Schema](<https://json-schema.org/understanding-json-schema/about>) dict directly when you need enums, ranges, optional fields, or nested objects.
* **Handler:** the async function that runs when Claude calls the tool. It receives the validated arguments and must return an object with:
* `content` (required): an array of result blocks, each with a `type` of `"text"`, `"image"`, `"audio"`, `"resource"`, or `"resource_link"`. See Return images and resources for non-text blocks.
* `structuredContent` (optional): a JSON object holding the result as machine-readable data, returned alongside `content`. See Return structured data.
* `isError` (optional): set to `true` to signal a tool failure so Claude can react to it. See Handle errors.
-After defining a tool, wrap it in a server with [`createSdkMcpServer`](</docs/en/agent-sdk/typescript#createsdkmcpserver>) (TypeScript) or [`create_sdk_mcp_server`](</docs/en/agent-sdk/python#create_sdk_mcp_server>) (Python). The server runs in-process inside your application, not as a separate process.
+After defining a tool, wrap it in a server with [`createSdkMcpServer`](</docs/en/agent-sdk/typescript#createsdkmcpserver>) (TypeScript) or [`create_sdk_mcp_server`](</docs/en/agent-sdk/python#create_sdk_mcp_server>) (Python). The server runs in-process inside your application, not as a separate process. Python examples on this page that make HTTP requests use [httpx](<https://www.python-httpx.org/>). Add it with the package manager your project uses:
+
+ * Python (uv)
+
+ * Python (pip)
+
+ uv add httpx
+
+ pip install httpx
###
@@ -42,13 +53,13 @@
Weather tool example
-This example defines a `get_temperature` tool and wraps it in an MCP server. It only sets up the tool; to pass it to `query` and run it, see Call a custom tool below.
+This example defines a `get_temperature` tool and wraps it in an MCP server, without passing the server to `query`. To run the tool, see Call a custom tool below.
Python
TypeScript
- from typing import Any
+ from typing import Annotated, Any
import httpx
from claude_agent_sdk import tool, create_sdk_mcp_server
@@ -56,7 +67,10 @@
@tool(
"get_temperature",
"Get the current temperature at a location",
- {"latitude": float, "longitude": float},
+ {
+ "latitude": Annotated[float, "Latitude coordinate"],
+ "longitude": Annotated[float, "Longitude coordinate"],
+ },
)
async def get_temperature(args: dict[str, Any]) -> dict[str, Any]:
async with httpx.AsyncClient() as client:
@@ -122,7 +136,18 @@
See the [`tool()`](</docs/en/agent-sdk/typescript#tool>) TypeScript reference or the [`@tool`](</docs/en/agent-sdk/python#tool>) Python reference for full parameter details, including JSON Schema input formats and return value structure.
-To make a parameter optional: in TypeScript, add `.optional()` to the Zod field and apply the default in the handler. In Python, the dict schema treats every key as required, so leave the parameter out of the schema, mention it in the description string, and read it with `args.get()` in the handler. The `get_precipitation_chance` tool below shows both patterns.
+###
+
+
+
+Make a parameter optional
+
+To make a parameter optional, declare it optional in the schema and apply the default in the handler:
+
+ * **TypeScript** : add `.optional()` to the Zod field.
+ * **Python** : the dict schema requires every key. Use the JSON Schema form, leave the parameter out of `required`, and read it with `args.get()`. For a typed schema with optional keys, see [TypedDict class](</docs/en/agent-sdk/python#input-schema-options>).
+
+The `get_precipitation_chance` tool below shows both patterns.
###
@@ -170,7 +195,23 @@
}
}
-Combine this snippet with the tool and server definitions from the weather tool example in one file, then run it with `python weather.py` for Python or `npx tsx weather.ts` for TypeScript. Claude calls `get_temperature` and the script prints a one-line answer with the current temperature in San Francisco.
+Combine this snippet with the tool and server definitions from the weather tool example in one file, `weather.py` or `weather.ts`, then run it from your terminal:
+
+ * TypeScript
+
+ * Python (uv)
+
+ * Python (pip)
+
+ npx tsx weather.ts
+
+ uv run weather.py
+
+Activate the virtual environment where you installed the SDK, then run:
+
+ python weather.py
+
+Claude calls `get_temperature` and the script prints a one-line answer with the current temperature in San Francisco.
###
@@ -187,12 +228,25 @@
# Define a second tool for the same server
@tool(
"get_precipitation_chance",
- "Get the hourly precipitation probability for a location. "
- "Optionally pass 'hours' (1-24) to control how many hours to return.",
- {"latitude": float, "longitude": float},
+ "Get the hourly precipitation probability for a location",
+ {
+ "type": "object",
+ "properties": {
+ "latitude": {"type": "number"},
+ "longitude": {"type": "number"},
+ "hours": {
+ "type": "integer",
+ "minimum": 1,
+ "maximum": 24,
+ "description": "How many hours of forecast to return",
+ },
+ },
+ # 'hours' is left out of required, so Claude can omit it
+ "required": ["latitude", "longitude"],
+ },
)
async def get_precipitation_chance(args: dict[str, Any]) -> dict[str, Any]:
- # 'hours' isn't in the schema - read it with .get() to make it optional
+ # 'hours' isn't in required - read it with .get() to fall back to a default
hours = args.get("hours", 12)
async with httpx.AsyncClient() as client:
response = await client.get(
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -122,7 +122,7 @@
See the [`tool()`](</docs/en/agent-sdk/typescript#tool>) TypeScript reference or the [`@tool`](</docs/en/agent-sdk/python#tool>) Python reference for full parameter details, including JSON Schema input formats and return value structure.
-To make a parameter optional: in TypeScript, add `.default()` to the Zod field. In Python, the dict schema treats every key as required, so leave the parameter out of the schema, mention it in the description string, and read it with `args.get()` in the handler. The `get_precipitation_chance` tool below shows both patterns.
+To make a parameter optional: in TypeScript, add `.optional()` to the Zod field and apply the default in the handler. In Python, the dict schema treats every key as required, so leave the parameter out of the schema, mention it in the description string, and read it with `args.get()` in the handler. The `get_precipitation_chance` tool below shows both patterns.
###
@@ -235,18 +235,19 @@
.int()
.min(1)
.max(24)
- .default(12) // .default() makes the parameter optional
+ .optional() // .optional() lets Claude omit the parameter
.describe("How many hours of forecast to return")
},
async (args) => {
+ const hours = args.hours ?? 12; // Apply the default in the handler
const response = await fetch(
`https://api.open-meteo.com/v1/forecast?latitude=${args.latitude}&longitude=${args.longitude}&hourly=precipitation_probability&forecast_days=1`
);
const data: any = await response.json();
- const chances = data.hourly.precipitation_probability.slice(0, args.hours);
+ const chances = data.hourly.precipitation_probability.slice(0, hours);
return {
- content: [{ type: "text", text: `Next ${args.hours} hours: ${chances.join("%, ")}%` }]
+ content: [{ type: "text", text: `Next ${hours} hours: ${chances.join("%, ")}%` }]
};
}
);
@@ -454,7 +455,7 @@
Images
-An image block carries the image bytes inline, encoded as base64. There is no URL field. To return an image that lives at a URL, fetch it in the handler, read the response bytes, and base64-encode them before returning. The result is processed as visual input.
+An image block carries the image bytes inline, encoded as base64. There is no URL field. To return an image that lives at a URL, fetch it in the handler, read the response bytes, and base64-encode them before returning. A PNG, JPEG, GIF, or WebP image reaches Claude as visual input; an image of any other type is saved to disk and Claude receives its file path as text instead.
Field| Type| Notes
---|---|---
@@ -522,7 +523,7 @@
Resources
-A resource block embeds a piece of content identified by a URI. The URI is a label for Claude to reference; the actual content rides in the block’s `text` or `blob` field. Use this when your tool produces something that makes sense to address by name later, such as a generated file or a record from an external system.
+A resource block embeds a piece of content identified by a URI. The actual content rides in the block’s `text` or `blob` field. Use this when your tool produces a generated file or a record from an external system.
Field| Type| Notes
---|---|---
@@ -532,7 +533,7 @@
`resource.blob`| `string`| The content base64-encoded, if it’s binary. TypeScript only: the Python SDK drops binary resources from the tool result and logs a warning
`resource.mimeType`| `string`| Optional
-This example shows a resource block returned from inside a tool handler. The URI `file:///tmp/report.md` is a label that Claude can reference later; the SDK does not read from that path.
+This example shows a resource block returned from inside a tool handler. The SDK doesn’t read from the example’s URI, `file:///tmp/report.md`.
TypeScript
@@ -556,7 +557,7 @@
{
"type": "resource",
"resource": {
- "uri": "file:///tmp/report.md", # Label for Claude to reference, not a path the SDK reads
+ "uri": "file:///tmp/report.md", # Not a path the SDK reads
"mimeType": "text/markdown",
"text": "# Report\n...", # The actual content, inline
},
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -6,7 +6,7 @@
Quick reference
-If you want to…| Do this
+What you want to do| Do this
---|---
Define a tool| Use [`@tool`](</docs/en/agent-sdk/python#tool>) (Python) or [`tool()`](</docs/en/agent-sdk/typescript#tool>) (TypeScript) with a name, description, schema, and handler. See Create a custom tool.
Register a tool with Claude| Wrap in `create_sdk_mcp_server` / `createSdkMcpServer` and pass to `mcpServers` in `query()`. See Call a custom tool.
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -130,7 +130,7 @@
Call a custom tool
-Pass the MCP server you created to `query` via the `mcpServers` option. The key in `mcpServers` becomes the `{server_name}` segment in each tool’s fully qualified name: `mcp__{server_name}__{tool_name}`. List that name in `allowedTools` so the tool runs without a permission prompt. These snippets reuse the `weatherServer` from the example above to ask Claude what the weather is in a specific location.
+Pass the MCP server you created to `query` via the `mcpServers` option. The key in `mcpServers` becomes the `{server_name}` segment in each tool’s fully qualified name: `mcp__{server_name}__{tool_name}`. List that name in `allowedTools` so the tool runs without a permission prompt. These snippets reuse the `weatherServer` from the weather tool example to ask Claude what the weather is in a specific location.
Python
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -328,7 +328,7 @@
`tools: ["Read", "Grep"]`| Availability| Only the listed built-ins are in Claude’s context. Unlisted built-ins are removed. MCP tools are unaffected.
`tools: []`| Availability| All built-ins are removed. Claude can only use your MCP tools.
allowed tools| Permission| Listed tools run without a permission prompt. Other unlisted tools remain available; calls go through the [permission flow](</docs/en/agent-sdk/permissions>).
-disallowed tools| Both| A bare tool name such as `"Bash"` removes the tool from Claude’s context, the same as omitting it from `tools`. A scoped rule such as `"Bash(rm *)"` leaves the tool in context and denies only matching calls.
+disallowed tools| Both| A bare tool name such as `"Bash"` removes the tool from Claude’s context, the same as omitting it from `tools`. A scoped rule such as `"Bash(rm *)"` leaves the tool in context and denies only calls that match [as written](</docs/en/permissions#bash-rule-limits>).
To remove a built-in entirely, omit it from `tools` or list its bare name in `disallowedTools` (Python: `disallowed_tools`); both keep the tool out of context so Claude never attempts it. A scoped `disallowedTools` rule blocks matching calls but leaves the tool visible, so Claude may waste a turn trying it. See [Configure permissions](</docs/en/agent-sdk/permissions>) for the full evaluation order.
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -446,7 +446,7 @@
Return images and resources
-The `content` array in a tool result accepts `text`, `image`, `audio`, `resource`, and `resource_link` blocks. You can mix them in the same response. In TypeScript, the SDK saves audio blocks to disk and Claude receives a text block with the saved file path; in Python, the SDK drops audio blocks from the tool result and logs a warning. The SDK converts resource link blocks to a text block containing the link’s name, URI, and description.
+The `content` array in a tool result accepts `text`, `image`, `audio`, `resource`, and `resource_link` blocks. You can mix them in the same response. In TypeScript, the SDK saves audio blocks to disk and Claude receives a text block with the saved file path; in Python, the SDK drops audio blocks from the tool result and logs a warning. Claude receives each resource link block as a text block containing the link’s name, URI, and description. In TypeScript, your application also receives the links themselves as [`resourceLinks`](</docs/en/agent-sdk/typescript#sdkmcpresourcelink>) on the user message’s `tool_use_result`; in Python, the SDK flattens them to text before the CLI sees the result, so the Python [`resourceLinks` key](</docs/en/agent-sdk/python#usermessage>) is never produced for in-process tools.
###
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -847,14 +847,3 @@
* If your server grows to dozens of tools, see [tool search](</docs/en/agent-sdk/tool-search>) to defer loading them until Claude needs them.
* To connect to external MCP servers (filesystem, GitHub, Slack) instead of building your own, see [Connect MCP servers](</docs/en/agent-sdk/mcp>).
* To control which tools run automatically versus requiring approval, see [Configure permissions](</docs/en/agent-sdk/permissions>).
-
-##
-
-
-
-Related documentation
-
- * [TypeScript SDK Reference](</docs/en/agent-sdk/typescript>)
- * [Python SDK Reference](</docs/en/agent-sdk/python>)
- * [MCP Documentation](<https://modelcontextprotocol.io>)
- * [SDK Overview](</docs/en/agent-sdk/overview>)
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -321,13 +321,13 @@
Configure allowed tools
-The `tools` option and the allowed/disallowed lists affect two layers: availability, which controls whether a tool appears in Claude’s context, and permission, which controls whether a call is approved once Claude attempts it. `tools` and bare-name `disallowedTools` entries change availability. `allowedTools` and scoped `disallowedTools` rules change permission only.
+The `tools` option and the allowed/disallowed lists affect two layers: availability, which controls whether a tool appears in Claude’s context, and permission, which controls whether a call is approved once Claude attempts it. `tools` and bare-name `disallowedTools` entries change availability. `allowedTools` and scoped `disallowedTools` rules change permission. If you name one of the [task-tracking tools](</docs/en/agent-sdk/todo-tracking#model-availability>) in `allowedTools`, Claude Code also opts the session in.
Option| Layer| Effect
---|---|---
`tools: ["Read", "Grep"]`| Availability| Only the listed built-ins are in Claude’s context. Unlisted built-ins are removed. MCP tools are unaffected.
`tools: []`| Availability| All built-ins are removed. Claude can only use your MCP tools.
-allowed tools| Permission| Listed tools run without a permission prompt. Unlisted tools remain available; calls go through the [permission flow](</docs/en/agent-sdk/permissions>).
+allowed tools| Permission| Listed tools run without a permission prompt. Other unlisted tools remain available; calls go through the [permission flow](</docs/en/agent-sdk/permissions>).
disallowed tools| Both| A bare tool name such as `"Bash"` removes the tool from Claude’s context, the same as omitting it from `tools`. A scoped rule such as `"Bash(rm *)"` leaves the tool in context and denies only matching calls.
To remove a built-in entirely, omit it from `tools` or list its bare name in `disallowedTools` (Python: `disallowed_tools`); both keep the tool out of context so Claude never attempts it. A scoped `disallowedTools` rule blocks matching calls but leaves the tool visible, so Claude may waste a turn trying it. See [Configure permissions](</docs/en/agent-sdk/permissions>) for the full evaluation order.
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -313,18 +313,7 @@
Control tool access
-The weather tool example registered a server and listed tools in `allowedTools`. This section covers how tool names are constructed and how to scope access when you have multiple tools or want to restrict built-ins.
-
-###
-
-
-
-Tool name format
-
-When MCP tools are exposed to Claude, their names follow a specific format:
-
- * Pattern: `mcp__{server_name}__{tool_name}`
- * Example: A tool named `get_temperature` in server `weather` becomes `mcp__weather__get_temperature`
+The weather tool example registered a server and listed tools in `allowedTools`. This section covers how to scope access when you have multiple tools or want to restrict built-ins. For how tool names are constructed, see Call a custom tool.
###
@@ -853,7 +842,7 @@
Next steps
-Custom tools wrap async functions in a standard interface. You can mix the patterns on this page in the same server: a single server can hold a database tool, an API gateway tool, and an image renderer alongside each other. From here:
+You can mix the patterns on this page in the same server: a single server can hold a database tool, an API gateway tool, and an image renderer alongside each other. From here:
* If your server grows to dozens of tools, see [tool search](</docs/en/agent-sdk/tool-search>) to defer loading them until Claude needs them.
* To connect to external MCP servers (filesystem, GitHub, Slack) instead of building your own, see [Connect MCP servers](</docs/en/agent-sdk/mcp>).
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -1,4 +1,4 @@
-Custom tools extend the Agent SDK by letting you define your own functions that Claude can call during a conversation. Using the SDK’s in-process MCP server, you can give Claude access to databases, external APIs, domain-specific logic, or any other capability your application needs. This guide covers how to define tools with input schemas and handlers, bundle them into an MCP server, pass them to `query`, and control which tools Claude can access. It also covers error handling, tool annotations, and returning non-text content like images.
+Custom tools extend the Agent SDK by letting you define your own functions that Claude can call during a conversation. Using the SDK’s in-process MCP server, you can give Claude access to databases, external APIs, domain-specific logic, or any other capability your application needs.
##
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -367,8 +367,6 @@
from typing import Any
from claude_agent_sdk import tool
- from claude_agent_sdk import tool
-
@tool(
"fetch_data",
"Fetch data from an API",
@@ -481,8 +479,6 @@
import base64
import httpx
- from claude_agent_sdk import tool
-
from claude_agent_sdk import tool
# Define a tool that fetches an image from a URL and returns it to Claude
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -459,7 +459,7 @@
Return images and resources
-The `content` array in a tool result accepts `text`, `image`, `audio`, `resource`, and `resource_link` blocks. You can mix them in the same response. In TypeScript, audio blocks are saved to disk and Claude receives a text block with the saved file path; in Python, the SDK drops audio blocks from the tool result and logs a warning. Resource link blocks are converted to a text block containing the link’s name, URI, and description.
+The `content` array in a tool result accepts `text`, `image`, `audio`, `resource`, and `resource_link` blocks. You can mix them in the same response. In TypeScript, the SDK saves audio blocks to disk and Claude receives a text block with the saved file path; in Python, the SDK drops audio blocks from the tool result and logs a warning. The SDK converts resource link blocks to a text block containing the link’s name, URI, and description.
###
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -170,13 +170,15 @@
}
}
+Combine this snippet with the tool and server definitions from the weather tool example in one file, then run it with `python weather.py` for Python or `npx tsx weather.ts` for TypeScript. Claude calls `get_temperature` and the script prints a one-line answer with the current temperature in San Francisco.
+
###
Add more tools
-A server holds as many tools as you list in its `tools` array. With more than one tool on a server, you can list each one in `allowedTools` individually or use the wildcard `mcp__weather__*` to cover every tool the server exposes. The example below adds a second tool, `get_precipitation_chance`, to the `weatherServer` from the weather tool example and rebuilds it with both tools in the array.
+A server holds as many tools as you list in its `tools` array. With more than one tool on a server, you can list each one in `allowedTools` individually or use the wildcard `mcp__weather__*` to cover every tool the server exposes. The example below defines a second tool, `get_precipitation_chance`, and replaces the `weatherServer` definition from the weather tool example with one that lists both tools in the array.
Python
@@ -256,7 +258,7 @@
tools: [getTemperature, getPrecipitationChance]
});
-Every tool in this array consumes context window space on every turn. If you’re defining dozens of tools, see [tool search](</docs/en/agent-sdk/tool-search>) to load them on demand instead.
+[Tool search](</docs/en/agent-sdk/tool-search>) is on by default and defers SDK MCP tools: Claude sees each tool’s name in a compact list and loads its full schema on demand. With tool search disabled, every tool in this array consumes context window space on every turn. In TypeScript, pass `alwaysLoad: true` in the `extras` argument of [`tool()`](</docs/en/agent-sdk/typescript#tool>) or in the options of [`createSdkMcpServer()`](</docs/en/agent-sdk/typescript#createsdkmcpserver>) to keep a tool’s full schema in the initial prompt.
###
@@ -363,6 +365,7 @@
import json
import httpx
from typing import Any
+ from claude_agent_sdk import tool
from claude_agent_sdk import tool
@@ -478,6 +481,7 @@
import base64
import httpx
+ from claude_agent_sdk import tool
from claude_agent_sdk import tool
@@ -768,7 +772,7 @@
tools: [convert]
});
-Once the server is defined, pass it to `query` the same way as the weather example. This example sends three different prompts in a loop to show the same tool handling different unit types. For each response, it inspects `AssistantMessage` objects (which contain the tool calls Claude made during that turn) and prints each `ToolUseBlock` before printing the final `ResultMessage` text. This lets you see when Claude is using the tool versus answering from its own knowledge.
+Once the server is defined, pass it to `query` the same way as the weather example. This example sends three different prompts in a loop to show the same tool handling different unit types. For each response, it inspects `AssistantMessage` objects (which contain the tool calls Claude made during that turn) and prints each `ToolUseBlock` before printing the final `ResultMessage` text. This lets you see when Claude is using the tool versus answering from its own knowledge. Because [tool search](</docs/en/agent-sdk/tool-search>) is on by default, the output may also include a `ToolSearch` call as Claude loads the deferred tool schema.
Python
Show diff (130 lines)
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -292,6 +292,9 @@
async def get_temperature(args):
return {"content": [{"type": "text", "text": "..."}]}
+ import { tool } from "@anthropic-ai/claude-agent-sdk";
+ import { z } from "zod";
+
tool(
"get_temperature",
"Get the current temperature at a location",
@@ -360,6 +363,8 @@
import json
import httpx
from typing import Any
+
+ from claude_agent_sdk import tool
@tool(
"fetch_data",
@@ -393,6 +398,9 @@
"is_error": True,
}
+ import { tool } from "@anthropic-ai/claude-agent-sdk";
+ import { z } from "zod";
+
tool(
"fetch_data",
"Fetch data from an API",
@@ -471,6 +479,8 @@
import base64
import httpx
+ from claude_agent_sdk import tool
+
# Define a tool that fetches an image from a URL and returns it to Claude
@tool("fetch_image", "Fetch an image from a URL and return it to Claude", {"url": str})
async def fetch_image(args):
@@ -491,6 +501,9 @@
]
}
+ import { tool } from "@anthropic-ai/claude-agent-sdk";
+ import { z } from "zod";
+
tool(
"fetch_image",
"Fetch an image from a URL and return it to Claude",
@@ -570,7 +583,7 @@
Return structured data
-`structuredContent` is an optional JSON object on the result, separate from the `content` array. Use it to return raw values that Claude can read as exact fields instead of parsing them out of a text string or image. When `structuredContent` is set, Claude receives the JSON plus any image or resource blocks from `content`. Text blocks in `content` are not forwarded, since they are assumed to duplicate the structured data. The example below renders a chart as an image block and returns the data points behind it in `structuredContent` from the same handler.
+`structuredContent` is an optional JSON object on the result, separate from the `content` array. Use it to return raw values that Claude can read as exact fields instead of parsing them out of a text string or image. When `structuredContent` is set, Claude receives the JSON plus any image or resource blocks from `content`. Text blocks in `content` are not forwarded, since they are assumed to duplicate the structured data. The example below renders a chart as an image block and returns the data points behind it in `structuredContent` from the same handler. In the snippet, `chartPngBuffer` is a `Buffer` holding the rendered PNG bytes.
TypeScript
@@ -783,13 +796,19 @@
]
for prompt in prompts:
- async for message in query(prompt=prompt, options=options):
- if isinstance(message, AssistantMessage):
- for block in message.content:
- if isinstance(block, ToolUseBlock):
- print(f"[tool call] {block.name}({block.input})")
- elif isinstance(message, ResultMessage) and message.subtype == "success":
- print(f"Q: {prompt}\nA: {message.result}\n")
+ try:
+ async for message in query(prompt=prompt, options=options):
+ if isinstance(message, AssistantMessage):
+ for block in message.content:
+ if isinstance(block, ToolUseBlock):
+ print(f"[tool call] {block.name}({block.input})")
+ elif isinstance(message, ResultMessage) and message.subtype == "success":
+ print(f"Q: {prompt}\nA: {message.result}\n")
+ except Exception as error:
+ # A single-shot query() raises after yielding an error result. Only success
+ # results are printed above, so handle the failure here and continue with
+ # the next prompt.
+ print(f"Call failed: {error}")
asyncio.run(main())
@@ -802,22 +821,29 @@
];
for (const prompt of prompts) {
- for await (const message of query({
- prompt,
- options: {
- mcpServers: { converter: converterServer },
- allowedTools: ["mcp__converter__convert_units"]
- }
- })) {
- if (message.type === "assistant") {
- for (const block of message.message.content) {
- if (block.type === "tool_use") {
- console.log(`[tool call] ${block.name}`, block.input);
+ try {
+ for await (const message of query({
+ prompt,
+ options: {
+ mcpServers: { converter: converterServer },
+ allowedTools: ["mcp__converter__convert_units"]
+ }
+ })) {
+ if (message.type === "assistant") {
+ for (const block of message.message.content) {
+ if (block.type === "tool_use") {
+ console.log(`[tool call] ${block.name}`, block.input);
+ }
}
+ } else if (message.type === "result" && message.subtype === "success") {
+ console.log(`Q: ${prompt}\nA: ${message.result}\n`);
}
- } else if (message.type === "result" && message.subtype === "success") {
- console.log(`Q: ${prompt}\nA: ${message.result}\n`);
- }
+ }
+ } catch (error) {
+ // A single-shot query() throws after yielding an error result. Only success
+ // results are logged above, so handle the failure here and continue with
+ // the next prompt.
+ console.error(`Call failed: ${error}`);
}
}
Show diff (339 lines)
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -88,6 +88,38 @@
tools=[get_temperature],
)
+ import { tool, createSdkMcpServer } from "@anthropic-ai/claude-agent-sdk";
+ import { z } from "zod";
+
+ // Define a tool: name, description, input schema, handler
+ const getTemperature = tool(
+ "get_temperature",
+ "Get the current temperature at a location",
+ {
+ latitude: z.number().describe("Latitude coordinate"), // .describe() adds a field description Claude sees
+ longitude: z.number().describe("Longitude coordinate")
+ },
+ async (args) => {
+ // args is typed from the schema: { latitude: number; longitude: number }
+ const response = await fetch(
+ `https://api.open-meteo.com/v1/forecast?latitude=${args.latitude}&longitude=${args.longitude}¤t=temperature_2m&temperature_unit=fahrenheit`
+ );
+ const data: any = await response.json();
+
+ // Return a content array - Claude sees this as the tool result
+ return {
+ content: [{ type: "text", text: `Temperature: ${data.current.temperature_2m}°F` }]
+ };
+ }
+ );
+
+ // Wrap the tool in an in-process MCP server
+ const weatherServer = createSdkMcpServer({
+ name: "weather",
+ version: "1.0.0",
+ tools: [getTemperature]
+ });
+
See the [`tool()`](</docs/en/agent-sdk/typescript#tool>) TypeScript reference or the [`@tool`](</docs/en/agent-sdk/python#tool>) Python reference for full parameter details, including JSON Schema input formats and return value structure.
To make a parameter optional: in TypeScript, add `.default()` to the Zod field. In Python, the dict schema treats every key as required, so leave the parameter out of the schema, mention it in the description string, and read it with `args.get()` in the handler. The `get_precipitation_chance` tool below shows both patterns.
@@ -122,6 +154,21 @@
print(message.result)
asyncio.run(main())
+
+ import { query } from "@anthropic-ai/claude-agent-sdk";
+
+ for await (const message of query({
+ prompt: "What's the temperature in San Francisco?",
+ options: {
+ mcpServers: { weather: weatherServer },
+ allowedTools: ["mcp__weather__get_temperature"]
+ }
+ })) {
+ // "result" is the final message after all tool calls complete
+ if (message.type === "result" && message.subtype === "success") {
+ console.log(message.result);
+ }
+ }
###
@@ -174,6 +221,41 @@
tools=[get_temperature, get_precipitation_chance],
)
+ // Define a second tool for the same server
+ const getPrecipitationChance = tool(
+ "get_precipitation_chance",
+ "Get the hourly precipitation probability for a location",
+ {
+ latitude: z.number(),
+ longitude: z.number(),
+ hours: z
+ .number()
+ .int()
+ .min(1)
+ .max(24)
+ .default(12) // .default() makes the parameter optional
+ .describe("How many hours of forecast to return")
+ },
+ async (args) => {
+ const response = await fetch(
+ `https://api.open-meteo.com/v1/forecast?latitude=${args.latitude}&longitude=${args.longitude}&hourly=precipitation_probability&forecast_days=1`
+ );
+ const data: any = await response.json();
+ const chances = data.hourly.precipitation_probability.slice(0, args.hours);
+
+ return {
+ content: [{ type: "text", text: `Next ${args.hours} hours: ${chances.join("%, ")}%` }]
+ };
+ }
+ );
+
+ // Rebuild the server with both tools in the array
+ const weatherServer = createSdkMcpServer({
+ name: "weather",
+ version: "1.0.0",
+ tools: [getTemperature, getPrecipitationChance]
+ });
+
Every tool in this array consumes context window space on every turn. If you’re defining dozens of tools, see [tool search](</docs/en/agent-sdk/tool-search>) to load them on demand instead.
###
@@ -209,6 +291,14 @@
)
async def get_temperature(args):
return {"content": [{"type": "text", "text": "..."}]}
+
+ tool(
+ "get_temperature",
+ "Get the current temperature at a location",
+ { latitude: z.number(), longitude: z.number() },
+ async (args) => ({ content: [{ type: "text", text: `...` }] }),
+ { annotations: { readOnlyHint: true } } // Lets Claude batch this with other read-only calls
+ );
See `ToolAnnotations` in the [TypeScript](</docs/en/agent-sdk/typescript#toolannotations>) or [Python](</docs/en/agent-sdk/python#toolannotations>) reference.
@@ -303,6 +393,55 @@
"is_error": True,
}
+ tool(
+ "fetch_data",
+ "Fetch data from an API",
+ {
+ endpoint: z.string().url().describe("API endpoint URL")
+ },
+ async (args) => {
+ try {
+ const response = await fetch(args.endpoint);
+
+ if (!response.ok) {
+ // Return the failure as a tool result so Claude can react to it.
+ // isError marks this as a failed call rather than odd-looking data.
+ return {
+ content: [
+ {
+ type: "text",
+ text: `API error: ${response.status} ${response.statusText}`
+ }
+ ],
+ isError: true
+ };
+ }
+
+ const data = await response.json();
+ return {
+ content: [
+ {
+ type: "text",
+ text: JSON.stringify(data, null, 2)
+ }
+ ]
+ };
+ } catch (error) {
+ // Composes the message Claude reads. An uncaught throw would
+ // reach Claude as the raw error message with no context.
+ return {
+ content: [
+ {
+ type: "text",
+ text: `Failed to fetch data: ${error instanceof Error ? error.message : String(error)}`
+ }
+ ],
+ isError: true
+ };
+ }
+ }
+ );
+
##
@@ -352,6 +491,29 @@
]
}
+ tool(
+ "fetch_image",
+ "Fetch an image from a URL and return it to Claude",
+ {
+ url: z.string().url()
+ },
+ async (args) => {
+ const response = await fetch(args.url); // Fetch the image bytes
+ const buffer = Buffer.from(await response.arrayBuffer()); // Read into a Buffer for base64 encoding
+ const mimeType = response.headers.get("content-type") ?? "image/png";
+
+ return {
+ content: [
+ {
+ type: "image",
+ data: buffer.toString("base64"), // Base64-encode the raw bytes
+ mimeType
+ }
+ ]
+ };
+ }
+ );
+
###
@@ -386,6 +548,19 @@
}
]
};
+
+ return {
+ "content": [
+ {
+ "type": "resource",
+ "resource": {
+ "uri": "file:///tmp/report.md", # Label for Claude to reference, not a path the SDK reads
+ "mimeType": "text/markdown",
+ "text": "# Report\n...", # The actual content, inline
+ },
+ }
+ ]
+ }
These block shapes come from the MCP `CallToolResult` type. See the [MCP specification](<https://modelcontextprotocol.io/specification/2025-06-18/server/tools#tool-result>) for the full definition.
@@ -509,6 +684,77 @@
tools=[convert_units],
)
+ import { tool, createSdkMcpServer } from "@anthropic-ai/claude-agent-sdk";
+ import { z } from "zod";
+
+ const convert = tool(
+ "convert_units",
+ "Convert a value from one unit to another",
+ {
+ unit_type: z.enum(["length", "temperature", "weight"]).describe("Category of unit"),
+ from_unit: z
+ .string()
+ .describe("Unit to convert from, e.g. kilometers, fahrenheit, pounds"),
+ to_unit: z.string().describe("Unit to convert to"),
+ value: z.number().describe("Value to convert")
+ },
+ async (args) => {
+ type Conversions = Record<string, Record<string, (v: number) => number>>;
+
+ const conversions: Conversions = {
+ length: {
+ kilometers_to_miles: (v) => v * 0.621371,
+ miles_to_kilometers: (v) => v * 1.60934,
+ meters_to_feet: (v) => v * 3.28084,
+ feet_to_meters: (v) => v * 0.3048
+ },
+ temperature: {
+ celsius_to_fahrenheit: (v) => (v * 9) / 5 + 32,
+ fahrenheit_to_celsius: (v) => ((v - 32) * 5) / 9,
+ celsius_to_kelvin: (v) => v + 273.15,
+ kelvin_to_celsius: (v) => v - 273.15
+ },
+ weight: {
+ kilograms_to_pounds: (v) => v * 2.20462,
+ pounds_to_kilograms: (v) => v * 0.453592,
+ grams_to_ounces: (v) => v * 0.035274,
+ ounces_to_grams: (v) => v * 28.3495
+ }
+ };
+
+ const key = `${args.from_unit}_to_${args.to_unit}`;
+ const fn = conversions[args.unit_type]?.[key];
+
+ if (!fn) {
+ return {
+ content: [
+ {
+ type: "text",
+ text: `Unsupported conversion: ${args.from_unit} to ${args.to_unit}`
+ }
+ ],
+ isError: true
+ };
+ }
+
+ const result = fn(args.value);
+ return {
+ content: [
+ {
+ type: "text",
+ text: `${args.value} ${args.from_unit} = ${result.toFixed(4)} ${args.to_unit}`
+ }
+ ]
+ };
+ }
+ );
+
+ const converterServer = createSdkMcpServer({
+ name: "converter",
+ version: "1.0.0",
+ tools: [convert]
+ });
+
Once the server is defined, pass it to `query` the same way as the weather example. This example sends three different prompts in a loop to show the same tool handling different unit types. For each response, it inspects `AssistantMessage` objects (which contain the tool calls Claude made during that turn) and prints each `ToolUseBlock` before printing the final `ResultMessage` text. This lets you see when Claude is using the tool versus answering from its own knowledge.
Python
@@ -547,6 +793,34 @@
asyncio.run(main())
+ import { query } from "@anthropic-ai/claude-agent-sdk";
+
+ const prompts = [
+ "Convert 100 kilometers to miles.",
+ "What is 72°F in Celsius?",
+ "How many pounds is 5 kilograms?"
+ ];
+
+ for (const prompt of prompts) {
+ for await (const message of query({
+ prompt,
+ options: {
+ mcpServers: { converter: converterServer },
+ allowedTools: ["mcp__converter__convert_units"]
+ }
+ })) {
+ if (message.type === "assistant") {
+ for (const block of message.message.content) {
+ if (block.type === "tool_use") {
+ console.log(`[tool call] ${block.name}`, block.input);
+ }
+ }
+ } else if (message.type === "result" && message.subtype === "success") {
+ console.log(`Q: ${prompt}\nA: ${message.result}\n`);
+ }
+ }
+ }
+
##
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -13,7 +13,7 @@
Pre-approve a tool| Add to your allowed tools. See Configure allowed tools.
Remove a built-in tool from Claude’s context| Pass a `tools` array listing only the built-ins you want. See Configure allowed tools.
Let Claude call tools in parallel| Set `readOnlyHint: true` on tools with no side effects. See Add tool annotations.
-Handle errors without stopping the loop| Return `isError: true` instead of throwing. See Handle errors.
+Control the error message Claude reads| Return `isError: true` to compose the message instead of surfacing the raw exception. See Handle errors.
Return images or files| Use `image` or `resource` blocks in the content array. See Return images and resources.
Return a machine-readable JSON result| Set `structuredContent` on the result. See Return structured data.
Scale to many tools| Use [tool search](</docs/en/agent-sdk/tool-search>) to load tools on demand.
@@ -254,14 +254,14 @@
Handle errors
-How your handler reports errors determines whether the agent loop continues or stops:
+A handler error doesn’t stop the agent loop. The SDK’s in-process MCP server catches uncaught exceptions and returns them as error results, so how you report an error determines what Claude reads, not whether the query fails:
What happens| Result
---|---
-Handler throws an uncaught exception| Agent loop stops. Claude never sees the error, and the `query` call fails.
-Handler catches the error and returns `isError: true` (TS) / `"is_error": True` (Python)| Agent loop continues. Claude sees the error as data and can retry, try a different tool, or explain the failure.
-
-The example below catches two kinds of failures inside the handler instead of letting them throw. A non-200 HTTP status is caught from the response and returned as an error result. A network error or invalid JSON is caught by the surrounding `try/except` (Python) or `try/catch` (TypeScript) and also returned as an error result. In both cases the handler returns normally and the agent loop continues.
+Handler throws an uncaught exception| The MCP server converts it to an error result carrying the raw exception message. Claude sees that message, and the agent loop continues.
+Handler catches the error and returns `isError: true` (TS) / `"is_error": True` (Python)| Claude sees the message you compose. You can add context the raw exception lacks, such as which request failed or what to try instead.
+
+In both cases Claude can retry, try a different tool, or explain the failure. Catch errors yourself when the raw exception message isn’t enough for Claude to act on. The example below catches two kinds of failures inside the handler and composes the error message Claude reads. A non-200 HTTP status is caught from the response and returned as an error result. A network error or invalid JSON is caught by the surrounding `try/except` (Python) or `try/catch` (TypeScript) and also returned as an error result. In both cases Claude receives a message that describes the failure instead of a bare exception string.
Python
@@ -296,8 +296,8 @@
data = response.json()
return {"content": [{"type": "text", "text": json.dumps(data, indent=2)}]}
except Exception as e:
- # Catching here keeps the agent loop alive. An uncaught exception
- # would end the whole query() call.
+ # Composes the message Claude reads. An uncaught exception would
+ # reach Claude as the raw str(e) with no context.
return {
"content": [{"type": "text", "text": f"Failed to fetch data: {str(e)}"}],
"is_error": True,
@@ -309,7 +309,7 @@
Return images and resources
-The `content` array in a tool result accepts `text`, `image`, `audio`, `resource`, and `resource_link` blocks. You can mix them in the same response. Audio blocks are saved to disk and Claude receives a text block with the saved file path. Resource link blocks are converted to a text block containing the link’s name, URI, and description.
+The `content` array in a tool result accepts `text`, `image`, `audio`, `resource`, and `resource_link` blocks. You can mix them in the same response. In TypeScript, audio blocks are saved to disk and Claude receives a text block with the saved file path; in Python, the SDK drops audio blocks from the tool result and logs a warning. Resource link blocks are converted to a text block containing the link’s name, URI, and description.
###
@@ -365,7 +365,7 @@
`type`| `"resource"`|
`resource.uri`| `string`| Identifier for the content. Any URI scheme
`resource.text`| `string`| The content, if it’s text. Provide this or `blob`, not both
-`resource.blob`| `string`| The content base64-encoded, if it’s binary
+`resource.blob`| `string`| The content base64-encoded, if it’s binary. TypeScript only: the Python SDK drops binary resources from the tool result and logs a warning
`resource.mimeType`| `string`| Optional
This example shows a resource block returned from inside a tool handler. The URI `file:///tmp/report.md` is a label that Claude can reference later; the SDK does not read from that path.
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -30,7 +30,7 @@
* **Description:** what the tool does. Claude reads this to decide when to call it.
* **Input schema:** the arguments Claude must provide. In TypeScript this is always a [Zod schema](<https://zod.dev/>), and the handler’s `args` are typed from it automatically. In Python this is a dict mapping names to types, like `{"latitude": float}`, which the SDK converts to JSON Schema for you. The Python decorator also accepts a full [JSON Schema](<https://json-schema.org/understanding-json-schema/about>) dict directly when you need enums, ranges, optional fields, or nested objects.
* **Handler:** the async function that runs when Claude calls the tool. It receives the validated arguments and must return an object with:
- * `content` (required): an array of result blocks, each with a `type` of `"text"`, `"image"`, or `"resource"`. See Return images and resources for non-text blocks.
+ * `content` (required): an array of result blocks, each with a `type` of `"text"`, `"image"`, `"audio"`, `"resource"`, or `"resource_link"`. See Return images and resources for non-text blocks.
* `structuredContent` (optional): a JSON object holding the result as machine-readable data, returned alongside `content`. See Return structured data.
* `isError` (optional): set to `true` to signal a tool failure so Claude can react to it. See Handle errors.
@@ -309,7 +309,7 @@
Return images and resources
-The `content` array in a tool result accepts `text`, `image`, and `resource` blocks. You can mix them in the same response.
+The `content` array in a tool result accepts `text`, `image`, `audio`, `resource`, and `resource_link` blocks. You can mix them in the same response. Audio blocks are saved to disk and Claude receives a text block with the saved file path. Resource link blocks are converted to a text block containing the link’s name, URI, and description.
###
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -1,9 +1,3 @@
-> ## Documentation Index
->
-> Fetch the complete documentation index at: <https://code.claude.com/docs/llms.txt>
->
-> Use this file to discover all available pages before exploring further.
-
Custom tools extend the Agent SDK by letting you define your own functions that Claude can call during a conversation. Using the SDK’s in-process MCP server, you can give Claude access to databases, external APIs, domain-specific logic, or any other capability your application needs. This guide covers how to define tools with input schemas and handlers, bundle them into an MCP server, pass them to `query`, and control which tools Claude can access. It also covers error handling, tool annotations, and returning non-text content like images.
##
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -243,16 +243,16 @@
Configure allowed tools
-The `tools` option and the allowed/disallowed lists operate on separate layers. `tools` controls which built-in tools appear in Claude’s context. Allowed and disallowed tool lists control whether calls are approved or denied once Claude attempts them.
+The `tools` option and the allowed/disallowed lists affect two layers: availability, which controls whether a tool appears in Claude’s context, and permission, which controls whether a call is approved once Claude attempts it. `tools` and bare-name `disallowedTools` entries change availability. `allowedTools` and scoped `disallowedTools` rules change permission only.
Option| Layer| Effect
---|---|---
`tools: ["Read", "Grep"]`| Availability| Only the listed built-ins are in Claude’s context. Unlisted built-ins are removed. MCP tools are unaffected.
`tools: []`| Availability| All built-ins are removed. Claude can only use your MCP tools.
allowed tools| Permission| Listed tools run without a permission prompt. Unlisted tools remain available; calls go through the [permission flow](</docs/en/agent-sdk/permissions>).
-disallowed tools| Permission| Every call to a listed tool is denied. The tool stays in Claude’s context, so Claude may still attempt it before the call is rejected.
-
-To limit which built-ins Claude can use, prefer `tools` over disallowed tools. Omitting a tool from `tools` removes it from context so Claude never attempts it; listing it in `disallowedTools` (Python: `disallowed_tools`) blocks the call but leaves the tool visible, so Claude may waste a turn trying it. See [Configure permissions](</docs/en/agent-sdk/permissions>) for the full evaluation order.
+disallowed tools| Both| A bare tool name such as `"Bash"` removes the tool from Claude’s context, the same as omitting it from `tools`. A scoped rule such as `"Bash(rm *)"` leaves the tool in context and denies only matching calls.
+
+To remove a built-in entirely, omit it from `tools` or list its bare name in `disallowedTools` (Python: `disallowed_tools`); both keep the tool out of context so Claude never attempts it. A scoped `disallowedTools` rule blocks matching calls but leaves the tool visible, so Claude may waste a turn trying it. See [Configure permissions](</docs/en/agent-sdk/permissions>) for the full evaluation order.
##
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -21,6 +21,7 @@
Let Claude call tools in parallel| Set `readOnlyHint: true` on tools with no side effects. See Add tool annotations.
Handle errors without stopping the loop| Return `isError: true` instead of throwing. See Handle errors.
Return images or files| Use `image` or `resource` blocks in the content array. See Return images and resources.
+Return a machine-readable JSON result| Set `structuredContent` on the result. See Return structured data.
Scale to many tools| Use [tool search](</docs/en/agent-sdk/tool-search>) to load tools on demand.
##
@@ -36,6 +37,7 @@
* **Input schema:** the arguments Claude must provide. In TypeScript this is always a [Zod schema](<https://zod.dev/>), and the handler’s `args` are typed from it automatically. In Python this is a dict mapping names to types, like `{"latitude": float}`, which the SDK converts to JSON Schema for you. The Python decorator also accepts a full [JSON Schema](<https://json-schema.org/understanding-json-schema/about>) dict directly when you need enums, ranges, optional fields, or nested objects.
* **Handler:** the async function that runs when Claude calls the tool. It receives the validated arguments and must return an object with:
* `content` (required): an array of result blocks, each with a `type` of `"text"`, `"image"`, or `"resource"`. See Return images and resources for non-text blocks.
+ * `structuredContent` (optional): a JSON object holding the result as machine-readable data, returned alongside `content`. See Return structured data.
* `isError` (optional): set to `true` to signal a tool failure so Claude can react to it. See Handle errors.
After defining a tool, wrap it in a server with [`createSdkMcpServer`](</docs/en/agent-sdk/typescript#createsdkmcpserver>) (TypeScript) or [`create_sdk_mcp_server`](</docs/en/agent-sdk/python#create_sdk_mcp_server>) (Python). The server runs in-process inside your application, not as a separate process.
@@ -392,6 +394,33 @@
};
These block shapes come from the MCP `CallToolResult` type. See the [MCP specification](<https://modelcontextprotocol.io/specification/2025-06-18/server/tools#tool-result>) for the full definition.
+
+##
+
+
+
+Return structured data
+
+`structuredContent` is an optional JSON object on the result, separate from the `content` array. Use it to return raw values that Claude can read as exact fields instead of parsing them out of a text string or image. When `structuredContent` is set, Claude receives the JSON plus any image or resource blocks from `content`. Text blocks in `content` are not forwarded, since they are assumed to duplicate the structured data. The example below renders a chart as an image block and returns the data points behind it in `structuredContent` from the same handler.
+
+TypeScript
+
+ return {
+ content: [
+ {
+ type: "image",
+ data: chartPngBuffer.toString("base64"),
+ mimeType: "image/png"
+ }
+ ],
+ structuredContent: {
+ series: "temperature_2m",
+ unit: "fahrenheit",
+ points: [62.1, 63.4, 65.0, 64.2]
+ }
+ };
+
+The Python `@tool` decorator forwards only `content` and `is_error` from the handler’s return dict. To return `structuredContent` from Python, run a [standalone MCP server](</docs/en/agent-sdk/mcp>) instead of an in-process SDK server.
##
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -38,7 +38,7 @@
* `content` (required): an array of result blocks, each with a `type` of `"text"`, `"image"`, or `"resource"`. See Return images and resources for non-text blocks.
* `isError` (optional): set to `true` to signal a tool failure so Claude can react to it. See Handle errors.
-After defining a tool, wrap it in a server with [`createSdkMcpServer`](</docs/en/agent-sdk/typescript#create-sdk-mcp-server>) (TypeScript) or [`create_sdk_mcp_server`](</docs/en/agent-sdk/python#create-sdk-mcp-server>) (Python). The server runs in-process inside your application, not as a separate process.
+After defining a tool, wrap it in a server with [`createSdkMcpServer`](</docs/en/agent-sdk/typescript#createsdkmcpserver>) (TypeScript) or [`create_sdk_mcp_server`](</docs/en/agent-sdk/python#create_sdk_mcp_server>) (Python). The server runs in-process inside your application, not as a separate process.
###
@@ -214,7 +214,7 @@
async def get_temperature(args):
return {"content": [{"type": "text", "text": "..."}]}
-See `ToolAnnotations` in the [TypeScript](</docs/en/agent-sdk/typescript#tool-annotations>) or [Python](</docs/en/agent-sdk/python#tool-annotations>) reference.
+See `ToolAnnotations` in the [TypeScript](</docs/en/agent-sdk/typescript#toolannotations>) or [Python](</docs/en/agent-sdk/python#toolannotations>) reference.
##
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -1,3 +1,9 @@
+> ## Documentation Index
+>
+> Fetch the complete documentation index at: <https://code.claude.com/docs/llms.txt>
+>
+> Use this file to discover all available pages before exploring further.
+
Custom tools extend the Agent SDK by letting you define your own functions that Claude can call during a conversation. Using the SDK’s in-process MCP server, you can give Claude access to databases, external APIs, domain-specific logic, or any other capability your application needs. This guide covers how to define tools with input schemas and handlers, bundle them into an MCP server, pass them to `query`, and control which tools Claude can access. It also covers error handling, tool annotations, and returning non-text content like images.
##
Show diff (545 lines)
--- a/custom-tools.md
+++ b/custom-tools.md
@@ -0,0 +1,542 @@
+Custom tools extend the Agent SDK by letting you define your own functions that Claude can call during a conversation. Using the SDK’s in-process MCP server, you can give Claude access to databases, external APIs, domain-specific logic, or any other capability your application needs. This guide covers how to define tools with input schemas and handlers, bundle them into an MCP server, pass them to `query`, and control which tools Claude can access. It also covers error handling, tool annotations, and returning non-text content like images.
+
+##
+
+
+
+Quick reference
+
+If you want to…| Do this
+---|---
+Define a tool| Use [`@tool`](</docs/en/agent-sdk/python#tool>) (Python) or [`tool()`](</docs/en/agent-sdk/typescript#tool>) (TypeScript) with a name, description, schema, and handler. See Create a custom tool.
+Register a tool with Claude| Wrap in `create_sdk_mcp_server` / `createSdkMcpServer` and pass to `mcpServers` in `query()`. See Call a custom tool.
+Pre-approve a tool| Add to your allowed tools. See Configure allowed tools.
+Remove a built-in tool from Claude’s context| Pass a `tools` array listing only the built-ins you want. See Configure allowed tools.
+Let Claude call tools in parallel| Set `readOnlyHint: true` on tools with no side effects. See Add tool annotations.
+Handle errors without stopping the loop| Return `isError: true` instead of throwing. See Handle errors.
+Return images or files| Use `image` or `resource` blocks in the content array. See Return images and resources.
+Scale to many tools| Use [tool search](</docs/en/agent-sdk/tool-search>) to load tools on demand.
+
+##
+
+
+
+Create a custom tool
+
+A tool is defined by four parts, passed as arguments to the [`tool()`](</docs/en/agent-sdk/typescript#tool>) helper in TypeScript or the [`@tool`](</docs/en/agent-sdk/python#tool>) decorator in Python:
+
+ * **Name:** a unique identifier Claude uses to call the tool.
+ * **Description:** what the tool does. Claude reads this to decide when to call it.
+ * **Input schema:** the arguments Claude must provide. In TypeScript this is always a [Zod schema](<https://zod.dev/>), and the handler’s `args` are typed from it automatically. In Python this is a dict mapping names to types, like `{"latitude": float}`, which the SDK converts to JSON Schema for you. The Python decorator also accepts a full [JSON Schema](<https://json-schema.org/understanding-json-schema/about>) dict directly when you need enums, ranges, optional fields, or nested objects.
+ * **Handler:** the async function that runs when Claude calls the tool. It receives the validated arguments and must return an object with:
+ * `content` (required): an array of result blocks, each with a `type` of `"text"`, `"image"`, or `"resource"`. See Return images and resources for non-text blocks.
+ * `isError` (optional): set to `true` to signal a tool failure so Claude can react to it. See Handle errors.
+
+After defining a tool, wrap it in a server with [`createSdkMcpServer`](</docs/en/agent-sdk/typescript#create-sdk-mcp-server>) (TypeScript) or [`create_sdk_mcp_server`](</docs/en/agent-sdk/python#create-sdk-mcp-server>) (Python). The server runs in-process inside your application, not as a separate process.
+
+###
+
+
+
+Weather tool example
+
+This example defines a `get_temperature` tool and wraps it in an MCP server. It only sets up the tool; to pass it to `query` and run it, see Call a custom tool below.
+
+Python
+
+TypeScript
+
+ from typing import Any
+ import httpx
+ from claude_agent_sdk import tool, create_sdk_mcp_server
+
+ # Define a tool: name, description, input schema, handler
+ @tool(
+ "get_temperature",
+ "Get the current temperature at a location",
+ {"latitude": float, "longitude": float},
+ )
+ async def get_temperature(args: dict[str, Any]) -> dict[str, Any]:
+ async with httpx.AsyncClient() as client:
+ response = await client.get(
+ "https://api.open-meteo.com/v1/forecast",
+ params={
+ "latitude": args["latitude"],
+ "longitude": args["longitude"],
+ "current": "temperature_2m",
+ "temperature_unit": "fahrenheit",
+ },
+ )
+ data = response.json()
+
+ # Return a content array - Claude sees this as the tool result
+ return {
+ "content": [
+ {
+ "type": "text",
+ "text": f"Temperature: {data['current']['temperature_2m']}°F",
+ }
+ ]
+ }
+
+ # Wrap the tool in an in-process MCP server
+ weather_server = create_sdk_mcp_server(
+ name="weather",
+ version="1.0.0",
+ tools=[get_temperature],
+ )
+
+See the [`tool()`](</docs/en/agent-sdk/typescript#tool>) TypeScript reference or the [`@tool`](</docs/en/agent-sdk/python#tool>) Python reference for full parameter details, including JSON Schema input formats and return value structure.
+
+To make a parameter optional: in TypeScript, add `.default()` to the Zod field. In Python, the dict schema treats every key as required, so leave the parameter out of the schema, mention it in the description string, and read it with `args.get()` in the handler. The `get_precipitation_chance` tool below shows both patterns.
+
+###
+
+
+
+Call a custom tool
+
+Pass the MCP server you created to `query` via the `mcpServers` option. The key in `mcpServers` becomes the `{server_name}` segment in each tool’s fully qualified name: `mcp__{server_name}__{tool_name}`. List that name in `allowedTools` so the tool runs without a permission prompt. These snippets reuse the `weatherServer` from the example above to ask Claude what the weather is in a specific location.
+
+Python
+
+TypeScript
+
+ import asyncio
+ from claude_agent_sdk import query, ClaudeAgentOptions, ResultMessage
+
+ async def main():
+ options = ClaudeAgentOptions(
+ mcp_servers={"weather": weather_server},
+ allowed_tools=["mcp__weather__get_temperature"],
+ )
+
+ async for message in query(
+ prompt="What's the temperature in San Francisco?",
+ options=options,
+ ):
+ # ResultMessage is the final message after all tool calls complete
+ if isinstance(message, ResultMessage) and message.subtype == "success":
+ print(message.result)
+
+ asyncio.run(main())
+
+###
+
+
+
+Add more tools
+
+A server holds as many tools as you list in its `tools` array. With more than one tool on a server, you can list each one in `allowedTools` individually or use the wildcard `mcp__weather__*` to cover every tool the server exposes. The example below adds a second tool, `get_precipitation_chance`, to the `weatherServer` from the weather tool example and rebuilds it with both tools in the array.
+
+Python
+
+TypeScript
+
+ # Define a second tool for the same server
+ @tool(
+ "get_precipitation_chance",
+ "Get the hourly precipitation probability for a location. "
+ "Optionally pass 'hours' (1-24) to control how many hours to return.",
+ {"latitude": float, "longitude": float},
+ )
+ async def get_precipitation_chance(args: dict[str, Any]) -> dict[str, Any]:
+ # 'hours' isn't in the schema - read it with .get() to make it optional
+ hours = args.get("hours", 12)
+ async with httpx.AsyncClient() as client:
+ response = await client.get(
+ "https://api.open-meteo.com/v1/forecast",
+ params={
+ "latitude": args["latitude"],
+ "longitude": args["longitude"],
+ "hourly": "precipitation_probability",
+ "forecast_days": 1,
+ },
+ )
+ data = response.json()
+ chances = data["hourly"]["precipitation_probability"][:hours]
+
+ return {
+ "content": [
+ {
+ "type": "text",
+ "text": f"Next {hours} hours: {'%, '.join(map(str, chances))}%",
+ }
+ ]
+ }
+
+ # Rebuild the server with both tools in the array
+ weather_server = create_sdk_mcp_server(
+ name="weather",
+ version="1.0.0",
+ tools=[get_temperature, get_precipitation_chance],
+ )
+
+Every tool in this array consumes context window space on every turn. If you’re defining dozens of tools, see [tool search](</docs/en/agent-sdk/tool-search>) to load them on demand instead.
+
+###
+
+
+
+Add tool annotations
+
+[Tool annotations](<https://modelcontextprotocol.io/docs/concepts/tools#tool-annotations>) are optional metadata describing how a tool behaves. Pass them as the fifth argument to `tool()` helper in TypeScript or via the `annotations` keyword argument for the `@tool` decorator in Python. All hint fields are Booleans.
+
+Field| Default| Meaning
+---|---|---
+`readOnlyHint`| `false`| Tool does not modify its environment. Controls whether the tool can be called in parallel with other read-only tools.
+`destructiveHint`| `true`| Tool may perform destructive updates. Informational only.
+`idempotentHint`| `false`| Repeated calls with the same arguments have no additional effect. Informational only.
+`openWorldHint`| `true`| Tool reaches systems outside your process. Informational only.
+
+Annotations are metadata, not enforcement. A tool marked `readOnlyHint: true` can still write to disk if that’s what the handler does. Keep the annotation accurate to the handler. This example adds `readOnlyHint` to the `get_temperature` tool from the weather tool example.
+
+Python
+
+TypeScript
+
+ from claude_agent_sdk import tool, ToolAnnotations
+
+ @tool(
+ "get_temperature",
+ "Get the current temperature at a location",
+ {"latitude": float, "longitude": float},
+ annotations=ToolAnnotations(
+ readOnlyHint=True
+ ), # Lets Claude batch this with other read-only calls
+ )
+ async def get_temperature(args):
+ return {"content": [{"type": "text", "text": "..."}]}
+
+See `ToolAnnotations` in the [TypeScript](</docs/en/agent-sdk/typescript#tool-annotations>) or [Python](</docs/en/agent-sdk/python#tool-annotations>) reference.
+
+##
+
+
+
+Control tool access
+
+The weather tool example registered a server and listed tools in `allowedTools`. This section covers how tool names are constructed and how to scope access when you have multiple tools or want to restrict built-ins.
+
+###
+
+
+
+Tool name format
+
+When MCP tools are exposed to Claude, their names follow a specific format:
+
+ * Pattern: `mcp__{server_name}__{tool_name}`
+ * Example: A tool named `get_temperature` in server `weather` becomes `mcp__weather__get_temperature`
+
+###
+
+
+
+Configure allowed tools
+
+The `tools` option and the allowed/disallowed lists operate on separate layers. `tools` controls which built-in tools appear in Claude’s context. Allowed and disallowed tool lists control whether calls are approved or denied once Claude attempts them.
+
+Option| Layer| Effect
+---|---|---
+`tools: ["Read", "Grep"]`| Availability| Only the listed built-ins are in Claude’s context. Unlisted built-ins are removed. MCP tools are unaffected.
+`tools: []`| Availability| All built-ins are removed. Claude can only use your MCP tools.
+allowed tools| Permission| Listed tools run without a permission prompt. Unlisted tools remain available; calls go through the [permission flow](</docs/en/agent-sdk/permissions>).
+disallowed tools| Permission| Every call to a listed tool is denied. The tool stays in Claude’s context, so Claude may still attempt it before the call is rejected.
+
+To limit which built-ins Claude can use, prefer `tools` over disallowed tools. Omitting a tool from `tools` removes it from context so Claude never attempts it; listing it in `disallowedTools` (Python: `disallowed_tools`) blocks the call but leaves the tool visible, so Claude may waste a turn trying it. See [Configure permissions](</docs/en/agent-sdk/permissions>) for the full evaluation order.
+
+##
+
+
+
+Handle errors
+
+How your handler reports errors determines whether the agent loop continues or stops:
+
+What happens| Result
+---|---
+Handler throws an uncaught exception| Agent loop stops. Claude never sees the error, and the `query` call fails.
+Handler catches the error and returns `isError: true` (TS) / `"is_error": True` (Python)| Agent loop continues. Claude sees the error as data and can retry, try a different tool, or explain the failure.
+
+The example below catches two kinds of failures inside the handler instead of letting them throw. A non-200 HTTP status is caught from the response and returned as an error result. A network error or invalid JSON is caught by the surrounding `try/except` (Python) or `try/catch` (TypeScript) and also returned as an error result. In both cases the handler returns normally and the agent loop continues.
+
+Python
+
+TypeScript
+
+ import json
+ import httpx
+ from typing import Any
+
+ @tool(
+ "fetch_data",
+ "Fetch data from an API",
+ {"endpoint": str}, # Simple schema
+ )
+ async def fetch_data(args: dict[str, Any]) -> dict[str, Any]:
+ try:
+ async with httpx.AsyncClient() as client:
+ response = await client.get(args["endpoint"])
+ if response.status_code != 200:
+ # Return the failure as a tool result so Claude can react to it.
+ # is_error marks this as a failed call rather than odd-looking data.
+ return {
+ "content": [
+ {
+ "type": "text",
+ "text": f"API error: {response.status_code} {response.reason_phrase}",
+ }
+ ],
+ "is_error": True,
+ }
+
+ data = response.json()
+ return {"content": [{"type": "text", "text": json.dumps(data, indent=2)}]}
+ except Exception as e:
+ # Catching here keeps the agent loop alive. An uncaught exception
+ # would end the whole query() call.
+ return {
+ "content": [{"type": "text", "text": f"Failed to fetch data: {str(e)}"}],
+ "is_error": True,
+ }
+
+##
+
+
+
+Return images and resources
+
+The `content` array in a tool result accepts `text`, `image`, and `resource` blocks. You can mix them in the same response.
+
+###
+
+
+
+Images
+
+An image block carries the image bytes inline, encoded as base64. There is no URL field. To return an image that lives at a URL, fetch it in the handler, read the response bytes, and base64-encode them before returning. The result is processed as visual input.
+
+Field| Type| Notes
+---|---|---
+`type`| `"image"`|
+`data`| `string`| Base64-encoded bytes. Raw base64 only, no `data:image/...;base64,` prefix
+`mimeType`| `string`| Required. For example `image/png`, `image/jpeg`, `image/webp`, `image/gif`
+
+Python
+
+TypeScript
+
+ import base64
+ import httpx
+
+ # Define a tool that fetches an image from a URL and returns it to Claude
+ @tool("fetch_image", "Fetch an image from a URL and return it to Claude", {"url": str})
+ async def fetch_image(args):
+ async with httpx.AsyncClient() as client: # Fetch the image bytes
+ response = await client.get(args["url"])
+
+ return {
+ "content": [
+ {
+ "type": "image",
+ "data": base64.b64encode(response.content).decode(
+ "ascii"
+ ), # Base64-encode the raw bytes
+ "mimeType": response.headers.get(
+ "content-type", "image/png"
+ ), # Read MIME type from the response
+ }
+ ]
+ }
+
+###
+
+
+
+Resources
+
+A resource block embeds a piece of content identified by a URI. The URI is a label for Claude to reference; the actual content rides in the block’s `text` or `blob` field. Use this when your tool produces something that makes sense to address by name later, such as a generated file or a record from an external system.
+
+Field| Type| Notes
+---|---|---
+`type`| `"resource"`|
+`resource.uri`| `string`| Identifier for the content. Any URI scheme
+`resource.text`| `string`| The content, if it’s text. Provide this or `blob`, not both
+`resource.blob`| `string`| The content base64-encoded, if it’s binary
+`resource.mimeType`| `string`| Optional
+
+This example shows a resource block returned from inside a tool handler. The URI `file:///tmp/report.md` is a label that Claude can reference later; the SDK does not read from that path.
+
+TypeScript
+
+Python
+
+ return {
+ content: [
+ {
+ type: "resource",
+ resource: {
+ uri: "file:///tmp/report.md", // Label for Claude to reference, not a path the SDK reads
+ mimeType: "text/markdown",
+ text: "# Report\n..." // The actual content, inline
+ }
+ }
+ ]
+ };
+
+These block shapes come from the MCP `CallToolResult` type. See the [MCP specification](<https://modelcontextprotocol.io/specification/2025-06-18/server/tools#tool-result>) for the full definition.
+
+##
+
+
+
+Example: unit converter
+
+This tool converts values between units of length, temperature, and weight. A user can ask “convert 100 kilometers to miles” or “what is 72°F in Celsius,” and Claude picks the right unit type and units from the request. It demonstrates two patterns:
+
+ * **Enum schemas:** `unit_type` is constrained to a fixed set of values. In TypeScript, use `z.enum()`. In Python, the dict schema doesn’t support enums, so the full JSON Schema dict is required.
+ * **Unsupported input handling:** when a conversion pair isn’t found, the handler returns `isError: true` so Claude can tell the user what went wrong rather than treating a failure as a normal result.
+
+Python
+
+TypeScript
+
+ from typing import Any
+ from claude_agent_sdk import tool, create_sdk_mcp_server
+
+ # z.enum() in TypeScript becomes an "enum" constraint in JSON Schema.
+ # The dict schema has no equivalent, so full JSON Schema is required.
+ @tool(
+ "convert_units",
+ "Convert a value from one unit to another",
+ {
+ "type": "object",
+ "properties": {
+ "unit_type": {
+ "type": "string",
+ "enum": ["length", "temperature", "weight"],
+ "description": "Category of unit",
+ },
+ "from_unit": {
+ "type": "string",
+ "description": "Unit to convert from, e.g. kilometers, fahrenheit, pounds",
+ },
+ "to_unit": {"type": "string", "description": "Unit to convert to"},
+ "value": {"type": "number", "description": "Value to convert"},
+ },
+ "required": ["unit_type", "from_unit", "to_unit", "value"],
+ },
+ )
+ async def convert_units(args: dict[str, Any]) -> dict[str, Any]:
+ conversions = {
+ "length": {
+ "kilometers_to_miles": lambda v: v * 0.621371,
+ "miles_to_kilometers": lambda v: v * 1.60934,
+ "meters_to_feet": lambda v: v * 3.28084,
+ "feet_to_meters": lambda v: v * 0.3048,
+ },
+ "temperature": {
+ "celsius_to_fahrenheit": lambda v: (v * 9) / 5 + 32,
+ "fahrenheit_to_celsius": lambda v: (v - 32) * 5 / 9,
+ "celsius_to_kelvin": lambda v: v + 273.15,
+ "kelvin_to_celsius": lambda v: v - 273.15,
+ },
+ "weight": {
+ "kilograms_to_pounds": lambda v: v * 2.20462,
+ "pounds_to_kilograms": lambda v: v * 0.453592,
+ "grams_to_ounces": lambda v: v * 0.035274,
+ "ounces_to_grams": lambda v: v * 28.3495,
+ },
+ }
+
+ key = f"{args['from_unit']}_to_{args['to_unit']}"
+ fn = conversions.get(args["unit_type"], {}).get(key)
+
+ if not fn:
+ return {
+ "content": [
+ {
+ "type": "text",
+ "text": f"Unsupported conversion: {args['from_unit']} to {args['to_unit']}",
+ }
+ ],
+ "is_error": True,
+ }
+
+ result = fn(args["value"])
+ return {
+ "content": [
+ {
+ "type": "text",
+ "text": f"{args['value']} {args['from_unit']} = {result:.4f} {args['to_unit']}",
+ }
+ ]
+ }
+
+ converter_server = create_sdk_mcp_server(
+ name="converter",
+ version="1.0.0",
+ tools=[convert_units],
+ )
+
+Once the server is defined, pass it to `query` the same way as the weather example. This example sends three different prompts in a loop to show the same tool handling different unit types. For each response, it inspects `AssistantMessage` objects (which contain the tool calls Claude made during that turn) and prints each `ToolUseBlock` before printing the final `ResultMessage` text. This lets you see when Claude is using the tool versus answering from its own knowledge.
+
+Python
+
+TypeScript
+
+ import asyncio
+ from claude_agent_sdk import (
+ query,
+ ClaudeAgentOptions,
+ ResultMessage,
+ AssistantMessage,
+ ToolUseBlock,
+ )
+
+ async def main():
+ options = ClaudeAgentOptions(
+ mcp_servers={"converter": converter_server},
+ allowed_tools=["mcp__converter__convert_units"],
+ )
+
+ prompts = [
+ "Convert 100 kilometers to miles.",
+ "What is 72°F in Celsius?",
+ "How many pounds is 5 kilograms?",
+ ]
+
+ for prompt in prompts:
+ async for message in query(prompt=prompt, options=options):
+ if isinstance(message, AssistantMessage):
+ for block in message.content:
+ if isinstance(block, ToolUseBlock):
+ print(f"[tool call] {block.name}({block.input})")
+ elif isinstance(message, ResultMessage) and message.subtype == "success":
+ print(f"Q: {prompt}\nA: {message.result}\n")
+
+ asyncio.run(main())
+
+##
+
+
+
+Next steps
+
+Custom tools wrap async functions in a standard interface. You can mix the patterns on this page in the same server: a single server can hold a database tool, an API gateway tool, and an image renderer alongside each other. From here:
+
+ * If your server grows to dozens of tools, see [tool search](</docs/en/agent-sdk/tool-search>) to defer loading them until Claude needs them.
+ * To connect to external MCP servers (filesystem, GitHub, Slack) instead of building your own, see [Connect MCP servers](</docs/en/agent-sdk/mcp>).
+ * To control which tools run automatically versus requiring approval, see [Configure permissions](</docs/en/agent-sdk/permissions>).
+
+##
+
+
+
+Related documentation
+
+ * [TypeScript SDK Reference](</docs/en/agent-sdk/typescript>)
+ * [Python SDK Reference](</docs/en/agent-sdk/python>)
+ * [MCP Documentation](<https://modelcontextprotocol.io>)
+ * [SDK Overview](</docs/en/agent-sdk/overview>)