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
feat(mcp): allow custom fetch for HTTP and SSE transports (#14134)
## 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:**
```ts
createMCPClient({
transport: {
type: 'http',
url,
fetch: event.fetch, // e.g. Nitro / Cloudflare Workers request-local fetch
},
})
```
## Test plan
- [x] 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
- [x] All 171 existing tests continue to pass (`pnpm test:node`)
- [x] Package builds cleanly (`pnpm build` in `packages/mcp`)
---------
Co-authored-by: Aayush Kapoor <83492835+aayush-kapoor@users.noreply.github.com>
Co-authored-by: Aayush Kapoor <aayushkapoor34@gmail.com>
0 commit comments