Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 112 lines 3.22 KB
Raw
1 /**
2 * Example for using Anthropic provider with Composio SDK
3 *
4 * Required environment variables:
5 * - COMPOSIO_API_KEY: Your Composio API key (get one at https://app.composio.dev)
6 * - ANTHROPIC_API_KEY: Your Anthropic API key (get one at https://console.anthropic.com)
7 */
8 import { Composio } from '@composio/core';
9 import { AnthropicProvider } from '@composio/anthropic';
10 import Anthropic from '@anthropic-ai/sdk';
11 import 'dotenv/config';
12
13 const anthropic = new Anthropic({
14 apiKey: process.env.ANTHROPIC_API_KEY,
15 });
16
17 /**
18 * Initialize Composio with the Anthropic provider
19 */
20 const composio = new Composio({
21 apiKey: process.env.COMPOSIO_API_KEY,
22 provider: new AnthropicProvider({ cacheTools: true }),
23 });
24
25 async function main() {
26 try {
27 console.log('🔄 Fetching tools from Composio...');
28 /**
29 * Get the Hacker News user tool
30 * This tool is automatically wrapped with the Anthropic provider format
31 */
32 const tools = await composio.tools.get('default', 'HACKERNEWS_GET_USER');
33 console.log(`✅ Fetched ${tools.length} tools`);
34
35 console.log('🔄 Creating message with Anthropic...');
36 const messages: Anthropic.Messages.MessageParam[] = [
37 {
38 role: 'user',
39 content: "Fetch the details of the user 'pg' from Hacker News and summarize their profile",
40 },
41 ];
42 /**
43 * Create a message with Anthropic that may use tools
44 */
45 const message = await anthropic.messages.create({
46 model: 'claude-sonnet-5',
47 max_tokens: 1024,
48 tools: tools,
49 messages,
50 });
51 messages.push({
52 role: 'assistant',
53 content: message.content,
54 });
55
56 console.log('✅ Message created, processing tool calls if any...');
57
58 /**
59 * Extract tool use blocks from the message content
60 */
61 const toolUseBlocks = message.content.filter(content => content.type === 'tool_use');
62
63 if (toolUseBlocks.length > 0) {
64 console.log(`✅ Found ${toolUseBlocks.length} tool calls`);
65
66 /**
67 * Execute the tool calls using Composio
68 */
69 const toolResults = await composio.provider.handleToolCalls('default', message);
70 messages.push(...toolResults);
71
72 console.log('✅ Tool results:', messages);
73
74 /**
75 * Send a follow-up message with the tool results
76 */
77 const followUpMessage = await anthropic.messages.create({
78 model: 'claude-sonnet-5',
79 max_tokens: 1024,
80 messages,
81 });
82
83 /**
84 * Extract the text response from the follow-up message
85 */
86 console.log('🔄 Extracting the text response from the follow-up message...');
87 const finalResponse = followUpMessage.content
88 .filter(content => content.type === 'text')
89 .map(content => content.text)
90 .join('\n');
91
92 console.log('✅ Final response:');
93 console.log(finalResponse);
94 } else {
95 /**
96 * If no tool calls were made, just show the text response
97 */
98 const response = message.content
99 .filter(content => content.type === 'text')
100 .map(content => content.text)
101 .join('\n');
102
103 console.log('❌ Response (no tool calls):');
104 console.log(response);
105 }
106 } catch (error) {
107 console.error('Error:', error);
108 process.exitCode = 1;
109 }
110 }
111
112 main();