python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

ClosedResourceError when using FastMCP(..., stateless_http=True, json_response=True) with MCP Inspector

Open yabramow opened this issue 2 months ago • 0 comments

Initial Checks

  • [x] I confirm that I'm using the latest version of MCP Python SDK
  • [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue

Description

Summary

Running the example MCP server fastmcp_quickstart.py with: FastMCP("Demo", stateless_http=True, json_response=True) and connecting to it using MCP Inspector triggers an exception inside the server’s message router: ClosedResourceError

This occurs immediately after the Inspector connects.

Steps to Reproduce

  1. Use the example server from the Python MCP SDK: uv run --with mcp examples/snippets/servers/fastmcp_quickstart.py
  2. Ensure the server instance is initialized as: FastMCP("Demo", stateless_http=True, json_response=True)
  3. Open MCP Inspector and connect to this server.
  4. Observe the server error.

Environment

  • MCP Python SDK version: latest
  • Python version: 3.13
  • OS: macOS (Apple Silicon)
  • MCP Inspector version: latest

Example Code

"""
FastMCP quickstart example.

Run from the repository root:
    uv run examples/snippets/servers/fastmcp_quickstart.py
"""

from mcp.server.fastmcp import FastMCP

# Create an MCP server
mcp = FastMCP("Demo", stateless_http=True, json_response=True)


# 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}!"


# Add a prompt
@mcp.prompt()
def greet_user(name: str, style: str = "friendly") -> str:
    """Generate a greeting prompt"""
    styles = {
        "friendly": "Please write a warm, friendly greeting",
        "formal": "Please write a formal, professional greeting",
        "casual": "Please write a casual, relaxed greeting",
    }

    return f"{styles.get(style, styles['friendly'])} for someone named {name}."


# Run with streamable HTTP transport
if __name__ == "__main__":
    mcp.run(transport="streamable-http")

Python & MCP Python SDK

Python: 3.13
MCP Python SDK: 1.22.1.dev4+b19fa6f

yabramow avatar Nov 24 '25 14:11 yabramow