mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 05:51:06 +00:00
* fix(setup): honor Claude Code local managed MCP policy * test(setup): cover Claude project registration under managed MCP * fix(doctor): report Claude name denies before missing config
116 lines
3.7 KiB
TypeScript
116 lines
3.7 KiB
TypeScript
import { ok, type Result } from "../domain/result.js";
|
|
import { PRODUCT_IDENTITY } from "../identity.js";
|
|
import { isRecord } from "../kernel/isRecord.js";
|
|
import type { ArtifactResourceScope } from "../resources/index.js";
|
|
import {
|
|
readConfigurationFile,
|
|
type ClientConfigurationFileError,
|
|
} from "./ClientConfigurationFiles.js";
|
|
|
|
const key = PRODUCT_IDENTITY.mcpServerKey;
|
|
|
|
export interface ClaudeCodeManagedPaths {
|
|
readonly mcp: string;
|
|
readonly settings: string;
|
|
}
|
|
|
|
interface Block {
|
|
readonly path: string;
|
|
readonly reason: string;
|
|
}
|
|
|
|
interface LocalPolicy {
|
|
readonly control?: Block;
|
|
readonly settingsPath?: string;
|
|
readonly denied: readonly (string | readonly string[])[];
|
|
}
|
|
|
|
export const readClaudeCodeMcpPolicy = async (
|
|
paths: ClaudeCodeManagedPaths | undefined,
|
|
resources: ArtifactResourceScope | undefined,
|
|
): Promise<Result<LocalPolicy, ClientConfigurationFileError>> => {
|
|
if (paths === undefined) return ok({ denied: [] });
|
|
const exclusive = await readConfigurationFile(
|
|
paths.mcp,
|
|
"claude_code",
|
|
resources,
|
|
);
|
|
if (!exclusive.ok) return exclusive;
|
|
if (exclusive.value !== undefined)
|
|
return ok({
|
|
control: {
|
|
path: paths.mcp,
|
|
reason: `Claude Code manages MCP through ${paths.mcp}. Use that file to inspect the managed server set. REA leaves this file and the shadowed registration alone during setup.`,
|
|
},
|
|
denied: [],
|
|
});
|
|
|
|
const settings = await readConfigurationFile(
|
|
paths.settings,
|
|
"claude_code",
|
|
resources,
|
|
);
|
|
if (!settings.ok) return settings;
|
|
if (settings.value === undefined) return ok({ denied: [] });
|
|
const { managedMcpServers, deniedMcpServers } = settings.value.document;
|
|
const provided = isRecord(managedMcpServers)
|
|
? managedMcpServers[key]
|
|
: undefined;
|
|
if (
|
|
isRecord(provided) &&
|
|
(provided.type === "http" || provided.type === "sse") &&
|
|
typeof provided.url === "string" &&
|
|
provided.url.length > 0
|
|
)
|
|
return ok({
|
|
control: {
|
|
path: paths.settings,
|
|
reason: `Claude Code provides the ${key} server through managedMcpServers in ${paths.settings}. Inspect that managed entry to see what Claude Code uses.`,
|
|
},
|
|
denied: [],
|
|
});
|
|
|
|
const denied: (string | readonly string[])[] = [];
|
|
if (Array.isArray(deniedMcpServers))
|
|
for (const entry of deniedMcpServers as readonly unknown[]) {
|
|
if (!isRecord(entry) || Object.keys(entry).length !== 1) continue;
|
|
if (typeof entry.serverName === "string") denied.push(entry.serverName);
|
|
else if (
|
|
Array.isArray(entry.serverCommand) &&
|
|
entry.serverCommand.length > 0 &&
|
|
entry.serverCommand.every(
|
|
(arg: unknown): arg is string =>
|
|
typeof arg === "string" && !arg.includes("${"),
|
|
)
|
|
)
|
|
denied.push(entry.serverCommand);
|
|
}
|
|
return ok({ denied, settingsPath: paths.settings });
|
|
};
|
|
|
|
export const claudeCodeMcpBlock = (
|
|
policy: LocalPolicy,
|
|
entry: unknown,
|
|
): Block | undefined => {
|
|
if (policy.control !== undefined) return policy.control;
|
|
if (policy.settingsPath === undefined) return undefined;
|
|
const command =
|
|
isRecord(entry) &&
|
|
typeof entry.command === "string" &&
|
|
(entry.args === undefined || Array.isArray(entry.args))
|
|
? [entry.command, ...(entry.args ?? [])]
|
|
: undefined;
|
|
const blocked = policy.denied.some((rule) =>
|
|
typeof rule === "string"
|
|
? rule === key
|
|
: command !== undefined &&
|
|
rule.length === command.length &&
|
|
rule.every((arg, index) => arg === command[index]),
|
|
);
|
|
if (!blocked) return undefined;
|
|
return {
|
|
path: policy.settingsPath,
|
|
reason: `Claude Code deniedMcpServers in ${policy.settingsPath} blocks the ${key} server. Ask the policy owner to review the matching rule.`,
|
|
};
|
|
};
|