mcp-server-git をdockerで起動したときに出たエラーを回避する方法をまとめます。
ここではgit操作したいディレクトリは/path/to/reposとします。userというユーザが作成しました。
公式のドキュメントを参考にして次のようなMCP設定をしたとします。これは操作したいホスト側のディレクトリ/path/to/reposがdockerコンテナ内で/workspaceとしてマウントされてコンテナ内からgit操作することになります。
{
"mcp": {
"servers": {
"git": {
"command": "docker",
"args": [
"run",
"--rm",
"-i",
"--mount", "type=bind,src=/path/to/repos,dst=/workspace",
"mcp/git"
]
}
}
}
}
何らかのMCPクライエント(例 MCP Inspectorなど)から適当なツールを実行すると、次のエラーが出てgit操作の結果が得られませんでした。
Cmd('git') failed due to: exit code(128)
cmdline: git status
stderr: 'fatal: detected dubious ownership in repository at '/workspace'
To add an exception for this directory, call:
git config --global --add safe.directory /workspace'
この原因は、gitのセキュリティ機能として所有者の違うディレクトリをgit操作をデフォルトでは許可していないためです。
dockerコンテナ内ではrootユーザでgit操作しようとするので、ホスト側でuserというユーザが作成したリポジトリに対してはgit操作が拒否されます。
これを許可するにはエラーメッセージにもある通りgitconfigにsafe.directoryとして設定すればよいです。
今回はdockerイメージの改修はせずにそのままdocker runで動かしたいので、設定済みの.gitconfigをコンテナ内にマウントする方法をとります。
以下の内容のファイルを作成します。
[safe]
directory = /workspace
そして、MCP設定を以下にします。
{
"mcp": {
"servers": {
"git": {
"command": "docker",
"args": [
"run",
"--rm",
"-i",
"--mount", "type=bind,src=/path/to/repos,dst=/workspace",
"-v", "/path/to/.gitconfig:/root/.gitconfig",
"mcp/git"
]
}
}
}
}
これでエラーがなくなって正常にgit操作してくれるようになりました。