Skip to content

Handle same-name re-registration during input serialization - #347

Merged
domfarolino merged 1 commit into
mainfrom
register-tool-duplicate-name-recheck
Oct 15, 2026
Merged

domfarolino merged 1 commit into
mainfrom
register-tool-duplicate-name-recheck

Conversation

@domfarolino

@domfarolino domfarolino commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Fix registerTool()'s duplicate tool name check, which can go stale during inputSchema serialization

registerTool() checks whether a tool with the same name already exists before serializing inputSchema. But serialization can run script (e.g., a toJSON() method), and that script can register another tool with the same name in the meantime. The algorithm would then go on to overwrite that tool's entry in the tool map.

This PR re-checks the tool map after serialization, right next to the "fully active" re-check added in #345, and rejects with an InvalidStateError, just like the up-front check.

Example:

document.modelContext.registerTool({
  name: 'same_name',
  description: 'Outer tool',
  inputSchema: {
    toJSON() {
      document.modelContext.registerTool(
          {name: 'same_name', description: 'Inner tool', execute() {}});
      return {};
    },
  },
  execute() {},
});  // Now rejects with an InvalidStateError; the inner tool stays registered.

See https://crrev.com/c/8583183 for WPTs.


<!--
    This comment and the below content is programmatically generated.
    You may add a comma-separated list of anchors you'd like a
    direct link to below (e.g. #idl-serializers, #idl-sequence):

    Don't remove this comment or modify anything below this line.
    If you don't want a preview generated for this pull request,
    just replace the whole of this comment's content by "no preview"
    and remove what's below.
-->
***
<a href="https://pr-preview.s3.amazonaws.com/webmachinelearning/webmcp/pull/347.html" title="Last updated on Oct 14, 2026, 10:54 PM UTC (5534fd4)">Preview</a> | <a href="https://pr-preview.s3.amazonaws.com/webmachinelearning/webmcp/347/1636b33...5534fd4.html" title="Last updated on Oct 14, 2026, 10:54 PM UTC (5534fd4)">Diff</a>

@domfarolino
domfarolino merged commit 3279798 into main Oct 15, 2026
2 checks passed
@domfarolino
domfarolino deleted the register-tool-duplicate-name-recheck branch October 15, 2026 00:00
github-actions Bot added a commit that referenced this pull request Oct 15, 2026
SHA: 3279798
Reason: push, by domfarolino

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
brave-builds pushed a commit to brave/chromium that referenced this pull request Oct 15, 2026
`registerTool()` rejects a duplicate tool name up front, but then
serializes `inputSchema`, which can run script (via `toJSON()`) that
registers another tool with the same name. `registerTool()` then still
sends the duplicate to the browser, which kills the renderer.

Check for a duplicate name again after serialization, next to the
existing document-activity re-check, and reject with the same
`InvalidStateError` as the up-front check.

See webmachinelearning/webmcp#347 for the spec
fix.

R=khushalsagar

Bug: 573722078
Change-Id: I044e3ad96fb00c624829eb182047aa30575f81a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8583183
Commit-Queue: Dominic Farolino <dom@chromium.org>
Reviewed-by: Khushal Sagar <khushalsagar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1719227}
beckysiegel pushed a commit to chromium/chromium that referenced this pull request Oct 15, 2026
…inputSchema"

This reverts commit bbf0329.

Reason for revert:
LUCI Bisection has identified this change as the culprit of a build failure. See the analysis: https://ci.chromium.org/ui/p/chromium/bisection/compile-analysis/b/8667914546796401745

Sample failed build: https://ci.chromium.org/b/8667914546796401745

If this is a false positive, please report it at http://b.corp.google.com/createIssue?component=1199205&description=Analysis%3A+https%3A%2F%2Fchromium-review.googlesource.com%2Fc%2Fchromium%2Fsrc%2F%2B%2F8583183&format=PLAIN&priority=P3&title=Wrongly+blamed+https%3A%2F%2Fci.chromium.org%2Fui%2Fp%2Fchromium%2Fbisection%2Fcompile-analysis%2Fb%2F8667914546796401745&type=BUG

