طلب Roots الخاصة بالعميل
اطلبوا قائمة أدلة العمل المسموح بها.
طلب Roots الخاصة بالعميل درس مجاني في MCP Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في MCP Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة MCP Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Now Let's Ask for Them
Knowing roots exist is step one. Step two is actually asking the client for its current list so your server can decide where to read or write. 📨
The Server Initiates
Here the direction flips: your server sends the request and the client answers. The method it calls is roots/list, asking for everything the client currently exposes.
{ "method": "roots/list" }Reach Roots via the Context
In the Python SDK you do not craft raw JSON. You use the request context object handed to your tool, which wraps the live session to the client.
async def my_tool(ctx: Context):
...One Friendly Call
From that context you call list_roots() and await it. The SDK sends roots/list for you and returns the client's answer as a typed result.
result = await ctx.session.list_roots()What Comes Back
The reply holds a roots list. Each item has a uri and an optional name, exactly the small shape you saw before, ready for you to loop over.
for root in result.roots:
print(root.uri, root.name)It May Be Empty
A valid answer can be an empty list. That means the client offers no roots right now, so plan a sensible fallback rather than assuming a folder exists.
Check the Capability First
Calling list_roots only makes sense if the client supports roots. A polite server checks the declared capability before asking, avoiding errors on hosts that lack it.
Ask at the Right Time
Fetch roots when you need them, like at the start of a file tool. Asking lazily keeps you current if the user has since opened a different folder.
Turn a Root into a Path
A root's uri is a string like file:///home/ada. To use it, parse the file:// URI into a real filesystem path before opening anything inside it.
from urllib.parse import urlparse
path = urlparse(root.uri).pathListen for Changes
If the client supports it, it can send a notification when roots change. Catching that lets you re-fetch the list instead of working from a stale view.
notifications/roots/list_changedHandle Failures Gently
The request can still fail or time out. Wrap the call so a missing answer leaves your tool with a clear message instead of a crash. 🛟
Quick Check
Pick the right way to get the client's roots in the SDK.
Recap: Asking for Roots
You await list_roots() on the session, loop over the returned roots, parse each file:// uri, and handle empty or failed replies. Re-ask when roots change. ✅
الأسئلة الشائعة
هل درس «طلب Roots الخاصة بالعميل» مجاني؟
نعم — نص درس «طلب Roots الخاصة بالعميل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة MCP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة MCP Academy 4 دروس في المجموع.
ماذا ستتعلم في «طلب Roots الخاصة بالعميل»؟
اطلبوا قائمة أدلة العمل المسموح بها. تتمرن على MCP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ MCP Academy؟
لا تُشترط خبرة سابقة. MCP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «طلب Roots الخاصة بالعميل»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس MCP Academy هذا؟
نعم. كل درس في MCP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- ما الذي تخبر به Roots الخادم؟
- طلب Roots الخاصة بالعميل
- التفاوض على القدرات
- احترام حدود العميل