@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 63 lines 1.98 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 externalUserId = 'default';
15
16 // 2. Create a tool router session.
17 // HACKERNEWS is unauthenticated, so it works with the repository's shared
18 // staging key without requiring a connected Gmail or GitHub account.
19 const session = await composio.sessions.create(externalUserId, {
20 toolkits: ['hackernews'],
21 mcp: true,
22 });
23
24 // 3. Retrieve the MCP server instance for the tool router
25
26 const tools: HostedMCPTool[] = [
27 hostedMcpTool({
28 serverLabel: 'composio-tool-router',
29 serverUrl: session.mcp.url,
30 // The MCP endpoint authenticates with your Composio API key
31 headers: { 'x-api-key': process.env.COMPOSIO_API_KEY! },
32 requireApproval: {
33 never: {
34 toolNames: ['HACKERNEWS_GET_USER_BY_USERNAME'],
35 },
36 },
37 }),
38 ];
39
40 // 4. Pass tools to OpenAI-specific Agent.
41 const agent = new OpenAIAgent({
42 name: 'HackerNews Assistant',
43 instructions: `
44 You are a helpful HackerNews assistant that looks up user profiles.
45 Be concise and summarize the user's HackerNews profile clearly.
46 `,
47 model: 'gpt-4o-mini',
48 tools: tools,
49 });
50
51 // 5. Execute the OpenAI-specific agent.
52 // Fetch a HackerNews user profile.
53 console.log('\n=== Fetching HackerNews User Profile ===');
54 const response = await run(
55 agent,
56 'Look up the HackerNews user `pg` and summarize their profile.'
57 );
58 console.log('\n📰 HackerNews Profile:');
59
60 const output = response.output.filter(({ type }) => type === 'message').at(0);
61
62 // @ts-expect-error: the agents SDK types `output` as a union whose message variant is not narrowed by the `type` filter above
63 console.log(output?.content[0].text);