Original change's description:
> WebMCP: Re-check for a duplicate tool name after serializing inputSchema
>
> `registerTool()` rejects a duplicate tool name up front, but then
> serializes `inputSchema`, which can run script (via `toJSON()`) that
> registers another tool with the same name. `registerTool()` then still
> sends the duplicate to the browser, which kills the renderer.
>
> Check for a duplicate name again after serialization, next to the
> existing document-activity re-check, and reject with the same
> `InvalidStateError` as the up-front check.
>
> See webmachinelearning/webmcp#347 for the spec
> fix.
>
> R=khushalsagar
>
> Bug: 573722078
> Change-Id: I044e3ad96fb00c624829eb182047aa30575f81a6
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8583183
> Commit-Queue: Dominic Farolino <dom@chromium.org>
> Reviewed-by: Khushal Sagar <khushalsagar@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#1719227}
>

Bug: 573722078
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Change-Id: I76d0fe146b570f7b8590eaadea632794afe24ea3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8575491
Commit-Queue: luci-bisection@appspot.gserviceaccount.com <luci-bisection@appspot.gserviceaccount.com>
Bot-Commit: luci-bisection@appspot.gserviceaccount.com <luci-bisection@appspot.gserviceaccount.com>
Owners-Override: luci-bisection@appspot.gserviceaccount.com <luci-bisection@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/heads/main@{#1719232}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Oct 15, 2026
`registerTool()` rejects a duplicate tool name up front, but then
serializes `inputSchema`, which can run script (via `toJSON()`) that
registers another tool with the same name. `registerTool()` then still
sends the duplicate to the browser, which kills the renderer.

Check for a duplicate name again after serialization, next to the
existing document-activity re-check, and reject with the same
`InvalidStateError` as the up-front check.

See webmachinelearning/webmcp#347 for the spec
fix.

R=khushalsagar

Bug: 573722078
Change-Id: I044e3ad96fb00c624829eb182047aa30575f81a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8583183
Commit-Queue: Dominic Farolino <dom@chromium.org>
Reviewed-by: Khushal Sagar <khushalsagar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1719227}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Oct 15, 2026
`registerTool()` rejects a duplicate tool name up front, but then
serializes `inputSchema`, which can run script (via `toJSON()`) that
registers another tool with the same name. `registerTool()` then still
sends the duplicate to the browser, which kills the renderer.

Check for a duplicate name again after serialization, next to the
existing document-activity re-check, and reject with the same
`InvalidStateError` as the up-front check.

See webmachinelearning/webmcp#347 for the spec
fix.

R=khushalsagar

Bug: 573722078
Change-Id: I044e3ad96fb00c624829eb182047aa30575f81a6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8583183
Commit-Queue: Dominic Farolino <dom@chromium.org>
Reviewed-by: Khushal Sagar <khushalsagar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1719227}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Oct 15, 2026
…inputSchema"

This reverts commit bbf032940645a5dcea55e6dc1bdb34b8c989650f.

Reason for revert:
LUCI Bisection has identified this change as the culprit of a build failure. See the analysis: https://ci.chromium.org/ui/p/chromium/bisection/compile-analysis/b/8667914546796401745

Sample failed build: https://ci.chromium.org/b/8667914546796401745

If this is a false positive, please report it at http://b.corp.google.com/createIssue?component=1199205&description=Analysis%3A+https%3A%2F%2Fchromium-review.googlesource.com%2Fc%2Fchromium%2Fsrc%2F%2B%2F8583183&format=PLAIN&priority=P3&title=Wrongly+blamed+https%3A%2F%2Fci.chromium.org%2Fui%2Fp%2Fchromium%2Fbisection%2Fcompile-analysis%2Fb%2F8667914546796401745&type=BUG

Original change's description:
> WebMCP: Re-check for a duplicate tool name after serializing inputSchema
>
> `registerTool()` rejects a duplicate tool name up front, but then
> serializes `inputSchema`, which can run script (via `toJSON()`) that
> registers another tool with the same name. `registerTool()` then still
> sends the duplicate to the browser, which kills the renderer.
>
> Check for a duplicate name again after serialization, next to the
> existing document-activity re-check, and reject with the same
> `InvalidStateError` as the up-front check.
>
> See webmachinelearning/webmcp#347 for the spec
> fix.
>
> R=khushalsagar
>
> Bug: 573722078
> Change-Id: I044e3ad96fb00c624829eb182047aa30575f81a6
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8583183
> Commit-Queue: Dominic Farolino <dom@chromium.org>
> Reviewed-by: Khushal Sagar <khushalsagar@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#1719227}
>

