0Pricing
MCP Academy · レッスン

クライアントのRootsをリクエストする

許可された作業ディレクトリの一覧を要求します。

「クライアントのRootsをリクエストする」はCoddyKit上の無料MCP Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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).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. ✅

よくある質問

「クライアントのRootsをリクエストする」レッスンは無料ですか?

はい。「クライアントのRootsをリクエストする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、MCP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 MCP Academyコースには全4レッスンが含まれています。

「クライアントのRootsをリクエストする」で何を学びますか?

許可された作業ディレクトリの一覧を要求します。 ブラウザで直接実行するハンズオンコードでMCP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

MCP Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMCP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「クライアントのRootsをリクエストする」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMCP Academyレッスンでコードを書いて実行できますか?

はい。すべてのMCP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Rootsがサーバーに伝えること
  2. クライアントのRootsをリクエストする
  3. 機能をネゴシエーションする
  4. クライアントの境界を尊重する
← MCP Academyに戻る