inspector icon indicating copy to clipboard operation
inspector copied to clipboard

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

Open harbachonak opened this issue 11 months ago • 4 comments

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!

Image

harbachonak avatar Apr 20 '25 19:04 harbachonak

I think the connection arguments are erroneous. The SSE connection you see could be because the browser uses SSE to connect to the proxy backend that the inspector uses. The actual backend will attempt an STDIO connection. Hence the SSE messages you're seeing (please someone correct me if I'm wrong, I just got back from vacation and diving back in). It looks like you're on Windows. Windows has a specific way of writing the paths that are not unix like. Maybe you need to change the way you pass in the path to the server.py by adding the \\ slashes (ask chatgpt to explain this nuance) . I'm on Linux right now, but if you upload your server on github and share the repo here - I'm happy to switch to my windows machine and test it out for you

QuantGeekDev avatar Apr 20 '25 20:04 QuantGeekDev

@QuantGeekDev Hi! I've pushed my project here: https://github.com/harbachonak/mcp_example, will be grateful if you have a look. Changing the slashes didn't help, unfortunately. Also, when I provide an incorrect path to the server.py, the error text is something like "File not found", so it seems like it parses the slashes fine and finds the server (also, all the env variables are shown with "/" on UI

harbachonak avatar Apr 20 '25 20:04 harbachonak

Hi! If anyone else is facing the same issue and can't get the Inspector to work, here's what worked for me:

I was only able to run the Inspector correctly in VS Code's Simple Browser (Ctrl + Shift + P → Simple 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. Could it be just my issue? Or is there something that might need to be fixed on the Inspector's side?

harbachonak avatar Apr 22 '25 11:04 harbachonak

Please close any unnecessary pages in the browser to ensure that only one page is accessed http://127.0.0.1:6274

Alternatively, you can use node to run and change the port number

CLIENT_PORT=8080 SERVER_PORT=9000 npx @modelcontextprotocol/inspector uv run --with mcp mcp run server.py

triangle959 avatar Apr 27 '25 07:04 triangle959

Closing since this doesn't look to be related to an open bug with Inspector, but feel free to re-open if needed.

olaservo avatar May 18 '25 13:05 olaservo