Skip to content

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

jkomynob120222a5d54
diff --git a/ts/examples/anthropic/src/mcp.ts b/ts/examples/anthropic/src/mcp.ts
index be64a9f3f..f612058b4 100644
--- a/ts/examples/anthropic/src/mcp.ts
+++ b/ts/examples/anthropic/src/mcp.ts
@@ -28,73 +28,66 @@ const composio = new Composio({
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",
- ]
- }
- ],
- { isChatAuth: true }
- );
+const authConfigId = '<auth_config_id>'; // Use your auth config ID
+const connectedAccountId = '<connected_account_id>'; // Replace it with the connected account id
+const allowedTools = ['GMAIL_FETCH_EMAILS'];
- 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
+// Create an MCP server with Gmail toolkit
+const mcpConfig = await composio.mcp.create(
+ "gmail-anthropic-" + Date.now(),
+ [
+ {
+ authConfigId,
+ allowedTools,
}
- });
+ ],
+ { isChatAuth: true }
+);
- // Alternative: You can also use the standalone method
- // const serverInstances = await composio.mcp.getServer(mcpConfig.id, {
- // connectedAccountIds: {
- // "gmail": "<connected_account_id>"
- // }
- // });
+console.log(`✅ MCP server created: ${mcpConfig.id}`);
+console.log(`🔧 Available toolkits: ${mcpConfig.toolkits.join(', ')}`);
- console.log("Server instances for connected accounts:", serverInstances);
+// Get server instance with connected accounts (using convenience method)
+const serverInstances = await mcpConfig.getServer({
+ userId: connectedAccountId,
+ connectedAccountIds: {
+ "gmail": connectedAccountId,
+ }
+});
- 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',
+// Alternative: You can also use the standalone method
+// const serverInstances = await composio.mcp.getServer(mcpConfig.id, {
+// userId: connectedAccountId,
+// connectedAccountIds: {
+// "gmail": connectedAccountId,
+// }
+// });
+
+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-4-sonnet-20250514',
+ max_tokens: 64_000,
+ mcp_servers: serverInstances,
+ messages: [
+ {
+ role: 'user',
+ 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.',
},
- },
- );
+ ],
+ },
+);
- 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);
- }
+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!');
}
+process.stdout.write('\n');
-// Run the example
-main().catch(console.error);
\ No newline at end of file
+console.log('\n✅ Anthropic MCP Example completed successfully!');
diff --git a/ts/examples/mastra/src/mcp.ts b/ts/examples/mastra/src/mcp.ts
index 7ddb92de3..b97fe7200 100644
--- a/ts/examples/mastra/src/mcp.ts
+++ b/ts/examples/mastra/src/mcp.ts
@@ -17,8 +17,31 @@ import { MCPClient } from '@mastra/mcp';
import { Agent } from '@mastra/core/agent';
import { openai } from '@ai-sdk/openai';
import type { MastraMCPServerDefinition } from '@mastra/mcp';
+import type { ToolAction } from '@mastra/core';
import 'dotenv/config';
+function wrapTools(
+ servers: Record<string, unknown>,
+ tools: Record<string, ToolAction<any, any, any>>
+): Record<string, ToolAction<any, any, any>> {
+ const prefixes = Object.keys(servers);
+
+ function removePrefix(str: string): string {
+ for (const prefix of prefixes) {
+ if (str.startsWith(prefix)) {
+ return str.slice(prefix.length + 1);
+ }
+ }
+ return str;
+ }
+
+ return Object.fromEntries(
+ Object.entries(tools).map(([key, tool]) => {
+ return [removePrefix(key), tool] as const;
+ })
+ );
+}
+
/**
* Initialize Composio
*/
@@ -27,16 +50,17 @@ const composio = new Composio({
provider: new MastraProvider(),
});
+const authConfigId = '<auth_config_id>'; // Use your auth config ID
+const connectedAccountId = '[email protected]'; // "<connected_account_id>"; // Replace it with the connected account id
+const allowedTools = ['GMAIL_FETCH_EMAILS'];
+
// Create an MCP server with Gmail toolkit
const mcpConfig = await composio.mcp.create(
"gmail-mcp-" + Date.now(),
[
{
- toolkit: "gmail",
- authConfigId: "<auth_config_id>", // Use your auth config ID
- allowedTools: [
- "GMAIL_FETCH_EMAILS"
- ]
+ authConfigId,
+ allowedTools,
}
],
{ isChatAuth: true }
@@ -47,15 +71,17 @@ console.log(`🔧 Available toolkits: ${mcpConfig.toolkits.join(', ')}`);
// Get server instance with connected accounts (using convenience method)
const serverInstance = await mcpConfig.getServer({
+ userId: connectedAccountId,
connectedAccountIds: {
- "gmail": "<connected_account_id>" // Replace it with the connected account id
+ "gmail": connectedAccountId,
}
});
// Alternative: You can also use the standalone method
-// const serverInstance = await composio.mcp.getServer(mcpConfig.id, {
+// const serverInstances = await composio.mcp.getServer(mcpConfig.id, {
+// userId: connectedAccountId,
// connectedAccountIds: {
-// "gmail": "<connected_account_id>"
+// "gmail": connectedAccountId,
// }
// });
@@ -84,15 +110,17 @@ const gmailAgent = new Agent({
Be concise and provide actionable information based on the email content.
`,
model: openai('gpt-4o-mini'),
- tools,
+ tools: wrapTools(serverInstance, tools),
});
// Fetch and summarize recent emails
console.log('\n=== Fetching and Summarizing Recent Emails ===');
const emailResponse = await gmailAgent.generate(
- 'Fetch the latest 10 emails and provide a detailed summary with sender, subject, date, and brief content overview for each email'
+ 'Fetch the latest 2 emails and provide a detailed summary with sender, subject, date, and brief content overview for each email'
);
console.log('\n📬 Email Summary:');
console.log(emailResponse.text);
console.log('\n✅ Gmail MCP Example completed successfully!');
+
+await mcpClient.disconnect();