🐙

Claude Code に GitHub の MCP サーバーを使ってPRを作らせる

に公開

動機

Claude の Max プランを契約したので、Claude Code には限界まで働いてもらいたい。GitHub Actions 上で動かしてるAPI課金のワークフローと同じことは Max 版の Claude Code にもやってほしい。そのためには GitHub 上の諸々を操作できるようになってほしい。

GitHub で Personal Access Token を作っておく

GitHub 公式による詳しい作り方

今回はPRを作らせたいので Repository permissions の Pull requests は最低でも必要で、コミットさせたりIssueを読み書きさせたりもしたくなるに決まっているので Contents と Issues の権限もつけた。

MCPサーバーをClaude Codeに追加する

GitHub公式のMCPサーバー を使うが、
ローカルには何もインストールする必要なし。Claude に設定値を追加するだけ。

claude mcp add github \
    -e GITHUB_PERSONAL_ACCESS_TOKEN=github_pat_*** -- \
    docker \
    run \
    -i \
    --rm \
    -e GITHUB_PERSONAL_ACCESS_TOKEN \
    ghcr.io/github/github-mcp-server

以下が ~/.claude.json に追加される。

      "mcpServers": {
        "github": {
          "type": "stdio",
          "command": "docker",
          "args": [
            "run",
            "-i",
            "--rm",
            "-e",
            "GITHUB_PERSONAL_ACCESS_TOKEN",
            "ghcr.io/github/github-mcp-server"
          ],
          "env": {
            "GITHUB_PERSONAL_ACCESS_TOKEN": "github_pat_****"
          }
        }
      }

claude に指示する

GitHub の MCP サーバーは Docker で動かすみたいなので、Docker のデーモンを起動させておく必要がある。

ちゃんと動くか試す。

> このブランチを元にGitHubにPRを作っていただけますか。MCPサーバーでgithubに接続してください。

⏺ I'll help you create a pull request from the current branch to GitHub. Let me first check the current branch status and recent changes, then create the PR.

⏺ Bash(git status && git log --oneline -5)…
  ⎿  On branch claude-code-setting
     Your branch is up to date with 'origin/claude-code-setting'.
     … +5 lines (ctrl+r to expand)
     72d21fb 言語のファイルを分割
     7c901a2 Merge pull request #15 from umigame-tech/claude.md

⏺ Bash(git diff main...HEAD --stat)…
  ⎿   .claude/settings.json | 43 +++++++++++++++++++++++++++++++++++++++++++
      1 file changed, 43 insertions(+)

⏺ github:create_pull_request (MCP)(owner: "umigame-tech", repo: "hoge", title: "Add Claude Code settings configuration", head: "claude-code-setting", base: "main", body: "## Summary\n- Claude …
                                  Code用の設定ファイル(.claude/settings.json)を追加\n\n## Test plan\n- [ ] 設定ファイルの構文が正しいことを確認\n- [ ] Claude Codeで正常に読み込まれることを確認\n\n🤖
                                  Generated with [Claude Code](https://claude.ai/code)")
  ⎿  {
       "id": 2556556818,
     … +312 lines (ctrl+r to expand)
     }
     }

⏺ PRを作成しました: https://github.com/umigame-tech/hoge/pull/17

PRを作ってくれた。

issueも読めるはずなので、issueを読んで作業してPR作れと指示すればやってくれるはず。さんざん使い倒してボロ雑巾のようにしてやる。

追記

実際に何度か Claude Code に指示を出してたら、こちらの指示を無視してMCPサーバーじゃなくて gh コマンドを使おうとするっぽかった。まあこちらがやりたいことができるなら何でも良いのだが。

Discussion