[BUG] Claude Code SDK: SDK MCP server faills to connect due to closed stream
Resolved 💬 26 comments Opened Aug 28, 2025 by codylund Closed Oct 8, 2025
💡 Likely answer: A maintainer (dltn, contributor)
responded on this thread — see the highlighted reply below.
Environment
- Platform (select one):
- [ ] Anthropic API
- [ ] AWS Bedrock
- [ ] Google Vertex AI
- [x] Other: Claude Code SDK
- Claude CLI version: 1.0.96
- Operating System: macOS 15.1
- Terminal: Terminal App
Bug Description
The new SDK MCP server type does not work with basic string prompts.
Running with DEBUG=true, I see the following error:
[ERROR] MCP server "test" Failed to connect SDK MCP server: Error: Stream closed
Steps to Reproduce
Below is a basic sample:
import { createSdkMcpServer, query, tool } from "@anthropic-ai/claude-code";
import { z } from "zod";
const mcp = createSdkMcpServer({
name: "test",
tools: [
tool(
"add",
"Add two numbers",
{
a: z.number(),
b: z.number(),
},
async ({ a, b }) => {
return {
content: [
{
type: "text",
text: `${a + b}`,
},
],
};
}
),
],
});
for await (const message of query({
//prompt: generateMessages(),
prompt: "Use the tool to add 1 and 2 and return the result.",
options: {
mcpServers: {
test: mcp,
},
allowedTools: ["mcp__test__add"],
},
})) {
console.log(JSON.stringify(message));
}
Expected Behavior
Prompt should run with successful connection and access to the SDK MCP server.
Actual Behavior
Claude CLI process exits with an error.
26 Comments
I encountered the same issue today and was able to work around it with a minimal adjustment:
@modelcontextprotocol/sdk(this was straightforward since the code format is nearly identical).mcpServersdefinition as shown below.Before:
After:
With this approach, I was able to get things running in under five minutes with only minimal changes.
Depending on your setup, you may need to use a built JS artifact instead of
tsx, but for my use case this was sufficient.@selenehyun I'm glad to hear this works for you, but this does not run the local MCP server in the same process. That's my main motivation for using the SDK type.
This still reproduces with v1.0.96...
same issue for me
Broken with v1.0.98 as well
same issue
same issue , even without the allowedTools prop
Trying the example here https://docs.anthropic.com/en/docs/claude-code/sdk/sdk-typescript#creating-custom-tools but it doesn't seem to work either:
FWIW, I have the SDK MCP server type working in a single Bun-based project using v1.0.98, but I can't get it working in any other project. I have no idea why it works yet...
@codylund by any other project, do you mean projects using node or deno?
@ShuangLiu1992 Just any other Bun-based projects in general. I don't know what's special about the project where the SDK MCP server is functional. It's an overall basic set up and is very similar to the example I added in my original post. It makes me suspect some kind of obscure race condition in the Claude Code SDK.
It seems to still be an issue in v1.0.105
Same issue here
Running into this bug as well!
Found a workaround
The key is keeping the input stream artificially open via the
await new Promise(res => setTimeout(res, 10000))sleep. It isn't ideal, but there is likely a better workaround that closes the input stream once claude-code returns the result.Looks like this was fixed in the Python Claude Code SDK: https://github.com/anthropics/claude-code-sdk-python/pull/157
same issue
same issue
work for me
Works for me as a workaround. Thanks for sharing @tariqkb ❤️
Here is the same workaround but not waiting for the full timeout simply by checking if "results" are in messages. Also no need to have
bypassPermissionsand you can allow and disallow tools. Thanks @claude for the help.Hi folks! Fix is ready and should be out soon in ~v1.0.121. You'll no longer need the timeout, but you will still need to provide a
AsyncIterable<SDKUserMessage>streaming prompt (not astringprompt)Hi @dltn i am using the 1.0128 version and the createSdkMcpServer creates the mcp server but in the init message it is always on status "failed". i am using an AsyncIterable and claude code is running but can't locate the custom tool, used exactly the same example in the docs.
any thoughts?
and in the init message i am always seeing this:
which dosn't let's claude code use the whether tool
@tom-n-terra What does your
npm list claude-codesay? With@anthropic-ai/claude-cli@2.0.3, your test file works for me:Going to close this. Please file another issue if you experience this again!
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.