Skip to content

Commit d14dc51

Browse files
authored
Merge pull request #1314 from github/fix/java-cookbook-sdk-0.2.1
fix: Java cookbook recipes to compile with copilot-sdk-java 0.2.1-java.1
2 parents 7c6c0be + aa6d771 commit d14dc51

File tree

5 files changed

+53
-36
lines changed

5 files changed

+53
-36
lines changed

cookbook/copilot-sdk/java/accessibility-report.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ public class AccessibilityReport {
6767
client.start().get();
6868
6969
// Configure Playwright MCP server for browser automation
70-
var mcpConfig = new McpServerConfig()
71-
.setType("local")
72-
.setCommand("npx")
73-
.setArgs(List.of("@playwright/mcp@latest"))
74-
.setTools(List.of("*"));
70+
Map<String, Object> mcpConfig = Map.of(
71+
"type", "local",
72+
"command", "npx",
73+
"args", List.of("@playwright/mcp@latest"),
74+
"tools", List.of("*")
75+
);
7576
7677
var session = client.createSession(
7778
new SessionConfig()
@@ -167,11 +168,12 @@ public class AccessibilityReport {
167168
The recipe configures a local MCP server that runs alongside the session:
168169
169170
```java
170-
var mcpConfig = new McpServerConfig()
171-
.setType("local")
172-
.setCommand("npx")
173-
.setArgs(List.of("@playwright/mcp@latest"))
174-
.setTools(List.of("*"));
171+
Map<String, Object> mcpConfig = Map.of(
172+
"type", "local",
173+
"command", "npx",
174+
"args", List.of("@playwright/mcp@latest"),
175+
"tools", List.of("*")
176+
);
175177
176178
var session = client.createSession(
177179
new SessionConfig()

cookbook/copilot-sdk/java/error-handling.md

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,27 +150,42 @@ try (var client = new CopilotClient()) {
150150
151151
## Handling tool errors
152152
153-
When defining tools, return an error result to signal a failure back to the model instead of throwing.
153+
When defining tools, return an error string to signal a failure back to the model instead of throwing.
154154
155155
```java
156-
import com.github.copilot.sdk.json.ToolResultObject;
157-
158-
session.addTool(
159-
ToolDefinition.builder()
160-
.name("read_file")
161-
.description("Read a file from disk")
162-
.parameter("path", "string", "File path", true)
163-
.build(),
164-
(args) -> {
156+
import com.github.copilot.sdk.json.ToolDefinition;
157+
import java.util.concurrent.CompletableFuture;
158+
159+
var readFileTool = ToolDefinition.create(
160+
"read_file",
161+
"Read a file from disk",
162+
Map.of(
163+
"type", "object",
164+
"properties", Map.of(
165+
"path", Map.of("type", "string", "description", "File path")
166+
),
167+
"required", List.of("path")
168+
),
169+
invocation -> {
165170
try {
171+
var path = (String) invocation.getArguments().get("path");
166172
var content = java.nio.file.Files.readString(
167-
java.nio.file.Path.of(args.get("path").toString()));
168-
return ToolResultObject.success(content);
169-
} catch (IOException ex) {
170-
return ToolResultObject.error("Failed to read file: " + ex.getMessage());
173+
java.nio.file.Path.of(path));
174+
return CompletableFuture.completedFuture(content);
175+
} catch (java.io.IOException ex) {
176+
return CompletableFuture.completedFuture(
177+
"Error: Failed to read file: " + ex.getMessage());
171178
}
172179
}
173180
);
181+
182+
// Register tools when creating the session
183+
var session = client.createSession(
184+
new SessionConfig()
185+
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
186+
.setModel("gpt-5")
187+
.setTools(List.of(readFileTool))
188+
).get();
174189
```
175190
176191
## Best practices
@@ -179,5 +194,5 @@ session.addTool(
179194
2. **Unwrap `ExecutionException`**: Call `getCause()` to inspect the real error — the outer `ExecutionException` is just a `CompletableFuture` wrapper.
180195
3. **Restore interrupt flag**: When catching `InterruptedException`, call `Thread.currentThread().interrupt()` to preserve the interrupted status.
181196
4. **Set timeouts**: Use `get(timeout, TimeUnit)` instead of bare `get()` for any call that could block indefinitely.
182-
5. **Return tool errors, don't throw**: Use `ToolResultObject.error()` so the model can recover gracefully.
197+
5. **Return tool errors, don't throw**: Return an error string from the `CompletableFuture` so the model can recover gracefully.
183198
6. **Log errors**: Capture error details for debugging — consider a logging framework like SLF4J for production applications.

cookbook/copilot-sdk/java/multiple-sessions.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public class MultipleSessions {
4848
session3.sendAndWait(new MessageOptions().setPrompt("How do I initialize a module?")).get();
4949
5050
// Clean up all sessions
51-
session1.destroy().get();
52-
session2.destroy().get();
53-
session3.destroy().get();
51+
session1.close();
52+
session2.close();
53+
session3.close();
5454
}
5555
}
5656
}
@@ -136,7 +136,7 @@ var session = client.createSession(new SessionConfig()
136136
137137
session.sendAndWait(new MessageOptions().setPrompt("Hello!")).get();
138138
139-
session.destroy().get();
139+
session.close();
140140
client.stop().get();
141141
executor.shutdown();
142142
```

cookbook/copilot-sdk/java/recipe/AccessibilityReport.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ public static void main(String[] args) throws Exception {
4343
client.start().get();
4444

4545
// Configure Playwright MCP server for browser automation
46-
var mcpConfig = new McpServerConfig()
47-
.setType("local")
48-
.setCommand("npx")
49-
.setArgs(List.of("@playwright/mcp@latest"))
50-
.setTools(List.of("*"));
46+
Map<String, Object> mcpConfig = Map.of(
47+
"type", "local",
48+
"command", "npx",
49+
"args", List.of("@playwright/mcp@latest"),
50+
"tools", List.of("*")
51+
);
5152

5253
var session = client.createSession(
5354
new SessionConfig()

cookbook/copilot-sdk/java/recipe/MultipleSessions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public static void main(String[] args) throws Exception {
3030
System.out.println("S3: " + s3.sendAndWait(new MessageOptions().setPrompt("Explain pattern matching")).get().getData().content());
3131

3232
// Clean up
33-
s1.destroy().get(); s2.destroy().get(); s3.destroy().get();
34-
client.stop().get();
33+
s1.close(); s2.close(); s3.close();
3534
}
3635
}
3736
}

0 commit comments

Comments
 (0)