|
| 1 | +import { Agent, run, MCPServerStreamableHttp, withTrace } from '@openai/agents'; |
| 2 | + |
| 3 | +async function main() { |
| 4 | + // Example of using a custom fetch implementation |
| 5 | + const customFetch = async (url: string | URL, init?: RequestInit) => { |
| 6 | + console.log(`Custom fetch called for URL: ${url}`); |
| 7 | + // You could add custom headers, logging, retries, etc. here |
| 8 | + const response = await fetch(url, { |
| 9 | + ...init, |
| 10 | + headers: { |
| 11 | + ...init?.headers, |
| 12 | + 'User-Agent': 'MyCustomAgent/1.0', |
| 13 | + 'X-Custom-Header': 'custom-value', |
| 14 | + }, |
| 15 | + }); |
| 16 | + console.log(`Response status: ${response.status}`); |
| 17 | + return response; |
| 18 | + }; |
| 19 | + |
| 20 | + const mcpServer = new MCPServerStreamableHttp({ |
| 21 | + url: 'https://gitmcp.io/openai/codex', |
| 22 | + name: 'GitMCP Documentation Server', |
| 23 | + fetch: customFetch, // Pass custom fetch implementation |
| 24 | + }); |
| 25 | + |
| 26 | + const agent = new Agent({ |
| 27 | + name: 'GitMCP Assistant', |
| 28 | + instructions: 'Use the tools to respond to user requests.', |
| 29 | + mcpServers: [mcpServer], |
| 30 | + }); |
| 31 | + |
| 32 | + try { |
| 33 | + await withTrace('GitMCP Documentation Server Example with Custom Fetch', async () => { |
| 34 | + await mcpServer.connect(); |
| 35 | + const result = await run( |
| 36 | + agent, |
| 37 | + 'Which language is this repo written in?', |
| 38 | + ); |
| 39 | + console.log(result.finalOutput); |
| 40 | + }); |
| 41 | + } finally { |
| 42 | + await mcpServer.close(); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +main().catch((err) => { |
| 47 | + console.error(err); |
| 48 | + process.exit(1); |
| 49 | +}); |
0 commit comments