تشغيل الخادم باستخدام stdio
شغّلوا الخادم لكي يتمكن العميل من الاتصال محليًا.
تشغيل الخادم باستخدام stdio درس مجاني في MCP Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في MCP Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة MCP Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Time to Start It Up
You have a server object and a tool. Now you need to actually run it so a client can connect and use what you built. 🏃
What stdio Means
The simplest transport is stdio: standard input and output. The client launches your script and talks to it through those two streams.
Call run()
Starting the server is one method. Calling mcp.run() hands control to FastMCP, which begins listening for messages right away.
mcp.run()Choose the Transport
Pass a transport argument to be explicit. For local use you want "stdio", which is also the default when you call run.
mcp.run(transport="stdio")The Main Guard
Wrap the call in the classic __main__ guard. That way the server starts only when the file is run directly, not when imported.
if __name__ == "__main__":
mcp.run()The Whole File
Here is a complete server: import, instance, tool, and run. Just a handful of lines and it speaks MCP.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("demo")
@mcp.tool()
def hello() -> str:
return "Hi!"
if __name__ == "__main__":
mcp.run()stdout Is Reserved
One golden rule: never print to standard output. That stream carries protocol messages, and stray text will corrupt them.
Log to stderr Instead
Need to debug? Write to stderr, not stdout. The client ignores stderr, so your notes stay out of the protocol stream.
import sys
print("starting up", file=sys.stderr)The Client Launches You
With stdio, you rarely run the script by hand. The host app starts your process for you using a configured command.
It Waits, Not Loops Idle
After run() the server blocks, waiting for messages. It isn't frozen; it's listening for the next request from the client.
stdio Is Local Only
Remember that stdio works only on the same machine, since it relies on a launched process. Remote servers use HTTP transport instead, which comes later.
Quick Check
Why must you avoid plain print() in a stdio server?
Recap
You ran the server with mcp.run() over stdio, guarded it with __main__, and kept stdout clean. It's live and listening. Let's confirm it works! ✅
الأسئلة الشائعة
هل درس «تشغيل الخادم باستخدام stdio» مجاني؟
نعم — نص درس «تشغيل الخادم باستخدام stdio» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MCP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MCP Academy 4 دروس في المجموع.
ماذا ستتعلم في «تشغيل الخادم باستخدام stdio»؟
شغّلوا الخادم لكي يتمكن العميل من الاتصال محليًا. تتمرن على MCP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ MCP Academy؟
لا تُشترط خبرة سابقة. MCP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «تشغيل الخادم باستخدام stdio»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس MCP Academy هذا؟
نعم. كل درس في MCP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إنشاء مثيل FastMCP
- إضافة أداة Hello
- تشغيل الخادم باستخدام stdio
- التأكد من تحميل الأدوات وسردها