-
Notifications
You must be signed in to change notification settings - Fork 213
feat: add roots-demo sample for client roots capability #763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MichielDean
wants to merge
10
commits into
modelcontextprotocol:main
Choose a base branch
from
MichielDean:feat/add-roots-sample
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6640475
feat: add roots-demo sample for client roots capability
ea6ade4
fix: address Copilot review comments on roots-demo sample
33be290
fix: address copilot review round 2 — error handling and deterministi…
81f2f12
fix: use AtomicInteger for thread-safe notification counting in roots…
c0f3ff0
fix: deterministic per-notification sync and safe shutdown in roots-demo
a4ec6d1
fix: complete notification signals in finally block so demo terminate…
637d368
fix: wrap await calls in withTimeoutOrNull and close in finally block
16c0cf8
refactor: simplify roots-demo to focus on the Roots API
89e6bc9
Merge branch 'main' into feat/add-roots-sample
MichielDean e241a98
fix: address all Copilot review feedback holistically
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Roots Demo | ||
|
|
||
| A minimal sample demonstrating the MCP **Roots** capability using in-memory | ||
| [ChannelTransport](../../kotlin-sdk-testing/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/testing/ChannelTransport.kt). | ||
|
|
||
| ## Overview | ||
|
|
||
| [Roots](https://modelcontextprotocol.io/docs/concepts/roots) allow an MCP client to | ||
| expose filesystem locations to a server. The server can request the list of roots and | ||
| receive notifications when that list changes. | ||
|
|
||
| This sample shows the full roots lifecycle: | ||
|
|
||
| 1. **Client declares roots capability** — the client advertises `roots` with | ||
| `listChanged = true` during initialization. | ||
| 2. **Client registers roots** — `addRoot(uri, name)` adds roots that the server can | ||
| discover. | ||
| 3. **Server queries roots** — `serverSession.listRoots()` sends a `roots/list` | ||
| request to the client. | ||
| 4. **Client sends change notification** — after dynamically adding or removing roots, | ||
| the client calls `sendRootsListChanged()` so the server knows to re-fetch. | ||
| 5. **Server reacts to changes** — the server listens for | ||
| `notifications/roots/list_changed` and re-queries the updated root list. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - JDK 17+ | ||
|
|
||
| ## Build & Run | ||
|
|
||
| ```shell | ||
| ./gradlew run | ||
| ``` | ||
|
|
||
| The program connects a client and server in-process using `ChannelTransport` and | ||
| prints the roots exchange to the console. No external server or network is required. | ||
|
|
||
| ## MCP Capabilities | ||
|
|
||
| ### Client | ||
|
|
||
| | Capability | Details | | ||
| |-----------|---------| | ||
| | `roots` | Advertised with `listChanged = true`. Registers project directories and notifies the server on changes. | | ||
|
|
||
| ### Server | ||
|
|
||
| Demonstrates consuming the roots capability by calling `listRoots()` and listening for | ||
| `notifications/roots/list_changed`. | ||
|
|
||
| ## Additional Resources | ||
|
|
||
| - [MCP Roots Specification](https://modelcontextprotocol.io/docs/concepts/roots) | ||
| - [Kotlin MCP SDK](https://github.com/modelcontextprotocol/kotlin-sdk) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| plugins { | ||
| alias(libs.plugins.kotlin.jvm) | ||
| alias(libs.plugins.shadow) | ||
| application | ||
| } | ||
|
|
||
| group = "org.example" | ||
| version = "0.1.0" | ||
|
|
||
| application { | ||
| mainClass.set("io.modelcontextprotocol.sample.roots.MainKt") | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(libs.mcp.kotlin.client) | ||
| implementation(libs.mcp.kotlin.server) | ||
| implementation(libs.mcp.kotlin.testing) | ||
| implementation(libs.slf4j.simple) | ||
| } | ||
|
|
||
| tasks.test { | ||
| useJUnitPlatform() | ||
| } | ||
|
|
||
| kotlin { | ||
| jvmToolchain(17) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| org.gradle.configuration-cache=true | ||
| org.gradle.parallel=true | ||
| org.gradle.caching=true | ||
|
|
||
| #mcp.kotlin.overrideVersion=0.0.1-SNAPSHOT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| [versions] | ||
| kotlin = "2.3.21" | ||
| mcp-kotlin = "0.12.0" | ||
| shadow = "9.4.1" | ||
| slf4j = "2.0.17" | ||
|
|
||
| [libraries] | ||
| mcp-kotlin-client = { group = "io.modelcontextprotocol", name = "kotlin-sdk-client", version.ref = "mcp-kotlin" } | ||
| mcp-kotlin-server = { group = "io.modelcontextprotocol", name = "kotlin-sdk-server", version.ref = "mcp-kotlin" } | ||
| mcp-kotlin-testing = { group = "io.modelcontextprotocol", name = "kotlin-sdk-testing", version.ref = "mcp-kotlin" } | ||
| slf4j-simple = { group = "org.slf4j", name = "slf4j-simple", version.ref = "slf4j" } | ||
|
|
||
| [plugins] | ||
| kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } | ||
| shadow = { id = "com.gradleup.shadow", version.ref = "shadow" } |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-9.5.0-bin.zip | ||
| networkTimeout=10000 | ||
| retries=0 | ||
| retryBackOffMs=500 | ||
| validateDistributionUrl=true | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in e241a98. The demo now demonstrates both adding AND removing roots. After adding "Shared Libraries" and notifying the server, the client waits for the server to process the notification (via CompletableDeferred), then removes the root with
removeRoot()and sends a secondsendRootsListChanged(). The server handler prints the updated list after each change, showing the intermediate 3-root state and the final 2-root state. The README already mentions "adding or removing roots" which now matches the code.