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 107 lines 2.77 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 import { stdout } from 'process';
13
14 const anthropic = new Anthropic({
15 apiKey: process.env.ANTHROPIC_API_KEY,
16 });
17
18 /**
19 * Initialize Composio with the Anthropic provider
20 */
21 const composio = new Composio({
22 apiKey: process.env.COMPOSIO_API_KEY,
23 provider: new AnthropicProvider(),
24 });
25
26 async function main() {
27 console.log('🔄 Fetching tools from Composio...');
28 const tools = await composio.tools.get('default', 'HACKERNEWS_GET_USER');
29 console.log(`✅ Fetched ${tools.length} tools`);
30
31 console.log('🔄 Creating message with Anthropic...');
32 const stream = await anthropic.messages
33 .stream({
34 model: 'claude-sonnet-5',
35 max_tokens: 1024,
36 messages: [
37 {
38 role: 'user',
39 content: 'Tell me about the user "pg" from Hacker News',
40 },
41 ],
42 tools: tools,
43 })
44 .on('text', text => {
45 stdout.write(text);
46 })
47 .on('end', () => {
48 stdout.write('\n');
49 });
50
51 const message = await stream.finalMessage();
52
53 /**
54 * Extract tool use blocks from the message content
55 */
56 const toolUseBlocks = message.content.filter(content => content.type === 'tool_use');
57
58 if (toolUseBlocks.length > 0) {
59 console.log(`✅ Found ${toolUseBlocks.length} tool calls`);
60
61 /**
62 * Execute the tool calls using Composio
63 */
64 const toolResults = await composio.provider.handleToolCalls('default', message);
65
66 console.log('✅ Tool results:', toolResults);
67
68 /**
69 * Send a follow-up message with the tool results
70 */
71 const followUpStream = await anthropic.messages
72 .stream({
73 model: 'claude-sonnet-5',
74 max_tokens: 1024,
75 messages: [
76 {
77 role: 'user',
78 content:
79 "Fetch the details of the user 'pg' from Hacker News and summarize their profile",
80 },
81 {
82 role: 'assistant',
83 content: [
84 {
85 type: 'text',
86 text: "I'll fetch the details for the Hacker News user 'pg'.",
87 },
88 ...toolUseBlocks,
89 ],
90 },
91 ...toolResults,
92 ],
93 })
94 .on('text', text => {
95 stdout.write(text);
96 })
97 .on('end', () => {
98 stdout.write('\n');
99 });
100
101 const followUpMessage = await followUpStream.finalMessage();
102
103 console.log(followUpMessage);
104 }
105 }
106
107 main();