|
| 1 | +/* |
| 2 | + * Copyright 2025-2026 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package com.agentclientprotocol.sdk.fixtures; |
| 6 | + |
| 7 | +import java.time.Duration; |
| 8 | +import java.util.Locale; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.concurrent.ConcurrentHashMap; |
| 11 | +import java.util.concurrent.atomic.AtomicInteger; |
| 12 | + |
| 13 | +import com.agentclientprotocol.sdk.agent.AcpAgent; |
| 14 | +import com.agentclientprotocol.sdk.agent.AcpAgentFactory; |
| 15 | +import com.agentclientprotocol.sdk.agent.transport.StreamableHttpAcpAgentTransport; |
| 16 | +import com.agentclientprotocol.sdk.json.AcpJsonMapper; |
| 17 | +import com.agentclientprotocol.sdk.spec.AcpSchema; |
| 18 | +import reactor.core.publisher.Mono; |
| 19 | + |
| 20 | +/** |
| 21 | + * Small runnable ACP agent server for manually exercising the Streamable HTTP transport. |
| 22 | + * |
| 23 | + * @author Kaiser Dandangi |
| 24 | + */ |
| 25 | +public final class StreamableHttpAgentDemoServer { |
| 26 | + |
| 27 | + private static final Duration START_TIMEOUT = Duration.ofSeconds(30); |
| 28 | + |
| 29 | + private static final Duration STOP_TIMEOUT = Duration.ofSeconds(5); |
| 30 | + |
| 31 | + private StreamableHttpAgentDemoServer() { |
| 32 | + } |
| 33 | + |
| 34 | + public static void main(String[] args) { |
| 35 | + Options options; |
| 36 | + try { |
| 37 | + options = Options.parse(args); |
| 38 | + } |
| 39 | + catch (IllegalArgumentException e) { |
| 40 | + System.err.println(e.getMessage()); |
| 41 | + printUsage(); |
| 42 | + System.exit(2); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + if (options.help()) { |
| 47 | + printUsage(); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + Map<String, String> sessionCwds = new ConcurrentHashMap<>(); |
| 52 | + AtomicInteger sessionCounter = new AtomicInteger(); |
| 53 | + |
| 54 | + AcpAgentFactory agentFactory = AcpAgentFactory.async(transport -> AcpAgent.async(transport) |
| 55 | + .requestTimeout(Duration.ofMinutes(2)) |
| 56 | + .initializeHandler(request -> Mono.just(AcpSchema.InitializeResponse.ok( |
| 57 | + new AcpSchema.AgentCapabilities(true, new AcpSchema.McpCapabilities(), |
| 58 | + new AcpSchema.PromptCapabilities())))) |
| 59 | + .newSessionHandler(request -> { |
| 60 | + String sessionId = "demo-session-" + sessionCounter.incrementAndGet(); |
| 61 | + sessionCwds.put(sessionId, request.cwd()); |
| 62 | + return Mono.just(new AcpSchema.NewSessionResponse(sessionId, null, null)); |
| 63 | + }) |
| 64 | + .loadSessionHandler(request -> { |
| 65 | + sessionCwds.put(request.sessionId(), request.cwd()); |
| 66 | + return Mono.just(new AcpSchema.LoadSessionResponse(null, null)); |
| 67 | + }) |
| 68 | + .promptHandler((request, context) -> { |
| 69 | + String text = request.text(); |
| 70 | + String normalized = text == null || text.isBlank() ? "(empty prompt)" : text; |
| 71 | + String cwd = sessionCwds.getOrDefault(request.sessionId(), "(unknown cwd)"); |
| 72 | + Mono<Void> response = normalized.toLowerCase(Locale.ROOT).contains("permission") |
| 73 | + ? context.askPermission("Demo agent permission check for session " + request.sessionId()) |
| 74 | + .flatMap(allowed -> context.sendMessage( |
| 75 | + "Permission " + (allowed ? "granted" : "denied") + ". Prompt: " + normalized)) |
| 76 | + .onErrorResume(error -> context.sendMessage( |
| 77 | + "Permission request failed in demo server: " + error.getMessage())) |
| 78 | + : context.sendMessage("Demo agent received: " + normalized + " [cwd=" + cwd + "]"); |
| 79 | + return response.thenReturn(AcpSchema.PromptResponse.endTurn()); |
| 80 | + }) |
| 81 | + .cancelHandler(notification -> { |
| 82 | + System.out.println("Received cancel for session " + notification.sessionId()); |
| 83 | + return Mono.empty(); |
| 84 | + }) |
| 85 | + .build()); |
| 86 | + |
| 87 | + StreamableHttpAcpAgentTransport server = new StreamableHttpAcpAgentTransport(options.port(), options.path(), |
| 88 | + AcpJsonMapper.createDefault(), agentFactory) |
| 89 | + .routingMode(options.routingMode()); |
| 90 | + |
| 91 | + Runtime.getRuntime().addShutdownHook(new Thread(() -> server.closeGracefully().block(STOP_TIMEOUT), |
| 92 | + "acp-demo-shutdown")); |
| 93 | + |
| 94 | + server.start().block(START_TIMEOUT); |
| 95 | + System.out.println("ACP Streamable HTTP demo agent listening at http://127.0.0.1:" + server.getPort() |
| 96 | + + options.path()); |
| 97 | + System.out.println("Press Ctrl-C to stop."); |
| 98 | + server.awaitTermination().block(); |
| 99 | + } |
| 100 | + |
| 101 | + private static void printUsage() { |
| 102 | + System.out.println(""" |
| 103 | + Usage: java -jar acp-streamable-http-agent-server.jar [options] |
| 104 | +
|
| 105 | + Options: |
| 106 | + --port <port> Port to listen on. Defaults to 8080. |
| 107 | + --path <path> ACP endpoint path. Defaults to /acp. |
| 108 | + --strict Use strict transport routing. |
| 109 | + --compatible Use compatible transport routing. This is the default. |
| 110 | + -h, --help Show this help. |
| 111 | + """); |
| 112 | + } |
| 113 | + |
| 114 | + private record Options(int port, String path, StreamableHttpAcpAgentTransport.RoutingMode routingMode, |
| 115 | + boolean help) { |
| 116 | + |
| 117 | + static Options parse(String[] args) { |
| 118 | + int port = 8080; |
| 119 | + String path = StreamableHttpAcpAgentTransport.DEFAULT_ACP_PATH; |
| 120 | + StreamableHttpAcpAgentTransport.RoutingMode routingMode = |
| 121 | + StreamableHttpAcpAgentTransport.RoutingMode.COMPATIBLE; |
| 122 | + boolean help = false; |
| 123 | + |
| 124 | + for (int i = 0; i < args.length; i++) { |
| 125 | + String arg = args[i]; |
| 126 | + switch (arg) { |
| 127 | + case "--port" -> port = parsePort(requireValue(args, ++i, "--port")); |
| 128 | + case "--path" -> path = requireValue(args, ++i, "--path"); |
| 129 | + case "--strict" -> routingMode = StreamableHttpAcpAgentTransport.RoutingMode.STRICT; |
| 130 | + case "--compatible" -> routingMode = StreamableHttpAcpAgentTransport.RoutingMode.COMPATIBLE; |
| 131 | + case "-h", "--help" -> help = true; |
| 132 | + default -> throw new IllegalArgumentException("Unknown option: " + arg); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if (!path.startsWith("/")) { |
| 137 | + throw new IllegalArgumentException("--path must start with /"); |
| 138 | + } |
| 139 | + return new Options(port, path, routingMode, help); |
| 140 | + } |
| 141 | + |
| 142 | + private static String requireValue(String[] args, int index, String option) { |
| 143 | + if (index >= args.length || args[index].startsWith("--")) { |
| 144 | + throw new IllegalArgumentException(option + " requires a value"); |
| 145 | + } |
| 146 | + return args[index]; |
| 147 | + } |
| 148 | + |
| 149 | + private static int parsePort(String value) { |
| 150 | + try { |
| 151 | + int port = Integer.parseInt(value); |
| 152 | + if (port <= 0) { |
| 153 | + throw new IllegalArgumentException("--port must be positive"); |
| 154 | + } |
| 155 | + return port; |
| 156 | + } |
| 157 | + catch (NumberFormatException e) { |
| 158 | + throw new IllegalArgumentException("--port must be a number", e); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | +} |
0 commit comments