iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🤖

Implementing MCP Integration with Claude Code Action

に公開

Introduction

Connecting Claude Code Action, which runs on GitHub Actions, with MCP (Model Context Protocol) allows AI agents to access external tools and data sources.

In this article, I will explain how to set up an MCP server in Claude Code Action to automate tasks such as issue management and PR reviews on GitHub.

What you can learn from this article:

  • Basic concepts and mechanism of MCP
  • How to configure MCP in Claude Code Action
  • Practical examples of using the GitHub MCP server
  • Key troubleshooting points

What is MCP?

MCP (Model Context Protocol) is an open standard developed by Anthropic that provides a protocol for LLMs to securely integrate with external tools and data sources.

The Three Components of MCP

MCP consists of the following three elements:

  1. Host - LLM applications (Claude Code, claude.ai, etc.)
  2. Client - Mediates interactions using the MCP protocol
  3. Server - Provides access to external tools and data sources

Key Features of MCP

MCP standardizes three basic functions:

  • Resources - Access to data such as files, DBs, and APIs
  • Tools - Execution of external operations (API calls, searches, writing, etc.)
  • Prompts - Provision of templated scenarios

Benefits of MCP

  • Reusability - Once configured, it can be used across multiple AI clients
  • Avoidance of Vendor Lock-in - Standardized integration independent of specific services
  • Security - Secure design based on the principle of least privilege
  • Scalability - New tools can be added as needed

What is Claude Code Action?

Claude Code Action is an official action for running Claude AI on GitHub Actions.

Main Features

  • Integration with GitHub Actions - Triggered by issue comments or PR reviews
  • MCP Support - Connect with external tools by configuring an MCP server
  • Flexible Configuration - Control available tools using allowed-tools

Relationship between Claude Code Action and MCP

Claude Code Action functions as an MCP host. It can integrate with various MCP servers through the MCP configuration file (.mcp.json).

How to Configure MCP Integration

From here, I will explain how to actually configure MCP in Claude Code Action.

Prerequisites

The following environment is required:

  • Access permissions to a GitHub repository
  • Anthropic API key (ANTHROPIC_API_KEY)
  • Node.js 18 or higher (may not be required depending on the MCP server)
  • GitHub Personal Access Token (when using the GitHub MCP server)

Step 1: Install the MCP Server

First, install the MCP server you want to use. Example of the GitHub MCP server:

npm install -g @modelcontextprotocol/server-github

Step 2: Create .mcp.json

Create an .mcp.json file in the project root:

{
  "mcpServers": {
    "github": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_PERSONAL_ACCESS_TOKEN}",
        "GITHUB_OWNER": "your-github-username",
        "GITHUB_REPO": "your-repo-name"
      }
    }
  }
}

Configuration points:

  • command - The command to run the MCP server
  • args - Arguments passed to the command (-y installs without confirmation)
  • env - Environment variables (referenced as ${variable_name})

Step 3: Create a GitHub Actions Workflow

Create .github/workflows/claude.yml:

name: Claude Code with MCP

on:
  issue_comment:
    types: [created]
  pull_request_review_comment:
    types: [created]
  issues:
    types: [opened, assigned]
  pull_request_review:
    types: [submitted]

jobs:
  claude:
    # Run only if there is a comment calling Claude
    if: >
      (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
      (github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
      (github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
      issues: write
      id-token: write
      actions: read
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      
      - name: Run Claude Code with MCP
        uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          claude_args: |
            --allowed-tools "mcp__github__get_issue,mcp__github__list_issues,mcp__github__create_issue,mcp__github__update_issue"
            --mcp-config .mcp.json
        env:
          GITHUB_PERSONAL_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Workflow points:

  • on - Configuration of trigger events (Issue comments, PR reviews, etc.)
  • if - Execution condition (runs only when @claude is included)
  • permissions - Explicitly setting required permissions
  • allowed-tools - Specifying the allowed MCP tools

Step 4: Configure Secrets

Configure the following in your GitHub repository under Settings > Secrets and variables > Actions:

  1. ANTHROPIC_API_KEY - Anthropic API key
  2. GITHUB_TOKEN - Automatically provided (no additional setup required)

Practical Usage Examples

Example 1: Having Claude Analyze an Issue

Describe the following in an issue comment:

@claude Analyze the content of this issue and list the necessary tasks.

Claude will use the GitHub MCP server configured in .mcp.json to:

  1. Retrieve the details of the current issue
  2. Search for related issues
  3. Generate a task list and post a comment

Example 2: Automating PR Reviews

In a PR review comment:

@claude Review the code changes in this PR and point out potential issues.

Claude will:

  1. Retrieve the content of the changes in the PR
  2. Analyze the code
  3. Add review comments

Example 3: Combining Multiple MCP Servers

Configure multiple servers in .mcp.json:

{
  "mcpServers": {
    "github": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_PERSONAL_ACCESS_TOKEN}"
      }
    },
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/workspace"]
    }
  }
}

