4.0 KiB
4.0 KiB
Running this sample
Here's how to run the classic HTTP streaming server and client, as well as the MCP streaming server and client using Python.
Overview
- You will set up an MCP server that streams progress notifications to the client as it processes items.
- The client will display each notification in real time.
- This guide covers prerequisites, setup, running, and troubleshooting.
Prerequisites
- Python 3.9 or newer
- The
mcpPython package (install withpip install mcp)
Installation & Setup
-
Clone the repository or download the solution files.
git clone https://github.com/microsoft/mcp-for-beginners -
Create and activate a virtual environment (recommended):
python -m venv venv .\venv\Scripts\Activate.ps1 # On Windows # or source venv/bin/activate # On Linux/macOS -
Install required dependencies:
pip install "mcp[cli]" fastapi requests
Files
Running the Classic HTTP Streaming Server
-
Navigate to the solution directory:
cd 03-GettingStarted/06-http-streaming/solution -
Start the classic HTTP streaming server:
python server.py -
The server will start and display:
Starting FastAPI server for classic HTTP streaming... INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
Running the Classic HTTP Streaming Client
-
Open a new terminal (activate the same virtual environment and directory):
cd 03-GettingStarted/06-http-streaming/solution python client.py -
You should see streamed messages printed sequentially:
Running classic HTTP streaming client... Connecting to http://localhost:8000/stream with message: hello --- Streaming Progress --- Processing file 1/3... Processing file 2/3... Processing file 3/3... Here's the file content: hello --- Stream Ended ---
Running the MCP Streaming Server
- Navigate to the solution directory:
cd 03-GettingStarted/06-http-streaming/solution - Start the MCP server with the streamable-http transport:
python server.py mcp - The server will start and display:
Starting MCP server with streamable-http transport... INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
Running the MCP Streaming Client
- Open a new terminal (activate the same virtual environment and directory):
cd 03-GettingStarted/06-http-streaming/solution python client.py mcp - You should see notifications printed in real time as the server processes each item:
Running MCP client... Starting client... Session ID before init: None Session ID after init: a30ab7fca9c84f5fa8f5c54fe56c9612 Session initialized, ready to call tools. Received message: root=LoggingMessageNotification(...) NOTIFICATION: root=LoggingMessageNotification(...) ... Tool result: meta=None content=[TextContent(type='text', text='Processed files: file_1.txt, file_2.txt, file_3.txt | Message: hello from client')]
Key Implementation Steps
- Create the MCP server using FastMCP.
- Define a tool that processes a list and sends notifications using
ctx.info()orctx.log(). - Run the server with
transport="streamable-http". - Implement a client with a message handler to display notifications as they arrive.
Code Walkthrough
- The server uses async functions and the MCP context to send progress updates.
- The client implements an async message handler to print notifications and the final result.
Tips & Troubleshooting
- Use
async/awaitfor non-blocking operations. - Always handle exceptions in both server and client for robustness.
- Test with multiple clients to observe real-time updates.
- If you encounter errors, check your Python version and ensure all dependencies are installed.