Skip to content

fix(core): fail the WebMCP search_site call on error - #3894

Merged
swissky merged 2 commits into
emdash-cms:mainfrom
oddharsh:fix/webmcp-search-tool-result
Oct 5, 2026
Merged

swissky merged 2 commits into
emdash-cms:mainfrom
oddharsh:fix/webmcp-search-tool-result

Conversation

@oddharsh

@oddharsh oddharsh commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

The search_site tool from WebMcpSearch (#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 whatever execute fulfills with into a JSON string and report it as success. Only a rejected promise counts as a failed call, so isError never 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 calling document.modelContext.executeTool():

execute does what executeTool returns
resolves { content: [{ type: "text", text: JSON.stringify(results) }] } fulfilled: a JSON string whose content[0].text is a second JSON string
resolves { content: [...], isError: true } fulfilled, so the call reads as a success
throws new Error("Search failed (HTTP 500).") rejected: "Tool was executed but the invocation failed"
resolves [{ title: "Hello" }] fulfilled: "[{\"title\":\"Hello\"}]"

The fix:

  • execute throws on an empty query, a non-OK response, or a network failure, so the browser reports the call as failed.
  • execute resolves the result array itself, so the browser serializes it once and the agent gets [{ title, url, collection, excerpt }].

One trade-off: on a rejection, executeTool rejects 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 marks query as 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 registerTool aborts, and a fresh registration afterwards gets the new config. So the component's disconnectedCallback cleanup works as intended, and this PR leaves it alone.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main.
  • I have added and reviewed the user-facing changeset (if this PR changes a published package)
  • New features link to an approved Discussion: https://github.com/emdash-cms/emdash/discussions/...
  • I have included screenshots below if this PR changes the UI

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Code + Claude Opus 5.5

Screenshots / test output

Not applicable (no UI change).

The tests now call the tool through a small invokeAsAgent helper 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:

× returns absolute URLs and plain-text excerpts
× reports an empty query or failed request to the agent as a failed call
Tests  2 failed | 6 passed (8)

With the fix:

Tests  8 passed (8)

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-bot

changeset-bot Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a12f3e9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/plugin-test Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
create-emdash Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

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

@github-actions github-actions Bot added review/needs-review No maintainer or bot review yet area/core size/M labels Oct 5, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@emdashbot emdashbot Bot added review/approved Approved; no new commits since and removed review/needs-review No maintainer or bot review yet labels Oct 5, 2026
@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@oddharsh

oddharsh commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Oct 5, 2026
@pkg-pr-new

pkg-pr-new Bot commented Oct 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@3894

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@3894

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@3894

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@3894

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@3894

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@3894

emdash

npm i https://pkg.pr.new/emdash@3894

create-emdash

npm i https://pkg.pr.new/create-emdash@3894

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@3894

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@3894

@emdash-cms/plugin-test

npm i https://pkg.pr.new/@emdash-cms/plugin-test@3894

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@3894

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@3894

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@3894

@emdash-cms/registry-loader

npm i https://pkg.pr.new/@emdash-cms/registry-loader@3894

@emdash-cms/registry-moderation

npm i https://pkg.pr.new/@emdash-cms/registry-moderation@3894

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@3894

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@3894

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@3894

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@3894

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@3894

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@3894

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@3894

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@3894

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@3894

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@3894

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@3894

commit: a12f3e9

@swissky
swissky merged commit 740de2b into emdash-cms:main Oct 5, 2026
40 checks passed
@emdashbot

emdashbot Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Thanks for contributing to EmDash! 🎉

Join the EmDash Discord, then run /link to connect your GitHub account and claim the Contributor role.

@swissky

swissky commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

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.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants