Feature: Language Server Protocol (LSP) Integration
Relevant source files
The following files were used as context for generating this wiki page:The Language Server Protocol (LSP) integration provides OpenCode with IDE-like features, such as code diagnostics, hover information, and more, directly within the terminal. It achieves this by dynamically managing and communicating with various language-specific servers based on the files being processed. This system is designed to be extensible, automatically detecting project configurations and even installing required language servers on-demand.
The core of the integration is a manager that orchestrates language server lifecycles, a client for handling JSON-RPC communication, and a set of tool definitions that expose LSP functionalities to the AI agent. This allows the agent to leverage deep linguistic understanding of the code it is working with, leading to more accurate and context-aware operations.
Core Architecture
The LSP system is composed of three main parts: the LSP Manager, the LSP Server Definitions, and the LSP Client. The Manager is the central controller that decides which language server to start. It uses the Server Definitions to find the correct configuration and spawning logic. Once a server is running, the LSP Client handles all communication with it. This functionality is then exposed to the agent through specific tools like LspDiagnostics and LspHover.
The following diagram illustrates the high-level architecture of the LSP integration.
LSP Manager
The LSP Manager, located in packages/opencode/src/lsp/index.ts, is responsible for the lifecycle of language server processes. Its primary function, LSP.start, identifies the appropriate language server for a given file, determines the project root, and spawns the server process if it's not already running for that root.
It maintains a cache of active server clients (clients) keyed by their project root directory to avoid spawning duplicate processes. When a server is no longer needed, the LSP.stop function can be called to terminate the process and clean up resources.
Sources: packages/opencode/src/lsp/index.ts:13-14, packages/opencode/src/lsp/index.ts:20-56
Server Startup Flow
The LSP.start function follows a specific flow to initialize a language server for a file.
LSP Server Definitions
All supported language servers are defined in packages/opencode/src/lsp/server.ts. Each language server is represented by an Info object that contains metadata and logic required for the LSP Manager to handle it.
Sources: packages/opencode/src/lsp/server.ts
LSPServer.Info Interface
The Info interface is the blueprint for defining a language server.
// packages/opencode/src/lsp/server.ts:29-35
export interface Info {
id: string;
extensions: string[];
global?: boolean;
root: RootFunction;
spawn(root: string): Promise<Handle | undefined>;
}
id: A unique string identifier for the server (e.g., "typescript").extensions: An array of file extensions this server is responsible for (e.g.,[".ts", ".tsx"]).global: An optional boolean indicating if the server runs globally.root: A function that determines the project root directory for a given file.spawn: An async function that spawns the language server process and returns aHandlecontaining the process and any initial configuration.
Sources: packages/opencode/src/lsp/server.ts:29-35
The diagram below shows the structure of the Info interface and its relationship with the Handle.
Root Directory Detection
The NearestRoot utility function is a key component for finding the appropriate workspace root for a language server. It searches upwards from a file's directory for specific marker files (e.g., package.json, go.mod) to identify the project boundary. This ensures that the language server is initialized with the correct project context.
Sources: packages/opencode/src/lsp/server.ts:17-27
Automatic Installation
A notable feature is the on-demand installation of language servers. If a server's binary is not found, the spawn function for many servers will attempt to download or install it automatically. For example, gopls, csharp-ls, and eslint servers are installed if they are missing. This process is controlled by the OPENCODE_DISABLE_LSP_DOWNLOAD flag.
Sources: packages/opencode/src/lsp/server.ts:251-269, packages/opencode/src/lsp/server.ts:511-529, packages/opencode/src/lsp/server.ts:145-175
Supported Language Servers
The system supports a wide range of languages out of the box. The table below summarizes the configurations for some of the defined servers.
| ID | Extensions | Root Detection Files |
|---|---|---|
deno | .ts, .tsx, .js, .jsx, .mjs | deno.json, deno.jsonc |
typescript | .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts | package-lock.json, bun.lockb, yarn.lock, etc. |
vue | .vue | package-lock.json, bun.lockb, yarn.lock, etc. |
eslint | .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts, .vue | package-lock.json, bun.lockb, yarn.lock, etc. |
gopls | .go | go.work, go.mod, go.sum |
ruby-lsp | .rb, .rake, .gemspec, .ru | Gemfile |
pyright | .py, .pyi | pyproject.toml, setup.py, requirements.txt, etc. |
elixir-ls | .ex, .exs | mix.exs, mix.lock |
zls | .zig, .zon | build.zig |
csharp | .cs | .sln, .csproj, global.json |
sourcekit-lsp | .swift, .objc, objcpp | Package.swift, *.xcodeproj, *.xcworkspace |
rust | .rs | Cargo.toml, Cargo.lock (finds workspace root) |
clangd | .c, .cpp, .h, .hpp, etc. | compile_commands.json, CMakeLists.txt, Makefile |
svelte | .svelte | package-lock.json, bun.lockb, yarn.lock, etc. |
astro | .astro | package-lock.json, bun-lockb, yarn.lock, etc. |
jdtls | .java | pom.xml, build.gradle, .project |
yaml-ls | .yaml, .yml | package-lock.json, bun.lockb, yarn.lock, etc. |
lua-ls | .lua | .luarc.json, .luacheckrc, stylua.toml, etc. |
php intelephense | .php | composer.json, composer.lock, .php-version |
dart | .dart | pubspec.yaml, analysis_options.yaml |
Sources: packages/opencode/src/lsp/server.ts
LSP Client
The LSPClient class in packages/opencode/src/lsp/client.ts is the communication bridge to a spawned language server process. It handles the JSON-RPC 2.0 protocol, sending requests and notifications, and processing responses. It manages message IDs, pending requests, and event listeners for server notifications.
Key methods include:
initialize(): Sends theinitializerequest to the server and handles the response, marking the client as ready.request(): Sends a generic LSP request and returns a promise that resolves with the response.notify(): Sends an LSP notification without expecting a response.didOpen(): A specific notification to inform the server that a file has been opened.
Sources: packages/opencode/src/lsp/client.ts
Client-Server Communication
When a new client is created, it undergoes an initialization handshake with the language server. The following sequence diagram shows this process, including opening a file and requesting hover information.
LSP Tools
The LSP integration is exposed to the OpenCode agent via "tools". The files lsp-diagnostics.ts and lsp-hover.ts define tools that leverage the LSP client to provide contextual information about the code.
Sources: packages/opencode/src/tool/lsp-diagnostics.ts, packages/opencode/src/tool/lsp-hover.ts
LspDiagnostics
This tool retrieves diagnostic information (errors, warnings) for a specific file. It uses LSP.start to get a client, waits for diagnostics to be published by the server (via the textDocument/publishDiagnostics notification), and then formats them for the agent.
The tool's run function shows the flow of getting a client, waiting for the relevant event, and returning the data.
LspHover
This tool fetches hover information for a specific position in a file. It starts an LSP client, opens the document, and sends a textDocument/hover request. The response, containing documentation or type information, is then returned to the agent.
// packages/opencode/src/tool/lsp-hover.ts:7-12
export const LspHover = Tool.create({
id: "lsp_hover",
name: "LSP Hover",
description: "Get hover information for a symbol in a file.",
input: LspHoverInput,
output: LspHoverOutput,
// ...
The run function orchestrates getting an LSP client, sending the didOpen notification, and then making the textDocument/hover request.
Sources: packages/opencode/src/tool/lsp-hover.ts:20-43
Conclusion
The LSP integration is a cornerstone feature of OpenCode, transforming it from a simple code executor into a powerful, context-aware coding assistant. By abstracting the complexity of managing different language servers and providing a simple tool-based interface, it allows the AI agent to perform sophisticated analysis and receive real-time feedback on the code it interacts with. The system's modular design and automatic installation capabilities make it both robust and easy to extend with support for new languages in the future.