mcp_utils
Class info¶
Classes¶
| Name | Children | Inherits |
|---|---|---|
| SdkMcpTool clawd_code_sdk.mcp_utils Definition for an SDK MCP tool. |
🛈 DocStrings¶
In-process (SDK) MCP servers: the @tool decorator and create_sdk_mcp_server.
Servers built here are regular mcp.server.Server instances. The SDK serves
them to the CLI through mcp's own in-memory transport (see
clawd_code_sdk._internal.sdk_mcp_bridge), which works on mcp 1.x and 2.x.
DOCUMENT_URI_PREFIX
module-attribute
¶
URI scheme marking an embedded resource that carries an Anthropic document block.
SdkMcpTool
dataclass
¶
Definition for an SDK MCP tool.
Source code in src/clawd_code_sdk/mcp_utils.py
build_input_schema
¶
Turn a tool's declared input_schema into the JSON Schema sent on the wire.
Accepts a full JSON Schema dict, a dict mapping parameter names to Python types (all required), or a type (TypedDict, dataclass, pydantic model).
Source code in src/clawd_code_sdk/mcp_utils.py
build_tool_meta
¶
build_tool_meta(tool_def: SdkMcpTool[Any]) -> dict[str, Any] | None
Build the _meta dict sent with a tool definition.
Client-specific hints travel in _meta under namespaced keys because MCP
clients drop annotation fields they do not know. Claude Code reads:
anthropic/maxResultSizeChars: the size up to which a tool result is kept inline rather than persisted with a preview (max_result_size_chars, or amaxResultSizeCharsfield on the annotations object, which only survives as an extra field on mcp 1.x),anthropic/alwaysLoad: never defer the tool behind tool search,anthropic/searchHint: extra text for tool search to match on.
Explicit meta from the tool definition is merged last and wins.
Source code in src/clawd_code_sdk/mcp_utils.py
convert_tool_content
¶
Map the content of a tool handler's result dict to MCP content blocks.
- text, image and audio blocks pass through;
documentblocks (Anthropic format) are carried as an embedded blob resource with adocument://URI and restored to a document block on the way to the CLI (seerestore_document_blocks);- resource links and text resources are flattened to text, which is what the CLI renders;
- other binary resources and unknown block types are dropped with a warning.
Source code in src/clawd_code_sdk/mcp_utils.py
create_sdk_mcp_server
¶
create_sdk_mcp_server(
name: str, version: str = "1.0.0", tools: list[SdkMcpTool[Any]] | None = None
) -> McpSdkServerConfigWithInstance
Create an in-process MCP server that runs within your Python application.
Unlike external MCP servers that run as separate processes, SDK MCP servers run directly in your application's process. This provides: - Better performance (no IPC overhead) - Simpler deployment (single process) - Easier debugging (same process) - Direct access to your application's state
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique identifier for the server. This name is used to reference the server in the mcp_servers configuration. |
required |
version
|
str
|
Server version string. Defaults to "1.0.0". This is for informational purposes and doesn't affect functionality. |
'1.0.0'
|
tools
|
list[SdkMcpTool[Any]] | None
|
List of SdkMcpTool instances created with the @tool decorator. These are the functions that Claude can call through this server. If None or empty, the server will have no tools (rarely useful). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
McpSdkServerConfigWithInstance |
McpSdkServerConfigWithInstance
|
A configuration object that can be passed to |
McpSdkServerConfigWithInstance
|
ClaudeAgentOptions.mcp_servers. Its |
|
McpSdkServerConfigWithInstance
|
|
|
McpSdkServerConfigWithInstance
|
in-memory MCP transport. |
Example
Simple calculator server:
@tool("add", "Add numbers", {"a": float, "b": float}) ... async def add(args): ... return {"content": [{"type": "text", "text": f"Sum: {args['a'] + args['b']}"}]}
calculator = create_sdk_mcp_server(name="calculator", tools=[add]) options = ClaudeAgentOptions(mcp_servers={"calc": calculator})
Bringing your own server: any mcp.server.Server built with the
installed mcp package can be used in place of this helper, and the
requests it handles (tools, resources, prompts, ...) are served.
Requests and notifications the server sends to the client (sampling,
elicitation, roots, logging, progress, list_changed) are not forwarded:
from mcp.server import Server my_server = Server("mine") # register handlers with the mcp API config = McpSdkServerConfigWithInstance(name="mine", instance=my_server)
Notes
- Tool arguments are validated against the tool's input schema. Unknown tools, invalid arguments and exceptions raised by a handler are all reported to Claude as error results rather than failing the request.
- Works with both mcp 1.x and mcp 2.x.
Source code in src/clawd_code_sdk/mcp_utils.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
restore_document_blocks
¶
Rewrite document resources in a tools/call JSON-RPC response for the CLI.
Embedded blob resources with a document:// URI or a PDF mime type are
turned back into Anthropic document blocks, which Claude Code passes
on to the model. Every other part of the response is left untouched.
Source code in src/clawd_code_sdk/mcp_utils.py
tool
¶
tool(
name: str,
description: str,
input_schema: type | dict[str, Any],
*,
title: str | None = None,
annotations: ToolAnnotations | None = None,
output_schema: dict[str, Any] | None = None,
meta: dict[str, Any] | None = None,
search_hint: str | None = None,
always_load: bool | None = None,
max_result_size_chars: int | None = None,
) -> Callable[[Callable[[Any], Awaitable[dict[str, Any]]]], SdkMcpTool[Any]]
Decorator for defining MCP tools with type safety.
Creates a tool that can be used with SDK MCP servers. The tool runs in-process within your Python application, providing better performance than external MCP servers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique identifier for the tool. This is what Claude will use to reference the tool in function calls. |
required |
description
|
str
|
Human-readable description of what the tool does. This helps Claude understand when to use the tool. |
required |
input_schema
|
type | dict[str, Any]
|
Schema defining the tool's input parameters. Can be either: - A dictionary mapping parameter names to types (e.g., {"text": str}) - A TypedDict class for more complex schemas - A JSON Schema dictionary for full validation |
required |
title
|
str | None
|
Optional human-readable display name for the tool. If not set, clients typically use the name. |
None
|
annotations
|
ToolAnnotations | None
|
Optional annotations for the tool. |
None
|
output_schema
|
dict[str, Any] | None
|
Optional JSON Schema describing the tool's output format. |
None
|
meta
|
dict[str, Any] | None
|
Optional metadata dict sent as |
None
|
search_hint
|
str | None
|
Optional hint text for tool search/discovery. |
None
|
always_load
|
bool | None
|
Optional flag indicating whether the tool should always be loaded. |
None
|
max_result_size_chars
|
int | None
|
Optional size (in characters) up to which Claude Code keeps the tool's result inline instead of persisting it with a preview. |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[[Any], Awaitable[dict[str, Any]]]], SdkMcpTool[Any]]
|
A decorator function that wraps the tool implementation and returns |
Callable[[Callable[[Any], Awaitable[dict[str, Any]]]], SdkMcpTool[Any]]
|
an SdkMcpTool instance ready for use with create_sdk_mcp_server(). |
Notes
- The tool function must be async
- The function receives a single dict argument with the input parameters
- The function should return a dict with a "content" key containing the response
- Errors can be indicated by including "is_error": True in the response