Repository navigation
Closed as not planned
Labels
bugSomething isn't workingclosed-v1-deprecatedClosed: v1 is deprecated and accepting security fixes onlyv1
Description
Describe the bug
STDIO server is not restarting after it is disconnected from the sidebar, could be related to #380
To Reproduce
Steps to reproduce the behavior:
- Using the python-sdk to build the server
- Currently seems specific to the
lifespanattribute when its included in the server, here is an example lifespan sample
@contextlib.asynccontextmanager
async def stdio_lifespan(app) -> t.AsyncIterator[None]:
"""Dummy lifespan context for stdio server."""
logger.info("Starting application with dummy stdio lifespan...")
# Simulate a long-running process
_loop_task = asyncio.create_task(custom_loop())
try:
yield
finally:
logger.info("Application shutting down...")
if _loop_task:
_loop_task.cancel()
try:
await _loop_task
except asyncio.CancelledError:
pass
except Exception as e:
logger.error(f"Error during shutdown: {e}")
logger.info("Application shutdown complete.")
async def main():
mcp = Server(name="stdio-server", lifespan=stdio_lifespan)
async with stdio_server() as (read_stream, write_stream):
await mcp.run(read_stream, write_stream, mcp.create_initialization_options())
asyncio.run(main())brave_xkjOXXNdyw.mp4
A similar above example seems to work when there is no lifespan set and works as expected
REPO URL: https://github.com/FallenDeity/discord-mcp
Expected behavior
Should be able to re-connect after disconnecting from the inspector, the whole process and proxy server shouldnt crash
Logs
If applicable, add logs to help explain your problem.
server side output of the npx @modelcontextprotocol/inspector command
STDIO transport: command=C:\Users\Triyan Mukherjee\AppData\Local\Programs\Python\Python310\Scripts\poetry.exe, args=--directory,C:\Users\Triyan Mukherjee\VsCodeProjects\discord-mcp,run,python,-m,discord_mcp,--file-logging,--server-type,STDIO
Created server transport
Created client transport
Received POST message for sessionId c815eda5-05ff-41aa-9b92-1f2dcb159e53
Received POST message for sessionId c815eda5-05ff-41aa-9b92-1f2dcb159e53
file:///C:/Users/Triyan%20Mukherjee/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/server/sse.js:146
throw new Error("Not connected");
^
Error: Not connected
at SSEServerTransport.send (file:///C:/Users/Triyan%20Mukherjee/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/sdk/dist/esm/server/sse.js:146:19)
at PassThrough.<anonymous> (file:///C:/Users/Triyan%20Mukherjee/AppData/Local/npm-cache/_npx/5a9d879542beca3a/node_modules/@modelcontextprotocol/inspector/server/build/index.js:311:33)
at PassThrough.emit (node:events:519:28)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Readable.push (node:internal/streams/readable:390:5)
at node:internal/streams/transform:178:12
at PassThrough._transform (node:internal/streams/passthrough:46:3)
at Transform._write (node:internal/streams/transform:171:8)
at writeOrBuffer (node:internal/streams/writable:570:12)
Node.js v20.17.0
It gives this error once I click the disconnect button
subsequent errors in console.log in browser on clicking connect
index-BNqYF5Na.js:24658 GET http://localhost:6277/health net::ERR_CONNECTION_REFUSED
checkProxyHealth @ index-BNqYF5Na.js:24658
connect @ index-BNqYF5Na.js:24699
Nb @ index-BNqYF5Na.js:1322
Tb @ index-BNqYF5Na.js:1334
Ub @ index-BNqYF5Na.js:1337
nf @ index-BNqYF5Na.js:2274
se @ index-BNqYF5Na.js:2297
(anonymous) @ index-BNqYF5Na.js:2569
Qk @ index-BNqYF5Na.js:5873
Jb @ index-BNqYF5Na.js:1275
hd @ index-BNqYF5Na.js:2370
fd @ index-BNqYF5Na.js:1768
ed @ index-BNqYF5Na.js:1751
index-BNqYF5Na.js:24664 Couldn't connect to MCP Proxy Server TypeError: Failed to fetch
at checkProxyHealth (index-BNqYF5Na.js:24658:41)
at connect (index-BNqYF5Na.js:24699:13)
at Object.Nb (index-BNqYF5Na.js:1322:9)
at Tb (index-BNqYF5Na.js:1334:8)
at Ub (index-BNqYF5Na.js:1337:8)
at nf (index-BNqYF5Na.js:2274:5)
at se (index-BNqYF5Na.js:2297:11)
at index-BNqYF5Na.js:2569:7
at Qk (index-BNqYF5Na.js:5873:14)
at Jb (index-BNqYF5Na.js:1275:14)
Additional context
Add any other context about the problem here.
Reacted by Paul Berg, Demid Semenchenko, Mathieu Rivest, avivnakar and Median
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingclosed-v1-deprecatedClosed: v1 is deprecated and accepting security fixes onlyv1
Activity
PaulRBerg commentedon Aug 14, 2025
still getting this error
daddybh commentedon Aug 19, 2025
any luck?
olaservo commentedon Aug 23, 2025
@claude could you help investigate this issue?
claude commentedon Aug 23, 2025
Claude finished @olaservo's task —— View job •
claude/issue-654-20250823-0414• Create PR ➔Investigating STDIO Server Restart Issue ✅
I've identified and fixed the STDIO server restart issue where servers with lifespan contexts fail to restart after disconnection.
Todo List:
Root Cause & Solution
Problem: When STDIO servers with lifespan contexts disconnect, the global
webAppTransportsandserverTransportsMaps aren't properly cleaned up. On reconnection attempts, staleSSEServerTransportinstances try to send messages through closed connections, causing the "Not connected" error.Solution: Added proper cleanup handlers that:
mcpProxyto accept anonCleanupcallback for external cleanup logicChanges Made
server/src/mcpProxy.ts:
onCleanupparameter to mcpProxy functionserver/src/index.ts:
This fix ensures that servers with lifespan contexts can properly restart after disconnect, resolving the ERR_CONNECTION_REFUSED errors on reconnection.
Fix STDIO server restart issue after disconnect
olaservo commentedon Aug 28, 2025
@claude could we add tests for this?
claude commentedon Aug 28, 2025
Claude finished @olaservo's task —— View job •
claude/issue-654-20250828-0243• Create PR ➔Adding Tests for STDIO Server Restart Fix ✅
I've successfully implemented the STDIO server restart fix and comprehensive tests to ensure the functionality works correctly.
Todo List:
📋 Implementation Details
Root Cause Analysis
The issue occurred because when STDIO servers with lifespan contexts disconnected, the global
webAppTransportsandserverTransportsMaps retained stale references. On reconnection attempts, these staleSSEServerTransportinstances tried to send messages through closed connections, causing "Not connected" errors.Fix Implementation
mcpProxy.ts: Added optionalonCleanupparameter to allow coordinated cleanupTest Coverage
Created comprehensive tests covering:
Unit Tests (
mcpProxy.test.ts):Integration Tests (
transport-cleanup.test.ts):Key Changes Made
server/src/mcpProxy.ts:server/src/index.ts(STDIO route example):Test Results
The fix ensures that servers with lifespan contexts can properly restart after disconnect, resolving the ERR_CONNECTION_REFUSED errors on reconnection attempts.
What's Done
✅ STDIO server restart fix implemented - Added proper transport cleanup handlers
✅ Comprehensive test suite created - 26 total tests covering unit and integration scenarios
✅ Jest configuration added - Set up testing infrastructure for server-side tests
✅ Changes committed and pushed - All code is ready for review
Fix STDIO server restart issue and add comprehensive tests
caarlos0 commentedon Nov 2, 2025
fwiw, same error when restarting the server through the inspector UI
median-dxz commentedon Mar 7, 2026
Still encountering the same error on the latest version.
cliffhall commentedon Aug 1, 2026
Closing: v1 is deprecated.
Thank you for reporting this, and apologies for the long wait for a response.
v1 will receive security fixes only — this issue describes a functionality, compatibility, or usability problem rather than a vulnerability, so it is being closed against v1. This is not a judgment on whether the problem is real — it's a consequence of the v1 line being frozen. This is happening as part of #1872.
If this still happens in v2, we'd genuinely like to know. v2 is a complete rewrite — three clients over a shared core — so many v1 behaviours no longer exist in the same form. Please open a fresh issue against v2 with steps to reproduce, and we'll look at it. Note that we accept external contributions as issues rather than pull requests; maintainers handle design and implementation through a prompt-driven workflow. See
CONTRIBUTING.md.Thanks for taking the time to report it.