Skip to content

feat(mcp): allow custom fetch for HTTP and SSE transports#14134

Merged
aayush-kapoor merged 5 commits into
vercel:mainfrom
jaydeep-pipaliya:feat/mcp-custom-fetch
Apr 6, 2026
Merged

feat(mcp): allow custom fetch for HTTP and SSE transports#14134
aayush-kapoor merged 5 commits into
vercel:mainfrom
jaydeep-pipaliya:feat/mcp-custom-fetch

Conversation

@jaydeep-pipaliya
Copy link
Copy Markdown
Contributor

Summary

Fixes #13449.

@ai-sdk/mcp hardcoded globalThis.fetch in its HTTP and SSE transports, breaking same-app MCP calls on runtimes that block self-fetches (e.g. Cloudflare Workers error 1042 via Nitro).

  • Adds an optional fetch field to MCPTransportConfig and both HttpMCPTransport / SseMCPTransport constructors
  • Falls back to globalThis.fetch when not provided (no breaking change)
  • Forwards the custom fetch to OAuth auth() calls so token discovery/exchange uses the same fetch implementation
  • Covers both http and sse transport types as requested in the issue

Usage:

createMCPClient({
  transport: {
    type: 'http',
    url,
    fetch: event.fetch, // e.g. Nitro / Cloudflare Workers request-local fetch
  },
})

Test plan

  • Added describe('custom fetch', ...) tests to both mcp-http-transport.test.ts and mcp-sse-transport.test.ts verifying the custom fetch function is called for GET (start) and POST (send) operations
  • All 171 existing tests continue to pass (pnpm test:node)
  • Package builds cleanly (pnpm build in packages/mcp)

Adds an optional `fetch` option to `MCPTransportConfig` (and both
`HttpMCPTransport` / `SseMCPTransport` constructors) so callers can
supply a request-local fetch instead of relying on `globalThis.fetch`.

The custom fetch is also forwarded to OAuth `auth()` calls so token
discovery and exchange use the same fetch implementation.

Fixes runtimes where self-fetches are blocked (e.g. Cloudflare Workers
error 1042 on same-worker MCP routes with Nitro).

Closes vercel#13449
@tigent tigent Bot added ai/mcp related to `@ai-sdk/mcp` package feature New feature or request labels Apr 4, 2026
@aayush-kapoor aayush-kapoor added the backport Admins only: add this label to a pull request in order to backport it to the prior version label Apr 6, 2026
@aayush-kapoor aayush-kapoor enabled auto-merge (squash) April 6, 2026 16:39
@aayush-kapoor aayush-kapoor merged commit a00d1d3 into vercel:main Apr 6, 2026
21 of 22 checks passed
ghost pushed a commit that referenced this pull request Apr 6, 2026
@ghost ghost removed the backport Admins only: add this label to a pull request in order to backport it to the prior version label Apr 6, 2026
@ghost
Copy link
Copy Markdown

ghost commented Apr 6, 2026

⚠️ Backport to release-v6.0 created but has conflicts: #14159

@ghost
Copy link
Copy Markdown

ghost commented Apr 6, 2026

🚀 Published in:

Package Version
@ai-sdk/mcp 2.0.0-beta.17

aayush-kapoor added a commit that referenced this pull request Apr 6, 2026
…14159)

This is an automated backport of #14134 to the release-v6.0 branch. FYI
@jaydeep-pipaliya
This backport has conflicts that need to be resolved manually.

### `git cherry-pick` output

```
Auto-merging packages/mcp/src/tool/mcp-http-transport.test.ts
Auto-merging packages/mcp/src/tool/mcp-http-transport.ts
CONFLICT (content): Merge conflict in packages/mcp/src/tool/mcp-http-transport.ts
Auto-merging packages/mcp/src/tool/mcp-sse-transport.test.ts
Auto-merging packages/mcp/src/tool/mcp-sse-transport.ts
CONFLICT (content): Merge conflict in packages/mcp/src/tool/mcp-sse-transport.ts
Auto-merging packages/mcp/src/tool/mcp-transport.ts
error: could not apply a00d1d3... feat(mcp): allow custom fetch for HTTP and SSE transports (#14134)
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
```

---------

Co-authored-by: Jaydeep pipaliya <71074587+jaydeep-pipaliya@users.noreply.github.com>
Co-authored-by: Aayush Kapoor <aayushkapoor34@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/mcp related to `@ai-sdk/mcp` package feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@ai-sdk/mcp should allow custom fetch for HTTP/SSE transports

2 participants