This allows access to both GitHub and the filesystem.

allowed-tools Configuration

MCP tools must be explicitly allowed.

Tool Name Format

mcp__<server_name>__<tool_name>

Primary Tools of the GitHub MCP Server

allowed-tools: |
  mcp__github__get_issue,
  mcp__github__list_issues,
  mcp__github__create_issue,
  mcp__github__update_issue,
  mcp__github__create_pull_request,
  mcp__github__search_code,
  mcp__github__get_file_contents

How to Check Available Tools

You can check available tools in the MCP server's documentation or source code:

Other MCP Server Examples

Terraform MCP

Allows searching for Terraform providers and more:

{
  "mcpServers": {
    "terraform": {
      "command": "docker",
      "args": [
        "run", "-i", "--rm", 
        "hashicorp/terraform-mcp-server:0.3.2"
      ]
    }
  }
}

Filesystem MCP

Access to local files:

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": [
        "-y", 
        "@modelcontextprotocol/server-filesystem",
        "/workspace"
      ]
    }
  }
}

Slack MCP

Slack integration:

{
  "mcpServers": {
    "slack": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-slack"],
      "env": {
        "SLACK_BOT_TOKEN": "${SLACK_BOT_TOKEN}",
        "SLACK_TEAM_ID": "${SLACK_TEAM_ID}"
      }
    }
  }
}

Troubleshooting

Problem 1: MCP Server Fails to Start

Causes:

  • Syntax error in .mcp.json
  • Environment variables not set correctly
  • Insufficient permissions

Solutions:

  1. Verify JSON syntax (use an online validator)
  2. Confirm that Secrets are correctly configured
  3. Check if permissions includes the necessary authority

Problem 2: Tool in allowed-tools Not Found

Causes:

  • Spelling mistake in the tool name
  • The MCP server does not support that tool

Solutions:

  1. Check the tool name (format: mcp__<server_name>__<tool_name>)
  2. Verify available tools in the MCP server's documentation
  3. Check error messages in the GitHub Actions logs

Problem 3: Authentication Error

Causes:

  • API keys or tokens have expired
  • Insufficient permission scopes

Solutions:

  1. Confirm that the Anthropic API key is valid
  2. Verify the permission scopes of the GitHub Personal Access Token
  3. Reissue the token if necessary

Problem 4: GitHub Actions Does Not Run

Causes:

  • Trigger conditions are not met
  • Incorrect path for the workflow file

Solutions:

  1. Verify that the file exists in the .github/workflows/ directory
  2. Check for syntax errors in the YAML
  3. Verify that the trigger condition (if) is correct

Security Best Practices

1. Principle of Least Privilege

Grant only the minimum required permissions:

permissions:
  contents: read  # For read-only access
  pull-requests: write  # Only if writing to PRs is required
  issues: write  # Only if writing to Issues is required

2. Management of Secrets

  • Always save API keys and tokens in GitHub Secrets
  • Do not write actual values in .mcp.json (reference them using ${variable_name})
  • Rotate tokens regularly

3. Restricting allowed-tools

  • Only allow necessary tools
  • Carefully select tools that require write permissions
  • Review the allowed list regularly

4. Checking Audit Logs

  • Regularly review execution logs of GitHub Actions
  • Check for any unusual activity

Summary

This article explained how to achieve MCP integration with Claude Code Action.

Key Points:

  • MCP is a standardized protocol - Uniformly achieves integration between LLMs and external tools
  • Configure via .mcp.json - Centrally manage MCP server settings
  • Control with allowed-tools - Explicitly specify usable tools
  • Security-focused - Principle of least privilege and proper management of Secrets

Next Steps:

  1. Try it out on a small project (start with read-only)
  2. Add tools as needed
  3. Consider creating custom MCP servers

By combining Claude Code Action and MCP, you can significantly automate workflows on GitHub. Give it a try!

Discussion