mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 14:05:49 +00:00
* fix(mcp): share registration environment admission Refs #12123 * docs(registration): explain the resulting behavior
210 lines
6.7 KiB
TypeScript
210 lines
6.7 KiB
TypeScript
import { ConfigurationError } from "../domain/configurationErrors.js";
|
|
import type { ClientRegistrationCommand } from "../domain/clientRegistrationCommand.js";
|
|
import { PRODUCT_IDENTITY } from "../identity.js";
|
|
import type { Result } from "../domain/result.js";
|
|
import {
|
|
clientConfigurationValuesEqual,
|
|
clientServerPath,
|
|
clientServerForEdit,
|
|
legacyClientServerPath,
|
|
effectiveClientServer,
|
|
parseClientConfiguration,
|
|
serializeClientConfiguration,
|
|
withClientServers,
|
|
type ClientConfigurationDocument,
|
|
} from "./ClientConfigurationDocument.js";
|
|
import {
|
|
admitClientConfigurationCandidate,
|
|
clientConfigurationForWrite,
|
|
mergeClientConfigurationDocuments,
|
|
readClientConfigurationFiles,
|
|
type ClientConfigurationFileError,
|
|
} from "./ClientConfigurationFiles.js";
|
|
import { replaceClientRegistrationLauncher } from "./ClientRegistration.js";
|
|
import { readClientPolicyBlock } from "./GeminiClientSettings.js";
|
|
import { admitClientStdioConnection } from "./ClientRegistrationConnection.js";
|
|
import type { McpRegistrationClient } from "./McpRegistrationCatalog.js";
|
|
import { readRegularFileTextSnapshot } from "../process/RegularFileRead.js";
|
|
|
|
const isMissing = (cause: unknown): boolean =>
|
|
cause instanceof Error && "code" in cause && cause.code === "ENOENT";
|
|
|
|
const registrationLocation = (client: McpRegistrationClient) => ({
|
|
...client.setupClient,
|
|
...client,
|
|
name: client.setupClient?.name ?? client.id,
|
|
});
|
|
|
|
const requireAdmittedConfiguration = <T>(
|
|
result: Result<T, ClientConfigurationFileError>,
|
|
): T => {
|
|
if (!result.ok)
|
|
throw (
|
|
result.error.cleanupFailure ?? new ConfigurationError(result.error.detail)
|
|
);
|
|
return result.value;
|
|
};
|
|
|
|
/**
|
|
* Merge REA's entry into one selected client document. `platform` is the host
|
|
* the catalog resolved the client for; it decides the document's environment
|
|
* name rules.
|
|
*/
|
|
export const prepareRegistration = async (
|
|
path: string,
|
|
client: McpRegistrationClient,
|
|
launcher: ClientRegistrationCommand,
|
|
platform: NodeJS.Platform,
|
|
) => {
|
|
const snapshot = await readRegularFileTextSnapshot(path, {
|
|
symlinks: "reject",
|
|
}).catch((cause: unknown) => {
|
|
if (isMissing(cause)) return undefined;
|
|
throw cause;
|
|
});
|
|
const original = snapshot?.text;
|
|
const empty = serializeClientConfiguration({}, client.format);
|
|
if (!empty.ok)
|
|
throw new ConfigurationError(
|
|
"An empty client configuration could not be serialized.",
|
|
);
|
|
const target = parseClientConfiguration(
|
|
original ?? empty.value,
|
|
client.format,
|
|
);
|
|
const location = registrationLocation(client);
|
|
const layers = requireAdmittedConfiguration(
|
|
await readClientConfigurationFiles(location, undefined, platform, {
|
|
targetDocument: target,
|
|
}),
|
|
);
|
|
const parsed = clientConfigurationForWrite(target, layers);
|
|
const targetIndex = layers.findLastIndex(
|
|
({ sourcePath }) => sourcePath === client.configPath,
|
|
);
|
|
const inherited = mergeClientConfigurationDocuments(
|
|
layers.slice(0, targetIndex),
|
|
platform,
|
|
);
|
|
const desired = replaceClientRegistrationLauncher(
|
|
{ dialect: parsed.dialect, profile: client.registrationProfile },
|
|
[launcher.command, ...launcher.args],
|
|
clientServerForEdit(parsed, PRODUCT_IDENTITY.mcpServerKey),
|
|
inherited === undefined
|
|
? undefined
|
|
: effectiveClientServer(inherited, PRODUCT_IDENTITY.mcpServerKey),
|
|
);
|
|
if (!desired.ok) throw desired.error;
|
|
// The client's own settings policy can exclude or override this entry.
|
|
if (client.setupClient !== undefined) {
|
|
const policy = await readClientPolicyBlock(
|
|
client.setupClient,
|
|
parsed,
|
|
desired.value,
|
|
);
|
|
if (!policy.ok)
|
|
throw (
|
|
policy.error.cleanupFailure ??
|
|
new ConfigurationError(policy.error.detail)
|
|
);
|
|
if (policy.value !== undefined)
|
|
throw new ConfigurationError(`${policy.value} No settings were written.`);
|
|
}
|
|
const updated = withClientServers(
|
|
parsed,
|
|
{
|
|
...parsed.servers,
|
|
[PRODUCT_IDENTITY.mcpServerKey]: desired.value,
|
|
},
|
|
platform,
|
|
PRODUCT_IDENTITY.mcpServerKey,
|
|
);
|
|
const legacy = legacyClientServerPath(parsed, PRODUCT_IDENTITY.mcpServerKey);
|
|
const serialized = serializeClientConfiguration(
|
|
updated,
|
|
client.format,
|
|
original,
|
|
[
|
|
clientServerPath(parsed, PRODUCT_IDENTITY.mcpServerKey),
|
|
...(legacy === undefined ? [] : [legacy]),
|
|
],
|
|
{ removeShadowedMembersAt: legacy === undefined ? [] : [legacy] },
|
|
);
|
|
if (!serialized.ok)
|
|
throw new ConfigurationError(
|
|
`Client configuration member is shadowed at ${serialized.error.path.join(".")}.`,
|
|
);
|
|
const candidate = parseClientConfiguration(serialized.value, client.format);
|
|
const effective = requireAdmittedConfiguration(
|
|
admitClientConfigurationCandidate(location, layers, candidate, platform),
|
|
);
|
|
assertEffectiveLauncher(client, effective, launcher);
|
|
return {
|
|
path,
|
|
client,
|
|
launcher,
|
|
platform,
|
|
snapshot,
|
|
original,
|
|
parsed,
|
|
updated,
|
|
contents: serialized.value,
|
|
};
|
|
};
|
|
|
|
const assertEffectiveLauncher = (
|
|
client: McpRegistrationClient,
|
|
effective: ClientConfigurationDocument | undefined,
|
|
launcher: ClientRegistrationCommand,
|
|
): void => {
|
|
const admitted = admitClientStdioConnection(
|
|
effective === undefined
|
|
? undefined
|
|
: effectiveClientServer(effective, PRODUCT_IDENTITY.mcpServerKey),
|
|
{
|
|
dialect: effective?.dialect ?? client.format,
|
|
profile: client.registrationProfile,
|
|
},
|
|
);
|
|
if (
|
|
!admitted.ok ||
|
|
admitted.value.command !== launcher.command ||
|
|
!clientConfigurationValuesEqual(admitted.value.args, launcher.args)
|
|
)
|
|
throw new ConfigurationError(
|
|
admitted.ok
|
|
? "A higher-priority configuration overrides the selected stdio launcher. Update that source before registering this command."
|
|
: admitted.error.cause.message,
|
|
{
|
|
...(admitted.ok ? {} : { cause: admitted.error.cause }),
|
|
diagnostics: {
|
|
settings_path: client.configPath,
|
|
...(client.inlineConfiguration === undefined
|
|
? {}
|
|
: { inline_origin: client.inlineConfiguration.origin }),
|
|
},
|
|
},
|
|
);
|
|
};
|
|
|
|
/** Re-admit native layers around the caller's identity-verified target snapshot. */
|
|
export const verifyEffectiveRegistration = async (
|
|
prepared: Awaited<ReturnType<typeof prepareRegistration>>,
|
|
targetText: string,
|
|
): Promise<void> => {
|
|
const { client, launcher, platform } = prepared;
|
|
const layers = requireAdmittedConfiguration(
|
|
await readClientConfigurationFiles(
|
|
registrationLocation(client),
|
|
undefined,
|
|
platform,
|
|
{ targetDocument: parseClientConfiguration(targetText, client.format) },
|
|
),
|
|
);
|
|
assertEffectiveLauncher(
|
|
client,
|
|
mergeClientConfigurationDocuments(layers, platform),
|
|
launcher,
|
|
);
|
|
};
|