0Pricing
MCP Academy · Aula

Solicitar as raízes do cliente

Peça a lista de diretórios de trabalho permitidos.

Solicitar as raízes do cliente é uma aula grátis de MCP Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de MCP Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de MCP Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em 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. ✅

Perguntas Frequentes

A aula “Solicitar as raízes do cliente” é grátis?

Sim — o texto completo de “Solicitar as raízes do cliente” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de MCP Academy, atualize para CoddyKit PRO. O curso de MCP Academy inclui 4 aulas no total.

O que vou aprender em “Solicitar as raízes do cliente”?

Peça a lista de diretórios de trabalho permitidos. Você pratica MCP Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar MCP Academy?

Nenhuma experiência prévia é necessária. MCP Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Solicitar as raízes do cliente”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de MCP Academy?

Sim. Cada aula de MCP Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. O que as raízes informam a um servidor
  2. Solicitar as raízes do cliente
  3. Negociar capacidades
  4. Respeitar os limites do cliente
← Voltar para MCP Academy