Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { Composio } from '@composio/core'; |
| 2 | import { GoogleGenAI, mcpToTool } from '@google/genai'; |
| 3 | import { GoogleProvider } from '@composio/google'; |
| 4 | import { Client as MCPClient } from '@modelcontextprotocol/sdk/client/index.js'; |
| 5 | import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; |
| 6 | |
| 7 | function wrapTools(client: MCPClient) { |
| 8 | return [mcpToTool(client)]; |
| 9 | } |
| 10 | |
| 11 | // 1. Initialize Composio. |
| 12 | const composio = new Composio({ |
| 13 | apiKey: process.env.COMPOSIO_API_KEY, |
| 14 | provider: new GoogleProvider(), |
| 15 | }); |
| 16 | |
| 17 | const externalUserId = process.env.COMPOSIO_EXAMPLES_USER_ID; // the user id from your database |
| 18 | if (!externalUserId) { |
| 19 | throw new Error('Set COMPOSIO_EXAMPLES_USER_ID'); |
| 20 | } |
| 21 | |
| 22 | // 2. Create a session scoped to the Gmail toolkit; it exposes a hosted MCP endpoint. |
| 23 | // manageConnections defaults to true, which would add connection-management tools |
| 24 | // this example does not use. The config it replaced set manuallyManageConnections. |
| 25 | const session = await composio.sessions.create(externalUserId, { |
| 26 | toolkits: ['gmail'], |
| 27 | manageConnections: false, |
| 28 | mcp: true, |
| 29 | }); |
| 30 | |
| 31 | // 3. Create a generic MCP client. |
| 32 | // This client needs to remain "alive" not be dropped by the GC until |
| 33 | // the tools are retrieved from it. |
| 34 | // session.mcp.headers carries the credentials the endpoint requires. |
| 35 | const serverParams = new StreamableHTTPClientTransport(new URL(session.mcp.url), { |
| 36 | requestInit: { headers: session.mcp.headers }, |
| 37 | }); |
| 38 | const mcpClient = new MCPClient({ |
| 39 | name: 'composio-mcp-client', |
| 40 | version: '1.0.0', |
| 41 | }); |
| 42 | |
| 43 | // 4. Retrieve tools. |
| 44 | await mcpClient.connect(serverParams); |
| 45 | const tools = wrapTools(mcpClient); |
| 46 | |
| 47 | // 5. Initialize Google Gemini client. |
| 48 | const gemini = new GoogleGenAI({ |
| 49 | apiKey: process.env.GEMINI_API_KEY, |
| 50 | }); |
| 51 | |
| 52 | // 6. Pass tools to Google Gemini-specific Agent. |
| 53 | const stream = await gemini.models.generateContentStream({ |
| 54 | model: 'gemini-2.5-flash', |
| 55 | contents: `Fetch the latest 2 emails and provide a detailed summary with sender, subject, date, and brief content overview for each email.`, |
| 56 | config: { |
| 57 | tools, |
| 58 | }, |
| 59 | }); |
| 60 | |
| 61 | // 7. Execute the Google Gemini-specific Agent. |
| 62 | for await (const chunk of stream) { |
| 63 | console.log(chunk.text); |
| 64 | } |
| 65 | |
| 66 | // 8. Close the generic MCP client. |
| 67 | await mcpClient.close(); |