You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make tools Document-scoped instead of Window-scoped #173
The case where this is observable is when you register tools with the initial about:blank Document, and then navigate away from it. Per the current spec, after the navigation, calls to navigator.modelContext return the same object that holds the same map of tools, which means tools are effectively shared across this navigation.
I recently caught this bug in Chromium when working on the iframe + permission policy implementation, and ended up changing Blink's implementation such that:
The backing ModelContext interface object is stored on Document instead of Navigator
The navigator.modelContext IDL getter doesn't just return the same object; instead it consults its Window's active Document, and returns itsModelContext object. It returns null if the Window isn't available (detached document) or if the document isn't fully active (bf-cache).
I'll update the spec to agree with this.
Follow-up: API placement
An oddity of the above is that the API is effectively scoped to the Document, yet it lives off of navigator. I personally never liked putting things on navigator, and would recommend that we do one of two things:
Move it to Document, giving us document.modelContext. This seems more intuitive
I agree document is the most intuitive as far as web pages go. If we eventually do implement WebMCP in service workers though, I'm not sure where the best home for modelContext would be, if not Navigator. Maybe self?
Good point about eventual Worker-exposed tools. The global (self) would make sense to cater to all contexts, but if the options really becomes self vs navigator, I'm wondering if we should even bother moving it at all...
The global (i.e. self, window) may be the right compromise here. We could expose the modelContext namespace there but use the currently active document as the backing storage so that tools aren't pesisted across navigations.
I spoke with the TAG and they recommended that we move the modelContext object to document, which I am supportive of, with a still-reasonable path for extending to workers in the future: w3ctag/design-reviews#1231 (comment). Does anyone object?
I spoke with the TAG and they recommended that we move the modelContext object to document, which I am supportive of, with a still-reasonable path for extending to workers in the future: w3ctag/design-reviews#1231 (comment). Does anyone object?
Well self.modelContext would mean the same thing as window.modelContext, since they are the same object. But lots of APIs hang off of window without too much confusion. Do you think that's alright?
I meant some developers (or agents nowadays) could be confused because they see self.modelContext without realizing they're in a worker context and start using it now in a window context which suddenly doesn't work because window.self.modelContext is not a thing IIUC, only window.self.document.modelContext right?
That's correct. But you'll have to know what context you're in even if we put the API on self. You need to know that just because self.modelContext exists, you can't always run:
self.modelContext.registerTool({name: "scrape_dom",description: "the DOM scraper"asyncexecute()=>document.querySelector('body').textContent;})
Activity
bwalderman commentedon Apr 28, 2026
I agree
documentis the most intuitive as far as web pages go. If we eventually do implement WebMCP in service workers though, I'm not sure where the best home formodelContextwould be, if notNavigator. Maybeself?domfarolino commentedon May 1, 2026
Good point about eventual Worker-exposed tools. The global (
self) would make sense to cater to all contexts, but if the options really becomesselfvsnavigator, I'm wondering if we should even bother moving it at all...bwalderman commentedon May 1, 2026
The global (i.e.
self,window) may be the right compromise here. We could expose themodelContextnamespace there but use the currently activedocumentas the backing storage so that tools aren't pesisted across navigations.domfarolino commentedon May 18, 2026
Hmm, but that's no different from keeping it on navigator, except for the fact that
selfis just more "general", but with more risk of collision.domfarolino commentedon May 20, 2026
I spoke with the TAG and they recommended that we move the
modelContextobject todocument, which I am supportive of, with a still-reasonable path for extending to workers in the future: w3ctag/design-reviews#1231 (comment). Does anyone object?domfarolino commentedon May 20, 2026
I spoke with the TAG and they recommended that we move the
modelContextobject todocument, which I am supportive of, with a still-reasonable path for extending to workers in the future: w3ctag/design-reviews#1231 (comment). Does anyone object?bwalderman commentedon May 21, 2026
SGTM. Thanks for bringing this to the TAG.
beaufortfrancois commentedon May 21, 2026
I do think
self.modelContextcould be potentially confusing givenwindow.selfin https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-self but we'll see. I won't oppose todocument.modelContext.domfarolino commentedon May 21, 2026
Well
self.modelContextwould mean the same thing aswindow.modelContext, since they are the same object. But lots of APIs hang off ofwindowwithout too much confusion. Do you think that's alright?beaufortfrancois commentedon May 22, 2026
I meant some developers (or agents nowadays) could be confused because they see
self.modelContextwithout realizing they're in a worker context and start using it now in a window context which suddenly doesn't work becausewindow.self.modelContextis not a thing IIUC, onlywindow.self.document.modelContextright?domfarolino commentedon May 22, 2026
That's correct. But you'll have to know what context you're in even if we put the API on
self. You need to know that just becauseself.modelContextexists, you can't always run:because the DOM doesn't exist in workers.
WebMCP: Expose document.modelContext
WebMCP: Expose document.modelContext
WebMCP: Expose document.modelContext
WebMCP: Expose document.modelContext
chore: bump @runtypelabs/webmcp-polyfill to ^0.3.0 (document.modelCon…
feat(webmcp-polyfill): install on document.modelContext per WebMCP PR #…
Bug 2042248 [wpt PR 60158] - WebMCP: Expose document.modelContext, a=…
Bug 2042248 [wpt PR 60158] - WebMCP: Expose document.modelContext, a=…
Bug 2042248 [wpt PR 60158] - WebMCP: Expose document.modelContext, a=…
Bug 2042248 [wpt PR 60158] - WebMCP: Expose document.modelContext, a=…
feat(widget): WebMCP consumption — snapshot page tools per turn, exec…
Align WebMCP imperative API with Document-scoped modelContext
refactor(webmcp): migrate from navigator.modelContext to document.mod…
Bug 2042248 [wpt PR 60158] - WebMCP: Expose document.modelContext, a=…