@devries-cameron20 / composio Mirror · read-only
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.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 72 lines 2.36 KB
Raw
1 import { createMCPClient } from '@ai-sdk/mcp';
2 import { openai } from '@ai-sdk/openai';
3 import { Composio } from '@composio/core';
4 import { VercelProvider } from '@composio/vercel';
5 import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
6 import { stepCountIs, streamText } from 'ai';
7 import 'dotenv/config';
8
9 // 1. Initialize Composio.
10 const composio = new Composio({
11 apiKey: process.env.COMPOSIO_API_KEY,
12 provider: new VercelProvider(),
13 });
14
15 const authConfigId = process.env.COMPOSIO_EXAMPLES_GMAIL_AUTH_CONFIG_ID; // your Gmail auth config ID
16 const externalUserId = process.env.COMPOSIO_EXAMPLES_USER_ID; // the user id from your database
17 if (!authConfigId || !externalUserId) {
18 throw new Error('Set COMPOSIO_EXAMPLES_GMAIL_AUTH_CONFIG_ID and COMPOSIO_EXAMPLES_USER_ID');
19 }
20 const allowedTools = ['GMAIL_FETCH_EMAILS'];
21
22 // 2. Create an MCP config
23 // Named `examples-<label>-<unix-seconds>` so the provisioning script's --gc can
24 // tell an example's throwaway config from one someone created by hand. The API
25 // caps this name at 30 characters, so keep the label short and use seconds.
26 const mcpConfig = await composio.mcp.create(`examples-gmail-${Math.floor(Date.now() / 1000)}`, {
27 toolkits: [
28 {
29 toolkit: 'gmail',
30 authConfigId,
31 },
32 ],
33 allowedTools,
34 manuallyManageConnections: false,
35 });
36
37 // 3. Retrieve the MCP server instance for the user
38 const server = await composio.mcp.generate(externalUserId, mcpConfig.id);
39
40 // The MCP endpoint authenticates with your Composio API key
41 const serverParams = new StreamableHTTPClientTransport(new URL(server.url), {
42 requestInit: { headers: { 'x-api-key': process.env.COMPOSIO_API_KEY! } },
43 });
44
45 const mcpClient = await createMCPClient({
46 name: 'composio-mcp-client',
47 transport: serverParams,
48 });
49
50 // 5. Retrieve tools.
51 const tools = await mcpClient.tools();
52
53 // 6. Pass tools to Vercel-specific Agent.
54 const stream = streamText({
55 model: openai('gpt-4o-mini'),
56 messages: [
57 {
58 role: 'user',
59 content: `Fetch the latest 2 emails and provide a detailed summary with sender, subject, date, and brief content overview for each email.`,
60 },
61 ],
62 stopWhen: stepCountIs(5),
63 tools,
64 });
65
66 // 7. Execute the Vercel AI-specific Agent.
67 for await (const textPart of stream.textStream) {
68 process.stdout.write(textPart);
69 }
70
71 // 8. Close Vercel AI's MCP client.
72 await mcpClient.close();