[Python] The Python MCP server with STDIO transport throws an 'Error: SSE connection not...

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    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:

    • Node.js v22.14.0
    • npm@10.9.2
    • C:\Users...\AppData\Roaming\npm ├── @modelcontextprotocol/inspector@0.10.2 ├── @modelcontextprotocol/server-brave-search@0.6.2 ├── @modelcontextprotocol/server-filesystem@2025.3.28 └── @modelcontextprotocol/server-memory@0.6.2

    My actions to run the server are:

    1. mcp dev src/weather.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

    Continue reading...

Compartilhe esta Página