@devries-cameron20 / composio-1 / commits / b120222a5
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(ts/examples): fix mcp examples

jkomyno committed Sep 17, 2025 at 14:46 UTC b120222a5d541dac04aa8558e6d4c1cd12881bd7
2 files changed +91 -70
ts/examples/anthropic/src/mcp.ts
+53 -60
@@ -28,73 +28,66 @@ const composio = new Composio({
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 - { isChatAuth: true }
45 - );
31 +const authConfigId = '<auth_config_id>'; // Use your auth config ID
32 +const connectedAccountId = '<connected_account_id>'; // Replace it with the connected account id
33 +const allowedTools = ['GMAIL_FETCH_EMAILS'];
34
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
35 +// Create an MCP server with Gmail toolkit
36 +const mcpConfig = await composio.mcp.create(
37 + "gmail-anthropic-" + Date.now(),
38 + [
39 + {
40 + authConfigId,
41 + allowedTools,
42 }
55 - });
43 + ],
44 + { isChatAuth: true }
45 +);
46
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 - // });
47 +console.log(`✅ MCP server created: ${mcpConfig.id}`);
48 +console.log(`🔧 Available toolkits: ${mcpConfig.toolkits.join(', ')}`);
49
64 - console.log("Server instances for connected accounts:", serverInstances);
50 +// Get server instance with connected accounts (using convenience method)
51 +const serverInstances = await mcpConfig.getServer({
52 + userId: connectedAccountId,
53 + connectedAccountIds: {
54 + "gmail": connectedAccountId,
55 + }
56 +});
57
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',
58 +// Alternative: You can also use the standalone method
59 +// const serverInstances = await composio.mcp.getServer(mcpConfig.id, {
60 +// userId: connectedAccountId,
61 +// connectedAccountIds: {
62 +// "gmail": connectedAccountId,
63 +// }
64 +// });
65 +
66 +console.log("Server instances for connected accounts:", serverInstances);
67 +
68 +console.log('\n=== Fetching and Summarizing Recent Emails ===');
69 +
70 +// Use Anthropic with the MCP servers
71 +const stream = await anthropic.beta.messages.stream(
72 + {
73 + model: 'claude-4-sonnet-20250514',
74 + max_tokens: 64_000,
75 + mcp_servers: serverInstances,
76 + messages: [
77 + {
78 + role: 'user',
79 + content: '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.',
80 },
85 - },
86 - );
81 + ],
82 + },
83 +);
84
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 - }
85 +console.log('\n📬 Email Summary:');
86 +for await (const event of stream) {
87 + if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
88 + process.stdout.write(event.delta.text);
89 }
94 - process.stdout.write('\n');
95 -
96 - console.log('\n✅ Anthropic MCP Example completed successfully!');
90 }
91 +process.stdout.write('\n');
92
99 -// Run the example
100 -main().catch(console.error);
\ No newline at end of file
93 +console.log('\n✅ Anthropic MCP Example completed successfully!');
ts/examples/mastra/src/mcp.ts
+38 -10
@@ -17,8 +17,31 @@ import { MCPClient } from '@mastra/mcp';
17 import { Agent } from '@mastra/core/agent';
18 import { openai } from '@ai-sdk/openai';
19 import type { MastraMCPServerDefinition } from '@mastra/mcp';
20 +import type { ToolAction } from '@mastra/core';
21 import 'dotenv/config';
22
23 +function wrapTools(
24 + servers: Record<string, unknown>,
25 + tools: Record<string, ToolAction<any, any, any>>
26 +): Record<string, ToolAction<any, any, any>> {
27 + const prefixes = Object.keys(servers);
28 +
29 + function removePrefix(str: string): string {
30 + for (const prefix of prefixes) {
31 + if (str.startsWith(prefix)) {
32 + return str.slice(prefix.length + 1);
33 + }
34 + }
35 + return str;
36 + }
37 +
38 + return Object.fromEntries(
39 + Object.entries(tools).map(([key, tool]) => {
40 + return [removePrefix(key), tool] as const;
41 + })
42 + );
43 +}
44 +
45 /**
46 * Initialize Composio
47 */
@@ -27,16 +50,17 @@ const composio = new Composio({
50 provider: new MastraProvider(),
51 });
52
53 +const authConfigId = '<auth_config_id>'; // Use your auth config ID
54 +const connectedAccountId = 'alberto.schiabel@gmail.com'; // "<connected_account_id>"; // Replace it with the connected account id
55 +const allowedTools = ['GMAIL_FETCH_EMAILS'];
56 +
57 // Create an MCP server with Gmail toolkit
58 const mcpConfig = await composio.mcp.create(
59 "gmail-mcp-" + Date.now(),
60 [
61 {
35 - toolkit: "gmail",
36 - authConfigId: "<auth_config_id>", // Use your auth config ID
37 - allowedTools: [
38 - "GMAIL_FETCH_EMAILS"
39 - ]
62 + authConfigId,
63 + allowedTools,
64 }
65 ],
66 { isChatAuth: true }
@@ -47,15 +71,17 @@ console.log(`🔧 Available toolkits: ${mcpConfig.toolkits.join(', ')}`);
71
72 // Get server instance with connected accounts (using convenience method)
73 const serverInstance = await mcpConfig.getServer({
74 + userId: connectedAccountId,
75 connectedAccountIds: {
51 - "gmail": "<connected_account_id>" // Replace it with the connected account id
76 + "gmail": connectedAccountId,
77 }
78 });
79
80 // Alternative: You can also use the standalone method
56 -// const serverInstance = await composio.mcp.getServer(mcpConfig.id, {
81 +// const serverInstances = await composio.mcp.getServer(mcpConfig.id, {
82 +// userId: connectedAccountId,
83 // connectedAccountIds: {
58 -// "gmail": "<connected_account_id>"
84 +// "gmail": connectedAccountId,
85 // }
86 // });
87
@@ -84,15 +110,17 @@ const gmailAgent = new Agent({
110 Be concise and provide actionable information based on the email content.
111 `,
112 model: openai('gpt-4o-mini'),
87 - tools,
113 + tools: wrapTools(serverInstance, tools),
114 });
115
116 // Fetch and summarize recent emails
117 console.log('\n=== Fetching and Summarizing Recent Emails ===');
118 const emailResponse = await gmailAgent.generate(
93 - 'Fetch the latest 10 emails and provide a detailed summary with sender, subject, date, and brief content overview for each email'
119 + 'Fetch the latest 2 emails and provide a detailed summary with sender, subject, date, and brief content overview for each email'
120 );
121 console.log('\n📬 Email Summary:');
122 console.log(emailResponse.text);
123
124 console.log('\n✅ Gmail MCP Example completed successfully!');
125 +
126 +await mcpClient.disconnect();