11import { FunctionTool , tool , Tool } from './tool' ;
22import { UserError } from './errors' ;
3- import { MCPServerStdio as UnderlyingMCPServerStdio } from '@openai/agents-core/_shims' ;
3+ import {
4+ MCPServerStdio as UnderlyingMCPServerStdio ,
5+ MCPServerStreamableHttp as UnderlyingMCPServerStreamableHttp ,
6+ } from '@openai/agents-core/_shims' ;
47import { getCurrentSpan , withMCPListToolsSpan } from './tracing' ;
58import { logger as globalLogger , getLogger , Logger } from './logger' ;
69import debug from 'debug' ;
@@ -15,6 +18,9 @@ import {
1518export const DEFAULT_STDIO_MCP_CLIENT_LOGGER_NAME =
1619 'openai-agents:stdio-mcp-client' ;
1720
21+ export const DEFAULT_STREAMABLE_HTTP_MCP_CLIENT_LOGGER_NAME =
22+ 'openai-agents:streamable-http-mcp-client' ;
23+
1824/**
1925 * Interface for MCP server implementations.
2026 * Provides methods for connecting, listing tools, calling tools, and cleanup.
@@ -63,6 +69,39 @@ export abstract class BaseMCPServerStdio implements MCPServer {
6369 }
6470}
6571
72+ export abstract class BaseMCPServerStreamableHttp implements MCPServer {
73+ public cacheToolsList : boolean ;
74+ protected _cachedTools : any [ ] | undefined = undefined ;
75+
76+ protected logger : Logger ;
77+ constructor ( options : MCPServerStreamableHttpOptions ) {
78+ this . logger =
79+ options . logger ??
80+ getLogger ( DEFAULT_STREAMABLE_HTTP_MCP_CLIENT_LOGGER_NAME ) ;
81+ this . cacheToolsList = options . cacheToolsList ?? true ;
82+ }
83+
84+ abstract get name ( ) : string ;
85+ abstract connect ( ) : Promise < void > ;
86+ abstract close ( ) : Promise < void > ;
87+ abstract listTools ( ) : Promise < any [ ] > ;
88+ abstract callTool (
89+ _toolName : string ,
90+ _args : Record < string , unknown > | null ,
91+ ) : Promise < CallToolResultContent > ;
92+
93+ /**
94+ * Logs a debug message when debug logging is enabled.
95+ * @param buildMessage A function that returns the message to log.
96+ */
97+ protected debugLog ( buildMessage : ( ) => string ) : void {
98+ if ( debug . enabled ( this . logger . namespace ) ) {
99+ // only when this is true, the function to build the string is called
100+ this . logger . debug ( buildMessage ( ) ) ;
101+ }
102+ }
103+ }
104+
66105/**
67106 * Minimum MCP tool data definition.
68107 * This type definition does not intend to cover all possible properties.
@@ -116,6 +155,40 @@ export class MCPServerStdio extends BaseMCPServerStdio {
116155 return this . underlying . callTool ( toolName , args ) ;
117156 }
118157}
158+
159+ export class MCPServerStreamableHttp extends BaseMCPServerStreamableHttp {
160+ private underlying : UnderlyingMCPServerStreamableHttp ;
161+ constructor ( options : MCPServerStreamableHttpOptions ) {
162+ super ( options ) ;
163+ this . underlying = new UnderlyingMCPServerStreamableHttp ( options ) ;
164+ }
165+ get name ( ) : string {
166+ return this . underlying . name ;
167+ }
168+ connect ( ) : Promise < void > {
169+ return this . underlying . connect ( ) ;
170+ }
171+ close ( ) : Promise < void > {
172+ return this . underlying . close ( ) ;
173+ }
174+ async listTools ( ) : Promise < MCPTool [ ] > {
175+ if ( this . cacheToolsList && this . _cachedTools ) {
176+ return this . _cachedTools ;
177+ }
178+ const tools = await this . underlying . listTools ( ) ;
179+ if ( this . cacheToolsList ) {
180+ this . _cachedTools = tools ;
181+ }
182+ return tools ;
183+ }
184+ callTool (
185+ toolName : string ,
186+ args : Record < string , unknown > | null ,
187+ ) : Promise < CallToolResultContent > {
188+ return this . underlying . callTool ( toolName , args ) ;
189+ }
190+ }
191+
119192/**
120193 * Fetches and flattens all tools from multiple MCP servers.
121194 * Logs and skips any servers that fail to respond.
@@ -292,6 +365,25 @@ export type MCPServerStdioOptions =
292365 | DefaultMCPServerStdioOptions
293366 | FullCommandMCPServerStdioOptions ;
294367
368+ export interface MCPServerStreamableHttpOptions {
369+ url : string ;
370+ cacheToolsList ?: boolean ;
371+ clientSessionTimeoutSeconds ?: number ;
372+ name ?: string ;
373+ logger ?: Logger ;
374+
375+ // ----------------------------------------------------
376+ // OAuth
377+ // import { OAuthClientProvider } from '@modelcontextprotocol/sdk/client/auth.js';
378+ authProvider ?: any ;
379+ // RequestInit
380+ requestInit ?: any ;
381+ // import { StreamableHTTPReconnectionOptions } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
382+ reconnectionOptions ?: any ;
383+ sessionId ?: string ;
384+ // ----------------------------------------------------
385+ }
386+
295387/**
296388 * Represents a JSON-RPC request message.
297389 */
0 commit comments