Repository navigation
Expand file tree
/
Copy pathclaude-agent-sdk-integration.ts
More file actions
70 lines (62 loc) · 2.45 KB
/
Copy pathclaude-agent-sdk-integration.ts
File metadata and controls
70 lines (62 loc) · 2.45 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* This example shows how to use StackOne tools with Claude Agent SDK.
*
* Claude Agent SDK allows you to create autonomous agents with custom tools
* via MCP (Model Context Protocol) servers.
*
* The `toClaudeAgentSdk()` method automatically converts StackOne tools
* to Claude Agent SDK format, handling the MCP server creation internally.
*/
import process from 'node:process';
import { query } from '@anthropic-ai/claude-agent-sdk';
import { StackOneToolSet } from '@stackone/ai';
const apiKey = process.env.STACKONE_API_KEY;
if (apiKey === undefined || apiKey === '') {
console.error('STACKONE_API_KEY environment variable is required');
process.exit(1);
}
if ((process.env.ANTHROPIC_API_KEY ?? '') === '') {
console.log('Skipping: ANTHROPIC_API_KEY is not set');
process.exit(0);
}
const claudeAgentSdkIntegration = async (): Promise<void> => {
// Initialize StackOne — reads STACKONE_API_KEY from env. The account id is passed explicitly;
// without one, the toolset uses every active shared account linked to the key
const accountId = process.env.STACKONE_ACCOUNT_ID;
const toolset = new StackOneToolSet({ accountId: accountId === '' ? undefined : accountId });
// Fetch tools from StackOne and convert to Claude Agent SDK format
const tools = await toolset.fetchTools();
const mcpServer = await tools.toClaudeAgentSdk();
console.log('Claude Agent SDK MCP server created');
// Use the Claude Agent SDK query with the StackOne MCP server
// Type assertion is needed because our interface is compatible but not identical
const result = query({
prompt: 'List the first 5 employees',
options: {
model: 'claude-sonnet-4-5-20250929',
mcpServers: {
// oxlint-disable-next-line typescript/no-explicit-any, typescript/no-unsafe-type-assertion -- Compatible MCP server type
'stackone-tools': mcpServer as any,
},
// Disable built-in tools, only use our custom tools
tools: [],
maxTurns: 3,
},
});
// Process the stream and collect results
console.log('Processing agent stream...');
for await (const message of result) {
if (message.type === 'assistant') {
for (const block of message.message.content) {
if (block.type === 'tool_use') {
console.log(` Tool use: ${block.name}`);
console.log(` Input: ${JSON.stringify(block.input)}`);
} else if (block.type === 'text') {
console.log(` Assistant: ${block.text}`);
}
}
}
}
console.log('Agent stream completed');
};
await claudeAgentSdkIntegration();