0Pricing
MCP Academy · Lesson

Request the Client's Roots

Ask for the list of allowed working directories.

Request the Client's Roots is a free MCP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MCP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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).path

Listen 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_changed

Handle 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. ✅

Frequently asked questions

Is the “Request the Client's Roots” lesson free?

Yes — the full text of “Request the Client's Roots” is free to read here on the web, and the MCP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MCP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Request the Client's Roots”?

Ask for the list of allowed working directories. You practise MCP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start MCP Academy?

No prior experience is required. MCP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Request the Client's Roots” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this MCP Academy lesson?

Yes. Every MCP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What Roots Tell a Server
  2. Request the Client's Roots
  3. Negotiating Capabilities
  4. Respect Client Boundaries
← Back to MCP Academy