@@ -62,6 +62,11 @@ export type ClientOptions = ProtocolOptions & {
62
62
* Default: 300
63
63
*/
64
64
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 ;
65
70
} ;
66
71
} ;
67
72
@@ -103,9 +108,11 @@ export class Client<
103
108
private _serverVersion ?: Implementation ;
104
109
private _capabilities : ClientCapabilities ;
105
110
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
+ } ;
109
116
private _toolRefreshDebounceTimer ?: ReturnType < typeof setTimeout > ;
110
117
111
118
/**
@@ -126,6 +133,7 @@ export class Client<
126
133
this . _toolRefreshOptions = {
127
134
autoRefresh : options ?. toolRefreshOptions ?. autoRefresh ?? true ,
128
135
debounceMs : options ?. toolRefreshOptions ?. debounceMs ?? 500 ,
136
+ onError : options ?. toolRefreshOptions ?. onError ,
129
137
} ;
130
138
131
139
// Set up notification handlers
@@ -147,7 +155,12 @@ export class Client<
147
155
// Set up debounced refresh
148
156
this . _toolRefreshDebounceTimer = setTimeout ( ( ) => {
149
157
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
+ }
151
164
} ) ;
152
165
} , this . _toolRefreshOptions . debounceMs ) ;
153
166
}
@@ -166,7 +179,12 @@ export class Client<
166
179
this . onToolListChanged ?.( result . tools ) ;
167
180
}
168
181
} 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
+ }
170
188
// Still call the callback even if refresh failed
171
189
this . onToolListChanged ?.( undefined ) ;
172
190
}
@@ -200,18 +218,28 @@ export class Client<
200
218
if ( options . debounceMs !== undefined ) {
201
219
this . _toolRefreshOptions . debounceMs = options . debounceMs ;
202
220
}
221
+ if ( options . onError !== undefined ) {
222
+ this . _toolRefreshOptions . onError = options . onError ;
223
+ }
203
224
}
204
225
}
205
226
206
227
/**
207
228
* Gets the current tool refresh options
208
229
*/
209
- public getToolRefreshOptions ( ) : Required <
210
- NonNullable < ClientOptions [ "toolRefreshOptions" ] >
211
- > {
230
+ public getToolRefreshOptions ( ) : typeof this . _toolRefreshOptions {
212
231
return { ...this . _toolRefreshOptions } ;
213
232
}
214
233
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
+
215
243
/**
216
244
* Manually triggers a refresh of the tools list
217
245
*/
@@ -226,7 +254,12 @@ export class Client<
226
254
const result = await this . listTools ( ) ;
227
255
return result . tools ;
228
256
} 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
+ }
230
263
return undefined ;
231
264
}
232
265
}
0 commit comments