Skip to content

Commit 4bfd911

Browse files
alonharseratch
andauthored
Add custom fetch support to StreamableHTTP MCP transport (#319)
Co-authored-by: Kazuhiro Sera <[email protected]>
1 parent a0b1f3b commit 4bfd911

File tree

8 files changed

+136
-29
lines changed

8 files changed

+136
-29
lines changed

.changeset/purple-chicken-protect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@openai/agents-core": patch
3+
---
4+
5+
Add custom fetch support to StreamableHTTP MCP transport

docs/src/content/docs/guides/mcp.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ When your Agent talks directly to a Streamable HTTP MCP server—local or remote
8080
title="Run with Streamable HTTP MCP servers"
8181
/>
8282

83-
The constructor also accepts additional MCP TypeScript‑SDK options such as `authProvider`, `requestInit`, `reconnectionOptions`, and `sessionId`. See the [MCP TypeScript SDK repository](https://github.com/modelcontextprotocol/typescript-sdk) and its documents for details.
83+
The constructor also accepts additional MCP TypeScript‑SDK options such as `authProvider`, `requestInit`, `fetch`, `reconnectionOptions`, and `sessionId`. See the [MCP TypeScript SDK repository](https://github.com/modelcontextprotocol/typescript-sdk) and its documents for details.
8484

8585
## 3. Stdio MCP servers
8686

docs/src/content/docs/ja/guides/mcp.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ import stdioExample from '../../../../../../examples/docs/mcp/stdio.ts?raw';
7979
title="Run with Streamable HTTP MCP servers"
8080
/>
8181

82-
コンストラクターでは `authProvider``requestInit``reconnectionOptions``sessionId` など、MCP TypeScript SDK の追加オプションも指定できます。詳細は [MCP TypeScript SDK リポジトリ](https://github.com/modelcontextprotocol/typescript-sdk) とそのドキュメントを参照してください。
82+
コンストラクターでは `authProvider``requestInit``fetch``reconnectionOptions``sessionId` など、MCP TypeScript SDK の追加オプションも指定できます。詳細は [MCP TypeScript SDK リポジトリ](https://github.com/modelcontextprotocol/typescript-sdk) とそのドキュメントを参照してください。
8383

8484
## 3. Stdio MCP サーバー
8585

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
});

packages/agents-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
],
8686
"license": "MIT",
8787
"optionalDependencies": {
88-
"@modelcontextprotocol/sdk": "^1.12.0"
88+
"@modelcontextprotocol/sdk": "^1.17.2"
8989
},
9090
"dependencies": {
9191
"@openai/zod": "npm:[email protected] - 3.25.67",

packages/agents-core/src/mcp.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,9 @@ export interface MCPServerStreamableHttpOptions {
536536
authProvider?: any;
537537
// RequestInit
538538
requestInit?: any;
539+
// Custom fetch implementation used for all network requests.
540+
// import { FetchLike } from '@modelcontextprotocol/sdk/shared/transport.js';
541+
fetch?: any;
539542
// import { StreamableHTTPReconnectionOptions } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
540543
reconnectionOptions?: any;
541544
sessionId?: string;

packages/agents-core/src/shims/mcp-server/node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ export class NodeMCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
322322
{
323323
authProvider: this.params.authProvider,
324324
requestInit: this.params.requestInit,
325+
fetch: this.params.fetch,
325326
reconnectionOptions: this.params.reconnectionOptions,
326327
sessionId: this.params.sessionId,
327328
},

pnpm-lock.yaml

Lines changed: 75 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)