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 22, 23:15 UTC
feat: add support for anthropic (#1668)
Apoorv Taneja committed
Jun 19, 2025 at 17:40 UTC
c825ebd09e6c7110dc1cd0a3c3925b83567fdd82
2 files changed
+113
-6
ts/examples/anthropic/src/mcp.ts
new
+100
@@ -0,0 +1,100 @@
1
+/**
2
+ * Anthropic MCP Gmail Example
3
+ *
4
+ * This example demonstrates how to use Composio SDK with Anthropic to:
5
+ * 1. Create an MCP server for Gmail toolkit
6
+ * 2. Connect to the server and use it with Anthropic's API
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
+// Initialize Composio with the Anthropic provider
26
+const composio = new Composio({
27
+ apiKey: process.env.COMPOSIO_API_KEY,
28
+ provider: new AnthropicProvider({ cacheTools: true }),
29
+});
30
+
31
+async function main() {
32
+ // Create an MCP server with Gmail toolkit
33
+ const mcpConfig = await composio.mcp.create(
34
+ "gmail-anthropic-" + Date.now(),
35
+ [
36
+ {
37
+ toolkit: "gmail",
38
+ authConfigId: "<auth_config_id>", // Replace with your auth config ID
39
+ allowedTools: [
40
+ "GMAIL_FETCH_EMAILS",
41
+ ]
42
+ }
43
+ ],
44
+ { useComposioManagedAuth: true }
45
+ );
46
+
47
+ console.log(`✅ MCP server created: ${mcpConfig.id}`);
48
+ console.log(`🔧 Available toolkits: ${mcpConfig.toolkits.join(', ')}`);
49
+
50
+ // Get server instance with connected accounts (using convenience method)
51
+ const serverInstances = await mcpConfig.getServer({
52
+ connectedAccountIds: {
53
+ "gmail": "<connected_account_id>" // Replace with your connected account ID
54
+ }
55
+ });
56
+
57
+ // Alternative: You can also use the standalone method
58
+ // const serverInstances = await composio.mcp.getServer(mcpConfig.id, {
59
+ // connectedAccountIds: {
60
+ // "gmail": "<connected_account_id>"
61
+ // }
62
+ // });
63
+
64
+ console.log("Server instances for connected accounts:", serverInstances);
65
+
66
+ console.log('\n=== Fetching and Summarizing Recent Emails ===');
67
+
68
+ // Use Anthropic with the MCP servers
69
+ const stream = await anthropic.beta.messages.stream(
70
+ {
71
+ model: 'claude-3-5-sonnet-20241022',
72
+ max_tokens: 1000,
73
+ mcp_servers: serverInstances,
74
+ messages: [
75
+ {
76
+ role: 'user',
77
+ 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.',
78
+ },
79
+ ],
80
+ },
81
+ {
82
+ headers: {
83
+ 'anthropic-beta': 'mcp-client-2025-04-04',
84
+ },
85
+ },
86
+ );
87
+
88
+ console.log('\n📬 Email Summary:');
89
+ for await (const event of stream) {
90
+ if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
91
+ process.stdout.write(event.delta.text);
92
+ }
93
+ }
94
+ process.stdout.write('\n');
95
+
96
+ console.log('\n✅ Anthropic MCP Example completed successfully!');
97
+}
98
+
99
+// Run the example
100
+main().catch(console.error);
\ No newline at end of file
ts/packages/providers/anthropic/src/index.ts
+13
-6
@@ -15,11 +15,16 @@ import {
15
ToolExecuteParams,
16
logger,
17
McpUrlResponse,
18
- McpServerGetResponse,
18
} from '@composio/core';
19
import Anthropic from '@anthropic-ai/sdk';
20
import { AnthropicTool, InputSchema } from './types';
21
22
+export type AnthropicMcpServerGetResponse = {
23
+ type: 'url';
24
+ url: string;
25
+ name: string;
26
+}[];
27
+
28
/**
29
* Collection of Anthropic tools
30
*/
@@ -48,7 +53,8 @@ export type AnthropicContentBlock = {
53
*/
54
export class AnthropicProvider extends BaseNonAgenticProvider<
55
AnthropicToolCollection,
51
- AnthropicTool
56
+ AnthropicTool,
57
+ AnthropicMcpServerGetResponse
58
> {
59
readonly name = 'anthropic';
60
private chacheTools: boolean = false;
@@ -300,11 +306,12 @@ export class AnthropicProvider extends BaseNonAgenticProvider<
306
* @param data - The MCP URL response data
307
* @returns Standard MCP server response format
308
*/
303
- wrapMcpServerResponse(data: McpUrlResponse): McpServerGetResponse {
304
- // Anthropic uses the standard format
309
+ wrapMcpServerResponse(data: McpUrlResponse): AnthropicMcpServerGetResponse {
310
+ // Anthropic uses the standard format with URL objects
311
return data.map(item => ({
306
- url: new URL(item.url),
312
+ url: item.url,
313
name: item.name,
308
- })) as McpServerGetResponse;
314
+ type: 'url',
315
+ }));
316
}
317
}