Repository navigation
onerror and other listener not remove after client close (stdio) #780
Copy link
Copy link
Open
Labels
P2Moderate issues affecting some users, edge cases, potentially valuable featureModerate issues affecting some users, edge cases, potentially valuable featurebugSomething isn't workingSomething isn't workingfix proposedBot has a verified fix diff in the commentBot has a verified fix diff in the commentready for workEnough information for someone to start working onEnough information for someone to start working on
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
P2Moderate issues affecting some users, edge cases, potentially valuable featureModerate issues affecting some users, edge cases, potentially valuable featurebugSomething isn't workingSomething isn't workingfix proposedBot has a verified fix diff in the commentBot has a verified fix diff in the commentready for workEnough information for someone to start working onEnough information for someone to start working on
Activity
shellRaining commentedon Jul 18, 2025
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?
ochafik commentedon Oct 13, 2025
Hi @shellRaining, thanks for filing this!
We'd probably want to remove only the very listeners we registered, not all of them.
shellRaining commentedon Oct 13, 2025
Okay, I'll take another look at this part of the code.
constantbourgois commentedon Sep 25, 2026
Hi I've created a PR #2872 for this issue and tested it locally (it backports #1762) to skip non-JSON lines