mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-23 06:54:13 +08:00
98 lines
2.6 KiB
TypeScript
98 lines
2.6 KiB
TypeScript
import * as core from "@actions/core";
|
|
|
|
type PrepareConfigParams = {
|
|
githubToken: string;
|
|
owner: string;
|
|
repo: string;
|
|
branch: string;
|
|
additionalMcpConfig?: string;
|
|
claudeCommentId?: string;
|
|
};
|
|
|
|
export async function prepareMcpConfig(
|
|
params: PrepareConfigParams,
|
|
): Promise<string> {
|
|
const {
|
|
githubToken,
|
|
owner,
|
|
repo,
|
|
branch,
|
|
additionalMcpConfig,
|
|
claudeCommentId,
|
|
} = params;
|
|
try {
|
|
const baseMcpConfig = {
|
|
mcpServers: {
|
|
github: {
|
|
command: "docker",
|
|
args: [
|
|
"run",
|
|
"-i",
|
|
"--rm",
|
|
"-e",
|
|
"GITHUB_PERSONAL_ACCESS_TOKEN",
|
|
"ghcr.io/anthropics/github-mcp-server:sha-7382253",
|
|
],
|
|
env: {
|
|
GITHUB_PERSONAL_ACCESS_TOKEN: githubToken,
|
|
},
|
|
},
|
|
github_file_ops: {
|
|
command: "bun",
|
|
args: [
|
|
"run",
|
|
`${process.env.GITHUB_ACTION_PATH}/src/mcp/github-file-ops-server.ts`,
|
|
],
|
|
env: {
|
|
GITHUB_TOKEN: githubToken,
|
|
REPO_OWNER: owner,
|
|
REPO_NAME: repo,
|
|
BRANCH_NAME: branch,
|
|
REPO_DIR: process.env.GITHUB_WORKSPACE || process.cwd(),
|
|
...(claudeCommentId && { CLAUDE_COMMENT_ID: claudeCommentId }),
|
|
GITHUB_EVENT_NAME: process.env.GITHUB_EVENT_NAME || "",
|
|
IS_PR: process.env.IS_PR || "false",
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
// Merge with additional MCP config if provided
|
|
if (additionalMcpConfig && additionalMcpConfig.trim()) {
|
|
try {
|
|
const additionalConfig = JSON.parse(additionalMcpConfig);
|
|
|
|
// Validate that parsed JSON is an object
|
|
if (typeof additionalConfig !== "object" || additionalConfig === null) {
|
|
throw new Error("MCP config must be a valid JSON object");
|
|
}
|
|
|
|
core.info(
|
|
"Merging additional MCP server configuration with built-in servers",
|
|
);
|
|
|
|
// Merge configurations with user config overriding built-in servers
|
|
const mergedConfig = {
|
|
...baseMcpConfig,
|
|
...additionalConfig,
|
|
mcpServers: {
|
|
...baseMcpConfig.mcpServers,
|
|
...additionalConfig.mcpServers,
|
|
},
|
|
};
|
|
|
|
return JSON.stringify(mergedConfig, null, 2);
|
|
} catch (parseError) {
|
|
core.warning(
|
|
`Failed to parse additional MCP config: ${parseError}. Using base config only.`,
|
|
);
|
|
}
|
|
}
|
|
|
|
return JSON.stringify(baseMcpConfig, null, 2);
|
|
} catch (error) {
|
|
core.setFailed(`Install MCP server failed with error: ${error}`);
|
|
process.exit(1);
|
|
}
|
|
}
|