#141
fix: lazy-load MCP SDK to avoid global state pollution in Next.js
The @modelcontextprotocol/sdk has dependencies (via undici) that modify the global Response object at import time. This breaks Next.js App Routes which rely on instanceof Response checks, causing "No response is returned" errors.
This fix:
- Converts eager SDK imports to type-only imports
- Adds a lazy loadSdk() function that dynamically imports SDK modules
- Defers StreamableHTTPServerTransport creation to first request
- Calls loadSdk() before SDK usage in both HTTP and SSE endpoints
This ensures the SDK is only loaded after Next.js has fully initialized.
Hello all, this issue is impacting my project as well. Using Mr Kelly's patch is working. Is there a timeline for incorporating a fix for this issue into the vercel mcp-handler project?