تغليف REST API في صورة أدوات
حوّلوا نقاط النهاية الخارجية إلى أدوات قابلة للاستدعاء.
تغليف REST API في صورة أدوات درس مجاني في MCP Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في MCP Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة MCP Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Turn Endpoints into Tools
Any REST API can become MCP tools. Each endpoint becomes a callable function the model can invoke to fetch or send data. 🌐
Call HTTP from a Tool
Inside a tool you make an ordinary HTTP request, then return the response as text the model can read and reason about.
Prefer an Async Client
Use an async client like httpx so a slow API call does not block other requests while it waits for the network.
import httpx
async with httpx.AsyncClient() as client:
r = await client.get(url)Map Params to Arguments
Expose the endpoint query as tool arguments. The model passes a city or id, and you place it into the request safely.
@mcp.tool()
async def get_weather(city: str) -> str:
...Keep Secrets Server-Side
Store the API key in your server environment, never as a tool argument. The model should never see or send credentials.
key = os.environ["WEATHER_API_KEY"]Check the Status Code
Inspect the HTTP status before trusting the body. A 404 or 500 means you should report a clear error, not garbage data.
r.raise_for_status()Parse and Trim JSON
Most APIs return JSON. Parse it and return only the fields the task needs, so the model is not flooded with noise.
data = r.json()
return data["temp_c"]Give Each Tool One Job
Map one endpoint to one focused tool with a clear name. Granular tools are easier for the model to pick correctly.
Write Helpful Descriptions
Describe what the endpoint does and when to use it. A precise description is how the model knows to call this tool at all.
Handle Network Failures
Wrap the call so a timeout or connection drop returns a readable error message instead of crashing the whole server.
Set Sensible Timeouts
Always pass a timeout so a hung upstream cannot freeze your tool forever waiting on a reply that never comes.
r = await client.get(url, timeout=10.0)Quick Check
Where should an API key live?
Recap: REST as Tools
You wrapped endpoints as async tools: arguments in, server-side keys, status checks, and trimmed JSON out. Next, pool connections. 🎯
الأسئلة الشائعة
هل درس «تغليف REST API في صورة أدوات» مجاني؟
نعم — نص درس «تغليف REST API في صورة أدوات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MCP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MCP Academy 4 دروس في المجموع.
ماذا ستتعلم في «تغليف REST API في صورة أدوات»؟
حوّلوا نقاط النهاية الخارجية إلى أدوات قابلة للاستدعاء. تتمرن على MCP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ MCP Academy؟
لا تُشترط خبرة سابقة. MCP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «تغليف REST API في صورة أدوات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس MCP Academy هذا؟
نعم. كل درس في MCP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إتاحة استعلامات SQL بأمان
- تغليف REST API في صورة أدوات
- تجميع الاتصالات في دورة الحياة
- التخزين المؤقت وتحديد معدل خدمات المنبع