Voici comment exécuter le serveur et le client de streaming HTTP classique, ainsi que le serveur et le client de streaming MCP en Python.
- Vous configurerez un serveur MCP qui diffuse des notifications de progression au client pendant le traitement des éléments.
- Le client affichera chaque notification en temps réel.
- Ce guide couvre les prérequis, l'installation, l'exécution et le dépannage.
- Python 3.9 ou une version plus récente
- Le package Python
mcp(installation viapip install mcp)
-
Clonez le dépôt ou téléchargez les fichiers de la solution.
git clone https://github.com/microsoft/mcp-for-beginners
-
Créez et activez un environnement virtuel (recommandé) :
python -m venv venv .\venv\Scripts\Activate.ps1 # On Windows # or source venv/bin/activate # On Linux/macOS
-
Installez les dépendances requises :
pip install "mcp[cli]"
-
Rendez-vous dans le répertoire de la solution :
cd 03-GettingStarted/06-http-streaming/solution
-
Lancez le serveur de streaming HTTP classique :
python server.py
-
Le serveur démarrera et affichera :
Starting FastAPI server for classic HTTP streaming... INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
-
Ouvrez un nouveau terminal (activez le même environnement virtuel et répertoire) :
cd 03-GettingStarted/06-http-streaming/solution python client.py
-
Vous verrez les messages diffusés s'afficher séquentiellement :
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 ---
- Rendez-vous dans le répertoire de la solution :
cd 03-GettingStarted/06-http-streaming/solution
- Lancez le serveur MCP avec le transport streamable-http :
python server.py mcp
- Le serveur démarrera et affichera :
Starting MCP server with streamable-http transport... INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
- Ouvrez un nouveau terminal (activez le même environnement virtuel et répertoire) :
cd 03-GettingStarted/06-http-streaming/solution python client.py mcp
- Vous verrez les notifications s'afficher en temps réel au fur et à mesure que le serveur traite chaque élément :
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')]
- Créez le serveur MCP avec FastMCP.
- Définissez un outil qui traite une liste et envoie des notifications via
ctx.info()orctx.log(). - Run the server with
transport="streamable-http". - Implement a client with a message handler to display notifications as they arrive.
- 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.
- Use
async/awaitpour des opérations non bloquantes.** - Gérez toujours les exceptions côté serveur et client pour assurer la robustesse.
- Testez avec plusieurs clients pour observer les mises à jour en temps réel.
- En cas d’erreurs, vérifiez votre version de Python et assurez-vous que toutes les dépendances sont installées.
Disclaimer:
Thiz documont haz been translatid yusing AI translatyun servyce Co-op Translator. Whyle we stryve for accurasy, pleez be awair that automatyd translatyons may contain errers or inaccuracyz. The orygynal documont in its natyve langwij shood be consydered the authoritatyve sorce. For crytical informashun, profeshunal hooman translatyun is recommynded. We ar not lyable for any missunderstandyngs or missinterpretashuns arysing from the use of this translatyun.