Omniscio

OmniscioGuidesCustom tools in the Agent SDK

Custom tools in the Claude Agent SDK

The server runs in-process inside your application rather than as a separate program, so a custom tool is a function you own rather than a service you host. Installing the SDK and running a first agent is the Agent SDK guide; this page starts where that one stops.

Last checked against Anthropic's documentation on November 24, 2026. By the Omniscio team. Updated .

The four parts of a tool

A tool is defined by four things, passed to tool() in TypeScript or the @tool decorator in Python. The name is the identifier Claude calls. The description is what Claude reads to decide when to call it, so write it for that decision rather than as reference material. The input schema is declared per language: a Zod schema in TypeScript, or a dict of names to types in Python that the SDK converts to JSON Schema for you.

The handler is an async function that receives the validated arguments and returns an object with content (required), and optionally structuredContent and isError.

Both languages can describe a field so Claude sees it. TypeScript calls .describe() on a Zod field; Python wraps the type in Annotated or passes a full JSON Schema dict when it needs enums, ranges, optional fields or nested objects.

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),
)
async def get_temperature(args):
    return {"content": [{"type": "text", "text": "..."}]}

The TypeScript shape is the same four parts, passed as the first four arguments of tool(name, description, schema, handler). Annotations ride in an options object as the fifth argument — tool(name, description, schema, handler, { annotations: { readOnlyHint: true } }) — which is also where alwaysLoad goes.

Registering it, and the name that connects the two halves

Defining a tool does nothing on its own. Wrap it in createSdkMcpServer or create_sdk_mcp_server, pass that server to query() under mcpServers, and list the tool's fully qualified name in allowedTools to pre-approve its calls.

options = ClaudeAgentOptions(
    mcp_servers={"weather": weather_server},
    allowed_tools=["mcp__weather__get_temperature"],
)

That name is the step people get wrong. It follows mcp__{server_name}__{tool_name}, where the server name is the key you gave the server in mcpServers — here weather — not anything inside the server. If that key contains a character such as a dot or a space, replace it with an underscore when you write the name.

Availability is not permission

Two things can go wrong with a tool and they are separate layers. Availability is whether the tool appears in Claude's context at all, and permission is whether a call is approved when Claude makes it. Anthropic's table is the clearest way to keep them apart.

OptionLayerEffect
tools: ["Read", "Grep"]AvailabilityOnly the listed built-ins are in context; MCP tools are unaffected
tools: []AvailabilityAll built-ins removed, so Claude can only use your MCP tools
allowed toolsPermissionListed tools are pre-approved, apart from the calls no allow rule approves
disallowed toolsBothA bare name such as "Bash" removes the tool; a scoped rule such as "Bash(rm *)" keeps it and denies matching calls

When a tool must ask even though it is on the allow list, the TypeScript SDK has a per-tool policy: set a tool to always_ask and its calls reach your canUseTool callback anyway, or always_deny and the tool is blocked. The field is TypeScript-only and spells its key permission_policy, snake case, in Anthropic's own TypeScript example. It needs Agent SDK v0.3.301 or later, and it belongs in the tools array on the config object createSdkMcpServer() returns, not in the options you pass into that function.

There is a bypass to close if you run auto or plan permission mode. Set destructiveHint: true in the tool's annotations, or the auto mode classifier can approve some always_ask calls in place of your callback — and an unset destructiveHint counts as false for exactly those calls, so leaving it out is the risky case.

What the handler returns, and what Claude reads

The handler's content array takes blocks of type text, image, audio, resource or resource_link, so a tool can hand back a rendered chart as an image rather than a path to one.

An uncaught exception does not stop the agent. The in-process MCP server catches it and returns an error result carrying the raw exception message, so Claude sees that text and the loop continues. Catching the error yourself and returning isError: true, or "is_error": True in Python, lets you compose what Claude reads — which request failed, and what to try instead.

return {
    "content": [{"type": "text", "text": f"API error: {response.status_code}"}],
    "is_error": True,
}

For machine-readable results there is structuredContent, a JSON object returned alongside content. It has a per-language catch: when a TypeScript handler sets it, Claude receives the JSON plus the image and resource blocks, and the text blocks do not reach it. The Python @tool decorator forwards only content and is_error, so structured content from Python needs a standalone MCP server instead of an in-process one. Returning typed data as its own subject is the structured output guide.

Annotations, and the one that changes behaviour

Annotations are optional metadata describing how a tool behaves, passed as the fifth argument to tool() or through the annotations keyword in Python. All four hint fields are booleans: readOnlyHint defaults to false, destructiveHint to true, idempotentHint to false and openWorldHint to true — with the one exception that matters below, where an unset destructiveHint counts as false.

Two of them change behaviour. readOnlyHint: true is what lets Claude call the tool in parallel with other read-only tools, and destructiveHint decides whether the auto mode classifier may approve an always_ask call in place of your callback.

They are metadata, not enforcement. A tool marked read-only can still write to disk if that is what the handler does, so keep the annotation honest about the handler rather than aspirational.

Where these answers come from

  • Custom tools in the Agent SDK, read November 24, 2026: the four parts of a tool, the in-process server, the qualified name convention, the availability-versus-permission table, permission_policy, error results, content block types and the annotations table.
  • MCP in the Agent SDK: the tool naming convention and standalone servers.
  • TypeScript reference and Python reference: the signatures of tool(), @tool and the server factories.

Common questions

What is the full name I put in allowedTools for my own tool?

It follows the convention mcp__{server_name}__{tool_name}, where the server name is the key you gave the server in mcpServers, not the name inside the server. If that key contains a character such as a dot or a space, replace it with an underscore when you write the name.

Does a thrown exception in my tool stop the agent?

No. The SDK's in-process MCP server catches uncaught exceptions and returns them as error results carrying the raw exception message, so Claude sees the exception text and the loop continues. Catching the error in the handler and returning isError: true, or is_error: True in Python, lets you compose the message Claude reads instead.

How do I remove a built-in tool like Bash?

Availability and permission are two layers. To take a built-in out of Claude's context, pass a tools array listing only the built-ins you want, or list its bare name in disallowedTools. A scoped rule such as Bash(rm *) is the other layer: the tool stays in context and only the matching calls are denied.

Can a custom tool return an image or a JSON object?

Yes. The handler's content array takes text, image, audio, resource and resource_link blocks, so a chart can come back as an image. For machine-readable values, set structuredContent on the result; note that the Python @tool decorator forwards only content and is_error, so returning structuredContent from Python needs a standalone MCP server instead of an in-process one.