أداة آلة حاسبة من البداية إلى النهاية
أنشئوا أداة حسابية صغيرة ومتكاملة.
أداة آلة حاسبة من البداية إلى النهاية درس مجاني في MCP Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في MCP Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة MCP Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Build One Complete Tool
Let's combine everything into one small calculator tool, from a clear name to a clean result the model can trust. 🧮
Start with the Server
First create a FastMCP instance to hold the tool. Give it a name so clients can identify your server.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calculator")Name the Operation
Pick a precise name. We will add two numbers, so the function add reads clearly to both you and the model.
Declare Typed Parameters
Use type hints so the model sends numbers, not text. We accept two floats to allow decimals as well as whole values.
def add(a: float, b: float) -> float:
...Write the Docstring
The docstring becomes the description, so state plainly what the tool does and what it returns.
def add(a: float, b: float) -> float:
"""Add two numbers and return the sum."""Decorate It as a Tool
Add the tool decorator so FastMCP registers the function and advertises it to any connected client.
@mcp.tool()
def add(a: float, b: float) -> float:
"""Add two numbers."""
return a + bReturn a Clear Result
Returning the sum works, but a labeled result reads better to the model and to the user who sees the answer.
return a + bAdd a Second Tool
One tool per job, so create a separate divide tool rather than packing modes into add. Each stays simple to call.
@mcp.tool()
def divide(a: float, b: float) -> float:
return a / bGuard Against Bad Input
Division by zero would crash, so check first and return a clear error message the model can read and explain.
if b == 0:
return "Error: cannot divide by zero."Run the Server
Finish with the run call so the server starts and listens for a client over stdio when you launch the file.
if __name__ == "__main__":
mcp.run()Test the Whole Tool
Launch it and let a client list your tools. Seeing add and divide with their descriptions confirms the wiring is correct.
Quick Check
Why give divide its own tool instead of a mode flag on add?
Recap
You built a full calculator tool: named clearly, typed parameters, docstring description, guarded errors, and a clean result the model trusts. ✅
الأسئلة الشائعة
هل درس «أداة آلة حاسبة من البداية إلى النهاية» مجاني؟
نعم — نص درس «أداة آلة حاسبة من البداية إلى النهاية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MCP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MCP Academy 4 دروس في المجموع.
ماذا ستتعلم في «أداة آلة حاسبة من البداية إلى النهاية»؟
أنشئوا أداة حسابية صغيرة ومتكاملة. تتمرن على MCP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ MCP Academy؟
لا تُشترط خبرة سابقة. MCP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «أداة آلة حاسبة من البداية إلى النهاية»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس MCP Academy هذا؟
نعم. كل درس في MCP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تسمية الأداة ووصفها جيدًا
- تعريف المعلمات باستخدام تلميحات الأنواع
- إرجاع نتائج مفيدة
- أداة آلة حاسبة من البداية إلى النهاية