Skip to content

Support MCP client communication with servers over Streamable HTTP transport #46

Open

Description

@Ashfaqbs

Description:
Currently, the Python MCP client example demonstrates communication with an MCP server via the STDIO transport. However, MCP also defines a streamable-http transport type that enables communication over HTTP with streaming capabilities.

It would be valuable to have an example or feature in this repository that shows how to implement a client communicating with an MCP server using streamable-http. This would help developers understand how to:

Initialize and configure the client for HTTP-based streaming communication

Handle request/response cycles and event streams over HTTP

Compare differences in setup and behavior between STDIO and streamable-http transports

Providing this example would make the quick start resources more complete and allow developers to experiment with both major transport types defined in MCP.

Activity

  1. Premansh12 commented on May 25, 2026

    @Premansh12

    Thanks for the detailed write-up — this is a well-scoped gap. The SDK already ships mcp.client.streamable_http.streamablehttp_client and the server side has examples under examples/servers/simple-streamablehttp-stateless/, but there's no matching client snippet to pair with it. Here's a PR-ready fix.


    What's added

    Two new files under examples/:

    File Role
    examples/server_streamable_http.py FastMCP server that starts with transport="streamable-http" (mirrors fastmcp_quickstart.py)
    examples/client_streamable_http.py Client that uses streamablehttp_client to list tools/resources/prompts and call a tool

    Key differences vs. the stdio client

    # ── STDIO (existing example) ──────────────────────────────────────────────
    from mcp import ClientSession, StdioServerParameters
    from mcp.client.stdio import stdio_client
    
    server_params = StdioServerParameters(command="uv", args=["run", "server", ...])
    
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()
            ...
    
    # ── Streamable HTTP (new example) ─────────────────────────────────────────
    from mcp import ClientSession
    from mcp.client.streamable_http import streamablehttp_client
    
    SERVER_URL = "http://127.0.0.1:8000/mcp"
    
    # streamablehttp_client yields a third value: get_session_id()
    async with streamablehttp_client(SERVER_URL) as (read, write, get_session_id):
        async with ClientSession(read, write) as session:
            await session.initialize()
            session_id = get_session_id()   # mcp-session-id header value
            ...

    The ClientSession API is identical after initialize() — same list_tools(), call_tool(), read_resource(), etc. — so existing client code can be ported just by swapping the transport context manager.


    How to run

    Terminal 1 — start the server:

    uv run examples/server_streamable_http.py
    # Listening on http://127.0.0.1:8000/mcp

    Terminal 2 — run the client:

    uv run examples/client_streamable_http.py
    # Connecting to MCP server via streamable-http: http://127.0.0.1:8000/mcp
    # Session established (id=<uuid>)
    # call_tool('add', {a: 7, b: 35}) → [TextContent(type='text', text='42')]

    Or point it at any other streamable-http server:

    SERVER_URL=https://my-remote-server.example.com/mcp uv run examples/client_streamable_http.py

    Transport comparison

    STDIO Streamable HTTP
    Import mcp.client.stdio mcp.client.streamable_http
    Config StdioServerParameters(command=…) URL string
    Connectivity Local subprocess only Local or remote
    Session tracking N/A (1:1 process) mcp-session-id header
    Recommended for Local dev / Claude Desktop Production / networked deployments
    SSE streaming ❌ ✅ (server-sent events per request)
    Resumability ❌ ✅ (via EventStore + last-event-id)

    I'll open a PR with these two files and a short README update pointing to both the stdio and streamable-http client examples. Happy to also add an asyncio-based integration test if that would help land this faster.

    client_streamable_http.py

    server_streamable_http.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions