@@ -42,6 +42,7 @@ const logger = createLogger('session-client');
4242export class SessionClient extends EventEmitter implements IMcpClient {
4343 private bridgeClient : BridgeClient ;
4444 private sessionName : string ;
45+ private requestTimeout ?: number ; // Per-request timeout in seconds
4546
4647 constructor ( sessionName : string , bridgeClient : BridgeClient ) {
4748 super ( ) ;
@@ -50,6 +51,13 @@ export class SessionClient extends EventEmitter implements IMcpClient {
5051 this . setupNotificationForwarding ( ) ;
5152 }
5253
54+ /**
55+ * Set request timeout for all subsequent requests (in seconds)
56+ */
57+ setRequestTimeout ( timeout : number ) : void {
58+ this . requestTimeout = timeout ;
59+ }
60+
5361 /**
5462 * Set up notification forwarding from bridge client
5563 */
@@ -112,34 +120,56 @@ export class SessionClient extends EventEmitter implements IMcpClient {
112120 // Server info (single IPC call for all server information)
113121 async getServerDetails ( ) : Promise < ServerDetails > {
114122 return this . withRetry (
115- ( ) => this . bridgeClient . request ( 'getServerDetails' ) as Promise < ServerDetails > ,
123+ ( ) =>
124+ this . bridgeClient . request (
125+ 'getServerDetails' ,
126+ undefined ,
127+ this . requestTimeout
128+ ) as Promise < ServerDetails > ,
116129 'getServerDetails'
117130 ) ;
118131 }
119132
120133 // MCP operations
121134 async ping ( ) : Promise < void > {
122- return this . withRetry ( ( ) => this . bridgeClient . request ( 'ping' ) . then ( ( ) => undefined ) , 'ping' ) ;
135+ return this . withRetry (
136+ ( ) => this . bridgeClient . request ( 'ping' , undefined , this . requestTimeout ) . then ( ( ) => undefined ) ,
137+ 'ping'
138+ ) ;
123139 }
124140
125141 async listTools ( cursor ?: string ) : Promise < ListToolsResult > {
126142 return this . withRetry (
127- ( ) => this . bridgeClient . request ( 'listTools' , cursor ) as Promise < ListToolsResult > ,
143+ ( ) =>
144+ this . bridgeClient . request (
145+ 'listTools' ,
146+ cursor ,
147+ this . requestTimeout
148+ ) as Promise < ListToolsResult > ,
128149 'listTools'
129150 ) ;
130151 }
131152
132153 async callTool ( name : string , args ?: Record < string , unknown > ) : Promise < CallToolResult > {
133154 return this . withRetry (
134155 ( ) =>
135- this . bridgeClient . request ( 'callTool' , { name, arguments : args } ) as Promise < CallToolResult > ,
156+ this . bridgeClient . request (
157+ 'callTool' ,
158+ { name, arguments : args } ,
159+ this . requestTimeout
160+ ) as Promise < CallToolResult > ,
136161 'callTool'
137162 ) ;
138163 }
139164
140165 async listResources ( cursor ?: string ) : Promise < ListResourcesResult > {
141166 return this . withRetry (
142- ( ) => this . bridgeClient . request ( 'listResources' , cursor ) as Promise < ListResourcesResult > ,
167+ ( ) =>
168+ this . bridgeClient . request (
169+ 'listResources' ,
170+ cursor ,
171+ this . requestTimeout
172+ ) as Promise < ListResourcesResult > ,
143173 'listResources'
144174 ) ;
145175 }
@@ -149,54 +179,78 @@ export class SessionClient extends EventEmitter implements IMcpClient {
149179 ( ) =>
150180 this . bridgeClient . request (
151181 'listResourceTemplates' ,
152- cursor
182+ cursor ,
183+ this . requestTimeout
153184 ) as Promise < ListResourceTemplatesResult > ,
154185 'listResourceTemplates'
155186 ) ;
156187 }
157188
158189 async readResource ( uri : string ) : Promise < ReadResourceResult > {
159190 return this . withRetry (
160- ( ) => this . bridgeClient . request ( 'readResource' , { uri } ) as Promise < ReadResourceResult > ,
191+ ( ) =>
192+ this . bridgeClient . request (
193+ 'readResource' ,
194+ { uri } ,
195+ this . requestTimeout
196+ ) as Promise < ReadResourceResult > ,
161197 'readResource'
162198 ) ;
163199 }
164200
165201 async subscribeResource ( uri : string ) : Promise < void > {
166202 return this . withRetry (
167- ( ) => this . bridgeClient . request ( 'subscribeResource' , { uri } ) . then ( ( ) => undefined ) ,
203+ ( ) =>
204+ this . bridgeClient
205+ . request ( 'subscribeResource' , { uri } , this . requestTimeout )
206+ . then ( ( ) => undefined ) ,
168207 'subscribeResource'
169208 ) ;
170209 }
171210
172211 async unsubscribeResource ( uri : string ) : Promise < void > {
173212 return this . withRetry (
174- ( ) => this . bridgeClient . request ( 'unsubscribeResource' , { uri } ) . then ( ( ) => undefined ) ,
213+ ( ) =>
214+ this . bridgeClient
215+ . request ( 'unsubscribeResource' , { uri } , this . requestTimeout )
216+ . then ( ( ) => undefined ) ,
175217 'unsubscribeResource'
176218 ) ;
177219 }
178220
179221 async listPrompts ( cursor ?: string ) : Promise < ListPromptsResult > {
180222 return this . withRetry (
181- ( ) => this . bridgeClient . request ( 'listPrompts' , cursor ) as Promise < ListPromptsResult > ,
223+ ( ) =>
224+ this . bridgeClient . request (
225+ 'listPrompts' ,
226+ cursor ,
227+ this . requestTimeout
228+ ) as Promise < ListPromptsResult > ,
182229 'listPrompts'
183230 ) ;
184231 }
185232
186233 async getPrompt ( name : string , args ?: Record < string , string > ) : Promise < GetPromptResult > {
187234 return this . withRetry (
188235 ( ) =>
189- this . bridgeClient . request ( 'getPrompt' , {
190- name,
191- arguments : args ,
192- } ) as Promise < GetPromptResult > ,
236+ this . bridgeClient . request (
237+ 'getPrompt' ,
238+ {
239+ name,
240+ arguments : args ,
241+ } ,
242+ this . requestTimeout
243+ ) as Promise < GetPromptResult > ,
193244 'getPrompt'
194245 ) ;
195246 }
196247
197248 async setLoggingLevel ( level : LoggingLevel ) : Promise < void > {
198249 return this . withRetry (
199- ( ) => this . bridgeClient . request ( 'setLoggingLevel' , level ) . then ( ( ) => undefined ) ,
250+ ( ) =>
251+ this . bridgeClient
252+ . request ( 'setLoggingLevel' , level , this . requestTimeout )
253+ . then ( ( ) => undefined ) ,
200254 'setLoggingLevel'
201255 ) ;
202256 }
@@ -231,10 +285,15 @@ export async function createSessionClient(sessionName: string): Promise<SessionC
231285 */
232286export async function withSessionClient < T > (
233287 sessionName : string ,
234- callback : ( client : IMcpClient ) => Promise < T >
288+ callback : ( client : IMcpClient ) => Promise < T > ,
289+ options ?: { timeout ?: number }
235290) : Promise < T > {
236291 const client = await createSessionClient ( sessionName ) ;
237292
293+ if ( options ?. timeout !== undefined ) {
294+ client . setRequestTimeout ( options . timeout ) ;
295+ }
296+
238297 try {
239298 return await callback ( client ) ;
240299 } finally {
0 commit comments