Add GitHub MCP server and context prefix to agent mode

- Include main GitHub MCP server (Docker-based) by default
- Fetch and prefix GitHub context to prompts when in PR/issue context
- Users no longer need to manually configure GitHub tools
This commit is contained in:
km-anthropic
2025-08-12 11:56:00 -07:00
parent a32b63580e
commit 49514f528d

View File

@@ -2,6 +2,10 @@ import * as core from "@actions/core";
import { mkdir, writeFile } from "fs/promises"; import { mkdir, writeFile } from "fs/promises";
import type { Mode, ModeOptions, ModeResult } from "../types"; import type { Mode, ModeOptions, ModeResult } from "../types";
import type { PreparedContext } from "../../create-prompt/types"; import type { PreparedContext } from "../../create-prompt/types";
import { GITHUB_API_URL, GITHUB_SERVER_URL } from "../../github/api/config";
import { fetchGitHubData } from "../../github/data/fetcher";
import { formatGitHubData } from "../../github/data/formatter";
import { isEntityContext } from "../../github/context";
/** /**
* Agent mode implementation. * Agent mode implementation.
@@ -39,21 +43,42 @@ export const agentMode: Mode = {
return false; return false;
}, },
async prepare({ context, githubToken }: ModeOptions): Promise<ModeResult> { async prepare({ context, githubToken, octokit }: ModeOptions): Promise<ModeResult> {
// Agent mode handles automation events and any event with explicit prompts // Agent mode handles automation events and any event with explicit prompts
console.log(`Agent mode: githubToken provided: ${!!githubToken}, length: ${githubToken?.length || 0}`); console.log(`Agent mode: githubToken provided: ${!!githubToken}, length: ${githubToken?.length || 0}`);
// TODO: handle by createPrompt (similar to tag and review modes)
// Create prompt directory // Create prompt directory
await mkdir(`${process.env.RUNNER_TEMP}/claude-prompts`, { await mkdir(`${process.env.RUNNER_TEMP}/claude-prompts`, {
recursive: true, recursive: true,
}); });
// Write the prompt file - the base action requires a prompt_file parameter.
// Use the unified prompt field from v1.0. // Fetch GitHub context data if we're in an entity context (PR/issue)
const promptContent = let githubContextPrefix = '';
context.inputs.prompt || if (isEntityContext(context)) {
try {
const githubData = await fetchGitHubData({
octokits: octokit,
repository: `${context.repository.owner}/${context.repository.repo}`,
prNumber: context.entityNumber.toString(),
isPR: context.isPR,
triggerUsername: context.actor,
});
// Format the GitHub data into a readable context
const formattedContext = formatGitHubData(githubData);
githubContextPrefix = `## GitHub Context\n\n${formattedContext}\n\n## Your Task\n\n`;
} catch (error) {
console.warn('Failed to fetch GitHub context:', error);
// Continue without GitHub context if fetching fails
}
}
// Write the prompt file with GitHub context prefix
const userPrompt = context.inputs.prompt ||
`Repository: ${context.repository.owner}/${context.repository.repo}`; `Repository: ${context.repository.owner}/${context.repository.repo}`;
const promptContent = githubContextPrefix + userPrompt;
await writeFile( await writeFile(
`${process.env.RUNNER_TEMP}/claude-prompts/claude-prompt.txt`, `${process.env.RUNNER_TEMP}/claude-prompts/claude-prompt.txt`,
promptContent, promptContent,
@@ -62,11 +87,11 @@ export const agentMode: Mode = {
// Agent mode: User has full control via claudeArgs // Agent mode: User has full control via claudeArgs
// No default tools are enforced - Claude Code's defaults will apply // No default tools are enforced - Claude Code's defaults will apply
// Always include the GitHub comment server in agent mode // Include both GitHub comment server and main GitHub MCP server by default
// This ensures GitHub tools (PR reviews, comments, etc.) work out of the box // This ensures comprehensive GitHub tools work out of the box
// without requiring users to manually configure the MCP server
const mcpConfig: any = { const mcpConfig: any = {
mcpServers: { mcpServers: {
// GitHub comment server for updating Claude comments
github_comment: { github_comment: {
command: "bun", command: "bun",
args: [ args: [
@@ -85,6 +110,24 @@ export const agentMode: Mode = {
process.env.GITHUB_API_URL || "https://api.github.com", process.env.GITHUB_API_URL || "https://api.github.com",
}, },
}, },
// Main GitHub MCP server for comprehensive GitHub operations
github: {
command: "docker",
args: [
"run",
"-i",
"--rm",
"-e",
"GITHUB_PERSONAL_ACCESS_TOKEN",
"-e",
"GITHUB_HOST",
"ghcr.io/github/github-mcp-server:sha-efef8ae", // https://github.com/github/github-mcp-server/releases/tag/v0.9.0
],
env: {
GITHUB_PERSONAL_ACCESS_TOKEN: githubToken || "",
GITHUB_HOST: GITHUB_SERVER_URL,
},
},
}, },
}; };