mcp-servers.mdx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. ---
  2. title: "MCP servers"
  3. description: ""
  4. ---
  5. OpenCode can connect to [Model Context Protocol](https://modelcontextprotocol.io/) servers and make their tools, prompts, and instructions available to agents. MCP tools consume model context, so enable only the servers you need.
  6. ## Configure servers
  7. Define each server by a unique name under `mcp.servers` in your [OpenCode configuration](/config). V2 does not place server names directly under `mcp`.
  8. ```jsonc title="opencode.jsonc"
  9. {
  10. "$schema": "https://opencode.ai/config.json",
  11. "mcp": {
  12. "servers": {
  13. "my-server": {
  14. "type": "local",
  15. "command": ["npx", "-y", "example-mcp-server"]
  16. }
  17. }
  18. }
  19. }
  20. ```
  21. Servers connect automatically unless `disabled` is `true`. There is no V2 `enabled` field.
  22. ```jsonc
  23. {
  24. "mcp": {
  25. "servers": {
  26. "my-server": {
  27. "type": "local",
  28. "command": ["npx", "-y", "example-mcp-server"],
  29. "disabled": true
  30. }
  31. }
  32. }
  33. }
  34. ```
  35. As with other configuration, a server in a higher-precedence project config replaces a server with the same name from a lower-precedence config. Use different names when you need separate connections or accounts.
  36. ## Local servers
  37. A local server is a command that OpenCode starts using the MCP stdio transport.
  38. ```jsonc title="opencode.jsonc"
  39. {
  40. "$schema": "https://opencode.ai/config.json",
  41. "mcp": {
  42. "servers": {
  43. "everything": {
  44. "type": "local",
  45. "command": [
  46. "npx",
  47. "-y",
  48. "@modelcontextprotocol/server-everything"
  49. ],
  50. "cwd": ".",
  51. "environment": {
  52. "LOG_LEVEL": "info",
  53. "MCP_API_KEY": "{env:MCP_API_KEY}"
  54. }
  55. }
  56. }
  57. }
  58. }
  59. ```
  60. | Field | Required | Description |
  61. | --- | --- | --- |
  62. | `type` | Yes | Must be `"local"`. |
  63. | `command` | Yes | Executable followed by its arguments. |
  64. | `cwd` | No | Process working directory. Relative paths resolve from the workspace directory; the workspace is the default. |
  65. | `environment` | No | String environment variables added to the inherited OpenCode process environment. |
  66. | `disabled` | No | Set to `true` to prevent the server from connecting. Defaults to `false`. |
  67. | `codemode` | No | Set to `false` to expose the server's tools directly to the model instead of through Code Mode. Defaults to `true`. |
  68. | `timeout` | No | Per-server timeout overrides. |
  69. Use `{env:NAME}` to substitute an environment variable while loading config. Shell expressions such as `$NAME` are not expanded in JSON strings.
  70. ## Remote servers
  71. A remote server uses the MCP Streamable HTTP transport. Its `url` must be a valid absolute URL.
  72. ```jsonc title="opencode.jsonc"
  73. {
  74. "$schema": "https://opencode.ai/config.json",
  75. "mcp": {
  76. "servers": {
  77. "context7": {
  78. "type": "remote",
  79. "url": "https://mcp.context7.com/mcp",
  80. "oauth": false,
  81. "headers": {
  82. "CONTEXT7_API_KEY": "{env:CONTEXT7_API_KEY}"
  83. }
  84. }
  85. }
  86. }
  87. }
  88. ```
  89. | Field | Required | Description |
  90. | --- | --- | --- |
  91. | `type` | Yes | Must be `"remote"`. |
  92. | `url` | Yes | Streamable HTTP endpoint. |
  93. | `headers` | No | String HTTP headers sent to the MCP endpoint. |
  94. | `oauth` | No | OAuth client settings, or `false` to disable OAuth support. |
  95. | `disabled` | No | Set to `true` to prevent the server from connecting. Defaults to `false`. |
  96. | `codemode` | No | Set to `false` to expose the server's tools directly to the model instead of through Code Mode. Defaults to `true`. |
  97. | `timeout` | No | Per-server timeout overrides. |
  98. Use `oauth: false` for a server that exclusively uses an API key or another header-based credential.
  99. ## OAuth
  100. OAuth support is enabled for remote servers unless `oauth` is `false`. OpenCode discovers the authorization server, uses PKCE, refreshes tokens, and attempts dynamic client registration when the server supports it. OAuth credentials are stored outside project configuration.
  101. For a server that supports dynamic client registration, only the remote server is required:
  102. ```jsonc title="opencode.jsonc"
  103. {
  104. "$schema": "https://opencode.ai/config.json",
  105. "mcp": {
  106. "servers": {
  107. "sentry": {
  108. "type": "remote",
  109. "url": "https://mcp.sentry.dev/mcp"
  110. }
  111. }
  112. }
  113. }
  114. ```
  115. When the server reports that it needs authentication, run `/connect` in the TUI:
  116. ```text
  117. /connect
  118. ```
  119. Select the MCP server under **Services**, then complete the browser authorization flow.
  120. You can also authenticate from the command line:
  121. ```bash
  122. opencode2 mcp auth sentry
  123. ```
  124. The CLI command prints the authorization URL and waits for the redirect to OpenCode's loopback callback server.
  125. If the provider issued client credentials, configure them using V2's snake_case field names:
  126. ```jsonc title="opencode.jsonc"
  127. {
  128. "$schema": "https://opencode.ai/config.json",
  129. "mcp": {
  130. "servers": {
  131. "company-tools": {
  132. "type": "remote",
  133. "url": "https://mcp.example.com/mcp",
  134. "oauth": {
  135. "client_id": "{env:MCP_CLIENT_ID}",
  136. "client_secret": "{env:MCP_CLIENT_SECRET}",
  137. "scope": "tools:read tools:execute",
  138. "callback_port": 19876,
  139. "redirect_uri": "http://127.0.0.1:19876/callback"
  140. }
  141. }
  142. }
  143. }
  144. }
  145. ```
  146. | OAuth field | Description |
  147. | --- | --- |
  148. | `client_id` | Pre-registered OAuth client ID. If omitted, OpenCode attempts dynamic client registration. |
  149. | `client_secret` | Client secret for a pre-registered client. |
  150. | `scope` | Space-delimited scopes to request. |
  151. | `callback_port` | Local callback port, from `1` through `65535`. An available ephemeral port is used by default. |
  152. | `redirect_uri` | Pre-registered loopback redirect URI. Its path and port must reach the local callback listener. |
  153. Remove stored credentials with:
  154. ```bash
  155. opencode2 mcp logout sentry
  156. ```
  157. ## Timeouts
  158. Timeouts are positive integer milliseconds. Configure defaults under `mcp.timeout`; a server's `timeout` fields override matching defaults.
  159. ```jsonc title="opencode.jsonc"
  160. {
  161. "$schema": "https://opencode.ai/config.json",
  162. "mcp": {
  163. "timeout": {
  164. "startup": 45000,
  165. "catalog": 30000,
  166. "execution": 600000
  167. },
  168. "servers": {
  169. "slow-tools": {
  170. "type": "remote",
  171. "url": "https://mcp.example.com/mcp",
  172. "timeout": {
  173. "catalog": 60000
  174. }
  175. }
  176. }
  177. }
  178. }
  179. ```
  180. | Timeout | Default | Applies to |
  181. | --- | --- | --- |
  182. | `startup` | 30 seconds | Establishing the transport and initializing the server. |
  183. | `catalog` | 30 seconds | Listing tools, prompts, resources, and resource templates. |
  184. | `execution` | 12 hours | Calling tools, getting prompts, and reading resources. |
  185. ## Names and permissions
  186. OpenCode combines the server name and MCP tool name as `<server>_<tool>`. Characters other than letters, numbers, `_`, and `-` are replaced with `_`; for example, server `context 7` and tool `resolve.library/id` become `context_7_resolve_library_id`. MCP prompts appear as slash commands named `<server>:<prompt>` using the same normalization.
  187. Choose short server names that remain unique after normalization. Under the default Code Mode, MCP tools are grouped by the normalized server name.
  188. Set `codemode` to `false` on a server when its tools should remain on the provider's native tool list:
  189. ```jsonc
  190. {
  191. "mcp": {
  192. "servers": {
  193. "context7": {
  194. "type": "remote",
  195. "url": "https://mcp.context7.com/mcp",
  196. "codemode": false
  197. }
  198. }
  199. }
  200. }
  201. ```
  202. Use permission actions to hide or deny a server's tools without stopping its connection:
  203. ```jsonc
  204. {
  205. "permissions": [
  206. {
  207. "action": "context7_*",
  208. "resource": "*",
  209. "effect": "deny"
  210. }
  211. ]
  212. }
  213. ```
  214. ## CLI commands
  215. V2 provides these MCP management commands:
  216. ```bash
  217. # Add a local server to the project config
  218. opencode2 mcp add everything --env LOG_LEVEL=info -- npx -y @modelcontextprotocol/server-everything
  219. # Add a remote server to the project config
  220. opencode2 mcp add context7 --url https://mcp.context7.com/mcp --header 'CONTEXT7_API_KEY={env:CONTEXT7_API_KEY}'
  221. # Add to the global config instead
  222. opencode2 mcp add context7 --global --url https://mcp.context7.com/mcp
  223. # List configured servers and connection status
  224. opencode2 mcp list
  225. # Authenticate or remove OAuth credentials
  226. opencode2 mcp auth context7
  227. opencode2 mcp logout context7
  228. ```
  229. `mcp add` accepts either `--url` for a remote server or a command after `--` for a local server, not both. Use `--header NAME=VALUE` only with remote servers and `--env NAME=VALUE` only with local servers. Edit the config directly for OAuth, timeout, working-directory, or enablement settings.