إليك كيفية تشغيل خادم وبائع HTTP التقليدي للبث، بالإضافة إلى خادم وبائع MCP للبث باستخدام Python.
- ستقوم بإعداد خادم MCP يبث إشعارات التقدم إلى العميل أثناء معالجة العناصر.
- سيعرض العميل كل إشعار في الوقت الفعلي.
- يغطي هذا الدليل المتطلبات الأساسية، والإعداد، والتشغيل، واستكشاف الأخطاء وإصلاحها.
- Python 3.9 أو أحدث
- حزمة
mcpالخاصة بـ Python (قم بالتثبيت باستخدامpip install mcp)
-
استنسخ المستودع أو قم بتنزيل ملفات الحل.
git clone https://github.com/microsoft/mcp-for-beginners
-
إنشاء وتفعيل بيئة افتراضية (موصى به):
python -m venv venv .\venv\Scripts\Activate.ps1 # On Windows # or source venv/bin/activate # On Linux/macOS
-
تثبيت التبعيات المطلوبة:
pip install "mcp[cli]"
-
انتقل إلى دليل الحل:
cd 03-GettingStarted/06-http-streaming/solution
-
ابدأ خادم البث HTTP التقليدي:
python server.py
-
سيبدأ الخادم ويعرض:
Starting FastAPI server for classic HTTP streaming... INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
-
افتح نافذة طرفية جديدة (فعّل نفس البيئة الافتراضية والدليل):
cd 03-GettingStarted/06-http-streaming/solution python client.py
-
يجب أن ترى الرسائل المتدفقة مطبوعة بالتسلسل:
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 ---
- انتقل إلى دليل الحل:
cd 03-GettingStarted/06-http-streaming/solution
- ابدأ خادم MCP مع نقل streamable-http:
python server.py mcp
- سيبدأ الخادم ويعرض:
Starting MCP server with streamable-http transport... INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
- افتح نافذة طرفية جديدة (فعّل نفس البيئة الافتراضية والدليل):
cd 03-GettingStarted/06-http-streaming/solution python client.py mcp
- يجب أن ترى الإشعارات مطبوعة في الوقت الفعلي أثناء معالجة الخادم لكل عنصر:
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')]
- إنشاء خادم MCP باستخدام FastMCP.
- تعريف أداة تعالج قائمة وترسل إشعارات باستخدام
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/awaitللعمليات غير المتزامنة.** - تعامل دائمًا مع الاستثناءات في كل من الخادم والعميل لضمان المتانة.
- اختبر مع عدة عملاء لملاحظة التحديثات في الوقت الفعلي.
- إذا واجهت أخطاء، تحقق من إصدار Python الخاص بك وتأكد من تثبيت جميع التبعيات.
إخلاء المسؤولية:
تمت ترجمة هذا المستند باستخدام خدمة الترجمة الآلية Co-op Translator. بينما نسعى لتحقيق الدقة، يرجى العلم أن الترجمات الآلية قد تحتوي على أخطاء أو عدم دقة. يجب اعتبار المستند الأصلي بلغته الأصلية المصدر الموثوق والمعتمد. للمعلومات الحساسة أو الهامة، يُنصح بالاعتماد على الترجمة البشرية المهنية. نحن غير مسؤولين عن أي سوء فهم أو تفسير خاطئ ناتج عن استخدام هذه الترجمة.