opencode serve 命令运行一个无头的 HTTP server,它暴露了一个 OpenAPI 端点,opencode 客户端可以使用该端点。
Usage
opencode serve [--port <number>] [--hostname <string>] [--cors <origin>]
Options
| Flag | Description | Default |
|---|
--port | 监听的端口 | 4096 |
--hostname | 监听的主机名 | 127.0.0.1 |
--mdns | 启用 mDNS 发现 | false |
--mdns-domain | mDNS 服务的自定义域名 | opencode.local |
--cors | 允许的额外浏览器来源 | [] |
--cors 可以重复传入:
opencode serve --cors http://localhost:5173 --cors https://app.example.com
Authentication
设置 OPENCODE_SERVER_PASSWORD,使用 HTTP basic auth 来保护 server。用户名默认为 opencode,也可以通过设置 OPENCODE_SERVER_USERNAME 来覆盖。这同时适用于 opencode serve 和 opencode web。
OPENCODE_SERVER_PASSWORD=your-password opencode serve
How it works
当你运行 opencode 时,它会启动一个 TUI 和一个 server。其中 TUI 是与 server 通信的 client。Server 暴露了一个 OpenAPI 3.1 规范端点。该端点同时也用于生成 SDK。
这种架构让 opencode 能够支持多个 client,并允许你以编程方式与 opencode 交互。
你可以运行 opencode serve 来启动一个独立的 server。如果你已经运行了 opencode TUI,那么 opencode serve 会启动一个新的 server。
Connect to an existing server
当你启动 TUI 时,它会随机分配一个端口和主机名。你也可以传入 --hostname 和 --port 参数,然后使用这些值连接到它的 server。
/tui 端点可用于通过 server 驱动 TUI。例如,你可以预填或运行一个 prompt。这种设置被 OpenCode IDE 插件所使用。
Spec
Server 发布了一个 OpenAPI 3.1 规范,可以在此查看:
http://<hostname>:<port>/doc
例如:http://localhost:4096/doc。使用该规范来生成 client 或检查请求与响应类型,也可以在 Swagger 浏览器中查看。
APIs
opencode server 暴露了以下 API。
Global
| Method | Path | Description | Response |
|---|
GET | /global/health | 获取 server 的健康状态和版本 | { healthy: true, version: string } |
GET | /global/event | 获取全局事件(SSE 流) | 事件流 |
Project
| Method | Path | Description | Response |
|---|
GET | /project | 列出所有项目 | Project[] |
GET | /project/current | 获取当前项目 | Project |
Path & VCS
| Method | Path | Description | Response |
|---|
GET | /path | 获取当前路径 | Path |
GET | /vcs | 获取当前项目的 VCS 信息 | VcsInfo |
Instance
| Method | Path | Description | Response |
|---|
POST | /instance/dispose | 释放当前实例 | boolean |
Config
| Method | Path | Description | Response |
|---|
GET | /config | 获取配置信息 | Config |
PATCH | /config | 更新配置 | Config |
GET | /config/providers | 列出 providers 和默认模型 | { providers: Provider[], default: { [key: string]: string } } |
Provider
| Method | Path | Description | Response |
|---|
GET | /provider | 列出所有 providers | { all: Provider[], default: {...}, connected: string[] } |
GET | /provider/auth | 获取 provider 的认证方式 | { [providerID: string]: ProviderAuthMethod[] } |
POST | /provider/{id}/oauth/authorize | 使用 OAuth 授权一个 provider | ProviderAuthAuthorization |
POST | /provider/{id}/oauth/callback | 处理 provider 的 OAuth 回调 | boolean |
Sessions
| Method | Path | Description | Notes |
|---|
GET | /session | 列出所有 sessions | 返回 Session[] |
POST | /session | 创建一个新的 session | body: { parentID?, title? },返回 Session |
GET | /session/status | 获取所有 sessions 的状态 | 返回 { [sessionID: string]: SessionStatus } |
GET | /session/:id | 获取 session 详情 | 返回 Session |
DELETE | /session/:id | 删除一个 session 及其所有数据 | 返回 boolean |
PATCH | /session/:id | 更新 session 属性 | body: { title? },返回 Session |
GET | /session/:id/children | 获取一个 session 的子 sessions | 返回 Session[] |
GET | /session/:id/todo | 获取 session 的 todo 列表 | 返回 Todo[] |
POST | /session/:id/init | 分析应用并创建 AGENTS.md | body: { messageID, providerID, modelID },返回 boolean |
POST | /session/:id/fork | 在某条消息处 fork 一个现有 session | body: { messageID? },返回 Session |
POST | /session/:id/abort | 中止一个正在运行的 session | 返回 boolean |
POST | /session/:id/share | 分享一个 session | 返回 Session |
DELETE | /session/:id/share | 取消分享一个 session | 返回 Session |
GET | /session/:id/diff | 获取该 session 的 diff | query: messageID?,返回 FileDiff[] |
POST | /session/:id/summarize | 摘要 session | body: { providerID, modelID },返回 boolean |
POST | /session/:id/revert | 回滚一条 message | body: { messageID, partID? },返回 boolean |
POST | /session/:id/unrevert | 恢复所有已回滚的 messages | 返回 boolean |
POST | /session/:id/permissions/:permissionID | 响应一个权限请求 | body: { response, remember? },返回 boolean |
Messages
| Method | Path | Description | Notes |
|---|
GET | /session/:id/message | 列出会话中的 messages | query: limit?,返回 { info: Message, parts: Part[] }[] |
POST | /session/:id/message | 发送一条消息并等待响应 | body: { messageID?, model?, agent?, noReply?, system?, tools?, parts },返回 { info: Message, parts: Part[] } |
GET | /session/:id/message/:messageID | 获取 message 详情 | 返回 { info: Message, parts: Part[] } |
POST | /session/:id/prompt_async | 异步发送消息(不等待) | body:与 /session/:id/message 相同,返回 204 No Content |
POST | /session/:id/command | 执行一条斜杠命令 | body: { messageID?, agent?, model?, command, arguments },返回 { info: Message, parts: Part[] } |
POST | /session/:id/shell | 运行 shell 命令 | body: { agent, model?, command },返回 { info: Message, parts: Part[] } |
Commands
| Method | Path | Description | Response |
|---|
GET | /command | 列出所有 commands | Command[] |
Files
| Method | Path | Description | Response |
|---|
GET | /find?pattern=<pat> | 在文件中搜索文本 | 包含 path、lines、line_number、absolute_offset、submatches 的匹配对象数组 |
GET | /find/file?query=<q> | 按名称查找文件和目录 | string[](路径列表) |
GET | /find/symbol?query=<q> | 查找工作区中的符号 | Symbol[] |
GET | /file?path=<path> | 列出文件和目录 | FileNode[] |
GET | /file/content?path=<p> | 读取文件 | FileContent |
GET | /file/status | 获取跟踪文件的状态 | File[] |
/find/file query parameters
query(必填)— 搜索字符串(模糊匹配)
type(可选)— 将结果限制为 "file" 或 "directory"
directory(可选)— 覆盖搜索的项目根目录
limit(可选)— 最大结果数(1–200)
dirs(可选)— 旧版标志("false" 表示仅返回文件)
| Method | Path | Description | Response |
|---|
GET | /experimental/tool/ids | 列出所有 tool IDs | ToolIDs |
GET | /experimental/tool?provider=<p>&model=<m> | 列出指定模型下的工具及其 JSON schemas | ToolList |
| Method | Path | Description | Response |
|---|
GET | /lsp | 获取 LSP server 状态 | LSPStatus[] |
GET | /formatter | 获取 formatter 状态 | FormatterStatus[] |
GET | /mcp | 获取 MCP server 状态 | { [name: string]: MCPStatus } |
POST | /mcp | 动态添加 MCP server | body: { name, config },返回 MCP 状态对象 |
Agents
| Method | Path | Description | Response |
|---|
GET | /agent | 列出所有可用的 agents | Agent[] |
Logging
| Method | Path | Description | Response |
|---|
POST | /log | 写入日志条目。Body: { service, level, message, extra? } | boolean |
TUI
| Method | Path | Description | Response |
|---|
POST | /tui/append-prompt | 追加文本到 prompt | boolean |
POST | /tui/open-help | 打开帮助对话框 | boolean |
POST | /tui/open-sessions | 打开 session 选择器 | boolean |
POST | /tui/open-themes | 打开主题选择器 | boolean |
POST | /tui/open-models | 打开模型选择器 | boolean |
POST | /tui/submit-prompt | 提交当前 prompt | boolean |
POST | /tui/clear-prompt | 清除 prompt | boolean |
POST | /tui/execute-command | 执行一条命令({ command }) | boolean |
POST | /tui/show-toast | 显示 toast({ title?, message, variant }) | boolean |
GET | /tui/control/next | 等待下一个 control 请求 | control 请求对象 |
POST | /tui/control/response | 响应一个 control 请求({ body }) | boolean |
Auth
| Method | Path | Description | Response |
|---|
PUT | /auth/:id | 设置认证凭据。Body 必须匹配 provider schema | boolean |
Events
| Method | Path | Description | Response |
|---|
GET | /event | Server-sent events 流。第一个事件是 server.connected,之后是 bus 事件 | Server-sent events 流 |
Docs
| Method | Path | Description | Response |
|---|
GET | /doc | OpenAPI 3.1 规范 | 包含 OpenAPI 规范的 HTML 页 |