Skip to content

onerror and other listener not remove after client close (stdio) #780

Open

Description

@shellRaining

Describe the bug

This is my first time using MCP. I noticed that after closing the MCP client, there's an issue with JSON parsing failure. I'm not entirely sure whether the problem lies with the server or the SDK, and I hope to get some assistance. thanks~

To Reproduce

create a new mcp client

import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";

function createTransport() {
  let transport: StdioClientTransport;

  transport = new StdioClientTransport({
    command: "uvx",
    args: ["docker-mcp"],
    stderr: "pipe",
  });
  transport.onerror = async (error) => {
    console.log("error", error);
  };
  transport.onclose = async () => {
    console.log("close");
  };

  return transport;
}

const client = new Client(
  { name: "shellRaining", version: "1.0.0" },
  { capabilities: {} },
);

const transport = createTransport();
client.connect(transport);

await new Promise((resolve) => {
  setTimeout(resolve, 1000);
});

await transport.close()
await client.close()

then run this project, it will show below error message

close
error 21 |     clear() {
22 |         this._buffer = undefined;
23 |     }
24 | }
25 | export function deserializeMessage(line) {
26 |     return JSONRPCMessageSchema.parse(JSON.parse(line));
                                                ^
SyntaxError: JSON Parse error: Unexpected identifier "Shutting"
      at deserializeMessage (/Users/shellraining/Documents/playground/mcp/typescript-sdk/dist/esm/shared/stdio.js:26:44)
      at processReadBuffer (/Users/shellraining/Documents/playground/mcp/typescript-sdk/dist/esm/client/stdio.js:141:50)
      at <anonymous> (/Users/shellraining/Documents/playground/mcp/typescript-sdk/dist/esm/client/stdio.js:103:22)
      at emit (node:events:95:22)
      at addChunk (internal:streams/readable:265:47)
      at readableAddChunkPushByteMode (internal:streams/readable:243:18)
      at handleNumberResult (internal:streams/native-readable:69:18)
      at <anonymous> (internal:streams/native-readable:43:68)

Expected behavior

not occur error message

Activity

  1. shellRaining commented on Jul 18, 2025

    @shellRaining
    Author

    I tried adding the following part of the code and found that the aforementioned error no longer occurs. If this change doesn't break the protocol implementation, may I submit a PR?

    Image
  2. ochafik commented on Oct 13, 2025

    @ochafik
    Contributor

    Hi @shellRaining, thanks for filing this!

    I tried adding the following part of the code and found that the aforementioned error no longer occurs. If this change doesn't break the protocol implementation, may I submit a PR?

    We'd probably want to remove only the very listeners we registered, not all of them.

  3. shellRaining commented on Oct 13, 2025

    @shellRaining
    Author

    Okay, I'll take another look at this part of the code.

  4. added
    ready for workEnough information for someone to start working on
    P3Nice to haves, rare edge cases
    on Nov 3, 2025
  5. added
    good first issueGood for newcomers - can be tackled without deep knowledge of the codebase
    and removed on Nov 12, 2025
  6. removed
    good first issueGood for newcomers - can be tackled without deep knowledge of the codebase
    on Dec 18, 2025
  7. ghost removed
    P3Nice to haves, rare edge cases
    on Apr 17, 2026
  8. ghost added
    P2Moderate issues affecting some users, edge cases, potentially valuable feature
    on Apr 17, 2026
  9. ghost added
    fix proposedBot has a verified fix diff in the comment
    on Apr 17, 2026
  10. constantbourgois commented on Sep 25, 2026

    @constantbourgois

    Hi I've created a PR #2872 for this issue and tested it locally (it backports #1762) to skip non-JSON lines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Moderate issues affecting some users, edge cases, potentially valuable featurebugSomething isn't workingfix proposedBot has a verified fix diff in the commentready for workEnough information for someone to start working on

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions