Repository navigation
fix(core): fail the WebMCP search_site call on error - #3894
Conversation
WebMCP serializes a tool's fulfilled value to JSON and reports only a
rejected promise as a failed call (spec: "invoke a tool"). The tool
resolved an MCP-style { content, isError } envelope instead, so a 500
or an empty query reached the agent as a successful result, and real
results arrived as a JSON string nested inside JSON.
Throw on failure and resolve the result array directly.
🦋 Changeset detectedLatest commit: a12f3e9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
This is the right fix for the reported bug. The WebMCP spec serializes whatever execute fulfills with and only treats a rejected promise as a failed tool call, so returning an MCP-style { content, isError } envelope from a WebMCP tool was always going to report errors as successes and double-encode results. The PR correctly flips the contract: errors now throw, and the result array is returned directly so the browser serializes it once.
I checked the changed source, the unit tests, the changeset, and the consumer in WebMcpSearch.astro. The createSiteSearchTool return type is internal to this module and is only consumed through registerSiteSearchTool, so narrowing execute to Promise<SiteSearchToolResult[]> is safe. The tests now assert what an agent actually receives via a spec-aligned invokeAsAgent helper, and the regression cases (empty query, HTTP error, network failure) are covered. The changeset is user-facing, names the affected component/tool, and explains both the previous and current behavior.
No blocking issues, no AGENTS.md violations, no i18n/SQL/auth/locale concerns, and no logged-out hot-path impact. LGTM.
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-test
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-loader
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
|
Thanks for contributing to EmDash! 🎉 Join the EmDash Discord, then run |
|
Thank you for digging into this, and for checking it against the spec and in both Chrome versions. That was my mistake in the original component. |
What does this PR do?
The
search_sitetool fromWebMcpSearch(#3632) tells an agent a failed search succeeded. When the search API returns a 500, the agent gets a successful call whose text says "Search failed". When results do come back, they arrive as a JSON string nested inside another JSON document.Both come from one mismatch. The tool resolves an MCP-style
{ content: [...], isError }envelope, but WebMCP has no envelope. The spec's invoke a tool steps serialize whateverexecutefulfills with into a JSON string and report it as success. Only a rejected promise counts as a failed call, soisErrornever reaches the agent as a signal.I measured this in Chrome 154 (stable,
--enable-features=WebMCP) and Chrome 157 (Canary), in a visible window, by registering tools shaped like this one and callingdocument.modelContext.executeTool():executedoesexecuteToolreturns{ content: [{ type: "text", text: JSON.stringify(results) }] }content[0].textis a second JSON string{ content: [...], isError: true }new Error("Search failed (HTTP 500).")[{ title: "Hello" }]"[{\"title\":\"Hello\"}]"The fix:
executethrows on an empty query, a non-OK response, or a network failure, so the browser reports the call as failed.executeresolves the result array itself, so the browser serializes it once and the agent gets[{ title, url, collection, excerpt }].One trade-off: on a rejection,
executeToolrejects with a generic "invocation failed" message, so "Provide a search query." probably no longer reaches the agent. I think that's acceptable, because the input schema already marksqueryas required. A specific message that arrives as a success is worse than a generic failure.I also checked the abort-signal path. Chrome 154 and 157 both remove the tool when the signal passed to
registerToolaborts, and a fresh registration afterwards gets the new config. So the component'sdisconnectedCallbackcleanup works as intended, and this PR leaves it alone.Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.AI-generated code disclosure
Screenshots / test output
Not applicable (no UI change).
The tests now call the tool through a small
invokeAsAgenthelper that copies the spec's invocation steps: serialize on fulfill, fail on reject. That way they assert exactly what an agent receives. Against the unfixed source, 2 of the 8 tests fail:With the fix: