Skip to content

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

@shellRaining

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

Metadata

Metadata

Assignees

No one assigned

    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