server.mdx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. ---
  2. title: Server
  3. description: Interact with opencode server over HTTP.
  4. ---
  5. import config from "../../../config.mjs"
  6. export const typesUrl = `${config.github}/blob/dev/packages/sdk/js/src/gen/types.gen.ts`
  7. The `opencode serve` command runs a headless HTTP server that exposes an OpenAPI endpoint that an opencode client can use.
  8. ---
  9. ### Usage
  10. ```bash
  11. opencode serve [--port <number>] [--hostname <string>] [--cors <origin>]
  12. ```
  13. #### Options
  14. | Flag | Description | Default |
  15. | --------------- | ----------------------------------- | ---------------- |
  16. | `--port` | Port to listen on | `4096` |
  17. | `--hostname` | Hostname to listen on | `127.0.0.1` |
  18. | `--mdns` | Enable mDNS discovery | `false` |
  19. | `--mdns-domain` | Custom domain name for mDNS service | `opencode.local` |
  20. | `--cors` | Additional browser origins to allow | `[]` |
  21. `--cors` can be passed multiple times:
  22. ```bash
  23. opencode serve --cors http://localhost:5173 --cors https://app.example.com
  24. ```
  25. ---
  26. ### Authentication
  27. Set `OPENCODE_SERVER_PASSWORD` to protect the server with HTTP basic auth. The username defaults to `opencode`, or set `OPENCODE_SERVER_USERNAME` to override it. This applies to both `opencode serve` and `opencode web`.
  28. ```bash
  29. OPENCODE_SERVER_PASSWORD=your-password opencode serve
  30. ```
  31. ---
  32. ### How it works
  33. When you run `opencode` it starts a TUI and a server. Where the TUI is the
  34. client that talks to the server. The server exposes an OpenAPI 3.1 spec
  35. endpoint. This endpoint is also used to generate an [SDK](/docs/sdk).
  36. :::tip
  37. Use the opencode server to interact with opencode programmatically.
  38. :::
  39. This architecture lets opencode support multiple clients and allows you to interact with opencode programmatically.
  40. You can run `opencode serve` to start a standalone server. If you have the
  41. opencode TUI running, `opencode serve` will start a new server.
  42. ---
  43. #### Connect to an existing server
  44. When you start the TUI it randomly assigns a port and hostname. You can instead pass in the `--hostname` and `--port` [flags](/docs/cli). Then use this to connect to its server.
  45. The [`/tui`](#tui) endpoint can be used to drive the TUI through the server. For example, you can prefill or run a prompt. This setup is used by the OpenCode [IDE](/docs/ide) plugins.
  46. ---
  47. ## Spec
  48. The server publishes an OpenAPI 3.1 spec that can be viewed at:
  49. ```
  50. http://<hostname>:<port>/doc
  51. ```
  52. For example, `http://localhost:4096/doc`. Use the spec to generate clients or inspect request and response types. Or view it in a Swagger explorer.
  53. ---
  54. ## APIs
  55. The opencode server exposes the following APIs.
  56. ---
  57. ### Global
  58. | Method | Path | Description | Response |
  59. | ------ | ---------------- | ------------------------------ | ------------------------------------ |
  60. | `GET` | `/global/health` | Get server health and version | `{ healthy: true, version: string }` |
  61. | `GET` | `/global/event` | Get global events (SSE stream) | Event stream |
  62. ---
  63. ### Project
  64. | Method | Path | Description | Response |
  65. | ------ | ------------------ | ----------------------- | --------------------------------------------- |
  66. | `GET` | `/project` | List all projects | <a href={typesUrl}><code>Project[]</code></a> |
  67. | `GET` | `/project/current` | Get the current project | <a href={typesUrl}><code>Project</code></a> |
  68. ---
  69. ### Path & VCS
  70. | Method | Path | Description | Response |
  71. | ------ | ------- | ------------------------------------ | ------------------------------------------- |
  72. | `GET` | `/path` | Get the current path | <a href={typesUrl}><code>Path</code></a> |
  73. | `GET` | `/vcs` | Get VCS info for the current project | <a href={typesUrl}><code>VcsInfo</code></a> |
  74. ---
  75. ### Instance
  76. | Method | Path | Description | Response |
  77. | ------ | ------------------- | ---------------------------- | --------- |
  78. | `POST` | `/instance/dispose` | Dispose the current instance | `boolean` |
  79. ---
  80. ### Config
  81. | Method | Path | Description | Response |
  82. | ------- | ------------------- | --------------------------------- | ---------------------------------------------------------------------------------------- |
  83. | `GET` | `/config` | Get config info | <a href={typesUrl}><code>Config</code></a> |
  84. | `PATCH` | `/config` | Update config | <a href={typesUrl}><code>Config</code></a> |
  85. | `GET` | `/config/providers` | List providers and default models | `{ providers: `<a href={typesUrl}>Provider[]</a>`, default: { [key: string]: string } }` |
  86. ---
  87. ### Provider
  88. | Method | Path | Description | Response |
  89. | ------ | -------------------------------- | ------------------------------------ | ----------------------------------------------------------------------------------- |
  90. | `GET` | `/provider` | List all providers | `{ all: `<a href={typesUrl}>Provider[]</a>`, default: {...}, connected: string[] }` |
  91. | `GET` | `/provider/auth` | Get provider authentication methods | `{ [providerID: string]: `<a href={typesUrl}>ProviderAuthMethod[]</a>` }` |
  92. | `POST` | `/provider/{id}/oauth/authorize` | Authorize a provider using OAuth | <a href={typesUrl}><code>ProviderAuthAuthorization</code></a> |
  93. | `POST` | `/provider/{id}/oauth/callback` | Handle OAuth callback for a provider | `boolean` |
  94. ---
  95. ### Sessions
  96. | Method | Path | Description | Notes |
  97. | -------- | ---------------------------------------- | ------------------------------------- | ---------------------------------------------------------------------------------- |
  98. | `GET` | `/session` | List all sessions | Returns <a href={typesUrl}><code>Session[]</code></a> |
  99. | `POST` | `/session` | Create a new session | body: `{ parentID?, title? }`, returns <a href={typesUrl}><code>Session</code></a> |
  100. | `GET` | `/session/status` | Get session status for all sessions | Returns `{ [sessionID: string]: `<a href={typesUrl}>SessionStatus</a>` }` |
  101. | `GET` | `/session/:id` | Get session details | Returns <a href={typesUrl}><code>Session</code></a> |
  102. | `DELETE` | `/session/:id` | Delete a session and all its data | Returns `boolean` |
  103. | `PATCH` | `/session/:id` | Update session properties | body: `{ title? }`, returns <a href={typesUrl}><code>Session</code></a> |
  104. | `GET` | `/session/:id/children` | Get a session's child sessions | Returns <a href={typesUrl}><code>Session[]</code></a> |
  105. | `GET` | `/session/:id/todo` | Get the todo list for a session | Returns <a href={typesUrl}><code>Todo[]</code></a> |
  106. | `POST` | `/session/:id/init` | Analyze app and create `AGENTS.md` | body: `{ messageID, providerID, modelID }`, returns `boolean` |
  107. | `POST` | `/session/:id/fork` | Fork an existing session at a message | body: `{ messageID? }`, returns <a href={typesUrl}><code>Session</code></a> |
  108. | `POST` | `/session/:id/abort` | Abort a running session | Returns `boolean` |
  109. | `POST` | `/session/:id/share` | Share a session | Returns <a href={typesUrl}><code>Session</code></a> |
  110. | `DELETE` | `/session/:id/share` | Unshare a session | Returns <a href={typesUrl}><code>Session</code></a> |
  111. | `GET` | `/session/:id/diff` | Get the diff for this session | query: `messageID?`, returns <a href={typesUrl}><code>FileDiff[]</code></a> |
  112. | `POST` | `/session/:id/summarize` | Summarize the session | body: `{ providerID, modelID }`, returns `boolean` |
  113. | `POST` | `/session/:id/revert` | Revert a message | body: `{ messageID, partID? }`, returns `boolean` |
  114. | `POST` | `/session/:id/unrevert` | Restore all reverted messages | Returns `boolean` |
  115. | `POST` | `/session/:id/permissions/:permissionID` | Respond to a permission request | body: `{ response, remember? }`, returns `boolean` |
  116. ---
  117. ### Messages
  118. | Method | Path | Description | Notes |
  119. | ------ | --------------------------------- | --------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  120. | `GET` | `/session/:id/message` | List messages in a session | query: `limit?`, returns `{ info: `<a href={typesUrl}>Message</a>`, parts: `<a href={typesUrl}>Part[]</a>`}[]` |
  121. | `POST` | `/session/:id/message` | Send a message and wait for response | body: `{ messageID?, model?, agent?, noReply?, system?, tools?, parts }`, returns `{ info: `<a href={typesUrl}>Message</a>`, parts: `<a href={typesUrl}>Part[]</a>`}` |
  122. | `GET` | `/session/:id/message/:messageID` | Get message details | Returns `{ info: `<a href={typesUrl}>Message</a>`, parts: `<a href={typesUrl}>Part[]</a>`}` |
  123. | `POST` | `/session/:id/prompt_async` | Send a message asynchronously (no wait) | body: same as `/session/:id/message`, returns `204 No Content` |
  124. | `POST` | `/session/:id/command` | Execute a slash command | body: `{ messageID?, agent?, model?, command, arguments }`, returns `{ info: `<a href={typesUrl}>Message</a>`, parts: `<a href={typesUrl}>Part[]</a>`}` |
  125. | `POST` | `/session/:id/shell` | Run a shell command | body: `{ agent, model?, command }`, returns `{ info: `<a href={typesUrl}>Message</a>`, parts: `<a href={typesUrl}>Part[]</a>`}` |
  126. ---
  127. ### Commands
  128. | Method | Path | Description | Response |
  129. | ------ | ---------- | ----------------- | --------------------------------------------- |
  130. | `GET` | `/command` | List all commands | <a href={typesUrl}><code>Command[]</code></a> |
  131. ---
  132. ### Files
  133. | Method | Path | Description | Response |
  134. | ------ | ------------------------ | ---------------------------------- | ------------------------------------------------------------------------------------------- |
  135. | `GET` | `/find?pattern=<pat>` | Search for text in files | Array of match objects with `path`, `lines`, `line_number`, `absolute_offset`, `submatches` |
  136. | `GET` | `/find/file?query=<q>` | Find files and directories by name | `string[]` (paths) |
  137. | `GET` | `/find/symbol?query=<q>` | Find workspace symbols | <a href={typesUrl}><code>Symbol[]</code></a> |
  138. | `GET` | `/file?path=<path>` | List files and directories | <a href={typesUrl}><code>FileNode[]</code></a> |
  139. | `GET` | `/file/content?path=<p>` | Read a file | <a href={typesUrl}><code>FileContent</code></a> |
  140. | `GET` | `/file/status` | Get status for tracked files | <a href={typesUrl}><code>File[]</code></a> |
  141. #### `/find/file` query parameters
  142. - `query` (required) — search string (fuzzy match)
  143. - `type` (optional) — limit results to `"file"` or `"directory"`
  144. - `directory` (optional) — override the project root for the search
  145. - `limit` (optional) — max results (1–200)
  146. - `dirs` (optional) — legacy flag (`"false"` returns only files)
  147. ---
  148. ### Tools (Experimental)
  149. | Method | Path | Description | Response |
  150. | ------ | ------------------------------------------- | ---------------------------------------- | -------------------------------------------- |
  151. | `GET` | `/experimental/tool/ids` | List all tool IDs | <a href={typesUrl}><code>ToolIDs</code></a> |
  152. | `GET` | `/experimental/tool?provider=<p>&model=<m>` | List tools with JSON schemas for a model | <a href={typesUrl}><code>ToolList</code></a> |
  153. ---
  154. ### LSP, Formatters & MCP
  155. | Method | Path | Description | Response |
  156. | ------ | ------------ | -------------------------- | -------------------------------------------------------- |
  157. | `GET` | `/lsp` | Get LSP server status | <a href={typesUrl}><code>LSPStatus[]</code></a> |
  158. | `GET` | `/formatter` | Get formatter status | <a href={typesUrl}><code>FormatterStatus[]</code></a> |
  159. | `GET` | `/mcp` | Get MCP server status | `{ [name: string]: `<a href={typesUrl}>MCPStatus</a>` }` |
  160. | `POST` | `/mcp` | Add MCP server dynamically | body: `{ name, config }`, returns MCP status object |
  161. ---
  162. ### Agents
  163. | Method | Path | Description | Response |
  164. | ------ | -------- | ------------------------- | ------------------------------------------- |
  165. | `GET` | `/agent` | List all available agents | <a href={typesUrl}><code>Agent[]</code></a> |
  166. ---
  167. ### Logging
  168. | Method | Path | Description | Response |
  169. | ------ | ------ | ------------------------------------------------------------ | --------- |
  170. | `POST` | `/log` | Write log entry. Body: `{ service, level, message, extra? }` | `boolean` |
  171. ---
  172. ### TUI
  173. | Method | Path | Description | Response |
  174. | ------ | ----------------------- | ------------------------------------------- | ---------------------- |
  175. | `POST` | `/tui/append-prompt` | Append text to the prompt | `boolean` |
  176. | `POST` | `/tui/open-help` | Open the help dialog | `boolean` |
  177. | `POST` | `/tui/open-sessions` | Open the session selector | `boolean` |
  178. | `POST` | `/tui/open-themes` | Open the theme selector | `boolean` |
  179. | `POST` | `/tui/open-models` | Open the model selector | `boolean` |
  180. | `POST` | `/tui/submit-prompt` | Submit the current prompt | `boolean` |
  181. | `POST` | `/tui/clear-prompt` | Clear the prompt | `boolean` |
  182. | `POST` | `/tui/execute-command` | Execute a command (`{ command }`) | `boolean` |
  183. | `POST` | `/tui/show-toast` | Show toast (`{ title?, message, variant }`) | `boolean` |
  184. | `GET` | `/tui/control/next` | Wait for the next control request | Control request object |
  185. | `POST` | `/tui/control/response` | Respond to a control request (`{ body }`) | `boolean` |
  186. ---
  187. ### Auth
  188. | Method | Path | Description | Response |
  189. | ------ | ----------- | --------------------------------------------------------------- | --------- |
  190. | `PUT` | `/auth/:id` | Set authentication credentials. Body must match provider schema | `boolean` |
  191. ---
  192. ### Events
  193. | Method | Path | Description | Response |
  194. | ------ | -------- | ----------------------------------------------------------------------------- | ------------------------- |
  195. | `GET` | `/event` | Server-sent events stream. First event is `server.connected`, then bus events | Server-sent events stream |
  196. ---
  197. ### Docs
  198. | Method | Path | Description | Response |
  199. | ------ | ------ | ------------------------- | --------------------------- |
  200. | `GET` | `/doc` | OpenAPI 3.1 specification | HTML page with OpenAPI spec |