15 min read

Setting Up MCP in OpenCode: Complete Configuration Guide

OpenCode is an open source AI coding agent that supports both local and remote MCP servers with OAuth authentication. This guide covers everything you need to know to configure MCP in OpenCode, from basic setup to advanced per-agent configuration.

OpenCode MCP configuration - connecting AI to external tools

Extend OpenCode with MCP servers

Key Takeaways

  • OpenCode supports both local (stdio) and remote (HTTP) MCP servers through its opencode.json configuration file.
  • Remote MCP servers can use OAuth authentication, which OpenCode handles automatically using Dynamic Client Registration.
  • MCP servers can be enabled globally or configured on a per-agent basis for focused context management.
  • Popular MCP servers like Context7 and Grep by Vercel can be added with minimal configuration.

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 | bash or 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 /connect command.
  • Project initialised: Run /init in 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:

opencode.jsonjson
{
  "$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

OptionTypeRequiredDescription
typeStringYesMust be "local"
commandArrayYesCommand and arguments to run the MCP server
environmentObjectNoEnvironment variables for the server
enabledBooleanNoEnable/disable the server on startup
timeoutNumberNoTimeout in ms (default: 5000)

Filesystem Server Example

opencode.jsonjson
{
  "$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

opencode.jsonjson
{
  "$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

OptionTypeRequiredDescription
typeStringYesMust be "remote"
urlStringYesURL of the remote MCP server
headersObjectNoHTTP headers for requests
oauthObjectNoOAuth configuration
timeoutNumberNoTimeout 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:

opencode.jsonjson
{
  "$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:

opencode.jsonjson
{
  "$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:

BASH
# 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-server

Token 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:

opencode.jsonjson
{
  "$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:

opencode.jsonjson
{
  "$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

opencode.jsonjson
{
  "$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 hooks

RemoteGrep by Vercel - GitHub Code Search

Configuration

opencode.jsonjson
{
  "$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 deployments

LocalFilesystem Server

Configuration

opencode.jsonjson
{
  "$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 project

LocalGitHub Server

Configuration

opencode.jsonjson
{
  "$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 repository

Troubleshooting

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-name to 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

OpenCode uses opencode.json in your project root for MCP configuration. Run /init in your project to create the configuration file if it doesn't exist. The file uses a JSON schema available at https://opencode.ai/config.json.
Local servers run on your machine via stdio transport and are configured with "type": "local" plus a command array. Remote servers are cloud-hosted, configured with "type": "remote" and a URL. Remote servers can use OAuth authentication which OpenCode handles automatically.
Check that Node.js is installed for npm-based servers. Verify the command syntax in your configuration. Try running the MCP server command manually in terminal. Ensure environment variables are correctly set using the {env:NAME} syntax.
Yes, use the tools configuration with glob patterns to disable servers globally, then enable them per-agent in the agent configuration section. For example: "tools": {"my-mcp*": false} globally, then "tools": {"my-mcp*": true} under a specific agent.
OpenCode automatically handles OAuth using Dynamic Client Registration. When a server returns a 401 response, OpenCode initiates the OAuth flow. Use "opencode mcp auth server-name" to authenticate manually. Tokens are stored securely in ~/.local/share/opencode/mcp-auth.json.

References & Further Reading

Related Articles

Ayodele Ajayi

Senior DevOps Engineer based in Kent, UK. Specialising in cloud infrastructure, DevSecOps, and platform engineering. Passionate about AI-assisted development and sharing knowledge through technical writing.