Skip to content

Commit 1f83650

Browse files
committed
Add custom fetch support to StreamableHTTP MCP transport
This enhancement significantly improves the flexibility of the StreamableHTTPClientTransport class by enabling the injection of a custom fetch implementation. This change empowers users to exert fine-grained control over network requests, facilitating advanced use cases such as custom authentication flows, specialized request handling, and improved adaptability of the transport layer. Key improvements: - Added optional 'fetch' parameter to MCPServerStreamableHttpOptions interface - Updated NodeMCPServerStreamableHttp implementation to pass custom fetch to underlying transport - Enables custom authentication flows for corporate environments - Supports request/response logging and debugging capabilities - Allows retry logic implementation for unreliable connections - Facilitates testing with mock fetch implementations - Enables custom header injection and request modification The implementation is fully backward compatible - existing code continues to work unchanged as the fetch parameter is optional and defaults to the standard fetch implementation. Changes include: - packages/agents-core/src/mcp.ts: Added fetch parameter to interface with proper documentation - packages/agents-core/src/shims/mcp-server/node.ts: Updated transport initialization - docs/src/content/docs/guides/mcp.mdx: Updated English documentation - docs/src/content/docs/ja/guides/mcp.mdx: Updated Japanese documentation - examples/mcp/streamable-http-custom-fetch-example.ts: Added comprehensive usage example This change aligns with the MCP SDK's existing support for custom fetch implementations and provides a clean, type-safe API for advanced network request customization.
1 parent 0858c98 commit 1f83650

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

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/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
},

0 commit comments

Comments
 (0)