Msg response = agent.call(userMsg).block();
// Check for pending tool calls
while (response.hasContentBlocks(ToolUseBlock.class)) {
// Display pending tools
List<ToolUseBlock> pending = response.getContentBlocks(ToolUseBlock.class);
for (ToolUseBlock tool : pending) {
System.out.println("Tool: " + tool.getName());
System.out.println("Input: " + tool.getInput());
}
if (userConfirms()) {
// User confirmed, continue execution
response = agent.call().block();
} else {
// User declined, return cancellation
Msg cancelResult = Msg.builder()
.role(MsgRole.TOOL)
.content(pending.stream()
.map(t -> ToolResultBlock.of(t.getId(), t.getName(),
TextBlock.builder().text("Operation cancelled").build()))
.toArray(ToolResultBlock[]::new))
.build();
response = agent.call(cancelResult).block();
}
}
// Final response
System.out.println(response.getTextContent());