Architecture: Client-Server Model
Relevant source files
The following files were used as context for generating this wiki page:OpenCode operates on a robust client-server architecture. This design decouples the user-facing clients from the core AI agent business logic, which runs in a separate server process. This allows multiple types of clients, such as the default Terminal UI (TUI), language-specific SDKs, and third-party editor integrations (like Zed), to communicate with a single, powerful agent backend.
The communication between clients and the server is standardized by the Agent Client Protocol (ACP), ensuring interoperability and a consistent interface. This architecture enables scenarios where the OpenCode server runs on a local machine while being driven by various clients, including potentially remote ones.
Sources: README.md, packages/opencode/src/acp/README.md
High-Level Architecture
The following diagram illustrates the major components of the OpenCode client-server model.
This diagram shows various clients connecting to the central OpenCode Server via the Agent Client Protocol (ACP). The server hosts the AI agent logic, tool execution, and session management.
Sources: README.md, packages/opencode/src/acp/README.md, packages/opencode/AGENTS.md
Server
The server is the core of OpenCode, containing the primary business logic, AI agent implementation, and state management.
Sources: CONTRIBUTING.md, packages/opencode/src/acp/README.md
Server Startup
The server is launched via the command line. The primary command for starting the server in ACP mode is opencode acp.
This flow shows the server initialization process, from command execution to listening for client connections.
Sources: packages/opencode/src/acp/README.md
The acp command supports the following options:
| Option | Description |
|---|---|
--cwd /path/to/project | Starts the ACP server in a specific directory. |
Sources: packages/opencode/src/acp/README.md
Core Components
The server-side ACP implementation is cleanly separated into several key components:
| Component | File | Description |
|---|---|---|
agent.ts | packages/opencode/src/acp/agent.ts | Implements the Agent interface from the ACP SDK. It handles initialization, session management (session/new, session/load), and processing prompts. |
client.ts | packages/opencode/src/acp/client.ts | Implements the Client interface for handling capabilities requested by the agent, such as file operations and permission requests. |
session.ts | packages/opencode/src/acp/session.ts | Manages session state, mapping ACP sessions to internal OpenCode sessions and maintaining the working directory context. |
server.ts | packages/opencode/src/acp/server.ts | Handles the server startup, lifecycle, and sets up JSON-RPC communication over stdio. |
Sources: packages/opencode/src/acp/README.md
Clients
Clients are the frontends that interact with the OpenCode server. The architecture supports various client types.
Client Implementations
- Terminal UI (TUI): The default, terminal-based client for interacting with OpenCode. It is written in Go and communicates with the TypeScript server.
- SDKs (JS/Python): Programmatic clients that allow developers to integrate OpenCode into their own applications or scripts. The JavaScript SDK is automatically generated from the server's API definition to ensure consistency.
- Editor Integrations: Third-party integrations, such as for the Zed editor, can use the ACP to communicate with the OpenCode agent.
Sources: README.md, packages/opencode/AGENTS.md, CONTRIBUTING.md, packages/opencode/src/acp/README.md
Client Capabilities
The ACP defines a set of capabilities that the agent can request the client to perform. This allows the agent, running on the server, to interact with the user's environment through the client.
- File Operations:
readTextFile,writeTextFile - Permission Requests: The agent can ask for permission before executing potentially sensitive commands.
- Terminal Support: A stubbed implementation for future full terminal integration.
Sources: packages/opencode/src/acp/README.md
Agent Client Protocol (ACP)
ACP is the JSON-RPC-based protocol that standardizes communication between the agent (server) and the client. OpenCode uses the official @agentclientprotocol/sdk to ensure compliance.
Sources: packages/opencode/src/acp/README.md
Key ACP Methods
The protocol defines several methods for managing the agent's lifecycle and interactions.
| Method | Description |
|---|---|
initialize | Negotiates protocol version and exchanges capabilities between client and agent. |
session/new | Creates a new conversation session. |
session/load | Resumes an existing session (basic support). |
session/prompt | Sends a user prompt to the agent for processing. |
Sources: packages/opencode/src/acp/README.md
Interaction Flow
The following sequence diagram shows a typical interaction where a user sends a prompt through a client, and the agent processes it and responds.
This diagram illustrates how a prompt from the client leads to the server-side agent performing a series of actions, such as reading files and executing tools, before returning a final response.
Sources: packages/opencode/src/acp/README.md, packages/opencode/src/session/prompt/gemini.txt, packages/opencode/src/session/prompt/beast.txt
Conclusion
The client-server architecture is a cornerstone of OpenCode's design, providing flexibility, modularity, and a clear separation of concerns. By standardizing communication with the Agent Client Protocol (ACP), OpenCode ensures that a diverse ecosystem of clients can seamlessly interact with the powerful, centralized AI agent. This model not only supports the default TUI but also opens the door for custom SDK-based applications and third-party integrations, making the agent accessible in a wide variety of development workflows.
Sources: README.md, packages/opencode/src/acp/README.md