コンテンツにスキップ

MCP サーバー

MCP servers

Model Context Protocol(MCP)を使用して、外部ツールを OpenCode に追加できます。OpenCode はローカルとリモートの両方のサーバーをサポートしています。

追加されると、MCP ツールは組み込みツールと一緒に LLM に自動的に利用可能になります。


Caveats

MCP サーバーを使用すると、コンテキストに追加されます。これは、多くのツールを持っている場合にすぐに蓄積する可能性があります。そのため、どの MCP サーバーを使用するかに注意することをお勧めします。


有効化

OpenCode Configmcp 下で MCP サーバーを定義できます。各 MCP を一意の名前で追加します。その MCP に対して LLM にプロンプトを出力ときにその名前で参照できます。

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "name-of-mcp-server": {
      // ...
      "enabled": true,
    },
    "name-of-other-mcp-server": {
      // ...
    },
  },
}

enabledfalse に設定してサーバーを無効にすることもできます。これは、構成から削除せずにサーバーを一時的に無効にしたい場合に便利です。


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
    }
  }
}

ローカル設定値は遥控デフォルトを上書きします。詳細については設定優先順位を参照してください。


ローカル

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",
      },
    },
  },
}

コマンドはローカル MCP サーバーの起動方法です。環境変数のリストを渡すこともできます。

例えば、テスト用 @modelcontextprotocol/server-everything 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 4

Options

ローカル MCP サーバーを設定するためのすべてのオプションです。

OptionTypeRequiredDescription
typeStringYType of MCP server connection, must be "local".
commandArrayYCommand and arguments to run the MCP server.
environmentObjectEnvironment variables to set when running the server.
enabledBooleanEnable or disable the MCP server on startup.
timeoutNumberTimeout in ms for fetching tools from the MCP server. Defaults to 5000 (5 seconds).

リモート

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

OptionTypeRequiredDescription
typeStringYType of MCP server connection, must be "remote".
urlStringYURL of the remote MCP server.
enabledBooleanEnable or disable the MCP server on startup.
headersObjectHeaders to send with the request.
oauthObjectOAuth authentication configuration. See OAuth section below.
timeoutNumberTimeout in ms for fetching tools from the MCP server. Defaults to 5000 (5 seconds).

OAuth

OpenCode は遥控 MCP サーバーの OAuth 認証を自動的に処理します。サーバーが認証を必要とするとき、OpenCode は以下を行います:

  1. 401 応答を検出し、OAuth フローを開始
  2. サーバーがサポートしている場合、Dynamic Client Registration (RFC 7591) を使用
  3. 将来の要求のためにトークンを安全に保存

Automatic

ほとんどの OAuth 対応 MCP サーバーの場合、特別な設定は不要です。遥控サーバーを設定するだけです:

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "my-oauth-server": {
      "type": "remote",
      "url": "https://mcp.example.com/mcp"
    }
  }
}

サーバーが認証を必要とする場合、初めて使用的时候我将领导您进行认证。そうでない場合は、opencode mcp auth <server-name>手動でフローをトリガーできます。


Pre-registered

MCP サーバー 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"
      }
    }
  }
}

Authenticating

手動で認証をトリガーしたり、認証情報を管理できます。

特定の MCP サーバーで認証します:

opencode mcp auth my-oauth-server

すべての MCP サーバーとその認証状態の一覧:

opencode mcp list

保存された認証情報を削除します:

opencode mcp logout my-oauth-server

mcp auth コマンドは承認のためにブラウザを開きます。承認後、OpenCode はトークンを ~/.local/share/opencode/mcp-auth.json に安全に保存します。


Disabling OAuth

サーバー(API キーのみを使用するサーバーなど)の自動 OAuth を無効にしたい場合、oauthfalse に設定します:

{
  "$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

OptionTypeDescription
oauthObject | falseOAuth config object, or false to disable OAuth auto-detection.
clientIdStringOAuth client ID. If not provided, dynamic client registration will be attempted.
clientSecretStringOAuth client secret, if required by the authorization server.
scopeStringOAuth scopes to request during authorization.

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

mcp debug コマンドは現在の認証状態を表示し、HTTP 接続をテストし、OAuth 検出フローを試行します。


管理

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
  }
}

ここでは glob パターン my-mcp* を使用してすべての MCP を無効にしています。


Per agent

多くの数の MCP サーバーがいる場合、エージェントごとにのみ有効にしてグローバルに無効にしたい場合があります。これを行うには:

  1. ツールとしてグローバルに無効にします。
  2. agent config で、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 パターンは単純な正規表現 globbing パターンを使用します:

  • * 0 個以上の任意の文字にマッチ(例:"my-mcp*"my-mcp_searchmy-mcp_list などにマッチ)
  • ? 正確に 1 文字にマッチ
  • 他のすべての文字は文字通りマッチ

以下は一般的な MCP サーバーの例です。他のサーバーをドキュメント化する場合は PR を提交してください。


Sentry

Sentry MCP サーバー を追加して、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 sentry

Context7

Context7 MCP サーバー を追加してドキュメントを検索します。

{
  "$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 環境変数が設定されていると想定しています。

プロンプトに use context7 を追加して Context7 MCP サーバーを使用します。

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 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.