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 57 lines 1.68 KB
Raw
1 import { Composio } from '@composio/core';
2 import { AnthropicProvider } from '@composio/anthropic';
3 import Anthropic from '@anthropic-ai/sdk';
4
5 // 1. Initialize Composio.
6 const composio = new Composio({
7 apiKey: process.env.COMPOSIO_API_KEY,
8 provider: new AnthropicProvider({ cacheTools: true }),
9 });
10
11 const externalUserId = process.env.COMPOSIO_EXAMPLES_USER_ID; // the user id from your database
12 if (!externalUserId) {
13 throw new Error('Set COMPOSIO_EXAMPLES_USER_ID');
14 }
15
16 // 2. Create a session scoped to the Gmail toolkit; it exposes a hosted MCP endpoint
17 const session = await composio.sessions.create(externalUserId, {
18 toolkits: ['gmail'],
19 mcp: true,
20 });
21
22 // 3. Initialize Anthropic client.
23 const anthropic = new Anthropic({
24 apiKey: process.env.ANTHROPIC_API_KEY,
25 });
26
27 // 4. Pass the session's MCP endpoint to Anthropic-specific Agent.
28 const stream = anthropic.beta.messages.stream({
29 model: 'claude-sonnet-5',
30 max_tokens: 64_000,
31 mcp_servers: [
32 {
33 name: 'composio-gmail',
34 url: session.mcp.url,
35 type: 'url',
36 authorization_token: process.env.COMPOSIO_API_KEY,
37 },
38 ],
39 messages: [
40 {
41 role: 'user',
42 content:
43 'Please fetch the latest 2 emails and provide a detailed summary with sender, subject, date, and brief content overview for each email. Format the response in a clear, organized way.',
44 },
45 ],
46 betas: ['mcp-client-2025-04-04'],
47 });
48
49 console.log('\n📬 Email Summary:');
50 for await (const event of stream) {
51 if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
52 process.stdout.write(event.delta.text);
53 }
54 }
55 process.stdout.write('\n');
56
57 console.log('\n✅ Anthropic MCP Example completed successfully!');