Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | /** |
| 2 | * Anthropic MCP Gmail Example |
| 3 | * |
| 4 | * This example demonstrates how to use Composio SDK with Anthropic to: |
| 5 | * 1. Create a tool-router session scoped to the Gmail toolkit |
| 6 | * 2. Connect Anthropic's MCP client to the session's hosted MCP endpoint |
| 7 | * 3. Fetch and summarize emails using the MCP tools |
| 8 | * |
| 9 | * Prerequisites: |
| 10 | * 1. Set up your COMPOSIO_API_KEY and ANTHROPIC_API_KEY in the .env file |
| 11 | * 2. Set up Gmail authentication in Composio dashboard |
| 12 | * 3. Run the example: pnpm tsx src/mcp.ts |
| 13 | */ |
| 14 | |
| 15 | import { Composio } from '@composio/core'; |
| 16 | import { AnthropicProvider } from '@composio/anthropic'; |
| 17 | import Anthropic from '@anthropic-ai/sdk'; |
| 18 | import 'dotenv/config'; |
| 19 | |
| 20 | // Initialize Anthropic client |
| 21 | const anthropic = new Anthropic({ |
| 22 | apiKey: process.env.ANTHROPIC_API_KEY, |
| 23 | }); |
| 24 | |
| 25 | const provider = new AnthropicProvider({ cacheTools: true }); |
| 26 | |
| 27 | // Initialize Composio with the Anthropic provider |
| 28 | const composio = new Composio({ |
| 29 | apiKey: process.env.COMPOSIO_API_KEY, |
| 30 | provider, |
| 31 | }); |
| 32 | |
| 33 | const externalUserId = process.env.COMPOSIO_EXAMPLES_USER_ID; // the user ID from your database |
| 34 | if (!externalUserId) { |
| 35 | throw new Error('Set COMPOSIO_EXAMPLES_USER_ID'); |
| 36 | } |
| 37 | |
| 38 | // Create a session scoped to the Gmail toolkit; every session exposes a hosted MCP endpoint |
| 39 | const session = await composio.sessions.create(externalUserId, { |
| 40 | toolkits: ['gmail'], |
| 41 | mcp: true, |
| 42 | }); |
| 43 | |
| 44 | console.log(`✅ MCP session created: ${session.sessionId}`); |
| 45 | |
| 46 | console.log('\n=== Fetching and Summarizing Recent Emails ==='); |
| 47 | |
| 48 | // Use Anthropic with the session's MCP endpoint |
| 49 | const stream = anthropic.beta.messages.stream({ |
| 50 | model: 'claude-sonnet-5', |
| 51 | max_tokens: 64_000, |
| 52 | mcp_servers: [ |
| 53 | { |
| 54 | type: 'url', |
| 55 | url: session.mcp.url, |
| 56 | name: 'composio-gmail', |
| 57 | authorization_token: process.env.COMPOSIO_API_KEY, |
| 58 | }, |
| 59 | ], |
| 60 | messages: [ |
| 61 | { |
| 62 | role: 'user', |
| 63 | content: |
| 64 | '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.', |
| 65 | }, |
| 66 | ], |
| 67 | betas: ['mcp-client-2025-04-04'], |
| 68 | }); |
| 69 | |
| 70 | console.log('\n📬 Email Summary:'); |
| 71 | for await (const event of stream) { |
| 72 | if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') { |
| 73 | process.stdout.write(event.delta.text); |
| 74 | } |
| 75 | } |
| 76 | process.stdout.write('\n'); |
| 77 | |
| 78 | console.log('\n✅ Anthropic MCP Example completed successfully!'); |