opencode serve 명령은 opencode 클라이언트가 사용할 수 있는 OpenAPI 엔드포인트를 노출하는 헤드리스 HTTP 서버를 실행합니다.
사용법
opencode serve [--port <number>] [--hostname <string>] [--cors <origin>]
옵션
| Flag | Description | Default |
|---|
--port | Port to listen on | 4096 |
--hostname | Hostname to listen on | 127.0.0.1 |
--mdns | Enable mDNS discovery | false |
--mdns-domain | Custom domain name for mDNS service | opencode.local |
--cors | Additional browser origins to allow | [] |
--cors는 여러 번 전달할 수 있습니다:
opencode serve --cors http://localhost:5173 --cors https://app.example.com
인증
OPENCODE_SERVER_PASSWORD를 설정하여 HTTP basic auth로 서버를 보호하세요. 사용자 이름의 기본값은 opencode이며, OPENCODE_SERVER_USERNAME으로 재정의할 수 있습니다. 이는 opencode serve와 opencode web 모두에 적용됩니다.
OPENCODE_SERVER_PASSWORD=your-password opencode serve
작동 방식
opencode를 실행하면 TUI와 서버가 시작됩니다. TUI는 서버와 통신하는 클라이언트입니다. 서버는 OpenAPI 3.1 사양 엔드포인트를 노출합니다. 이 엔드포인트는 SDK를 생성하는 데에도 사용됩니다.
이 아키텍처를 통해 opencode는 여러 클라이언트를 지원하고 프로그래밍 방식으로 상호작용할 수 있습니다.
opencode serve를 실행하여 독립 서버를 시작할 수 있습니다. opencode TUI가 실행 중인 경우, opencode serve는 새 서버를 시작합니다.
기존 서버에 연결
TUI를 시작하면 포트와 호스트명이 무작위로 할당됩니다. 대신 --hostname과 --port 플래그를 전달할 수 있습니다. 그런 다음 이를 사용하여 서버에 연결합니다.
/tui 엔드포인트는 서버를 통해 TUI를 구동하는 데 사용할 수 있습니다. 예를 들어 프롬프트를 미리 채우거나 실행할 수 있습니다. 이 설정은 OpenCode IDE 플러그인에서 사용됩니다.
사양
서버는 다음에서 볼 수 있는 OpenAPI 3.1 사양을 게시합니다:
http://<hostname>:<port>/doc
예를 들어, http://localhost:4096/doc. 사양을 사용하여 클라이언트를 생성하거나 요청 및 응답 타입을 확인하세요. 또는 Swagger 탐색기에서 확인하세요.
APIs
opencode 서버는 다음 API를 노출합니다.
Global
| Method | Path | Description | Response |
|---|
GET | /global/health | Get server health and version | { healthy: true, version: string } |
GET | /global/event | Get global events (SSE stream) | Event stream |
Project
| Method | Path | Description | Response |
|---|
GET | /project | List all projects | Project[] |
GET | /project/current | Get the current project | Project |
Path & VCS
| Method | Path | Description | Response |
|---|
GET | /path | Get the current path | Path |
GET | /vcs | Get VCS info for the current project | VcsInfo |
Instance
| Method | Path | Description | Response |
|---|
POST | /instance/dispose | Dispose the current instance | boolean |
Config
| Method | Path | Description | Response |
|---|
GET | /config | Get config info | Config |
PATCH | /config | Update config | Config |
GET | /config/providers | List providers and default models | { providers: Provider[], default: { [key: string]: string } } |
Provider
| Method | Path | Description | Response |
|---|
GET | /provider | List all providers | { all: Provider[], default: {...}, connected: string[] } |
GET | /provider/auth | Get provider authentication methods | { [providerID: string]: ProviderAuthMethod[] } |
POST | /provider/{id}/oauth/authorize | Authorize a provider using OAuth | ProviderAuthAuthorization |
POST | /provider/{id}/oauth/callback | Handle OAuth callback for a provider | boolean |
Sessions
| Method | Path | Description | Notes |
|---|
GET | /session | List all sessions | Returns Session[] |
POST | /session | Create a new session | body: { parentID?, title? }, returns Session |
GET | /session/status | Get session status for all sessions | Returns { [sessionID: string]: SessionStatus } |
GET | /session/:id | Get session details | Returns Session |
DELETE | /session/:id | Delete a session and all its data | Returns boolean |
PATCH | /session/:id | Update session properties | body: { title? }, returns Session |
GET | /session/:id/children | Get a session’s child sessions | Returns Session[] |
GET | /session/:id/todo | Get the todo list for a session | Returns Todo[] |
POST | /session/:id/init | Analyze app and create AGENTS.md | body: { messageID, providerID, modelID }, returns boolean |
POST | /session/:id/fork | Fork an existing session at a message | body: { messageID? }, returns Session |
POST | /session/:id/abort | Abort a running session | Returns boolean |
POST | /session/:id/share | Share a session | Returns Session |
DELETE | /session/:id/share | Unshare a session | Returns Session |
GET | /session/:id/diff | Get the diff for this session | query: messageID?, returns FileDiff[] |
POST | /session/:id/summarize | Summarize the session | body: { providerID, modelID }, returns boolean |
POST | /session/:id/revert | Revert a message | body: { messageID, partID? }, returns boolean |
POST | /session/:id/unrevert | Restore all reverted messages | Returns boolean |
POST | /session/:id/permissions/:permissionID | Respond to a permission request | body: { response, remember? }, returns boolean |
Messages
| Method | Path | Description | Notes |
|---|
GET | /session/:id/message | List messages in a session | query: limit?, returns { info: Message, parts: Part[] }[] |
POST | /session/:id/message | Send a message and wait for response | body: { messageID?, model?, agent?, noReply?, system?, tools?, parts }, returns { info: Message, parts: Part[] } |
GET | /session/:id/message/:messageID | Get message details | Returns { info: Message, parts: Part[] } |
POST | /session/:id/prompt_async | Send a message asynchronously (no wait) | body: same as /session/:id/message, returns 204 No Content |
POST | /session/:id/command | Execute a slash command | body: { messageID?, agent?, model?, command, arguments }, returns { info: Message, parts: Part[] } |
POST | /session/:id/shell | Run a shell command | body: { agent, model?, command }, returns { info: Message, parts: Part[] } |
Commands
| Method | Path | Description | Response |
|---|
GET | /command | List all commands | Command[] |
Files
| Method | Path | Description | Response |
|---|
GET | /find?pattern=<pat> | Search for text in files | Array of match objects with path, lines, line_number, absolute_offset, submatches |
GET | /find/file?query=<q> | Find files and directories by name | string[] (paths) |
GET | /find/symbol?query=<q> | Find workspace symbols | Symbol[] |
GET | /file?path=<path> | List files and directories | FileNode[] |
GET | /file/content?path=<p> | Read a file | FileContent |
GET | /file/status | Get status for tracked files | File[] |
/find/file 쿼리 매개변수
query (필수) — 검색 문자열 (퍼지 매치)
type (선택) — 결과를 "file" 또는 "directory"로 제한
directory (선택) — 검색의 프로젝트 루트 재정의
limit (선택) — 최대 결과 수 (1–200)
dirs (선택) — 레거시 플래그 ("false"는 파일만 반환)
| Method | Path | Description | Response |
|---|
GET | /experimental/tool/ids | List all tool IDs | ToolIDs |
GET | /experimental/tool?provider=<p>&model=<m> | List tools with JSON schemas for a model | ToolList |
| Method | Path | Description | Response |
|---|
GET | /lsp | Get LSP server status | LSPStatus[] |
GET | /formatter | Get formatter status | FormatterStatus[] |
GET | /mcp | Get MCP server status | { [name: string]: MCPStatus } |
POST | /mcp | Add MCP server dynamically | body: { name, config }, returns MCP status object |
Agents
| Method | Path | Description | Response |
|---|
GET | /agent | List all available agents | Agent[] |
Logging
| Method | Path | Description | Response |
|---|
POST | /log | Write log entry. Body: { service, level, message, extra? } | boolean |
TUI
| Method | Path | Description | Response |
|---|
POST | /tui/append-prompt | Append text to the prompt | boolean |
POST | /tui/open-help | Open the help dialog | boolean |
POST | /tui/open-sessions | Open the session selector | boolean |
POST | /tui/open-themes | Open the theme selector | boolean |
POST | /tui/open-models | Open the model selector | boolean |
POST | /tui/submit-prompt | Submit the current prompt | boolean |
POST | /tui/clear-prompt | Clear the prompt | boolean |
POST | /tui/execute-command | Execute a command ({ command }) | boolean |
POST | /tui/show-toast | Show toast ({ title?, message, variant }) | boolean |
GET | /tui/control/next | Wait for the next control request | Control request object |
POST | /tui/control/response | Respond to a control request ({ body }) | boolean |
Auth
| Method | Path | Description | Response |
|---|
PUT | /auth/:id | Set authentication credentials. Body must match provider schema | boolean |
Events
| Method | Path | Description | Response |
|---|
GET | /event | Server-sent events stream. First event is server.connected, then bus events | Server-sent events stream |
Docs
| Method | Path | Description | Response |
|---|
GET | /doc | OpenAPI 3.1 specification | HTML page with OpenAPI spec |