Die Roots des Clients anfordern
Fordern Sie die Liste der zulässigen Arbeitsverzeichnisse an.
Die Roots des Clients anfordern ist eine kostenlose MCP Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des MCP Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der MCP Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. ✅
Häufig gestellte Fragen
Ist die Lektion „Die Roots des Clients anfordern“ kostenlos?
Ja — der vollständige Text von „Die Roots des Clients anfordern“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des MCP Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der MCP Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Die Roots des Clients anfordern“?
Fordern Sie die Liste der zulässigen Arbeitsverzeichnisse an. Du übst MCP Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um MCP Academy zu starten?
Keine Vorkenntnisse erforderlich. MCP Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Die Roots des Clients anfordern“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser MCP Academy-Lektion Code schreiben und ausführen?
Ja. Jede MCP Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Was Roots einem Server mitteilen
- Die Roots des Clients anfordern
- Fähigkeiten aushandeln
- Grenzen des Clients respektieren