pythonmodel-context-protocol

The Python MCP server with STDIO transport throws an 'Error: SSE connection not established' when attempting to connect via MCP Inspector


I've been trying to run an example from the official repo of Model Context Protocol for Python (https://github.com/modelcontextprotocol/python-sdk). But it keeps giving me Error in /message route: Error: SSE connection not established", when I click the "Connect" button in the webpage of MCP Inspector. The problem is that I'm not even trying to use the SSE connection, so I'm really confused about the error and the logs.

Here's the code (located in src/ folder):

# server.py
from mcp.server.fastmcp import FastMCP

# Create an MCP server
mcp = FastMCP("Demo")


# Add an addition tool
@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b


# Add a dynamic greeting resource
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
    """Get a personalized greeting"""
    return f"Hello, {name}!"

That's how I prepared the project: I used uv to initialize the virtual environment in the root folder. Installed project dependencies with uv add "mcp[cli]", as said in the repo's guide. Info on program's versions:

My actions to run the server are:

  1. mcp dev src/server.py in the Powershell while being in the project's root directory.
  2. Then, I see
⚙️ Proxy server listening on port 6277
🔍 MCP Inspector is up and running at http://127.0.0.1:6274 🚀

In the console, and go to the page http://127.0.0.1:6274. 3. On the page the STDIO trasport method is already set. There's also a command uv with arguments run --with mcp mcp run src/server.py(please see the screenshot attached), so I click on "Connect" button. Nothing happens on the UI, but in the logs in the console I see

New SSE connection
Query parameters: [Object: null prototype] {
  transportType: 'stdio',
  command: 'uv',
  args: 'run --with mcp mcp run src/server.py',
  env: '{ ... # all my env variables, PATH and etc.}'
}
Stdio transport: command=C:\Users\...\.local\bin\uv.exe, args=run,--with,mcp,mcp,run,src/server.py
Spawned stdio transport
Connected MCP client to backing server transport
Created web app transport
Created web app transport
Set up MCP proxy
  1. I click on "Connect" button again (please see the screenshot attached) and see the "Connection Error, is your MCP server running?" on UI, and the following in logs:
New SSE connection
Query parameters: [Object: null prototype] {
  transportType: 'stdio',
  command: 'uv',
  args: 'run --with mcp mcp run src/server.py',
  env: '{...}'
}
Stdio transport: command=C:\Users\...\.local\bin\uv.exe, args=run,--with,mcp,mcp,run,src/server.py
Spawned stdio transport
Connected MCP client to backing server transport
Created web app transport
Created web app transport
Set up MCP proxy
Received message for sessionId 5ed68d2c-6c0f-47e7-9972-3fe99c43a630
Error in /message route: Error: SSE connection not established
    at SSEServerTransport.handlePostMessage (file:///C:/Users/.../AppData/Roaming/npm/node_modules/@modelcontextprotocol/inspector/node_modules/@modelcontextprotocol/sdk/dist/esm/server/sse.js:61:19)
    at file:///C:/Users/.../AppData/Roaming/npm/node_modules/@modelcontextprotocol/inspector/server/build/index.js:130:25
    at Layer.handleRequest (C:\Users\...\AppData\Roaming\npm\node_modules\@modelcontextprotocol\inspector\node_modules\router\lib\layer.js:152:17)
    at next (C:\Users\...\AppData\Roaming\npm\node_modules\@modelcontextprotocol\inspector\node_modules\router\lib\route.js:157:13)
    at Route.dispatch (C:\Users\...\AppData\Roaming\npm\node_modules\@modelcontextprotocol\inspector\node_modules\router\lib\route.js:117:3)
    at handle (C:\Users\...\AppData\Roaming\npm\node_modules\@modelcontextprotocol\inspector\node_modules\router\index.js:435:11) 
    at Layer.handleRequest (C:\Users\...\AppData\Roaming\npm\node_modules\@modelcontextprotocol\inspector\node_modules\router\lib\layer.js:152:17)
    at C:\Users\...\AppData\Roaming\npm\node_modules\@modelcontextprotocol\inspector\node_modules\router\index.js:295:15
    at processParams (C:\Users\...\AppData\Roaming\npm\node_modules\@modelcontextprotocol\inspector\node_modules\router\index.js:582:12)
    at next (C:\Users\...\AppData\Roaming\npm\node_modules\@modelcontextprotocol\inspector\node_modules\router\index.js:291:5)    
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (node:_http_outgoing:699:11)
    at ServerResponse.header (C:\Users\...\AppData\Roaming\npm\node_modules\@modelcontextprotocol\inspector\node_modules\express\lib\response.js:684:10)
    at ServerResponse.json (C:\Users\...\AppData\Roaming\npm\node_modules\@modelcontextprotocol\inspector\node_modules\express\lib\response.js:247:10)
    at file:///C:/Users/.../AppData/Roaming/npm/node_modules/@modelcontextprotocol/inspector/server/build/index.js:134:25
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

P.S. I have tried adding

if __name__ == "__main__":
    mcp.run(transport='stdio')

At the end of the server.py, as some examples propose, but it didn't affect anything.

Any help appreciated! Thanks!

MCP Inspector UI page Screenshot


Solution

  • Here's what worked for me:

    I was only able to run the Inspector correctly in VS Code's Simple Browser (Ctrl + Shift + PSimple Browser: Show → Paste your Inspector link—in my case, it was running on http://127.0.0.1:6274).

    I still have no idea what the problem is. I couldn't make the Inspector work in Edge or Chrome. It seems I have tried EVERYTHING: replacing STDIO with SSE, running the MCP server with SSE transport on Ubuntu server and connecting to it from the Inspector running locally, cleaning cache, opening the Inspector in Incognito mode, stopping other processes even though I couldn't find anything messing up with the ports needed, etc., etc. Nothing helped but simply running the Inspector in VS Code Browser did. It works !! I am really confused how there aren't any discussions on this. Hope there's something that might be fixed on the SDK/Inspector's side