Skip to content

Commit 6ab1fa2

Browse files
committed
add documentation
1 parent 5882b3d commit 6ab1fa2

File tree

2 files changed

+97
-14
lines changed

2 files changed

+97
-14
lines changed

docs/mcp-declarative/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,42 @@ Below is an example of a tool that uses the Sampling feature. `McpSampling` obje
505505
```java
506506
@Mcp.Tool("Uses MCP Sampling to ask the connected client model.")
507507
List<McpToolContent> samplingTool(McpSampling sampling) {
508+
if (!sampling.enabled()) {
509+
throw new McpToolErrorException("This tool requires sampling feature");
510+
}
511+
512+
try {
513+
var message = McpSamplingMessages.textContent("Write a 3-line summary of Helidon MCP Sampling.", McpRole.USER);
514+
McpSamplingResponse response = sampling.request(req -> req
515+
.timeout(Duration.ofSeconds(10))
516+
.systemPrompt("You are a concise, helpful assistant.")
517+
.addMessage(message));
518+
return List.of(McpToolContents.textContent(response.asTextMessage()));
519+
} catch (McpSamplingException e) {
520+
throw new McpToolErrorException(e.getMessage());
521+
}
522+
}
523+
```
524+
525+
### Roots
526+
527+
See the full [roots documentation details](../mcp/README.md#roots)
528+
529+
#### Example
530+
531+
Below is an example of a tool that uses the Roots feature. `McpRoots` object can be used as method parameter.
532+
533+
```java
534+
@Mcp.Tool("Request MCP Roots to the connected client.")
535+
List<McpToolContent> rootsTool(McpRoots roots) {
536+
if (!roots.enabled()) {
537+
throw new McpToolErrorException(McpToolContents.textContent("Roots are not supported by the client"));
538+
}
539+
roots = request.features().roots().listRoots();
540+
McpRoot root = roots.getFirst();
541+
URI uri = root.uri();
542+
String name = root.name().orElse("Unknown");
543+
return List.of(McpToolContents.textContent("Server updated roots"));
508544
}
509545
```
510546

docs/mcp/README.md

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -736,22 +736,69 @@ class SamplingTool implements McpTool {
736736
}
737737

738738
@Override
739-
public List<McpToolContent> process(McpRequest request) {
740-
var sampling = request.features().sampling();
739+
public Function<McpRequest, List<McpToolContent>> tool() {
740+
return request -> {
741+
var sampling = request.features().sampling();
741742

742-
if (!sampling.enabled()) {
743-
throw new McpToolErrorException("This tool requires sampling feature");
744-
}
743+
if (!sampling.enabled()) {
744+
throw new McpToolErrorException("This tool requires sampling feature");
745+
}
745746

746-
try {
747-
McpSamplingResponse response = sampling.request(req -> req
748-
.timeout(Duration.ofSeconds(10))
749-
.systemPrompt("You are a concise, helpful assistant.")
750-
.addMessage(McpSamplingMessages.textContent("Write a 3-line summary of Helidon MCP Sampling.", McpRole.USER)));
751-
return List.of(McpToolContents.textContent(response.asTextMessage()));
752-
} catch (McpSamplingException e) {
753-
throw new McpToolErrorException(e.getMessage());
754-
}
747+
try {
748+
var message = McpSamplingMessages.textContent("Write a 3-line summary of Helidon MCP Sampling.", McpRole.USER);
749+
McpSamplingResponse response = sampling.request(req -> req
750+
.timeout(Duration.ofSeconds(10))
751+
.systemPrompt("You are a concise, helpful assistant.")
752+
.addMessage(message));
753+
return List.of(McpToolContents.textContent(response.asTextMessage()));
754+
} catch (McpSamplingException e) {
755+
throw new McpToolErrorException(e.getMessage());
756+
}
757+
};
758+
}
759+
}
760+
```
761+
762+
### Roots
763+
764+
Roots establish the boundaries within the filesystem that define where servers are permitted to operate. They determine which
765+
directories and files a server can access. Servers can request the current list of roots from compatible clients and receive
766+
notifications whenever that list is updated.
767+
768+
#### Example
769+
770+
```java
771+
class RootNameTool implements McpTool {
772+
private List<McpRoot> roots;
773+
774+
@Override
775+
public String name() {
776+
return "roots-name-tool";
777+
}
778+
779+
@Override
780+
public String description() {
781+
return "Get the list of roots available";
782+
}
783+
784+
@Override
785+
public String schema() {
786+
return "";
787+
}
788+
789+
@Override
790+
public Function<McpRequest, List<McpToolContent>> tool() {
791+
return request -> {
792+
McpRoots roots = request.features().roots();
793+
if (!roots.enabled()) {
794+
throw new McpToolErrorException(McpToolContents.textContent("Roots are not supported by the client"));
795+
}
796+
roots = request.features().roots().listRoots();
797+
McpRoot root = roots.getFirst();
798+
URI uri = root.uri();
799+
String name = root.name().orElse("Unknown");
800+
return List.of(McpToolContents.textContent("Server updated roots"));
801+
};
755802
}
756803
}
757804
```

0 commit comments

Comments
 (0)