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

next
ts 50 lines 1.33 KB
Raw
1 import { openai } from '@ai-sdk/openai';
2 import { createMCPClient } from '@ai-sdk/mcp';
3 import { Composio } from '@composio/core';
4 import { stepCountIs, streamText } from 'ai';
5 import ora from 'ora';
6
7 const composio = new Composio();
8 const trProgress = ora('Creating tool router session...').start();
9 const { mcp } = await composio.create('default', {
10 toolkits: ['gmail'],
11 manageConnections: true,
12 mcp: true,
13 tools: {
14 gmail: {
15 enable: ['GMAIL_FETCH_EMAILS'],
16 },
17 },
18 });
19 trProgress.succeed(`Tool router session created: ${mcp.url}`);
20
21 const mcpProgress = ora('Retrieving tools from MCP...').start();
22 const client = await createMCPClient({
23 transport: {
24 type: 'http',
25 url: mcp.url,
26 headers: mcp.headers,
27 },
28 });
29
30 const tools = await client.tools();
31 mcpProgress.succeed(`${Object.values(tools).length} tools retrieved from MCP`);
32
33 console.log(`🤖 Waiting for agent response...`);
34 const stream = await streamText({
35 model: openai('gpt-4o-mini'),
36 prompt: 'Summarize my latest received email from gmail.',
37 stopWhen: stepCountIs(10),
38 onStepFinish: step => {
39 if (step.toolCalls.length > 0) {
40 for (const toolCall of step.toolCalls) {
41 console.log(`🔧 Executed ${toolCall.toolName}`);
42 }
43 }
44 },
45 tools,
46 });
47
48 for await (const textPart of stream.textStream) {
49 process.stdout.write(textPart);
50 }