| applyTo | **/*.java, **/pom.xml |
|---|---|
| description | This file provides guidance on building Java applications using GitHub Copilot SDK for Java. |
| name | GitHub Copilot SDK Java Instructions |
- The SDK is in public preview and may have breaking changes
- Requires Java 17 or later for baseline SDK usage. Some examples use newer JDK features and therefore require JDK 21 or later (for example, virtual threads via
Executors.newVirtualThreadPerTaskExecutor()andswitchpattern matching). Java 25 or later highly recommended. - Requires GitHub Copilot CLI installed and in PATH
- Uses
CompletableFuturefor all async operations - Implements
AutoCloseablefor resource cleanup (try-with-resources) - Getters on configuration classes return
Optional<T>(orOptionalInt/OptionalDouble) to distinguish "not set" from explicit values; setters accept raw types and returnthisfor chaining. Use theclearmethods to unset values if needed.
<dependency>
<groupId>com.github</groupId>
<artifactId>copilot-sdk-java</artifactId>
<version>${copilot-sdk-java.version}</version>
</dependency>implementation "com.github:copilot-sdk-java:${copilotSdkJavaVersion}"try (var client = new CopilotClient()) {
client.start().get();
// Use client...
}Virtual threads were introduced in JDK 21, but significant performance bugs were not fixed until JDK 25, making JDK 25 the minimum recommended version for production use of virtual threads. On JDK 25+, use a virtual-thread executor for significantly better scalability. The SDK's async operations will run on virtual threads instead of the default ForkJoinPool:
var options = new CopilotClientOptions()
.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
try (var client = new CopilotClient(options)) {
client.start().get();
// Use client...
}When creating a CopilotClient, use CopilotClientOptions:
cliPath- Path to CLI executable (default: "copilot" from PATH)cliArgs- Extra arguments prepended before SDK-managed flagscliUrl- URL of existing CLI server (e.g., "localhost:8080"). When provided, client won't spawn a processport- Server port (default: 0 for random, only whenuseStdiois false)useStdio- Use stdio transport instead of TCP (default: true)logLevel- Log level: "error", "warn", "info", "debug", "trace" (default: "info")autoStart- Auto-start server on first request (default: true)autoRestart- Auto-restart on crash (default: true)cwd- Working directory for the CLI processenvironment- Environment variables for the CLI processgitHubToken- GitHub token for authenticationuseLoggedInUser- Use logged-inghCLI auth (default: true unless token provided)onListModels- Custom model list handler for BYOK scenariosremote- Enable Mission Control / cloud session integration (default: false)telemetry-TelemetryConfigfor OpenTelemetry export (since 1.2.0)sessionIdleTimeoutSeconds- Idle timeout before session auto-closes (since 1.3.0)executor- CustomExecutorfor async operations (default: ForkJoinPool)tcpConnectionToken- Security token for TCP transport authentication
var options = new CopilotClientOptions()
.setCliPath("/path/to/copilot")
.setLogLevel("debug")
.setAutoStart(true)
.setAutoRestart(true)
.setGitHubToken(System.getenv("GITHUB_TOKEN"));
try (var client = new CopilotClient(options)) {
client.start().get();
// Use client...
}For explicit control:
var client = new CopilotClient(new CopilotClientOptions().setAutoStart(false));
client.start().get();
// Use client...
client.stop().get();Use forceStop() when stop() takes too long.
Use SessionConfig for configuration. The permission handler is required:
var session = client.createSession(new SessionConfig()
.setModel("gpt-5")
.setStreaming(true)
.setTools(List.of(...))
.setSystemMessage(new SystemMessageConfig()
.setMode(SystemMessageMode.APPEND)
.setContent("Custom instructions"))
.setAvailableTools(List.of("tool1", "tool2"))
.setExcludedTools(List.of("tool3"))
.setProvider(new ProviderConfig().setType("openai"))
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();sessionId- Custom session IDclientName- Application namemodel- Model name ("gpt-5", "claude-sonnet-4.5", etc.)reasoningEffort- "low", "medium", "high", "xhigh"tools- Custom tools exposed to the CLIsystemMessage- System message customizationavailableTools- Allowlist of tool namesexcludedTools- Blocklist of tool namesprovider- Custom API provider configuration (BYOK)streaming- Enable streaming response chunks (default: false)workingDirectory- Session working directorymcpServers- MCP server configurationscustomAgents- Custom agent configurationsagent- Pre-select agent by nameinfiniteSessions- Infinite sessions configurationskillDirectories- Skill SKILL.md directoriesdisabledSkills- Skills to disableconfigDir- Config directory pathhooks- Session lifecycle hooksonPermissionRequest- REQUIRED permission handleronUserInputRequest- User input handleronEvent- Event handler registered before session creation
All setters return SessionConfig for method chaining.
var session = client.resumeSession(sessionId, new ResumeSessionConfig()
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();session.getSessionId()- Get session identifiersession.send(prompt)/session.send(MessageOptions)- Send message, returnsCompletableFuture<String>(the message ID, useful for correlation)session.sendAndWait(prompt)/session.sendAndWait(MessageOptions)- Send and wait for response (60s timeout)session.sendAndWait(options, timeoutMs)- Send and wait with custom timeoutsession.abort()- Abort current processingsession.getMessages()- Get all events/messagessession.setModel(modelId)- Switch to a different modelsession.setModel(modelId, reasoningEffort)- Switch model with reasoning effort ("low", "medium", "high", "xhigh")session.setModel(modelId, reasoningEffort, modelCapabilities)- Switch model withModelCapabilitiesOverride(since 1.3.0)session.log(message)/session.log(message, "warning", false)/session.log(message, "error", false)- Log to session timeline with level"info","warning", or"error"session.log(message, level, ephemeral, url)- Log with a clickable URL linksession.close()- Clean up resources
Use CompletableFuture for waiting on session events:
var done = new CompletableFuture<Void>();
session.on(event -> {
if (event instanceof AssistantMessageEvent msg) {
System.out.println(msg.getData().content());
} else if (event instanceof SessionIdleEvent) {
done.complete(null);
}
});
session.send(new MessageOptions().setPrompt("Hello"));
done.get();Use the typed on() overload for compile-time safety:
var done = new java.util.concurrent.CompletableFuture<Void>();
session.on(AssistantMessageEvent.class, msg -> {
System.out.println(msg.getData().content());
});
session.on(SessionIdleEvent.class, idle -> {
done.complete(null);
});The on() method returns a Closeable:
var subscription = session.on(event -> { /* handler */ });
// Later...
subscription.close();Use pattern matching (Java 17+) for event handling:
session.on(event -> {
if (event instanceof UserMessageEvent userMsg) {
// Handle user message
} else if (event instanceof AssistantMessageEvent assistantMsg) {
System.out.println(assistantMsg.getData().content());
} else if (event instanceof AssistantMessageDeltaEvent delta) {
System.out.print(delta.getData().deltaContent());
} else if (event instanceof ToolExecutionStartEvent toolStart) {
// Tool execution started
} else if (event instanceof ToolExecutionCompleteEvent toolComplete) {
// Tool execution completed
} else if (event instanceof SessionStartEvent start) {
// Session started
} else if (event instanceof SessionIdleEvent idle) {
// Session is idle (processing complete)
} else if (event instanceof SessionErrorEvent error) {
System.err.println("Error: " + error.getData().message());
}
});Control how errors in event handlers are handled:
// Set a custom error handler
session.setEventErrorHandler(ex -> {
logger.error("Event handler error", ex);
});
// Or set the error propagation policy
session.setEventErrorPolicy(EventErrorPolicy.SUPPRESS_AND_LOG_ERRORS);EventErrorPolicy values:
PROPAGATE_AND_LOG_ERRORS- Stop event dispatch on error (default)SUPPRESS_AND_LOG_ERRORS- Continue dispatch, log the error
Set streaming(true) in SessionConfig:
var session = client.createSession(new SessionConfig()
.setModel("gpt-5")
.setStreaming(true)
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();Handle both delta events (incremental) and final events:
Requires Java 21+, but 25 preferred.
var done = new CompletableFuture<Void>();
session.on(event -> {
switch (event) {
case AssistantMessageDeltaEvent delta ->
// Incremental text chunk
System.out.print(delta.getData().deltaContent());
case AssistantReasoningDeltaEvent reasoningDelta ->
// Incremental reasoning chunk (model-dependent)
System.out.print(reasoningDelta.getData().deltaContent());
case AssistantMessageEvent msg ->
// Final complete message
System.out.println("\n--- Final ---\n" + msg.getData().content());
case AssistantReasoningEvent reasoning ->
// Final reasoning content
System.out.println("--- Reasoning ---\n" + reasoning.getData().content());
case SessionIdleEvent idle ->
done.complete(null);
default -> { }
}
});
session.send(new MessageOptions().setPrompt("Tell me a story"));
done.get();Note: Final events (AssistantMessageEvent, AssistantReasoningEvent) are ALWAYS sent regardless of streaming setting.
Use ToolDefinition.create() with JSON Schema parameters and a ToolHandler:
var tool = ToolDefinition.create(
"get_weather",
"Get weather for a location",
Map.of(
"type", "object",
"properties", Map.of(
"location", Map.of("type", "string", "description", "City name")
),
"required", List.of("location")
),
invocation -> {
String location = (String) invocation.getArguments().get("location");
return CompletableFuture.completedFuture("Sunny in " + location);
}
);
var session = client.createSession(new SessionConfig()
.setModel("gpt-5")
.setTools(List.of(tool))
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();Use getArgumentsAs() for deserialization into a typed record or class:
record WeatherArgs(String location, String unit) {}
var tool = ToolDefinition.create(
"get_weather",
"Get weather for a location",
Map.of(
"type", "object",
"properties", Map.of(
"location", Map.of("type", "string"),
"unit", Map.of("type", "string", "enum", List.of("celsius", "fahrenheit"))
),
"required", List.of("location")
),
invocation -> {
var args = invocation.getArgumentsAs(WeatherArgs.class);
return CompletableFuture.completedFuture(
Map.of("temp", 72, "unit", args.unit(), "location", args.location())
);
}
);var override = ToolDefinition.createOverride(
"built_in_tool_name",
"Custom description",
Map.of("type", "object", "properties", Map.of(...)),
invocation -> CompletableFuture.completedFuture("custom result")
);Use createSkipPermission() to define a tool that bypasses the CLI's permission request flow:
var tool = ToolDefinition.createSkipPermission(
"safe_read_only_tool",
"A tool that needs no permission confirmation",
Map.of("type", "object", "properties", Map.of(...)),
invocation -> CompletableFuture.completedFuture("result")
);- Return any JSON-serializable value (String, Map, List, record, POJO)
- The SDK automatically serializes the return value and sends it back to the CLI
When Copilot invokes a tool, the client automatically:
- Deserializes the arguments
- Runs your handler function
- Serializes the return value
- Responds to the CLI
A permission handler is mandatory when creating or resuming sessions:
// Approve all requests (for development/testing)
new SessionConfig()
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
// Custom permission logic
new SessionConfig()
.setOnPermissionRequest((request, invocation) -> {
if ("dangerous-action".equals(request.getKind())) {
return CompletableFuture.completedFuture(
new PermissionRequestResult().setKind(PermissionRequestResultKind.DENIED)
);
}
return CompletableFuture.completedFuture(
new PermissionRequestResult().setKind(PermissionRequestResultKind.APPROVED)
);
})Handle user input requests from the agent:
new SessionConfig()
.setOnUserInputRequest((request, invocation) -> {
System.out.println("Agent asks: " + request.getQuestion());
String answer = scanner.nextLine();
return CompletableFuture.completedFuture(
new UserInputResponse()
.setAnswer(answer)
.setWasFreeform(true)
);
})var session = client.createSession(new SessionConfig()
.setModel("gpt-5")
.setSystemMessage(new SystemMessageConfig()
.setMode(SystemMessageMode.APPEND)
.setContent("""
<workflow_rules>
- Always check for security vulnerabilities
- Suggest performance improvements when applicable
</workflow_rules>
"""))
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();var session = client.createSession(new SessionConfig()
.setModel("gpt-5")
.setSystemMessage(new SystemMessageConfig()
.setMode(SystemMessageMode.REPLACE)
.setContent("You are a helpful assistant."))
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();Attach files to messages using Attachment:
session.send(new MessageOptions()
.setPrompt("Analyze this file")
.setAttachments(List.of(
new Attachment("file", "/path/to/file.java", "My File")
))
);Use the mode property in MessageOptions:
"enqueue"- Queue message for processing (default)"immediate"- Process message immediately
session.send(new MessageOptions()
.setPrompt("...")
.setMode("enqueue")
);Use sendAndWait() to send a message and block until the assistant responds:
// With default 60-second timeout
AssistantMessageEvent response = session.sendAndWait("What is 2+2?").get();
System.out.println(response.getData().content());
// With custom timeout
AssistantMessageEvent response = session.sendAndWait(
new MessageOptions().setPrompt("Write a long story"),
120_000 // 120 seconds
).get();Sessions are independent and can run concurrently:
var session1 = client.createSession(new SessionConfig()
.setModel("gpt-5")
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();
var session2 = client.createSession(new SessionConfig()
.setModel("claude-sonnet-4.5")
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();
session1.send(new MessageOptions().setPrompt("Hello from session 1"));
session2.send(new MessageOptions().setPrompt("Hello from session 2"));Use custom API providers via ProviderConfig:
// OpenAI
var session = client.createSession(new SessionConfig()
.setProvider(new ProviderConfig()
.setType("openai")
.setBaseUrl("https://api.openai.com/v1")
.setApiKey("sk-..."))
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();
// Azure OpenAI
var session = client.createSession(new SessionConfig()
.setProvider(new ProviderConfig()
.setType("azure")
.setAzure(new AzureOptions()
.setEndpoint("https://my-resource.openai.azure.com")
.setDeployment("gpt-4"))
.setBearerToken("..."))
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();var sessions = client.listSessions().get();
for (var metadata : sessions) {
System.out.println("Session: " + metadata.getSessionId());
}Use SessionListFilter to narrow results by working directory, git root, repository, or branch:
var filter = new SessionListFilter()
.setRepository("owner/repo")
.setBranch("main");
var sessions = client.listSessions(filter).get();client.deleteSession(sessionId).get();var state = client.getState();AutoCloseable subscription = client.onLifecycle(event -> {
System.out.println("Lifecycle event: " + event);
});
// Later...
subscription.close();Subscribe to specific lifecycle event types:
AutoCloseable subscription = client.onLifecycle("session.created", event -> {
System.out.println("New session created");
});try {
var session = client.createSession(new SessionConfig()
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();
session.sendAndWait("Hello").get();
} catch (ExecutionException ex) {
Throwable cause = ex.getCause();
System.err.println("Error: " + cause.getMessage());
} catch (Exception ex) {
System.err.println("Error: " + ex.getMessage());
}Monitor SessionErrorEvent for runtime errors:
session.on(SessionErrorEvent.class, error -> {
System.err.println("Session Error: " + error.getData().message());
});Use ping() to verify server connectivity:
var response = client.ping("test message").get();// Get CLI version and protocol info
var status = client.getStatus().get();
// Check authentication status
var authStatus = client.getAuthStatus().get();
// List available models
var models = client.listModels().get();ALWAYS use try-with-resources for automatic disposal:
try (var client = new CopilotClient()) {
client.start().get();
try (var session = client.createSession(new SessionConfig()
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get()) {
// Use session...
}
}
// Resources automatically cleaned upIf not using try-with-resources:
var client = new CopilotClient();
try {
client.start().get();
// Use client...
} finally {
client.stop().get();
}- Always use try-with-resources for
CopilotClientandCopilotSession - Always provide a permission handler - it is required for
createSessionandresumeSession - Use
CompletableFutureproperly - call.get()to block, or chain with.thenApply()/.thenCompose() - Use
sendAndWait()for simple request-response patterns instead of manual event handling - Handle
SessionErrorEventfor robust error handling - Use pattern matching (switch with sealed types) for event handling
- Enable streaming for better UX in interactive scenarios
- Close event subscriptions (
Closeable) when no longer needed - Use
SystemMessageMode.APPENDto preserve safety guardrails - Provide descriptive tool names and descriptions for better model understanding
- Handle both delta and final events when streaming is enabled
- Use
getArgumentsAs()for type-safe tool argument deserialization
try (var client = new CopilotClient()) {
client.start().get();
try (var session = client.createSession(new SessionConfig()
.setModel("gpt-5")
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get()) {
var response = session.sendAndWait("What is 2+2?").get();
System.out.println(response.getData().content());
}
}try (var client = new CopilotClient()) {
client.start().get();
try (var session = client.createSession(new SessionConfig()
.setModel("gpt-5")
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get()) {
var done = new CompletableFuture<Void>();
session.on(AssistantMessageEvent.class, msg ->
System.out.println(msg.getData().content()));
session.on(SessionIdleEvent.class, idle ->
done.complete(null));
session.send(new MessageOptions().setPrompt("What is 2+2?"));
done.get();
}
}try (var session = client.createSession(new SessionConfig()
.setModel("gpt-5")
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get()) {
var response1 = session.sendAndWait("What is the capital of France?").get();
System.out.println(response1.getData().content());
var response2 = session.sendAndWait("What is its population?").get();
System.out.println(response2.getData().content());
}record UserInfo(String id, String name, String email, String role) {}
var tool = ToolDefinition.create(
"get_user",
"Retrieve user information",
Map.of(
"type", "object",
"properties", Map.of(
"userId", Map.of("type", "string", "description", "User ID")
),
"required", List.of("userId")
),
invocation -> {
String userId = (String) invocation.getArguments().get("userId");
return CompletableFuture.completedFuture(
new UserInfo(userId, "John Doe", "john@example.com", "Developer")
);
}
);var session = client.createSession(new SessionConfig()
.setModel("gpt-5")
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setHooks(new SessionHooks()
.setOnPreToolUse((input, invocation) -> {
System.out.println("About to execute tool: " + input.getToolName());
// Use static factory methods on PreToolUseHookOutput:
// PreToolUseHookOutput.allow()
// PreToolUseHookOutput.deny()
// PreToolUseHookOutput.deny("reason")
// PreToolUseHookOutput.ask()
return CompletableFuture.completedFuture(PreToolUseHookOutput.allow());
})
.setOnPostToolUse((output, invocation) -> {
System.out.println("Tool execution complete: " + output);
return CompletableFuture.completedFuture(null);
})
.setOnUserPromptSubmitted((prompt, invocation) -> {
// Intercept user prompts before processing
return CompletableFuture.completedFuture(null);
})
.setOnSessionStart((event, invocation) -> {
return CompletableFuture.completedFuture(null);
})
.setOnSessionEnd((event, invocation) -> {
return CompletableFuture.completedFuture(null);
}))
).get();Configure Model Context Protocol servers via SessionConfig.setMcpServers():
var mcpServers = Map.of(
"my-server", new McpStdioServerConfig()
.setCommand("node")
.setArgs(List.of("path/to/server.js"))
);
var session = client.createSession(new SessionConfig()
.setModel("gpt-5")
.setMcpServers(mcpServers)
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();var mcpServers = Map.of(
"remote-server", new McpHttpServerConfig()
.setUrl("https://my-mcp-server.example.com/sse")
);
var session = client.createSession(new SessionConfig()
.setModel("gpt-5")
.setMcpServers(mcpServers)
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();Override model capabilities for BYOK or custom providers:
var capabilities = new ModelCapabilitiesOverride()
.setSupports(new ModelCapabilitiesOverride.Supports()
.setVision(true)
.setReasoningEffort(true))
.setLimits(new ModelCapabilitiesOverride.Limits()
.setMaxPromptTokens(128000));
var session = client.createSession(new SessionConfig()
.setModel("custom-model")
.setModelCapabilities(capabilities)
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
).get();