Skip to content

fix(assistant): use internal fetch for MCP on workers#1309

Open
onmax wants to merge 1 commit intonuxt-content:mainfrom
onmax:fix/assistant-worker-self-fetch
Open

fix(assistant): use internal fetch for MCP on workers#1309
onmax wants to merge 1 commit intonuxt-content:mainfrom
onmax:fix/assistant-worker-self-fetch

Conversation

@onmax
Copy link
Copy Markdown
Contributor

@onmax onmax commented Mar 15, 2026

Follow-up to #1304.
related vercel/ai#13449

Summary

The assistant handler creates an MCP client that fetches the same worker's /mcp endpoint. On Cloudflare Workers this triggers error 1042 (self-fetch recursion protection), so /__docus__/assistant always returns 500.

The fix creates a custom MCP transport that uses event.fetch (Nitro's localFetch) instead of event.$fetch (which maps to globalThis.$fetch → global fetch() → CF self-fetch error). On CF Workers:

  • event.fetchuseNitroApp().localFetch (internal routing, no HTTP)
  • event.$fetchglobalThis.$fetchfetch() (external HTTP, blocked by CF)

External MCP servers and local dev still use the standard HTTP transport.

Reproduction

Variant Source Deployed worker Result
Bug docus-assistant-worker-repro docus-assistant-repro.je-cf9.workers.dev 500
Fix docus-assistant-worker-repro-fix docus-assistant-repro-fix.je-cf9.workers.dev ✅ streaming response

Verify

Bug worker:

curl -X POST https://docus-assistant-repro.je-cf9.workers.dev/__docus__/assistant \
  -H 'content-type: application/json' \
  -d '{"id":"t","messages":[{"parts":[{"type":"text","text":"hello"}],"id":"t1","role":"user"}],"trigger":"submit-message"}'

Fixed worker:

curl -X POST https://docus-assistant-repro-fix.je-cf9.workers.dev/__docus__/assistant \
  -H 'content-type: application/json' \
  -d '{"id":"t","messages":[{"parts":[{"type":"text","text":"hello"}],"id":"t1","role":"user"}],"trigger":"submit-message"}'

Local verification:

pnpm typecheck

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 15, 2026

@onmax is attempting to deploy a commit to the NuxtLabs Team on Vercel.

A member of the Team first needs to authorize it.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 15, 2026

Open in StackBlitz

npm i https://pkg.pr.new/create-docus@1309
npm i https://pkg.pr.new/docus@1309

commit: fa6d475

Copy link
Copy Markdown
Collaborator

@larbish larbish left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the PR!

StreamableHTTPClientTransport (which is used internally by createMCPClient) already accepts a fetch option, so you could drop the custom transport and use new StreamableHTTPClientTransport IMO. Could you have a try and tell me if it works in your case?

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.

2 participants