server.mdx 17 KB

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