Skip to content

Commit d9fe2f3

Browse files
committed
Add imports into MCP blog code snippets
1 parent 9b9f535 commit d9fe2f3

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

posts/2025-10-23-mcp-standalone-blog.adoc

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ The following is an example of a tool for getting the weather forecast. The tool
5454

5555
[source,java]
5656
----
57+
package com.example.mcp;
58+
59+
import io.openliberty.mcp.annotations.Tool;
60+
import io.openliberty.mcp.annotations.ToolArg;
61+
import jakarta.enterprise.context.ApplicationScoped;
62+
import jakarta.inject.Inject;
63+
5764
@ApplicationScoped
5865
public class WeatherTools {
5966
@@ -105,7 +112,26 @@ You can add these hints within your `@Tool` annotation. Below is an example:
105112

106113
[source,java]
107114
----
108-
@Tool(annotations = @Annotations(readOnlyHint = true, openWorldHint = false)
115+
package com.example.mcp;
116+
117+
import io.openliberty.mcp.annotations.Tool;
118+
import io.openliberty.mcp.annotations.Tool.Annotations;
119+
import jakarta.enterprise.context.ApplicationScoped;
120+
121+
@ApplicationScoped
122+
public class BasicTools {
123+
124+
@Tool(name = "toolWithAnnotation", title = "Tool with Annotation", description = "A tool that has annotations",
125+
annotations = @Annotations(
126+
readOnlyHint = true,
127+
destructiveHint = true,
128+
idempotentHint = true,
129+
openWorldHint = false))
130+
public String toolWithAnnotation() {
131+
return "This tool has some annotations";
132+
}
133+
134+
}
109135
----
110136

111137
The hints you can currently add to your tool include:
@@ -126,14 +152,26 @@ To enable cancellation for your tool, you must accept a `Cancellation` parameter
126152

127153
[source,java]
128154
----
129-
@Tool
130-
public String bigComputationTool(Cancellation cancellation) {
131-
ArrayList<Data> result = new ArrayList<>();
132-
for (Data someData : getData()) {
133-
cancellation.skipProcessingIfCancelled();
134-
result.add(process(someData));
135-
}
136-
return result;
155+
package com.example.mcp;
156+
157+
import java.util.Arrays;
158+
159+
import io.openliberty.mcp.annotations.Tool;
160+
import io.openliberty.mcp.messaging.Cancellation;
161+
import jakarta.enterprise.context.ApplicationScoped;
162+
163+
@ApplicationScoped
164+
public class BasicTools {
165+
@Tool(name = "cancellationTool", title = "Cancellable tool", description = "A tool that waits to be cancelled")
166+
public String bigComputationTool(Cancellation cancellation) throws InterruptedException {
167+
String[] citiesArray = { "London", "Japan", "New York" };
168+
for (String city : citiesArray) {
169+
cancellation.skipProcessingIfCancelled();
170+
System.out.println(city);
171+
}
172+
return Arrays.toString(citiesArray);
173+
}
174+
137175
}
138176
----
139177

0 commit comments

Comments
 (0)