Skip to content

Commit 39a5e7c

Browse files
committed
refactor(examples): use toClaudeAgentSdk() in Claude Agent SDK example
Simplify the Claude Agent SDK example by using the new toClaudeAgentSdk() method instead of manual tool wrapping with Zod schemas. Before: Required importing tool(), createSdkMcpServer(), z from dependencies and manually defining Zod schemas for each tool. After: Single method call tools.toClaudeAgentSdk() handles all conversion and MCP server creation automatically. Also updates account ID placeholder to use generic 'hris' prefix for consistency with other examples.
1 parent 0104868 commit 39a5e7c

File tree

1 file changed

+13
-34
lines changed

1 file changed

+13
-34
lines changed

examples/claude-agent-sdk-integration.ts

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
*
44
* Claude Agent SDK allows you to create autonomous agents with custom tools
55
* via MCP (Model Context Protocol) servers.
6+
*
7+
* The `toClaudeAgentSdk()` method automatically converts StackOne tools
8+
* to Claude Agent SDK format, handling the MCP server creation internally.
69
*/
710

811
import assert from 'node:assert';
912
import process from 'node:process';
10-
import { query, tool, createSdkMcpServer } from '@anthropic-ai/claude-agent-sdk';
11-
import { z } from 'zod';
13+
import { query } from '@anthropic-ai/claude-agent-sdk';
1214
import { StackOneToolSet } from '@stackone/ai';
1315

1416
const apiKey = process.env.STACKONE_API_KEY;
@@ -18,7 +20,7 @@ if (!apiKey) {
1820
}
1921

2022
// Replace with your actual account ID from StackOne dashboard
21-
const accountId = 'your-bamboohr-account-id';
23+
const accountId = 'your-hris-account-id';
2224

2325
const claudeAgentSdkIntegration = async (): Promise<void> => {
2426
// Initialize StackOne
@@ -27,42 +29,19 @@ const claudeAgentSdkIntegration = async (): Promise<void> => {
2729
baseUrl: process.env.STACKONE_BASE_URL ?? 'https://api.stackone.com',
2830
});
2931

30-
// Fetch tools from StackOne
32+
// Fetch tools from StackOne and convert to Claude Agent SDK format
3133
const tools = await toolset.fetchTools();
34+
const mcpServer = await tools.toClaudeAgentSdk();
3235

33-
// Get a specific tool
34-
const employeeTool = tools.getTool('bamboohr_get_employee');
35-
assert(employeeTool !== undefined, 'Expected to find bamboohr_get_employee tool');
36-
37-
// Create a Claude Agent SDK tool from the StackOne tool
38-
const getEmployeeTool = tool(
39-
employeeTool.name,
40-
employeeTool.description,
41-
{
42-
id: z.string().describe('The employee ID'),
43-
},
44-
async (args) => {
45-
const result = await employeeTool.execute(args);
46-
return {
47-
content: [{ type: 'text', text: JSON.stringify(result) }],
48-
};
49-
},
50-
);
51-
52-
// Create an MCP server with the StackOne tool
53-
const mcpServer = createSdkMcpServer({
54-
name: 'stackone-tools',
55-
version: '1.0.0',
56-
tools: [getEmployeeTool],
57-
});
58-
59-
// Use the Claude Agent SDK query with the custom MCP server
36+
// Use the Claude Agent SDK query with the StackOne MCP server
37+
// Type assertion is needed because our interface is compatible but not identical
6038
const result = query({
6139
prompt: 'Get the employee with id: c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA',
6240
options: {
6341
model: 'claude-sonnet-4-5-20250929',
6442
mcpServers: {
65-
'stackone-tools': mcpServer,
43+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Compatible MCP server type
44+
'stackone-tools': mcpServer as any,
6645
},
6746
// Disable built-in tools, only use our custom tools
6847
tools: [],
@@ -75,14 +54,14 @@ const claudeAgentSdkIntegration = async (): Promise<void> => {
7554
for await (const message of result) {
7655
if (message.type === 'assistant') {
7756
for (const block of message.message.content) {
78-
if (block.type === 'tool_use' && block.name === 'bamboohr_get_employee') {
57+
if (block.type === 'tool_use' && block.name === 'hris_get_employee') {
7958
hasToolCall = true;
8059
}
8160
}
8261
}
8362
}
8463

85-
assert(hasToolCall, 'Expected at least one tool call to bamboohr_get_employee');
64+
assert(hasToolCall, 'Expected at least one tool call to hris_get_employee');
8665
};
8766

8867
await claudeAgentSdkIntegration();

0 commit comments

Comments
 (0)