You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sdk__custom-tools.md
+73-35Lines changed: 73 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,6 +80,10 @@ Use the `createSdkMcpServer` and `tool` helper functions to define type-safe cus
80
80
81
81
Pass the custom server to the `query` function via the `mcpServers` option as a dictionary/object.
82
82
83
+
<Note>
84
+
**Important:** Custom MCP tools require streaming input mode. You must use an async generator/iterable for the `prompt` parameter - a simple string will not work with MCP servers.
85
+
</Note>
86
+
83
87
### Tool Name Format
84
88
85
89
When MCP tools are exposed to Claude, their names follow a specific format:
@@ -95,9 +99,19 @@ You can control which tools Claude can use via the `allowedTools` option:
95
99
```typescript TypeScript
96
100
import { query } from"@anthropic-ai/claude-code";
97
101
98
-
// Use the custom tools in your query
102
+
// Use the custom tools in your query with streaming input
103
+
asyncfunction* generateMessages() {
104
+
yield {
105
+
type: "user"asconst,
106
+
message: {
107
+
role: "user"asconst,
108
+
content: "What's the weather in San Francisco?"
109
+
}
110
+
};
111
+
}
112
+
99
113
forawait (const message ofquery({
100
-
prompt: "What's the weather in San Francisco?",
114
+
prompt: generateMessages(), // Use async generator for streaming input
101
115
options: {
102
116
mcpServers: {
103
117
"my-custom-tools": customServer// Pass as object/dictionary, not array
@@ -119,23 +133,30 @@ You can control which tools Claude can use via the `allowedTools` option:
119
133
```python Python
120
134
from claude_code_sdk import ClaudeSDKClient, ClaudeCodeOptions
121
135
122
-
# Use the custom tools in your query
123
-
options = ClaudeCodeOptions(
124
-
mcp_servers={"my-custom-tools": custom_server}, # Pass as dict, not list
125
-
# Optionally specify which tools Claude can use
126
-
allowed_tools=[
127
-
"mcp__my-custom-tools__get_weather", # Allow the weather tool
128
-
# Add other tools as needed
129
-
],
130
-
max_turns=3
131
-
)
136
+
# Use the custom tools in your query with streaming input
137
+
asyncdefmessage_generator():
138
+
yield {
139
+
"type": "user",
140
+
"message": {
141
+
"role": "user",
142
+
"content": "What's the weather in San Francisco?"
143
+
}
144
+
}
132
145
133
-
asyncwith ClaudeSDKClient(options=options) as client:
134
-
await client.query("What's the weather in San Francisco?")
135
-
136
-
# Extract and print response
137
-
asyncfor msg in client.receive_response():
138
-
print(msg)
146
+
asyncfor message in query(
147
+
prompt=message_generator(), # Use async generator for streaming input
148
+
options=ClaudeCodeOptions(
149
+
mcp_servers={"my-custom-tools": custom_server}, # Pass as dict, not list
150
+
# Optionally specify which tools Claude can use
151
+
allowed_tools=[
152
+
"mcp__my-custom-tools__get_weather", # Allow the weather tool
153
+
# Add other tools as needed
154
+
],
155
+
max_turns=3
156
+
)
157
+
):
158
+
ifhasattr(message, 'result'):
159
+
print(message.result)
139
160
```
140
161
</CodeGroup>
141
162
@@ -155,9 +176,19 @@ When your MCP server has multiple tools, you can selectively allow them:
155
176
]
156
177
});
157
178
158
-
// Allow only specific tools
179
+
// Allow only specific tools with streaming input
180
+
asyncfunction* generateMessages() {
181
+
yield {
182
+
type: "user"asconst,
183
+
message: {
184
+
role: "user"asconst,
185
+
content: "Calculate 5 + 3 and translate 'hello' to Spanish"
186
+
}
187
+
};
188
+
}
189
+
159
190
forawait (const message ofquery({
160
-
prompt: "Calculate 5 + 3 and translate 'hello' to Spanish",
191
+
prompt: generateMessages(), // Use async generator for streaming input
161
192
options: {
162
193
mcpServers: {
163
194
utilities: multiToolServer
@@ -198,22 +229,29 @@ When your MCP server has multiple tools, you can selectively allow them:
0 commit comments