Introduction to OpenCode MCP
OpenCode is an open source AI coding agent available as a terminal-based interface, desktop app, or IDE extension. It leverages the Model Context Protocol (MCP) to extend its capabilities beyond built-in tools, allowing you to connect to external data sources, APIs, and services.
MCP servers add to your context, so be careful with which ones you enable. Certain servers like the GitHub MCP server tend to add many tokens and can exceed context limits.
Once configured, MCP tools are automatically available to the LLM alongside OpenCode's built-in tools. You can reference them by name in your prompts to leverage their capabilities.
Prerequisites
Before configuring MCP servers in OpenCode, ensure you have:
- OpenCode installed: Install via
curl -fsSL https://opencode.ai/install | bashor npm/brew. - Node.js: Required for running npm-based MCP servers. Verify with
node --version. - API provider configured: Connect OpenCode to an LLM provider via
/connectcommand. - Project initialised: Run
/initin your project to create the configuration file.
OpenCode MCP Features
OpenCode provides comprehensive MCP support with the following features:
Local MCP Servers
Run MCP servers locally using stdio transport for file system access, database queries, and more.
Remote MCP Servers
Connect to cloud-hosted MCP servers via HTTP for documentation search, web scraping, and API integrations.
OAuth Authentication
Automatic OAuth handling for secure authentication with remote MCP servers that require it.
Per-Agent Configuration
Enable or disable MCP servers on a per-agent basis to keep your context focused and efficient.
Configuring Local MCP Servers
Local MCP servers run on your machine and communicate via standard input/output (stdio). They're ideal for file system access, local database queries, and tools that need access to your local environment.
Basic Configuration
Add local MCP servers to your opencode.json file:
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-local-mcp-server": {
"type": "local",
"command": ["npx", "-y", "my-mcp-command"],
"enabled": true,
"environment": {
"MY_ENV_VAR": "my_env_var_value"
}
}
}
}Configuration Options
| Option | Type | Required | Description |
|---|---|---|---|
| type | String | Yes | Must be "local" |
| command | Array | Yes | Command and arguments to run the MCP server |
| environment | Object | No | Environment variables for the server |
| enabled | Boolean | No | Enable/disable the server on startup |
| timeout | Number | No | Timeout in ms (default: 5000) |
Filesystem Server Example
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"filesystem": {
"type": "local",
"command": [
"npx",
"-y",
"@modelcontextprotocol/server-filesystem",
"/Users/username/projects",
"/Users/username/documents"
],
"enabled": true
}
}
}Configuring Remote MCP Servers
Remote MCP servers are hosted in the cloud and accessed via HTTP. They're perfect for documentation search, web scraping, and third-party API integrations.
Basic Configuration
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-remote-mcp": {
"type": "remote",
"url": "https://my-mcp-server.com",
"enabled": true,
"headers": {
"Authorization": "Bearer MY_API_KEY"
}
}
}
}Remote Server Options
| Option | Type | Required | Description |
|---|---|---|---|
| type | String | Yes | Must be "remote" |
| url | String | Yes | URL of the remote MCP server |
| headers | Object | No | HTTP headers for requests |
| oauth | Object | No | OAuth configuration |
| timeout | Number | No | Timeout in ms (default: 5000) |
OAuth Authentication
OpenCode automatically handles OAuth authentication for remote MCP servers that require it. When a server returns a 401 response, OpenCode initiates the OAuth flow automatically.
Automatic OAuth
For most OAuth-enabled servers, no special configuration is needed:
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-oauth-server": {
"type": "remote",
"url": "https://mcp.example.com/mcp"
}
}
}Pre-registered Client Credentials
If you have client credentials from the MCP server provider:
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-oauth-server": {
"type": "remote",
"url": "https://mcp.example.com/mcp",
"oauth": {
"clientId": "{env:MY_MCP_CLIENT_ID}",
"clientSecret": "{env:MY_MCP_CLIENT_SECRET}",
"scope": "tools:read tools:execute"
}
}
}
}Managing OAuth Credentials
Use these commands to manage OAuth:
# Authenticate with a specific MCP server# List all MCP servers and their auth status# Remove stored credentials# Remove stored credentials# Remove stored credentialsls
opencode mcp logout my-oauth-serverToken Storage
OAuth tokens are stored securely in ~/.local/share/opencode/mcp-auth.json.
Managing MCP Servers
Global Enable/Disable
You can enable or disable MCP servers globally using the tools configuration:
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-mcp-foo": {
"type": "local",
"command": ["bun", "x", "my-mcp-command-foo"]
},
"my-mcp-bar": {
"type": "local",
"command": ["bun", "x", "my-mcp-command-bar"]
}
},
"tools": {
"my-mcp-foo": false,
"my-mcp*": false // Glob pattern to disable all matching
}
}Per-Agent Configuration
For large numbers of MCP servers, enable them per-agent rather than globally:
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-mcp": {
"type": "local",
"command": ["bun", "x", "my-mcp-command"],
"enabled": true
}
},
"tools": {
"my-mcp*": false // Disable globally
},
"agent": {
"my-agent": {
"tools": {
"my-mcp*": true // Enable for this agent only
}
}
}
}Glob Patterns
OpenCode supports glob patterns for tool configuration:
*- Matches zero or more of any character?- Matches exactly one character- All other characters match literally
Real-World Examples
Here are complete configuration examples for popular MCP servers:
RemoteContext7 - Documentation Search
Configuration
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"context7": {
"type": "remote",
"url": "https://mcp.context7.com/mcp"
}
}
}Example Prompt
use context7 to search the React documentation for hooksRemoteGrep by Vercel - GitHub Code Search
Configuration
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"gh_grep": {
"type": "remote",
"url": "https://mcp.grep.app"
}
}
}Example Prompt
use the gh_grep tool to find examples of SST Astro deploymentsLocalFilesystem Server
Configuration
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"filesystem": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-filesystem", "/path/to/directory"],
"enabled": true
}
}
}Example Prompt
use filesystem to list all TypeScript files in the projectLocalGitHub Server
Configuration
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"github": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-github"],
"environment": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "{env:GITHUB_TOKEN}"
},
"enabled": true
}
}
}Example Prompt
use github to list open issues in the current repositoryTroubleshooting
MCP server not connecting
If your MCP server fails to connect:
- • Check that Node.js is installed and accessible
- • Verify the command syntax in your configuration
- • Try running the MCP server command manually in terminal
- • Check environment variables are correctly set
Context limit exceeded
MCP servers add tokens to your context. If you're hitting limits:
- • Disable unused MCP servers
- • Use per-agent configuration for focused contexts
- • Consider using remote servers over local ones
OAuth authentication fails
For OAuth-related issues:
- • Run
opencode mcp auth server-nameto re-authenticate - • Check that your browser can open for the OAuth flow
- • Verify client credentials if using pre-registered client
Start Building with MCP
OpenCode's MCP support opens up endless possibilities for extending your AI coding workflow. Start with a simple remote server like Context7, then explore local servers for file system and database access as you become more comfortable with the configuration.
Frequently Asked Questions
References & Further Reading
- OpenCode MCP Documentation- Official guide to MCP in OpenCode
- OpenCode - Open Source AI Coding Agent- Terminal-based AI coding assistant
- OpenCode GitHub Repository- Open source codebase and contributions
- Model Context Protocol - Official Documentation- Complete MCP specification and guides
- Official MCP Servers Repository- Reference implementations and pre-built servers
- Context7 - Remote MCP Server- Hosted MCP server for documentation context
- Building MCP Servers- Guide to creating custom MCP servers

