Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
feat: add support for anthropic (#1668)
Apoorv Tanejac825ebd09e6c
diff --git a/ts/examples/anthropic/src/mcp.ts b/ts/examples/anthropic/src/mcp.tsnew file mode 100644index 000000000..df4490e00--- /dev/null+++ b/ts/examples/anthropic/src/mcp.ts@@ -0,0 +1,100 @@+/**+ * Anthropic MCP Gmail Example+ *+ * This example demonstrates how to use Composio SDK with Anthropic to:+ * 1. Create an MCP server for Gmail toolkit+ * 2. Connect to the server and use it with Anthropic's API+ * 3. Fetch and summarize emails using the MCP tools+ *+ * Prerequisites:+ * 1. Set up your COMPOSIO_API_KEY and ANTHROPIC_API_KEY in the .env file+ * 2. Set up Gmail authentication in Composio dashboard+ * 3. Run the example: pnpm tsx src/mcp.ts+ */++import { Composio } from '@composio/core';+import { AnthropicProvider } from '@composio/anthropic';+import Anthropic from '@anthropic-ai/sdk';+import 'dotenv/config';++// Initialize Anthropic client+const anthropic = new Anthropic({+ apiKey: process.env.ANTHROPIC_API_KEY,+});++// Initialize Composio with the Anthropic provider+const composio = new Composio({+ apiKey: process.env.COMPOSIO_API_KEY,+ provider: new AnthropicProvider({ cacheTools: true }),+});++async function main() {+ // Create an MCP server with Gmail toolkit+ const mcpConfig = await composio.mcp.create(+ "gmail-anthropic-" + Date.now(),+ [+ {+ toolkit: "gmail",+ authConfigId: "<auth_config_id>", // Replace with your auth config ID+ allowedTools: [+ "GMAIL_FETCH_EMAILS",+ ]+ }+ ],+ { useComposioManagedAuth: true }+ );++ console.log(`✅ MCP server created: ${mcpConfig.id}`);+ console.log(`🔧 Available toolkits: ${mcpConfig.toolkits.join(', ')}`);++ // Get server instance with connected accounts (using convenience method)+ const serverInstances = await mcpConfig.getServer({+ connectedAccountIds: {+ "gmail": "<connected_account_id>" // Replace with your connected account ID+ }+ });++ // Alternative: You can also use the standalone method+ // const serverInstances = await composio.mcp.getServer(mcpConfig.id, {+ // connectedAccountIds: {+ // "gmail": "<connected_account_id>"+ // }+ // });++ console.log("Server instances for connected accounts:", serverInstances);++ console.log('\n=== Fetching and Summarizing Recent Emails ===');++ // Use Anthropic with the MCP servers+ const stream = await anthropic.beta.messages.stream(+ {+ model: 'claude-3-5-sonnet-20241022',+ max_tokens: 1000,+ mcp_servers: serverInstances,+ messages: [+ {+ role: 'user',+ content: 'Please fetch the latest 10 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.',+ },+ ],+ },+ {+ headers: {+ 'anthropic-beta': 'mcp-client-2025-04-04',+ },+ },+ );++ console.log('\n📬 Email Summary:');+ for await (const event of stream) {+ if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {+ process.stdout.write(event.delta.text);+ }+ }+ process.stdout.write('\n');++ console.log('\n✅ Anthropic MCP Example completed successfully!');+}++// Run the example+main().catch(console.error);\ No newline at end of filediff --git a/ts/packages/providers/anthropic/src/index.ts b/ts/packages/providers/anthropic/src/index.tsindex 82bbddc55..add2180d5 100644--- a/ts/packages/providers/anthropic/src/index.ts+++ b/ts/packages/providers/anthropic/src/index.ts@@ -15,11 +15,16 @@ import {ToolExecuteParams,logger,McpUrlResponse,- McpServerGetResponse,} from '@composio/core';import Anthropic from '@anthropic-ai/sdk';import { AnthropicTool, InputSchema } from './types';+export type AnthropicMcpServerGetResponse = {+ type: 'url';+ url: string;+ name: string;+}[];+/*** Collection of Anthropic tools*/@@ -48,7 +53,8 @@ export type AnthropicContentBlock = {*/export class AnthropicProvider extends BaseNonAgenticProvider<AnthropicToolCollection,- AnthropicTool+ AnthropicTool,+ AnthropicMcpServerGetResponse> {readonly name = 'anthropic';private chacheTools: boolean = false;@@ -300,11 +306,12 @@ export class AnthropicProvider extends BaseNonAgenticProvider<* @param data - The MCP URL response data* @returns Standard MCP server response format*/- wrapMcpServerResponse(data: McpUrlResponse): McpServerGetResponse {- // Anthropic uses the standard format+ wrapMcpServerResponse(data: McpUrlResponse): AnthropicMcpServerGetResponse {+ // Anthropic uses the standard format with URL objectsreturn data.map(item => ({- url: new URL(item.url),+ url: item.url,name: item.name,- })) as McpServerGetResponse;+ type: 'url',+ }));}}