0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

mcp-server-gitをdocker起動したときのエラーの回避策

Posted at

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をコンテナ内にマウントする方法をとります。

以下の内容のファイルを作成します。

.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操作してくれるようになりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?