-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcustom-tool-example.ts
More file actions
48 lines (44 loc) · 1.33 KB
/
Copy pathcustom-tool-example.ts
File metadata and controls
48 lines (44 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Example: give Claude a custom tool via the Agent SDK.
*
* Custom tools are the main extension point for the SDK. Each tool is
* declared as JSONSchema + a handler, and becomes callable by Claude
* like any built-in tool.
*/
import { query, tool } from "@anthropic-ai/claude-agent-sdk";
import { z } from "zod";
const weather = tool({
name: "get_weather",
description: "Return the current temperature in Celsius for a city.",
inputSchema: z.object({
city: z.string().describe("City name, e.g. 'Berlin'"),
}),
handler: async ({ city }) => {
// Stubbed. In a real tool, call a weather API here.
const fake: Record<string, number> = { Berlin: 14, Tokyo: 22, Austin: 31 };
const c = fake[city];
if (c === undefined) throw new Error(`no data for ${city}`);
return { temperature_c: c };
},
});
async function main() {
const session = query({
prompt: "What's the weather in Berlin and Tokyo? Which is warmer?",
options: {
model: "claude-sonnet-4-6",
tools: [weather],
maxTurns: 6,
},
});
for await (const message of session) {
if (message.type === "assistant") {
for (const block of message.message.content) {
if (block.type === "text") process.stdout.write(block.text);
}
}
}
}
main().catch((err) => {
console.error(err);
process.exit(1);
});