@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 69 lines 2.54 KB
Raw
1 import { Composio } from '@composio/core';
2 import { OpenAIAgentsProvider } from '@composio/openai-agents';
3 import { hostedMcpTool, Agent as OpenAIAgent, run } from '@openai/agents';
4 import type { HostedMCPTool } from '@openai/agents';
5 import 'dotenv/config';
6
7 // 1. Initialize Composio.
8 const composio = new Composio({
9 apiKey: process.env.COMPOSIO_API_KEY,
10 provider: new OpenAIAgentsProvider(),
11 allowTracking: false,
12 });
13
14 const authConfigId = process.env.COMPOSIO_EXAMPLES_GMAIL_AUTH_CONFIG_ID; // your Gmail auth config ID
15 const toolkit = 'gmail'; // slug of the toolkit
16 const externalUserId = process.env.COMPOSIO_EXAMPLES_USER_ID; // the userId 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 (names must be unique within a project)
23 const mcpConfig = await composio.mcp.create(`examples-gm-agents-${Math.floor(Date.now() / 1000)}`, {
24 toolkits: [{ toolkit, authConfigId }],
25 allowedTools,
26 });
27
28 // 3. Retrieve the MCP server instance for the connected accounts
29 const server = await composio.mcp.generate(externalUserId, mcpConfig.id);
30
31 const tools: HostedMCPTool[] = [
32 hostedMcpTool({
33 serverLabel: server.name,
34 serverUrl: server.url,
35 // The MCP endpoint authenticates with your Composio API key
36 headers: { 'x-api-key': process.env.COMPOSIO_API_KEY! },
37 requireApproval: {
38 never: {
39 toolNames: ['GMAIL_FETCH_EMAILS'],
40 },
41 },
42 }),
43 ];
44
45 // 4. Pass tools to OpenAI-specific Agent.
46 const agent = new OpenAIAgent({
47 name: 'Gmail Assistant',
48 instructions: `
49 You are a helpful Gmail assistant that fetches and summarizes emails.
50 When fetching emails, provide a clear summary of the results including sender, subject, and date.
51 Be concise and provide actionable information based on the email content.
52 `,
53 model: 'gpt-4o-mini',
54 tools: tools,
55 });
56
57 // 5. Execute the OpenAI-specific agent.
58 // Fetch and summarize recent emails
59 console.log('\n=== Fetching and Summarizing Recent Emails ===');
60 const emailResponse = await run(
61 agent,
62 'Fetch the latest 2 emails and provide a detailed summary with sender, subject, date, and brief content overview for each email'
63 );
64 console.log('\n📬 Email Summary:');
65
66 const output = emailResponse.output.filter(({ type }) => type === 'message').at(0);
67
68 // @ts-expect-error: the agents SDK types `output` as a union whose message variant is not narrowed by the `type` filter above
69 console.log(output?.content[0].text);