How github/copilot-sdk-java works
1. What this repository is
- This is a Java 17+ SDK for controlling the GitHub Copilot CLI programmatically.
- The runtime boundary is not a hosted web service; the SDK is a local client library that launches or connects to the Copilot CLI process and communicates with it over JSON-RPC.
2. Primary runtime layers
- The top-level handwritten API lives in src/main/java/com/github/copilot.
- The lower-level data model and transport-facing types live in src/main/java/com/github/copilot/rpc.
- A large generated surface in src/generated/java/com/github/copilot/generated and src/generated/java/com/github/copilot/generated/rpc mirrors the CLI protocol, events, requests, and responses.
3. Client/session boundary
- CopilotClient is the main entry point for applications.
- It manages process/session startup via CliServerManager, connection state, and lifecycle coordination.
- CopilotSession is the primary unit of work once connected: it represents an active Copilot conversation/session and exposes send, resume, event subscription, and session-scoped operations.
4. Transport and protocol handling
- JsonRpcClient, JsonRpcRequest, JsonRpcResponse, and JsonRpcException implement the messaging layer over the CLI boundary.
- RpcHandlerDispatcher routes incoming RPC callbacks/events to the appropriate handlers.
- SessionRequestBuilder assembles request payloads for session operations.
- This means the architecture separates transport concerns from Copilot semantics rather than exposing raw CLI I/O to consumers.
5. Event-driven core
- The SDK is strongly event-driven: session activity is surfaced through generated event classes such as assistant messages, tool execution events, usage updates, warnings, and shutdown notifications.
- LifecycleEventManager and related classes coordinate session lifecycle events and dispatch them to consumers.
- Tests like SessionEventHandlingTest, StreamingFidelityTest, and EventFidelityTest suggest event ordering and payload accuracy are important architectural guarantees.
6. Domain model and configuration
- The handwritten com.github.copilot.rpc package contains the SDK’s public domain objects and configuration types: session config, message options, model info, permissions, telemetry, attachments, agents, tools, hooks, and remote-session options.
- These types are the stable API boundary for Java users, while the generated models track the underlying Copilot protocol more directly.
- The distinction between handwritten API objects and generated wire types is a major architectural seam.
7. Hooks, permissions, and user interaction
- The SDK supports several callback boundaries where application code can intervene: permission requests, user input requests, tool execution hooks, pre/post tool use hooks, MCP tool call hooks, elicitation, exit-plan mode, session start/end hooks, and user-prompt submission hooks.
- These callbacks are modeled as explicit handlers and invocation/result objects in com.github.copilot.rpc.
- Architecturally, this is where the SDK becomes an orchestration layer for agentic workflows rather than a simple request/response client.
8. Tools, MCP, skills, and agents
- The SDK exposes higher-level integration points for tools, built-in tools, custom agents, skills, and Model Context Protocol (MCP) servers.
- Generated RPC APIs cover discovery, enable/disable, execute, reload, list, and status operations for these subsystems.
- This indicates the Copilot CLI is treated as a local agent runtime that can be extended through plugin/tool ecosystems, with the SDK acting as the control plane.
9. Sessions as managed state
- Session-related APIs cover creation, resume, fork, close, list, metadata, compaction, history truncation, workspace file operations, and foreground/background handling.
- The SDK therefore manages more than chat messages: it also models persistent session state, workspace context, and session scheduling/queueing.
- Several response types show the SDK can interrogate and mutate session metadata and execution state directly.
10. Generated protocol surface
- The generated classes are extensive and intentionally verbose because they mirror the underlying CLI schema and RPC contract.
- They cover event types, enums, request/response records, and per-area API namespaces such as sessions, tools, MCP, permissions, workspace, models, auth, telemetry, and usage.
- The architecture uses generation to keep the Java client aligned with the reference implementation and reduce hand-maintained protocol drift.
11. Build, codegen, and release pipeline
- pom.xml plus the Maven Wrapper define the Java build.
- scripts/codegen uses TypeScript/Node tooling to generate protocol bindings, showing that code generation is a first-class part of the repo.
- GitHub Actions handles build/test, site deployment, Maven publishing, snapshot publishing, release notes/changelog updates, and syncing with the reference implementation.
- The CI/CD surface is tightly coupled to protocol synchronization and documentation publication.
12. Documentation and site generation
- src/site contains the Maven site content and documentation examples, including getting started, advanced usage, hooks, MCP, and cookbooks.
- docs/WORKFLOWS.md and ADRs document architectural decisions and release/sync mechanics.
- This repo ships both a library and a documentation site, so diagramming should include the docs build as a separate output from the runtime SDK.
13. Testing strategy
- Tests are broad and layered: unit tests for wrappers and models, fidelity tests for event and stream behavior, end-to-end tests against the official Copilot test harness, and coverage tests for generated code.
- The test suite validates not only business logic but also protocol compatibility, JSON serialization/deserialization, and module/package boundaries.
- That suggests the main engineering risk here is keeping the Java façade synchronized with a fast-moving CLI protocol.
14. Key architectural takeaway
- The repo is best understood as a three-part system: a public Java SDK surface, a generated protocol mirror, and a JSON-RPC bridge to the local Copilot CLI.
- Around that core are extensibility boundaries for hooks, permissions, tools, MCP, and agent/session lifecycle management, plus a CI/codegen pipeline that keeps the SDK aligned with the upstream reference implementation.
Main entry points: package-info.java RPC domain · SessionEvent.java Generated events · CopilotSession.java CopilotSession · CopilotClient.java CopilotClient
Components 19
Runtime SDK
Handwritten client, session, transport, and public domain API.
- CopilotClient · Main public entry point for applications using the SDK.
src/main/java/com/github/copilot/CopilotClient.java - CopilotSession · Active session facade for send, resume, events, and session-scoped operations.
src/main/java/com/github/copilot/CopilotSession.java - CLI server · Launches and coordinates the local Copilot CLI process/session boundary.
src/main/java/com/github/copilot/CliServerManager.java - JSON-RPC · Messaging layer over the local CLI boundary.
src/main/java/com/github/copilot/JsonRpcClient.java - Dispatcher · Routes inbound RPC callbacks and events to the right handlers.
src/main/java/com/github/copilot/RpcHandlerDispatcher.java - Lifecycle · Coordinates session lifecycle events and consumer dispatch.
src/main/java/com/github/copilot/LifecycleEventManager.java - Request builder · Assembles session request payloads and operation inputs.
src/main/java/com/github/copilot/SessionRequestBuilder.java - RPC domain · Handwritten stable API types for sessions, models, permissions, hooks, tools, MCP, telemetry, and user interaction.
src/main/java/com/github/copilot/rpc/package-info.java - Session config · Session, client, model, telemetry, permission, and workspace configuration objects.
src/main/java/com/github/copilot/rpc/SessionConfig.java - Hooks · Permission, user input, tool, MCP, elicitation, and lifecycle callback contracts.
src/main/java/com/github/copilot/rpc/SessionHooks.java - Tools & agents · Tool sets, agents, skills, MCP server configs, and related invocation/result types.
src/main/java/com/github/copilot/rpc/ToolSet.java
Generated Protocol
Protocol mirror and RPC namespaces generated from the Copilot CLI schema.
- Generated events · Generated session/event classes that mirror the CLI protocol stream.
src/generated/java/com/github/copilot/generated/SessionEvent.java - Generated RPC · Generated request/response records and per-area RPC namespaces.
src/generated/java/com/github/copilot/generated/rpc/SessionRpc.java
Delivery
Build, code generation, documentation, and CI/release pipeline.
- Codegen · TypeScript/Node generation pipeline for protocol bindings.
scripts/codegen/java.ts - Maven build · Java build, wrapper, packaging, and site publication entrypoints.
pom.xml - CI/CD · GitHub Actions for build/test, sync, publish, release notes, and site deployment.
.github/workflows/build-test.yml - Docs site · Maven site content, examples, and architecture/release docs.
src/site/markdown/index.md
Tests
Architecture- and protocol-fidelity test layers.
- Test suite · Layered tests for wrappers, fidelity, compatibility, and end-to-end behavior.
src/test/java/com/github/copilot/CopilotClientTest.java - Fidelity tests · Event streaming and serialization fidelity checks against the CLI protocol.
src/test/java/com/github/copilot/StreamingFidelityTest.java
Connections 24
- CopilotClient → CLI server: starts/connects · Client coordinates local CLI process startup and connection lifecycle.
- CopilotClient → JSON-RPC: uses transport · Client delegates protocol messaging to JSON-RPC.
- CopilotClient → CopilotSession: creates sessions · Client hands back the primary session unit of work.
- CopilotSession → Request builder: builds requests · Session-scoped operations are assembled into protocol payloads.
- CopilotSession → Dispatcher: handles callbacks · Incoming events and callbacks are routed to handlers.
- CopilotSession → Lifecycle: emits lifecycle · Session lifecycle changes are coordinated and dispatched.
- JSON-RPC → Generated RPC: speaks protocol · Transport operates on generated wire records and APIs.
- Dispatcher → Generated events: dispatches events · Dispatcher maps protocol event types to consumers.
- Lifecycle → Generated events: publishes events · Lifecycle state changes surface as generated session events.
- Request builder → RPC domain: uses domain types · Request assembly depends on handwritten API objects.
- RPC domain → Session config: includes config · The public domain layer carries stable configuration objects.
- RPC domain → Hooks: defines callbacks · Permission, user input, tool, and lifecycle hooks live here.
- RPC domain → Tools & agents: defines extensions · Tools, agents, skills, and MCP integration boundaries are modeled here.
- Generated RPC → Generated events: mirrors schema · Generated RPC surface aligns with generated protocol event/model types.
- Codegen → Generated RPC: produces · Generator emits the RPC protocol mirror.
- Codegen → Generated events: produces · Generator emits event and enum surfaces from upstream schema.
- Maven build → Codegen: invokes · Build coordinates generation as part of the repo workflow.
- CI/CD → Maven build: runs · CI validates, packages, publishes, and deploys the library/site.
- CI/CD → Codegen: syncs · Automation keeps generated bindings and upstream protocol in step.
- Docs site → RPC domain: documents · Documentation explains public SDK usage and extension points.
- Test suite → CopilotClient: verifies · Tests cover the public entry point and session lifecycle.
- Test suite → RPC domain: verifies · Tests cover handwritten wrappers and models.
- Fidelity tests → Generated events: checks · Event fidelity tests validate generated event shapes and ordering.
- Fidelity tests → Generated RPC: checks · Protocol compatibility tests validate generated RPC contracts.