0Pricing
MCP Academy · Lección

Solicitar las raíces del cliente

Solicite la lista de directorios de trabajo permitidos.

Solicitar las raíces del cliente es una lección gratuita de MCP Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de MCP Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de MCP Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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

Preguntas frecuentes

¿La lección «Solicitar las raíces del cliente» es gratis?

Sí — el texto completo de «Solicitar las raíces del cliente» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de MCP Academy, actualiza a CoddyKit PRO. El curso de MCP Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Solicitar las raíces del cliente»?

Solicite la lista de directorios de trabajo permitidos. Practicas MCP Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar MCP Academy?

No se requiere experiencia previa. MCP Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.

¿Cuánto tiempo toma la lección «Solicitar las raíces del cliente»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de MCP Academy?

Sí. Cada lección de MCP Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Qué indican las raíces a un servidor
  2. Solicitar las raíces del cliente
  3. Negociar capacidades
  4. Respetar los límites del cliente
← Volver a MCP Academy