@@ -62,6 +62,11 @@ export type ClientOptions = ProtocolOptions & {
6262 * Default: 300
6363 */
6464 debounceMs ?: number ;
65+ /**
66+ * Optional callback for handling tool list refresh errors.
67+ * When provided, this will be called instead of logging to console.
68+ */
69+ onError ?: ( error : Error ) => void ;
6570 } ;
6671} ;
6772
@@ -103,9 +108,11 @@ export class Client<
103108 private _serverVersion ?: Implementation ;
104109 private _capabilities : ClientCapabilities ;
105110 private _instructions ?: string ;
106- private _toolRefreshOptions : Required <
107- NonNullable < ClientOptions [ "toolRefreshOptions" ] >
108- > ;
111+ private _toolRefreshOptions : {
112+ autoRefresh : boolean ;
113+ debounceMs : number ;
114+ onError ?: ( error : Error ) => void ;
115+ } ;
109116 private _toolRefreshDebounceTimer ?: ReturnType < typeof setTimeout > ;
110117
111118 /**
@@ -126,6 +133,7 @@ export class Client<
126133 this . _toolRefreshOptions = {
127134 autoRefresh : options ?. toolRefreshOptions ?. autoRefresh ?? true ,
128135 debounceMs : options ?. toolRefreshOptions ?. debounceMs ?? 500 ,
136+ onError : options ?. toolRefreshOptions ?. onError ,
129137 } ;
130138
131139 // Set up notification handlers
@@ -147,7 +155,12 @@ export class Client<
147155 // Set up debounced refresh
148156 this . _toolRefreshDebounceTimer = setTimeout ( ( ) => {
149157 this . _refreshToolsList ( ) . catch ( ( error ) => {
150- console . error ( "Failed to refresh tools list:" , error ) ;
158+ // Use error callback if provided, otherwise log to console
159+ if ( this . _toolRefreshOptions . onError ) {
160+ this . _toolRefreshOptions . onError ( error instanceof Error ? error : new Error ( String ( error ) ) ) ;
161+ } else {
162+ console . error ( "Failed to refresh tools list:" , error ) ;
163+ }
151164 } ) ;
152165 } , this . _toolRefreshOptions . debounceMs ) ;
153166 }
@@ -166,7 +179,12 @@ export class Client<
166179 this . onToolListChanged ?.( result . tools ) ;
167180 }
168181 } catch ( error ) {
169- console . error ( "Failed to refresh tools list:" , error ) ;
182+ // Use error callback if provided, otherwise log to console
183+ if ( this . _toolRefreshOptions . onError ) {
184+ this . _toolRefreshOptions . onError ( error instanceof Error ? error : new Error ( String ( error ) ) ) ;
185+ } else {
186+ console . error ( "Failed to refresh tools list:" , error ) ;
187+ }
170188 // Still call the callback even if refresh failed
171189 this . onToolListChanged ?.( undefined ) ;
172190 }
@@ -200,18 +218,28 @@ export class Client<
200218 if ( options . debounceMs !== undefined ) {
201219 this . _toolRefreshOptions . debounceMs = options . debounceMs ;
202220 }
221+ if ( options . onError !== undefined ) {
222+ this . _toolRefreshOptions . onError = options . onError ;
223+ }
203224 }
204225 }
205226
206227 /**
207228 * Gets the current tool refresh options
208229 */
209- public getToolRefreshOptions ( ) : Required <
210- NonNullable < ClientOptions [ "toolRefreshOptions" ] >
211- > {
230+ public getToolRefreshOptions ( ) : typeof this . _toolRefreshOptions {
212231 return { ...this . _toolRefreshOptions } ;
213232 }
214233
234+ /**
235+ * Sets an error handler for tool list refresh errors
236+ *
237+ * @param handler Function to call when a tool list refresh error occurs
238+ */
239+ public setToolRefreshErrorHandler ( handler : ( error : Error ) => void ) : void {
240+ this . _toolRefreshOptions . onError = handler ;
241+ }
242+
215243 /**
216244 * Manually triggers a refresh of the tools list
217245 */
@@ -226,7 +254,12 @@ export class Client<
226254 const result = await this . listTools ( ) ;
227255 return result . tools ;
228256 } catch ( error ) {
229- console . error ( "Failed to manually refresh tools list:" , error ) ;
257+ // Use error callback if provided, otherwise log to console
258+ if ( this . _toolRefreshOptions . onError ) {
259+ this . _toolRefreshOptions . onError ( error instanceof Error ? error : new Error ( String ( error ) ) ) ;
260+ } else {
261+ console . error ( "Failed to manually refresh tools list:" , error ) ;
262+ }
230263 return undefined ;
231264 }
232265 }
0 commit comments