Skip to content

Next AppRouter routes break because hono globally overwrites the response objects (v1.25.x +) #1369

Closed
langfuse/langfuse
#11645

Description

@nimarb

Describe the bug

Version 1.25.x introduced hono as dependency. Hono defaults to overrideGlobalObjects: true (see their docs).
After initializing the MCP SDK in our web server (i.e. the first MCP endpoint is called), the hono SDK overwrites all our Next Responses on App Router routes, breaking our app.

To Reproduce

package.json:

{
  "name": "mcp-hono-response-bug",
  "version": "1.0.0",
  "description": "MCP SDK 1.25.x breaks Next.js App Router Response instanceof check",
  "type": "module",
  "scripts": {},
  "dependencies": {
    "@modelcontextprotocol/sdk": "1.25.2"
  }
}
#!/usr/bin/env bun
/**
 * @modelcontextprotocol/sdk v1.25.x uses @hono/node-server which overwrites
 * global.Response when StreamableHTTPServerTransport is instantiated. This breaks
 * Next.js App Router routes because NextResponse extends the original Response class.
 *
 */

import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";

// Capture original Response (simulates Next.js startup)
const OriginalResponse = globalThis.Response;
console.log("\n[1] Original Response captured");
console.log("    Response.name:", Response.name);

// Simulate NextResponse (what next/server does internally)
class NextResponse extends OriginalResponse {
  static json(data: unknown, init?: ResponseInit) {
    return new NextResponse(JSON.stringify(data), {
      ...init,
      headers: { ...init?.headers, "content-type": "application/json" },
    });
  }
}

// Verify instanceof works BEFORE MCP SDK instantiation
const before = NextResponse.json({ status: "before" });
console.log("\n[2] BEFORE StreamableHTTPServerTransport instantiation:");
console.log("    response instanceof Response:", before instanceof Response);
console.log("    Response === OriginalResponse:", Response === OriginalResponse);
console.log("    global.Response:", Response.toString().slice(0, 60) + "...");
console.log("    OriginalResponse:", OriginalResponse.toString().slice(0, 60) + "...");

// Instantiate MCP SDK's StreamableHTTPServerTransport
// This is what triggers the bug - internally calls @hono/node-server's getRequestListener()
// which overwrites global.Response with Hono's implementation
console.log("\n[3] Instantiating StreamableHTTPServerTransport...");
const transport = new StreamableHTTPServerTransport({
  sessionIdGenerator: undefined,
});
console.log("    Transport created");

// Check instanceof AFTER MCP SDK instantiation
const after = NextResponse.json({ status: "after" });
console.log("\n[4] AFTER StreamableHTTPServerTransport instantiation:");
console.log("    Response === OriginalResponse:", Response === OriginalResponse);
console.log("    response instanceof Response:", after instanceof Response);
console.log(
  "    NextResponse.prototype instanceof Response:",
  NextResponse.prototype instanceof Response,
);
console.log("    global.Response:", Response.toString().slice(0, 60) + "...");
console.log("    OriginalResponse:", OriginalResponse.toString().slice(0, 60) + "...");

this outputs:

[1] Original Response captured
    Response.name: Response

[2] BEFORE StreamableHTTPServerTransport instantiation:
    response instanceof Response: true
    Response === OriginalResponse: true
    global.Response: function Response() {
    [native code]
}...
    OriginalResponse: function Response() {
    [native code]
}...

[3] Instantiating StreamableHTTPServerTransport...
    Transport created

[4] AFTER StreamableHTTPServerTransport instantiation:
    Response === OriginalResponse: false
    response instanceof Response: false
    NextResponse.prototype instanceof Response: false
    global.Response: class _Response {
  #body;
  #init;
  [getResponseCache]() {...
    OriginalResponse: function Response() {
    [native code]
}...

confirming that just adding the MCP SDK and initiating a function basically globally breaks all app router routes in NextJS because it overwrites the response object.

Expected behavior
Don't overwrite the response objects.

Logs
This is the error you get from the application if you call an app router route:

web:dev: [Error: No response is returned from route handler '[project]/web/src/app/api/chatCompletion/route.ts'. Ensure you return a `Response` or a `NextResponse` in all branches of your handler.]

Additional context
i would like to upgrade to 1.25.x because of security implications.

Activity

  1. changed the title [-]Inclusion of HONO as dependency after 1.25.x breaks Next AppRouter Routes by globally overwriting response objects[/-] [+]Next AppRouter routes break because hono globally overwrites the response objects (v1.25.x +)[/+] on Jan 8, 2026
  2. HemangRajvanshy commented on Jan 8, 2026

    @HemangRajvanshy

    Related: #1209

    Potential Fix (in src/server/streamableHttp.ts):

      getRequestListener(..., { overrideGlobalObjects: false })
    
  3. mattzcarey commented on Jan 9, 2026

    @mattzcarey
    Member

    can you use the webStandardStreamableHTTPServerTransport to unblock yourselves?

  4. self-assigned this
    on Jan 9, 2026
  5. TiKa-Nexus commented on Jan 19, 2026

    @TiKa-Nexus

    I stumbled upon a problem in my app which likely results from the same underlying issue.

    Whenever I run my app on localhost and trigger the mcp server - also hosted on the same localhost port - the app breaks. Its whatever is first "using" the server works but the other one does not. So, I can either use only mcp or only use the app on localhost not both.

    The error is always the same:

     TypeError: Expected an instance of Response to be returned
        at ignore-listed frames
    

    I use [email protected]and @modelcontextprotocol/[email protected]

  6. nimarb commented on Jan 19, 2026

    @nimarb
    Author

    hi @mattzcarey , is there a timely plan to fix this / what are your thoughts? work around could be done here potentially (i would just patch out hono personally, as there is no trust), but ideally i would like to see it addressed in the MCP SDK perhaps also on process level when adding such libraries.

    this might have been fixed here now, if you bump the deps: honojs/node-server#295

  7. mattzcarey commented on Jan 19, 2026

    @mattzcarey
    Member

    Hey I think the latest version of '@hono/node-server' should fix this. Will raise a PR soon

  8. shiftan commented on Jan 20, 2026

    @shiftan

    Hey,
    Had a similar issue. I believe bumping @hono/node-server won't fix this - PR honojs/node-server#295 only removes the global.fetch override, not Response.

    I think the fix is passing { overrideGlobalObjects: false } to getRequestListener() in the SDK.

  9. added 4 commits that reference this issue on Jan 20, 2026
    fafa0d1
    5cd5d70
    223d7ff
    2bf9b8f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions