MCP servers
ローカルおよびリモートの MCP ツールを追加します。
Model Context Protocol(MCP)を使って、OpenCode に外部ツールを追加できます。OpenCode はローカルサーバーとリモートサーバーの両方をサポートしています。
追加されると、MCP ツールは組み込みツールと並んで自動的に LLM で利用できるようになります。
Caveats
MCP サーバーを使うと、コンテキストに追加されます。ツールが多いと、これはすぐに積み重なります。そのため、使用する MCP サーバーには注意することをおすすめします。
ヒント: MCP サーバーはコンテキストに追加されるため、有効にするものには注意したいところです。
GitHub MCP サーバーのような特定の MCP サーバーは、多くのトークンを追加する傾向があり、コンテキスト制限を簡単に超えてしまうことがあります。
Enable
OpenCode Config (opens in a new tab) の mcp の下で MCP サーバーを定義できます。各 MCP に一意の名前を付けて追加します。LLM にプロンプトを出すときに、その名前で MCP を参照できます。
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"name-of-mcp-server": {
// ...
"enabled": true,
},
"name-of-other-mcp-server": {
// ...
},
},
}enabled を false に設定して、サーバーを無効にすることもできます。これは、設定から削除せずに一時的にサーバーを無効にしたい場合に便利です。
Overriding remote defaults
組織は、.well-known/opencode エンドポイントを通じてデフォルトの MCP サーバーを提供できます。これらのサーバーはデフォルトで無効になっている場合があり、ユーザーは必要なものをオプトインできます。
組織のリモート設定から特定のサーバーを有効にするには、ローカル設定に enabled: true を付けて追加します:
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"jira": {
"type": "remote",
"url": "https://jira.example.com/mcp",
"enabled": true
}
}
}ローカル設定の値はリモートのデフォルトをオーバーライドします。詳細については設定の優先順位を参照してください。
Local
MCP オブジェクト内で type を "local" にして、ローカル MCP サーバーを追加します。
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-local-mcp-server": {
"type": "local",
// Or ["bun", "x", "my-mcp-command"]
"command": ["npx", "-y", "my-mcp-command"],
"enabled": true,
"environment": {
"MY_ENV_VAR": "my_env_var_value",
},
},
},
}command は、ローカル MCP サーバーを起動する方法です。環境変数のリストを渡すこともできます。
例えば、テスト用の @modelcontextprotocol/server-everything (opens in a new tab) MCP サーバーを追加する方法は次のとおりです。
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"mcp_everything": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-everything"],
},
},
}そしてこれを使うには、プロンプトに use the mcp_everything tool を追加します。
use the mcp_everything tool to add the number 3 and 4Options
ローカル MCP サーバーを設定するためのすべてのオプションは次のとおりです。
| Option | Type | Required | Description |
|---|---|---|---|
type | String | Y | MCP サーバー接続のタイプ。"local" である必要があります。 |
command | Array | Y | MCP サーバーを実行するためのコマンドと引数。 |
environment | Object | サーバー実行時に設定する環境変数。 | |
enabled | Boolean | 起動時に MCP サーバーを有効または無効にします。 | |
timeout | Number | MCP サーバーからツールを取得する際のタイムアウト(ミリ秒)。デフォルトは 5000(5 秒)。 |
Remote
type を "remote" に設定して、リモート MCP サーバーを追加します。
{
"$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"
}
}
}
}url はリモート MCP サーバーの URL で、headers オプションでヘッダーのリストを渡せます。
Options
| Option | Type | Required | Description |
|---|---|---|---|
type | String | Y | MCP サーバー接続のタイプ。"remote" である必要があります。 |
url | String | Y | リモート MCP サーバーの URL。 |
enabled | Boolean | 起動時に MCP サーバーを有効または無効にします。 | |
headers | Object | リクエストとともに送信するヘッダー。 | |
oauth | Object | OAuth 認証の設定。下記の OAuth セクションを参照してください。 | |
timeout | Number | MCP サーバーからツールを取得する際のタイムアウト(ミリ秒)。デフォルトは 5000(5 秒)。 |
OAuth
OpenCode は、リモート MCP サーバーの OAuth 認証を自動的に処理します。サーバーが認証を必要とする場合、OpenCode は次のことを行います:
- 401 レスポンスを検出し、OAuth フローを開始します
- サーバーがサポートしている場合、Dynamic Client Registration (RFC 7591) を使用します
- 今後のリクエストのためにトークンを安全に保存します
Automatic
ほとんどの OAuth 対応 MCP サーバーでは、特別な設定は不要です。リモートサーバーを設定するだけです:
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-oauth-server": {
"type": "remote",
"url": "https://mcp.example.com/mcp"
}
}
}サーバーが認証を必要とする場合、OpenCode は初めて使おうとしたときに認証するよう促します。そうでない場合は、opencode mcp auth <server-name> で手動でフローをトリガーできます。
Pre-registered
MCP サーバープロバイダーからクライアント認証情報を取得している場合は、それらを設定できます:
{
"$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"
}
}
}
}Authenticating
認証を手動でトリガーしたり、認証情報を管理したりできます。
特定の MCP サーバーで認証します:
opencode mcp auth my-oauth-serverすべての MCP サーバーとその認証ステータスを一覧表示します:
opencode mcp list保存された認証情報を削除します:
opencode mcp logout my-oauth-servermcp auth コマンドは、認可のためにブラウザを開きます。認可後、OpenCode はトークンを ~/.local/share/opencode/mcp-auth.json に安全に保存します。
Disabling OAuth
サーバーの自動 OAuth を無効にしたい場合(例えば、代わりに API キーを使うサーバーの場合)は、oauth を false に設定します:
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-api-key-server": {
"type": "remote",
"url": "https://mcp.example.com/mcp",
"oauth": false,
"headers": {
"Authorization": "Bearer {env:MY_API_KEY}"
}
}
}
}OAuth Options
| Option | Type | Description |
|---|---|---|
oauth | Object | false | OAuth 設定オブジェクト、または OAuth の自動検出を無効にする false。 |
clientId | String | OAuth クライアント ID。指定されない場合、動的クライアント登録が試みられます。 |
clientSecret | String | 認可サーバーが必要とする場合の OAuth クライアントシークレット。 |
scope | String | 認可時にリクエストする OAuth スコープ。 |
Debugging
リモート MCP サーバーの認証が失敗している場合は、次の方法で問題を診断できます:
# View auth status for all OAuth-capable servers
opencode mcp auth list
# Debug connection and OAuth flow for a specific server
opencode mcp debug my-oauth-servermcp debug コマンドは、現在の認証ステータスを表示し、HTTP 接続をテストし、OAuth ディスカバリーフローを試みます。
Manage
あなたの MCP は、組み込みツールと並んで OpenCode のツールとして利用できます。そのため、他のツールと同様に OpenCode の設定を通じて管理できます。
Global
これは、それらをグローバルに有効または無効にできることを意味します。
{
"$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
}
}glob パターンを使って、一致するすべての MCP を無効にすることもできます。
{
"$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*": false
}
}ここでは、すべての MCP を無効にするために glob パターン my-mcp* を使用しています。
Per agent
MCP サーバーが多数ある場合は、エージェントごとにのみ有効にしてグローバルでは無効にしたいことがあります。これを行うには:
- ツールとしてグローバルに無効にします。
- エージェント設定で、MCP サーバーをツールとして有効にします。
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-mcp": {
"type": "local",
"command": ["bun", "x", "my-mcp-command"],
"enabled": true
}
},
"tools": {
"my-mcp*": false
},
"agent": {
"my-agent": {
"tools": {
"my-mcp*": true
}
}
}
}Glob patterns
glob パターンは、シンプルな正規表現の glob パターンを使用します:
*は任意の文字 0 個以上に一致します(例:"my-mcp*"はmy-mcp_search、my-mcp_listなどに一致します)?はちょうど 1 文字に一致します- その他のすべての文字は文字どおりに一致します
注記: MCP サーバーのツールはサーバー名をプレフィックスとして登録されるため、サーバーのすべてのツールを無効にするには単純に次を使用します:
"mymcpservername_*": false
Examples
以下は、いくつかの一般的な MCP サーバーの例です。他のサーバーをドキュメント化したい場合は、PR を提出できます。
Sentry
Sentry MCP サーバー (opens in a new tab)を追加して、Sentry のプロジェクトや issue とやり取りします。
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"sentry": {
"type": "remote",
"url": "https://mcp.sentry.dev/mcp",
"oauth": {}
}
}
}設定を追加した後、Sentry で認証します:
opencode mcp auth sentryこれにより、OAuth フローを完了するためのブラウザウィンドウが開き、OpenCode が Sentry アカウントに接続されます。
認証されると、プロンプトで Sentry ツールを使って、issue、プロジェクト、エラーデータをクエリできます。
Show me the latest unresolved issues in my project. use sentryContext7
Context7 MCP サーバー (opens in a new tab)を追加して、ドキュメントを検索します。
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"context7": {
"type": "remote",
"url": "https://mcp.context7.com/mcp"
}
}
}無料アカウントに登録している場合は、API キーを使ってより高いレート制限を得られます。
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"context7": {
"type": "remote",
"url": "https://mcp.context7.com/mcp",
"headers": {
"CONTEXT7_API_KEY": "{env:CONTEXT7_API_KEY}"
}
}
}
}ここでは、CONTEXT7_API_KEY 環境変数が設定されていることを前提としています。
Context7 MCP サーバーを使うには、プロンプトに use context7 を追加します。
Configure a Cloudflare Worker script to cache JSON API responses for five minutes. use context7あるいは、AGENTS.md に次のようなものを追加することもできます。
When you need to search docs, use `context7` tools.Grep by Vercel
Grep by Vercel (opens in a new tab) MCP サーバーを追加して、GitHub 上のコードスニペットを検索します。
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"gh_grep": {
"type": "remote",
"url": "https://mcp.grep.app"
}
}
}MCP サーバーに gh_grep という名前を付けたので、プロンプトに use the gh_grep tool を追加して、エージェントにそれを使わせることができます。
What's the right way to set a custom domain in an SST Astro component? use the gh_grep toolあるいは、AGENTS.md に次のようなものを追加することもできます。
If you are unsure how to do something, use `gh_grep` to search code examples from GitHub.