mcp-servers.mdx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. ---
  2. title: MCP servers
  3. description: Add local and remote MCP tools.
  4. ---
  5. You can add external tools to OpenCode using the _Model Context Protocol_, or MCP. OpenCode supports both local and remote servers.
  6. Once added, MCP tools are automatically available to the LLM alongside built-in tools.
  7. ---
  8. #### Caveats
  9. When you use an MCP server, it adds to the context. This can quickly add up if you have a lot of tools. So we recommend being careful with which MCP servers you use.
  10. :::tip
  11. MCP servers add to your context, so you want to be careful with which ones you enable.
  12. :::
  13. Certain MCP servers, like the GitHub MCP server, tend to add a lot of tokens and can easily exceed the context limit.
  14. ---
  15. ## Enable
  16. You can define MCP servers in your [OpenCode Config](https://opencode.ai/docs/config/) under `mcp`. Add each MCP with a unique name. You can refer to that MCP by name when prompting the LLM.
  17. ```jsonc title="opencode.jsonc" {6}
  18. {
  19. "$schema": "https://opencode.ai/config.json",
  20. "mcp": {
  21. "name-of-mcp-server": {
  22. // ...
  23. "enabled": true,
  24. },
  25. "name-of-other-mcp-server": {
  26. // ...
  27. },
  28. },
  29. }
  30. ```
  31. You can also disable a server by setting `enabled` to `false`. This is useful if you want to temporarily disable a server without removing it from your config.
  32. ---
  33. ### Overriding remote defaults
  34. Organizations can provide default MCP servers via their `.well-known/opencode` endpoint. These servers may be disabled by default, allowing users to opt-in to the ones they need.
  35. To enable a specific server from your organization's remote config, add it to your local config with `enabled: true`:
  36. ```json title="opencode.json"
  37. {
  38. "$schema": "https://opencode.ai/config.json",
  39. "mcp": {
  40. "jira": {
  41. "type": "remote",
  42. "url": "https://jira.example.com/mcp",
  43. "enabled": true
  44. }
  45. }
  46. }
  47. ```
  48. Your local config values override the remote defaults. See [config precedence](/docs/config#precedence-order) for more details.
  49. ---
  50. ## Local
  51. Add local MCP servers using `type` to `"local"` within the MCP object.
  52. ```jsonc title="opencode.jsonc" {15}
  53. {
  54. "$schema": "https://opencode.ai/config.json",
  55. "mcp": {
  56. "my-local-mcp-server": {
  57. "type": "local",
  58. // Or ["bun", "x", "my-mcp-command"]
  59. "command": ["npx", "-y", "my-mcp-command"],
  60. "enabled": true,
  61. "environment": {
  62. "MY_ENV_VAR": "my_env_var_value",
  63. },
  64. },
  65. },
  66. }
  67. ```
  68. The command is how the local MCP server is started. You can also pass in a list of environment variables as well.
  69. For example, here's how you can add the test [`@modelcontextprotocol/server-everything`](https://www.npmjs.com/package/@modelcontextprotocol/server-everything) MCP server.
  70. ```jsonc title="opencode.jsonc"
  71. {
  72. "$schema": "https://opencode.ai/config.json",
  73. "mcp": {
  74. "mcp_everything": {
  75. "type": "local",
  76. "command": ["npx", "-y", "@modelcontextprotocol/server-everything"],
  77. },
  78. },
  79. }
  80. ```
  81. And to use it I can add `use the mcp_everything tool` to my prompts.
  82. ```txt "mcp_everything"
  83. use the mcp_everything tool to add the number 3 and 4
  84. ```
  85. ---
  86. #### Options
  87. Here are all the options for configuring a local MCP server.
  88. | Option | Type | Required | Description |
  89. | ------------- | ------- | -------- | ----------------------------------------------------------------------------------- |
  90. | `type` | String | Y | Type of MCP server connection, must be `"local"`. |
  91. | `command` | Array | Y | Command and arguments to run the MCP server. |
  92. | `environment` | Object | | Environment variables to set when running the server. |
  93. | `enabled` | Boolean | | Enable or disable the MCP server on startup. |
  94. | `timeout` | Number | | Timeout in ms for fetching tools from the MCP server. Defaults to 5000 (5 seconds). |
  95. ---
  96. ## Remote
  97. Add remote MCP servers by setting `type` to `"remote"`.
  98. ```json title="opencode.json"
  99. {
  100. "$schema": "https://opencode.ai/config.json",
  101. "mcp": {
  102. "my-remote-mcp": {
  103. "type": "remote",
  104. "url": "https://my-mcp-server.com",
  105. "enabled": true,
  106. "headers": {
  107. "Authorization": "Bearer MY_API_KEY"
  108. }
  109. }
  110. }
  111. }
  112. ```
  113. The `url` is the URL of the remote MCP server and with the `headers` option you can pass in a list of headers.
  114. ---
  115. #### Options
  116. | Option | Type | Required | Description |
  117. | --------- | ------- | -------- | ----------------------------------------------------------------------------------- |
  118. | `type` | String | Y | Type of MCP server connection, must be `"remote"`. |
  119. | `url` | String | Y | URL of the remote MCP server. |
  120. | `enabled` | Boolean | | Enable or disable the MCP server on startup. |
  121. | `headers` | Object | | Headers to send with the request. |
  122. | `oauth` | Object | | OAuth authentication configuration. See [OAuth](#oauth) section below. |
  123. | `timeout` | Number | | Timeout in ms for fetching tools from the MCP server. Defaults to 5000 (5 seconds). |
  124. ---
  125. ## OAuth
  126. OpenCode automatically handles OAuth authentication for remote MCP servers. When a server requires authentication, OpenCode will:
  127. 1. Detect the 401 response and initiate the OAuth flow
  128. 2. Use **Dynamic Client Registration (RFC 7591)** if supported by the server
  129. 3. Store tokens securely for future requests
  130. ---
  131. ### Automatic
  132. For most OAuth-enabled MCP servers, no special configuration is needed. Just configure the remote server:
  133. ```json title="opencode.json"
  134. {
  135. "$schema": "https://opencode.ai/config.json",
  136. "mcp": {
  137. "my-oauth-server": {
  138. "type": "remote",
  139. "url": "https://mcp.example.com/mcp"
  140. }
  141. }
  142. }
  143. ```
  144. If the server requires authentication, OpenCode will prompt you to authenticate when you first try to use it. If not, you can [manually trigger the flow](#authenticating) with `opencode mcp auth <server-name>`.
  145. ---
  146. ### Pre-registered
  147. If you have client credentials from the MCP server provider, you can configure them:
  148. ```json title="opencode.json" {7-11}
  149. {
  150. "$schema": "https://opencode.ai/config.json",
  151. "mcp": {
  152. "my-oauth-server": {
  153. "type": "remote",
  154. "url": "https://mcp.example.com/mcp",
  155. "oauth": {
  156. "clientId": "{env:MY_MCP_CLIENT_ID}",
  157. "clientSecret": "{env:MY_MCP_CLIENT_SECRET}",
  158. "scope": "tools:read tools:execute"
  159. }
  160. }
  161. }
  162. }
  163. ```
  164. ---
  165. ### Authenticating
  166. You can manually trigger authentication or manage credentials.
  167. Authenticate with a specific MCP server:
  168. ```bash
  169. opencode mcp auth my-oauth-server
  170. ```
  171. List all MCP servers and their auth status:
  172. ```bash
  173. opencode mcp list
  174. ```
  175. Remove stored credentials:
  176. ```bash
  177. opencode mcp logout my-oauth-server
  178. ```
  179. The `mcp auth` command will open your browser for authorization. After you authorize, OpenCode will store the tokens securely in `~/.local/share/opencode/mcp-auth.json`.
  180. ---
  181. #### Disabling OAuth
  182. If you want to disable automatic OAuth for a server (e.g., for servers that use API keys instead), set `oauth` to `false`:
  183. ```json title="opencode.json" {7}
  184. {
  185. "$schema": "https://opencode.ai/config.json",
  186. "mcp": {
  187. "my-api-key-server": {
  188. "type": "remote",
  189. "url": "https://mcp.example.com/mcp",
  190. "oauth": false,
  191. "headers": {
  192. "Authorization": "Bearer {env:MY_API_KEY}"
  193. }
  194. }
  195. }
  196. }
  197. ```
  198. ---
  199. #### OAuth Options
  200. | Option | Type | Description |
  201. | -------------- | --------------- | -------------------------------------------------------------------------------- |
  202. | `oauth` | Object \| false | OAuth config object, or `false` to disable OAuth auto-detection. |
  203. | `clientId` | String | OAuth client ID. If not provided, dynamic client registration will be attempted. |
  204. | `clientSecret` | String | OAuth client secret, if required by the authorization server. |
  205. | `scope` | String | OAuth scopes to request during authorization. |
  206. #### Debugging
  207. If a remote MCP server is failing to authenticate, you can diagnose issues with:
  208. ```bash
  209. # View auth status for all OAuth-capable servers
  210. opencode mcp auth list
  211. # Debug connection and OAuth flow for a specific server
  212. opencode mcp debug my-oauth-server
  213. ```
  214. The `mcp debug` command shows the current auth status, tests HTTP connectivity, and attempts the OAuth discovery flow.
  215. ---
  216. ## Manage
  217. Your MCPs are available as tools in OpenCode, alongside built-in tools. So you can manage them through the OpenCode config like any other tool.
  218. ---
  219. ### Global
  220. This means that you can enable or disable them globally.
  221. ```json title="opencode.json" {14}
  222. {
  223. "$schema": "https://opencode.ai/config.json",
  224. "mcp": {
  225. "my-mcp-foo": {
  226. "type": "local",
  227. "command": ["bun", "x", "my-mcp-command-foo"]
  228. },
  229. "my-mcp-bar": {
  230. "type": "local",
  231. "command": ["bun", "x", "my-mcp-command-bar"]
  232. }
  233. },
  234. "tools": {
  235. "my-mcp-foo": false
  236. }
  237. }
  238. ```
  239. We can also use a glob pattern to disable all matching MCPs.
  240. ```json title="opencode.json" {14}
  241. {
  242. "$schema": "https://opencode.ai/config.json",
  243. "mcp": {
  244. "my-mcp-foo": {
  245. "type": "local",
  246. "command": ["bun", "x", "my-mcp-command-foo"]
  247. },
  248. "my-mcp-bar": {
  249. "type": "local",
  250. "command": ["bun", "x", "my-mcp-command-bar"]
  251. }
  252. },
  253. "tools": {
  254. "my-mcp*": false
  255. }
  256. }
  257. ```
  258. Here we are using the glob pattern `my-mcp*` to disable all MCPs.
  259. ---
  260. ### Per agent
  261. If you have a large number of MCP servers you may want to only enable them per agent and disable them globally. To do this:
  262. 1. Disable it as a tool globally.
  263. 2. In your [agent config](/docs/agents#tools), enable the MCP server as a tool.
  264. ```json title="opencode.json" {11, 14-18}
  265. {
  266. "$schema": "https://opencode.ai/config.json",
  267. "mcp": {
  268. "my-mcp": {
  269. "type": "local",
  270. "command": ["bun", "x", "my-mcp-command"],
  271. "enabled": true
  272. }
  273. },
  274. "tools": {
  275. "my-mcp*": false
  276. },
  277. "agent": {
  278. "my-agent": {
  279. "tools": {
  280. "my-mcp*": true
  281. }
  282. }
  283. }
  284. }
  285. ```
  286. ---
  287. #### Glob patterns
  288. The glob pattern uses simple regex globbing patterns:
  289. - `*` matches zero or more of any character (e.g., `"my-mcp*"` matches `my-mcp_search`, `my-mcp_list`, etc.)
  290. - `?` matches exactly one character
  291. - All other characters match literally
  292. :::note
  293. MCP server tools are registered with server name as prefix, so to disable all tools for a server simply use:
  294. ```
  295. "mymcpservername_*": false
  296. ```
  297. :::
  298. ---
  299. ## Examples
  300. Below are examples of some common MCP servers. You can submit a PR if you want to document other servers.
  301. ---
  302. ### Sentry
  303. Add the [Sentry MCP server](https://mcp.sentry.dev) to interact with your Sentry projects and issues.
  304. ```json title="opencode.json" {4-8}
  305. {
  306. "$schema": "https://opencode.ai/config.json",
  307. "mcp": {
  308. "sentry": {
  309. "type": "remote",
  310. "url": "https://mcp.sentry.dev/mcp",
  311. "oauth": {}
  312. }
  313. }
  314. }
  315. ```
  316. After adding the configuration, authenticate with Sentry:
  317. ```bash
  318. opencode mcp auth sentry
  319. ```
  320. This will open a browser window to complete the OAuth flow and connect OpenCode to your Sentry account.
  321. Once authenticated, you can use Sentry tools in your prompts to query issues, projects, and error data.
  322. ```txt "use sentry"
  323. Show me the latest unresolved issues in my project. use sentry
  324. ```
  325. ---
  326. ### Context7
  327. Add the [Context7 MCP server](https://github.com/upstash/context7) to search through docs.
  328. ```json title="opencode.json" {4-7}
  329. {
  330. "$schema": "https://opencode.ai/config.json",
  331. "mcp": {
  332. "context7": {
  333. "type": "remote",
  334. "url": "https://mcp.context7.com/mcp"
  335. }
  336. }
  337. }
  338. ```
  339. If you have signed up for a free account, you can use your API key and get higher rate-limits.
  340. ```json title="opencode.json" {7-9}
  341. {
  342. "$schema": "https://opencode.ai/config.json",
  343. "mcp": {
  344. "context7": {
  345. "type": "remote",
  346. "url": "https://mcp.context7.com/mcp",
  347. "headers": {
  348. "CONTEXT7_API_KEY": "{env:CONTEXT7_API_KEY}"
  349. }
  350. }
  351. }
  352. }
  353. ```
  354. Here we are assuming that you have the `CONTEXT7_API_KEY` environment variable set.
  355. Add `use context7` to your prompts to use Context7 MCP server.
  356. ```txt "use context7"
  357. Configure a Cloudflare Worker script to cache JSON API responses for five minutes. use context7
  358. ```
  359. Alternatively, you can add something like this to your [AGENTS.md](/docs/rules/).
  360. ```md title="AGENTS.md"
  361. When you need to search docs, use `context7` tools.
  362. ```
  363. ---
  364. ### Grep by Vercel
  365. Add the [Grep by Vercel](https://grep.app) MCP server to search through code snippets on GitHub.
  366. ```json title="opencode.json" {4-7}
  367. {
  368. "$schema": "https://opencode.ai/config.json",
  369. "mcp": {
  370. "gh_grep": {
  371. "type": "remote",
  372. "url": "https://mcp.grep.app"
  373. }
  374. }
  375. }
  376. ```
  377. Since we named our MCP server `gh_grep`, you can add `use the gh_grep tool` to your prompts to get the agent to use it.
  378. ```txt "use the gh_grep tool"
  379. What's the right way to set a custom domain in an SST Astro component? use the gh_grep tool
  380. ```
  381. Alternatively, you can add something like this to your [AGENTS.md](/docs/rules/).
  382. ```md title="AGENTS.md"
  383. If you are unsure how to do something, use `gh_grep` to search code examples from GitHub.
  384. ```