Skip to content

Batch toolChange notifications for tool registry changes #374

Open

Description

@khushalsagar

We currently dispatch a toolChange event for every unregistration here and every registration here. This doesn't seem ideal since it's likely that the tool registry is being modified multiple times in one task.

This should likely be done with a JS task finishes to better batch these events.

Activity

  1. domfarolino commented on Nov 4, 2026

    @domfarolino
    Collaborator

    I'm not strictly opposed, but is there any precedent for this kind of batching besides the microtask that image element mutations queue? /cc @jakearchibald

    since it's likely that the tool registry is being modified multiple times in one task.

    Is this true?

  2. khushalsagar commented on Nov 4, 2026

    @khushalsagar
    CollaboratorAuthor

    Is this true?

    I think it's common with frameworks. #199 brought up a similar issue, React apps typically tie a tool's lifetime to a component: register in useEffect, abort in cleanup. React runs all of an update's effect cleanups and setups in one synchronous pass, so a single render can unregister and register many tools at once in a single task.

    updateTool()/keys from #199 would help when the same component re-renders with new state. But on a route change, components are actually removed and new ones are added, so we'd still fire one event per tool. Batching to one toolchange per task is better to avoid redundant notifications.

    I'm not sure about the difference between using a task vs microtask for this. Can you clarify?

  3. webmcpx commented on Nov 9, 2026

    @webmcpx

    We currently mitigate this on the consumer side in webmcpx, maintained by @julianosig: page events are debounced, and refreshes coalesce into one active refresh plus at most one pending refresh. There is a burst test.

    This is not the same as batching the platform event per task. It does provide a case for testing: mount several tool-owning components, replace them during a route change, and check that the final catalog is observed without refreshing once per registration. Would that be useful as an implementation fixture?

  4. khushalsagar commented on Nov 9, 2026

    @khushalsagar
    CollaboratorAuthor

    sorry i didn't follow what you meant by "Would that be useful as an implementation fixture?".

    But this data point helps to make a case for better toolChange batching that once per register/unregister. Doing it once per task seems like the most straightforward way but i'm open to other options.

  5. khushalsagar commented on Nov 10, 2026

    @khushalsagar
    CollaboratorAuthor

    Another related thing came up during the review of #373. If we start batching these notifications, its possible the document toggles to "not fully active" before the toolChange events are dispatched. Like an iframe is detached before the task to dispatch the event runs. we should probably dispatch the suppressed notifications when the document becomes fully active again.

    @markafoltz FYI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions