streamable_http_client.py

Overview

streamable_http_client.py is a lightweight asynchronous Python client script designed to demonstrate how to interact with an MCP (Modular Control Protocol) server over HTTP using a streamable HTTP client interface. The script establishes a bidirectional streaming HTTP connection to an MCP server endpoint, creates an MCP client session, initializes it, lists available tools on the server, and calls a specific tool with provided arguments. This file primarily serves as an example or utility script to showcase how to use the streamablehttp_client and ClientSession for remote procedure calls via streaming HTTP.


Detailed Explanation

Imports


Main Components

Function: main()

An asynchronous entry-point function that performs the following steps:

  1. Establishes a streaming HTTP connection to the MCP server at http://localhost:9382/mcp/.

  2. Opens a ClientSession over the established streams.

  3. Initializes the session.

  4. Retrieves and prints the list of tools available on the MCP server.

  5. Calls the tool named "ragflow_retrieval" with a set of arguments, including dataset IDs, document IDs, and a question.

  6. Prints the response from the tool call.

  7. Catches and prints any exceptions that occur during the process.

Parameters
Returns
Usage Example
import anyio
from streamable_http_client import main

anyio.run(main)

This will connect to the MCP endpoint, list available tools, invoke a specific tool, and print the results.


Script Entry Point

if __name__ == "__main__":
    from anyio import run
    run(main)

This ensures that when the script is run as the main program, it will execute the asynchronous main function within an anyio event loop.


Important Implementation Details


Interaction with Other System Components

This file itself acts as a consumer of the MCP services exposed by the server and is likely part of a larger ecosystem where multiple tools and services are managed via the MCP protocol.


Usage Workflow Summary

  1. Enter main() coroutine.

  2. Open streaming HTTP connection with streamablehttp_client.

  3. Create ClientSession over streams.

  4. Initialize the session.

  5. Fetch available tools.

  6. Call a specific tool with parameters.

  7. Print results.

  8. Handle any errors.


Mermaid Class Diagram

classDiagram
    class streamablehttp_client {
        <<context manager>>
        +__aenter__()
        +__aexit__()
    }

    class ClientSession {
        +__aenter__()
        +__aexit__()
        +initialize()
        +list_tools()
        +call_tool(name: str, arguments: dict)
    }

    class main {
        <<async function>>
        +main()
    }

    main --> streamablehttp_client : uses
    main --> ClientSession : uses

Summary

The streamable_http_client.py script is a concise asynchronous client example demonstrating how to connect to an MCP server via streaming HTTP, initialize a session, list available tools, and perform a tool invocation. It serves as a practical reference for developers working with the MCP protocol and the InfiniFlow ecosystem, showcasing effective use of asynchronous context managers and streaming IO for remote procedure calls.