بنية الخادم متعددة الطبقات
افصلوا بين النقل والمعالجات ومنطق الأعمال.
بنية الخادم متعددة الطبقات درس مجاني في MCP Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في MCP Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة MCP Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why One Big File Hurts
A server crammed into one file mixes transport, tool logic, and data calls. As it grows, every change risks breaking something far away.
Think in Layers
A clean server splits into layers: transport at the edge, MCP handlers in the middle, and pure business logic underneath.
The Transport Layer
The outer transport layer only moves messages, over stdio or HTTP. It should know nothing about what your tools actually do.
Handlers Are Thin Adapters
Your tool and resource handlers should be thin: parse arguments, call a service, shape the result. Keep real work out of them.
@mcp.tool()
def summarize(text: str) -> str:
return service.summarize(text)The Service Layer Does the Work
Put the actual logic in a plain service module with no MCP imports. It is just normal Python you can call from anywhere.
def summarize(text: str) -> str:
return shorten(clean(text))Why the Split Pays Off
With logic decoupled from MCP, you can test it without spinning up a server, and reuse it in a CLI or web app later.
Dependencies Point Inward
Keep the rule one-way: handlers may import services, but services must never import handlers or the transport above them.
A Folder Per Concern
Map layers to folders so the structure tells the story: server entry, handlers, services, and models each get their own place.
src/
server.py
handlers/
services/
models.pyRegister, Don't Define, in Entry
Your server.py should wire handlers to the FastMCP instance, not contain logic. It is an assembly point, not a workhorse.
from handlers import register_tools
register_tools(mcp)Cross-Cutting Helpers Stay Shared
Logging, config, and validation are shared concerns. Give them a common module so every layer uses the same helpers.
Grow Without Rewrites
Layering lets the server scale in size: add a new tool by adding a handler plus a service, leaving everything else untouched.
Quick Check
Where should the real business logic of a tool live?
Recap: Layered Design
Split your server into transport, thin handlers, and pure services, with dependencies pointing inward. Next: configuration. 🧱
الأسئلة الشائعة
هل درس «بنية الخادم متعددة الطبقات» مجاني؟
نعم — نص درس «بنية الخادم متعددة الطبقات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MCP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MCP Academy 4 دروس في المجموع.
ماذا ستتعلم في «بنية الخادم متعددة الطبقات»؟
افصلوا بين النقل والمعالجات ومنطق الأعمال. تتمرن على MCP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ MCP Academy؟
لا تُشترط خبرة سابقة. MCP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «بنية الخادم متعددة الطبقات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس MCP Academy هذا؟
نعم. كل درس في MCP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- بنية الخادم متعددة الطبقات
- الإعدادات والأسرار عبر البيئة
- أدوات متكررة وآمنة ومدركة للآثار الجانبية
- إصدار الأدوات والمخططات