Bug: 573722078
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Change-Id: I76d0fe146b570f7b8590eaadea632794afe24ea3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8575491
Commit-Queue: luci-bisection@appspot.gserviceaccount.com <luci-bisection@appspot.gserviceaccount.com>
Bot-Commit: luci-bisection@appspot.gserviceaccount.com <luci-bisection@appspot.gserviceaccount.com>
Owners-Override: luci-bisection@appspot.gserviceaccount.com <luci-bisection@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/heads/main@{#1719232}
Comment thread index.bs
beckysiegel pushed a commit to chromium/chromium that referenced this pull request Oct 15, 2026
…inputSchema"

This is a reland of commit bbf0329,
which was reverted in 8a0f949 for breaking the build.

The original CL was written against a `ModelContext` whose `tool_map_`
was keyed by tool name, and checked for a duplicate with
`tool_map_.find(tool->name())`. https://crrev.com/c/8510757 landed in
between and changed `tool_map_` to be keyed by tool ID, so that no
longer compiled. The reland uses `FindToolDataByName()`, just like the
up-front duplicate check does.

Original change's description:
> WebMCP: Re-check for a duplicate tool name after serializing inputSchema
>
> `registerTool()` rejects a duplicate tool name up front, but then
> serializes `inputSchema`, which can run script (via `toJSON()`) that
> registers another tool with the same name. `registerTool()` then still
> sends the duplicate to the browser, which kills the renderer.
>
> Check for a duplicate name again after serialization, next to the
> existing document-activity re-check, and reject with the same
> `InvalidStateError` as the up-front check.
>
> See webmachinelearning/webmcp#347 for the spec
> fix.
>
> R=khushalsagar
>
> Bug: 573722078
> Change-Id: I044e3ad96fb00c624829eb182047aa30575f81a6

R=khushalsagar

Bug: 573722078
Change-Id: I6b07f7ba6900724d4695ac5f3ee4524e0149736c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8587455
Commit-Queue: Dominic Farolino <dom@chromium.org>
Commit-Queue: Fr <beaufort.francois@gmail.com>
Auto-Submit: Dominic Farolino <dom@chromium.org>
Reviewed-by: Fr <beaufort.francois@gmail.com>
Cr-Commit-Position: refs/heads/main@{#1719748}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Oct 15, 2026
…inputSchema"

This is a reland of commit bbf032940645a5dcea55e6dc1bdb34b8c989650f,
which was reverted in 8a0f949dda972 for breaking the build.

The original CL was written against a `ModelContext` whose `tool_map_`
was keyed by tool name, and checked for a duplicate with
`tool_map_.find(tool->name())`. https://crrev.com/c/8510757 landed in
between and changed `tool_map_` to be keyed by tool ID, so that no
longer compiled. The reland uses `FindToolDataByName()`, just like the
up-front duplicate check does.

Original change's description:
> WebMCP: Re-check for a duplicate tool name after serializing inputSchema
>
> `registerTool()` rejects a duplicate tool name up front, but then
> serializes `inputSchema`, which can run script (via `toJSON()`) that
> registers another tool with the same name. `registerTool()` then still
> sends the duplicate to the browser, which kills the renderer.
>
> Check for a duplicate name again after serialization, next to the
> existing document-activity re-check, and reject with the same
> `InvalidStateError` as the up-front check.
>
> See webmachinelearning/webmcp#347 for the spec
> fix.
>
> R=khushalsagar
>
> Bug: 573722078
> Change-Id: I044e3ad96fb00c624829eb182047aa30575f81a6

R=khushalsagar

Bug: 573722078
Change-Id: I6b07f7ba6900724d4695ac5f3ee4524e0149736c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8587455
Commit-Queue: Dominic Farolino <dom@chromium.org>
Commit-Queue: Fr <beaufort.francois@gmail.com>
Auto-Submit: Dominic Farolino <dom@chromium.org>
Reviewed-by: Fr <beaufort.francois@gmail.com>
Cr-Commit-Position: refs/heads/main@{#1719748}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants