@devries-cameron20 / composio Mirror · read-only / commits / 5649af3df
Fork 0

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

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 23, 01:45 UTC

docs: Update Anthropic example with better environment variable documentation

Co-Authored-By: musthaq@composio.dev <musthu.gm@gmail.com>

Devin AI committed May 28, 2025 at 05:19 UTC 5649af3df911891109460cbc3f5051f7ad05f8e2
1 file changed +35 -16
examples/anthropic/src/index.ts
+35 -16
@@ -1,25 +1,22 @@
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
9 -if (!process.env.COMPOSIO_API_KEY) {
10 - console.error('Missing COMPOSIO_API_KEY environment variable');
11 - process.exit(1);
12 -}
13 -
14 -if (!process.env.ANTHROPIC_API_KEY) {
15 - console.error('Missing ANTHROPIC_API_KEY environment variable');
16 - process.exit(1);
17 -}
18 -
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(),
@@ -28,14 +25,21 @@ const composio = new Composio({
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 + /**
37 + * Create a message with Anthropic that may use tools
38 + */
39 const message = await anthropic.messages.create({
40 model: 'claude-3-sonnet-20240229',
41 max_tokens: 1024,
38 - tools: tools,
42 + tools,
43 messages: [
44 {
45 role: 'user',
@@ -46,13 +50,19 @@ async function main() {
50
51 console.log('Message created, processing tool calls if any...');
52
53 + /**
54 + * Extract tool use blocks from the message content
55 + */
56 const toolUseBlocks = message.content.filter(
50 - (content: any) => content.type === 'tool_use'
57 + (content) => content.type === 'tool_use'
58 );
59
60 if (toolUseBlocks.length > 0) {
61 console.log(`Found ${toolUseBlocks.length} tool calls`);
62
63 + /**
64 + * Execute the tool calls using Composio
65 + */
66 const toolResults = await composio.provider.handleToolCalls(
67 'default',
68 message
@@ -60,6 +70,9 @@ async function main() {
70
71 console.log('Tool results:', toolResults);
72
73 + /**
74 + * Send a follow-up message with the tool results
75 + */
76 const followUpMessage = await anthropic.messages.create({
77 model: 'claude-3-sonnet-20240229',
78 max_tokens: 1024,
@@ -91,17 +104,23 @@ async function main() {
104 ],
105 });
106
107 + /**
108 + * Extract the text response from the follow-up message
109 + */
110 const finalResponse = followUpMessage.content
95 - .filter((content: any) => content.type === 'text')
96 - .map((content: any) => content.text)
111 + .filter((content) => content.type === 'text')
112 + .map((content) => content.text)
113 .join('\n');
114
115 console.log('Final response:');
116 console.log(finalResponse);
117 } else {
118 + /**
119 + * If no tool calls were made, just show the text response
120 + */
121 const response = message.content
103 - .filter((content: any) => content.type === 'text')
104 - .map((content: any) => content.text)
122 + .filter((content) => content.type === 'text')
123 + .map((content) => content.text)
124 .join('\n');
125
126 console.log('Response (no tool calls):');