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 | * Example for using Claude Agent SDK provider with Composio SDK |
| 3 | * |
| 4 | * This example demonstrates how to use Composio tools with the Claude Agent SDK |
| 5 | * to create an AI agent that can interact with external services like Gmail. |
| 6 | * |
| 7 | * Required environment variables: |
| 8 | * - COMPOSIO_API_KEY: Your Composio API key (get one at https://app.composio.dev) |
| 9 | * - ANTHROPIC_API_KEY: Your Anthropic API key (get one at https://console.anthropic.com) |
| 10 | * |
| 11 | * Prerequisites: |
| 12 | * - Claude Code CLI must be installed: npm install -g @anthropic-ai/claude-code |
| 13 | * |
| 14 | * Usage: |
| 15 | * bun src/claude-code-agents.ts |
| 16 | */ |
| 17 | import { Composio } from '@composio/core'; |
| 18 | import { ClaudeAgentSDKProvider } from '@composio/claude-agent-sdk'; |
| 19 | import { query, createSdkMcpServer } from '@anthropic-ai/claude-agent-sdk'; |
| 20 | import 'dotenv/config'; |
| 21 | |
| 22 | /** |
| 23 | * Initialize Composio with the Claude Agent SDK provider |
| 24 | */ |
| 25 | const composio = new Composio({ |
| 26 | apiKey: process.env.COMPOSIO_API_KEY, |
| 27 | provider: new ClaudeAgentSDKProvider(), |
| 28 | }); |
| 29 | |
| 30 | async function main() { |
| 31 | console.log('🔄 Fetching Gmail tools from Composio...'); |
| 32 | |
| 33 | /** |
| 34 | * Get the Gmail fetch emails tool |
| 35 | * This tool allows the agent to retrieve emails from Gmail |
| 36 | * The tools are already wrapped by composio.tools.get() into Claude Agent SDK format |
| 37 | */ |
| 38 | const tools = await composio.tools.get('default', 'GMAIL_FETCH_EMAILS'); |
| 39 | console.log(`✅ Fetched ${tools.length} tool(s)`); |
| 40 | |
| 41 | /** |
| 42 | * Create an MCP server configuration with the Composio tools |
| 43 | * Using createSdkMcpServer directly from the Claude Agent SDK |
| 44 | */ |
| 45 | console.log('🔄 Creating MCP server with Composio tools...'); |
| 46 | const mcpServer = createSdkMcpServer({ |
| 47 | name: 'composio', |
| 48 | version: '1.0.0', |
| 49 | tools, |
| 50 | }); |
| 51 | console.log('✅ MCP server created'); |
| 52 | |
| 53 | /** |
| 54 | * Run the Claude agent with access to Composio tools |
| 55 | * The agent will use the Gmail tool to fetch and summarize emails |
| 56 | */ |
| 57 | console.log('🔄 Running Claude agent...'); |
| 58 | console.log('─'.repeat(50)); |
| 59 | |
| 60 | let finalResponse = ''; |
| 61 | |
| 62 | for await (const message of query({ |
| 63 | prompt: 'Fetch my latest email from Gmail and provide a brief summary of it, including the sender, subject, and a one-sentence summary of the content.', |
| 64 | options: { |
| 65 | mcpServers: { composio: mcpServer }, |
| 66 | // Allow the agent to use MCP tools without manual permission prompts |
| 67 | permissionMode: 'bypassPermissions', |
| 68 | allowDangerouslySkipPermissions: true, |
| 69 | // Limit the number of turns to prevent infinite loops |
| 70 | maxTurns: 10, |
| 71 | }, |
| 72 | })) { |
| 73 | // Handle different message types from the agent |
| 74 | if (message.type === 'assistant') { |
| 75 | // Assistant's text response |
| 76 | if (typeof message.message === 'string') { |
| 77 | finalResponse = message.message; |
| 78 | } else if (message.message?.content) { |
| 79 | // Handle structured content |
| 80 | const textContent = message.message.content |
| 81 | .filter((block: { type: string }) => block.type === 'text') |
| 82 | .map((block: { type: string; text?: string }) => block.text) |
| 83 | .join('\n'); |
| 84 | if (textContent) { |
| 85 | finalResponse = textContent; |
| 86 | } |
| 87 | } |
| 88 | } else if (message.type === 'result') { |
| 89 | // Tool execution result |
| 90 | console.log(`📧 Tool executed: ${message.subtype || 'unknown'}`); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | console.log('─'.repeat(50)); |
| 95 | console.log('✅ Agent completed'); |
| 96 | console.log('\n📋 Final Response:'); |
| 97 | console.log(finalResponse || 'No response generated'); |
| 98 | |
| 99 | } |
| 100 | |
| 101 | main(); |