Agents can request file operations and permissions from the client.
What You’ll Learn
Reading files from the client with context.readFile()
Writing files with context.writeFile()
Requesting permissions with context.client().requestPermission()
The client-side handler pattern for responding to agent requests
The Agent
The prompt handler uses SyncPromptContext convenience methods:
.promptHandler((req, context) -> { String sessionId = context.getSessionId(); // 1. Read a file (convenience method) context.sendMessage("Reading pom.xml from your system...\n"); String content = context.readFile("pom.xml", 0, 10); context.sendMessage("File content (first 10 lines):\n" + content + "\n\n"); // 2. Request permission (full API for complex permissions) ToolCallUpdate toolCall = new ToolCallUpdate( "tool-write-1", "Create summary.txt", // title: what the user sees "write_file", // name: the tool's own identifier (may be null) ToolKind.EDIT, ToolCallStatus.PENDING, null, null, null, null, null // content, locations, rawInput, rawOutput, _meta ); List<PermissionOption> options = List.of( new PermissionOption("allow", "Allow this once", PermissionOptionKind.ALLOW_ONCE), new PermissionOption("allow_always", "Always allow", PermissionOptionKind.ALLOW_ALWAYS), new PermissionOption("deny", "Deny", PermissionOptionKind.REJECT_ONCE) ); var permissionResponse = context.client().requestPermission( new RequestPermissionRequest(sessionId, toolCall, options)); context.sendMessage("Permission: " + permissionResponse.outcome() + "\n"); // 3. Write a file (convenience method) context.writeFile("summary.txt", "Created by the FileRequestingAgent.\n"); context.sendMessage("Successfully wrote summary.txt!\n"); return PromptResponse.endTurn();})
The Client
The client registers handlers to respond to agent requests:
AcpSyncClient client = AcpClient.sync(transport) .readTextFileHandler(req -> { String fileContent = Files.readString(Path.of(req.path())); return new ReadTextFileResponse(fileContent); }) .writeTextFileHandler(req -> { Files.writeString(Path.of(req.path()), req.content()); return new WriteTextFileResponse(); }) .requestPermissionHandler(req -> { // Auto-approve with first option return new RequestPermissionResponse( req.options().getFirst().id()); }) .build();
Throw exceptions from handlers for errors. The SDK converts exceptions to JSON-RPC error responses. Do not return error strings as content — agents will misinterpret them as file content.
Client Capabilities
As of 0.80.0, the client advertises file system support automatically: registering
readTextFileHandler and writeTextFileHandler, shown above, is enough for initialize() to send
fs.readTextFile and fs.writeTextFile, with no separate clientCapabilities(...) call needed.
initialize(InitializeRequest) is removed as of 0.80.0: capabilities come from the client builder,
either derived from registered handlers (as here) or set explicitly with clientCapabilities(..)
when a handler alone can’t express what to advertise. See the
0.80.0 migration guide.