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
| 1 | import { Composio } from '@composio/core'; |
| 2 | import { OpenAIResponsesProvider } from '@composio/openai'; |
| 3 | import OpenAI from 'openai'; |
| 4 | import 'dotenv/config'; |
| 5 | |
| 6 | // 1. Initialize Composio. |
| 7 | const composio = new Composio({ |
| 8 | apiKey: process.env.COMPOSIO_API_KEY, |
| 9 | provider: new OpenAIResponsesProvider(), |
| 10 | allowTracking: false, |
| 11 | }); |
| 12 | |
| 13 | const authConfigId = process.env.COMPOSIO_EXAMPLES_GMAIL_AUTH_CONFIG_ID; // your Gmail auth config ID |
| 14 | const externalUserId = process.env.COMPOSIO_EXAMPLES_USER_ID; // the user id from your database |
| 15 | if (!authConfigId || !externalUserId) { |
| 16 | throw new Error('Set COMPOSIO_EXAMPLES_GMAIL_AUTH_CONFIG_ID and COMPOSIO_EXAMPLES_USER_ID'); |
| 17 | } |
| 18 | const allowedTools = ['GMAIL_FETCH_EMAILS']; |
| 19 | |
| 20 | // 2. Create an MCP config |
| 21 | const mcpConfig = await composio.mcp.create(`examples-gm-resp-${Math.floor(Date.now() / 1000)}`, { |
| 22 | toolkits: [ |
| 23 | { |
| 24 | toolkit: 'gmail', |
| 25 | authConfigId, |
| 26 | }, |
| 27 | ], |
| 28 | allowedTools, |
| 29 | manuallyManageConnections: true, |
| 30 | }); |
| 31 | |
| 32 | // 3. Retrieve the MCP server instance for the connected accounts |
| 33 | const mcp = await composio.mcp.generate(externalUserId, mcpConfig.id); |
| 34 | |
| 35 | const tools = [ |
| 36 | { |
| 37 | type: 'mcp' as const, |
| 38 | server_label: mcp.name, |
| 39 | server_url: mcp.url, |
| 40 | // The MCP endpoint authenticates with your Composio API key |
| 41 | headers: { 'x-api-key': process.env.COMPOSIO_API_KEY! }, |
| 42 | }, |
| 43 | ]; |
| 44 | |
| 45 | // 4. Pass tools to OpenAI-specific Agent. |
| 46 | const openai = new OpenAI(); |
| 47 | const emailResponse = await openai.responses.create({ |
| 48 | model: 'gpt-4o-mini', |
| 49 | instructions: ` |
| 50 | You are a helpful Gmail assistant that fetches and summarizes emails. |
| 51 | When fetching emails, provide a clear summary of the results including sender, subject, and date. |
| 52 | Be concise and provide actionable information based on the email content. |
| 53 | `, |
| 54 | input: `Fetch the latest 2 emails and provide a detailed summary with sender, subject, date, and brief content overview for each email.`, |
| 55 | tools: tools, |
| 56 | }); |
| 57 | |
| 58 | const result = await composio.provider.handleToolCalls('default', emailResponse.output); |
| 59 | console.log({ result }); |
| 60 | |
| 61 | const output = emailResponse.output.filter(({ type }) => type === 'message').at(0); |
| 62 | |
| 63 | console.log( |
| 64 | output?.type === 'message' |
| 65 | ? output.content[0].type === 'output_text' |
| 66 | ? output.content[0].text |
| 67 | : '<refusal>' |
| 68 | : '<unexpected response>' |
| 69 | ); |