@@ -38,7 +38,8 @@ import {
3838 type Tool ,
3939 type UnsubscribeRequest ,
4040 ToolListChangedNotificationSchema ,
41- ToolListChangedOptions
41+ ToolListChangedOptions ,
42+ ToolListChangedOptionsSchema
4243} from '../types.js' ;
4344import { AjvJsonSchemaValidator } from '../validation/ajv-provider.js' ;
4445import type { JsonSchemaType , JsonSchemaValidator , jsonSchemaValidator } from '../validation/types.js' ;
@@ -83,11 +84,11 @@ export type ClientOptions = ProtocolOptions & {
8384 /**
8485 * Configure automatic refresh behavior for tool list changed notifications
8586 *
87+ * Here's an example of how to get the updated tool list when the tool list changed notification is received:
88+ *
8689 * @example
87- * ```ts
90+ * ```typescript
8891 * {
89- * autoRefresh: true,
90- * debounceMs: 300,
9192 * onToolListChanged: (err, tools) => {
9293 * if (err) {
9394 * console.error('Failed to refresh tool list:', err);
@@ -99,10 +100,13 @@ export type ClientOptions = ProtocolOptions & {
99100 * }
100101 * ```
101102 *
103+ * Here is an example of how to manually refresh the tool list when the tool list changed notification is received:
104+ *
102105 * @example
103- * ```ts
106+ * ```typescript
104107 * {
105108 * autoRefresh: false,
109+ * debounceMs: 0,
106110 * onToolListChanged: (err, tools) => {
107111 * // err is always null when autoRefresh is false
108112 *
@@ -484,36 +488,45 @@ export class Client<
484488 public setToolListChangedOptions ( options : ToolListChangedOptions | null ) : void {
485489 // Set up tool list changed options and add notification handler
486490 if ( options ) {
487- const toolListChangedOptions : ToolListChangedOptions = {
488- autoRefresh : ! ! options . autoRefresh ,
489- debounceMs : options . debounceMs ?? 300 ,
490- onToolListChanged : options . onToolListChanged
491- } ;
491+ const parseResult = ToolListChangedOptionsSchema . safeParse ( options ) ;
492+ if ( parseResult . error ) {
493+ throw new Error ( `Tool List Changed options are invalid: ${ parseResult . error . message } ` ) ;
494+ }
495+
496+ const toolListChangedOptions = parseResult . data ;
492497 this . _toolListChangedOptions = toolListChangedOptions ;
498+
499+ const refreshToolList = async ( ) => {
500+ let tools : Tool [ ] | null = null ;
501+ let error : Error | null = null ;
502+ try {
503+ const result = await this . listTools ( ) ;
504+ tools = result . tools ;
505+ } catch ( e ) {
506+ error = e instanceof Error ? e : new Error ( String ( e ) ) ;
507+ }
508+ toolListChangedOptions . onToolListChanged ?.( error , tools ) ;
509+ } ;
510+
493511 this . setNotificationHandler ( ToolListChangedNotificationSchema , ( ) => {
494512 // If autoRefresh is false, call the callback for the notification, but without tools data
495513 if ( ! toolListChangedOptions . autoRefresh ) {
496514 toolListChangedOptions . onToolListChanged ?.( null , null ) ;
497515 return ;
498516 }
499517
500- // Clear any pending debounce timer
501- if ( this . _toolListChangedDebounceTimer ) {
502- clearTimeout ( this . _toolListChangedDebounceTimer ) ;
503- }
504-
505- // Set up debounced refresh
506- this . _toolListChangedDebounceTimer = setTimeout ( async ( ) => {
507- let tools : Tool [ ] | null = null ;
508- let error : Error | null = null ;
509- try {
510- const result = await this . listTools ( ) ;
511- tools = result . tools ;
512- } catch ( e ) {
513- error = e instanceof Error ? e : new Error ( String ( e ) ) ;
518+ if ( toolListChangedOptions . debounceMs ) {
519+ // Clear any pending debounce timer
520+ if ( this . _toolListChangedDebounceTimer ) {
521+ clearTimeout ( this . _toolListChangedDebounceTimer ) ;
514522 }
515- toolListChangedOptions . onToolListChanged ?.( error , tools ) ;
516- } , toolListChangedOptions . debounceMs ) ;
523+
524+ // Set up debounced refresh
525+ this . _toolListChangedDebounceTimer = setTimeout ( refreshToolList , toolListChangedOptions . debounceMs ) ;
526+ } else {
527+ // No debounce, refresh immediately
528+ refreshToolList ( ) ;
529+ }
517530 } ) ;
518531 }
519532 // Reset tool list changed options and remove notification handler
0 commit comments