الوصول إلى كائن السياق
الوصول إلى سياق الطلب داخل أداة.
الوصول إلى كائن السياق درس مجاني في MCP Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في MCP Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة MCP Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Meet the Context Object
The Context object is your tool's gateway to the request: it carries shared state plus helpers for logging and progress.
Ask for It by Type
Add a parameter type-hinted as Context and FastMCP injects it automatically. Clients never see this argument.
from mcp.server.fastmcp import Context
@mcp.tool()
async def greet(name: str, ctx: Context) -> str:
return "hi " + nameReach Your Shared State
Drill into ctx.request_context.lifespan_context to get the exact object your lifespan yielded at startup.
app = ctx.request_context.lifespan_context
rows = await app.db.query("...")Use the Live Connection
Now your tool runs work against the shared pool instead of opening a brand new connection on every call.
Log Through the Context
Call ctx.info to send a log line to the client, which is far more useful than a hidden print on the server.
await ctx.info("loading user " + str(user_id))Report Progress Too
For slow work, ctx.report_progress streams percent-done updates so the host can show the user a moving bar.
await ctx.report_progress(progress=3, total=10)It's Injected, Not Passed
The model never fills in ctx. FastMCP recognizes the type hint and supplies it, so it stays out of your tool schema.
Async Tools Love Context
Most context helpers are awaitable, so define context-using tools as async functions to call them cleanly.
Name It Anything
The parameter name is up to you; the type hint is what matters. People often just call it ctx by convention.
One Context per Request
Each tool call gets its own request context, but they all point at the same long-lived lifespan state underneath.
Read Request Metadata
The context also exposes request metadata, so a tool can inspect details about the specific call it is handling.
Quick Check
How do you get the Context object inside a FastMCP tool?
Recap: The Context Object
You reach shared state via ctx.request_context.lifespan_context and use ctx for logging and progress. Next: shutting down cleanly. 🔌
الأسئلة الشائعة
هل درس «الوصول إلى كائن السياق» مجاني؟
نعم — نص درس «الوصول إلى كائن السياق» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MCP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MCP Academy 4 دروس في المجموع.
ماذا ستتعلم في «الوصول إلى كائن السياق»؟
الوصول إلى سياق الطلب داخل أداة. تتمرن على MCP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ MCP Academy؟
لا تُشترط خبرة سابقة. MCP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «الوصول إلى كائن السياق»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس MCP Academy هذا؟
نعم. كل درس في MCP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- خطاف بدء التشغيل لدورة الحياة
- مشاركة الحالة عبر سياق التطبيق
- الوصول إلى كائن السياق
- الإيقاف النظيف وتحرير الموارد