refactor(plugins): share MCP server map extraction

This commit is contained in:
Peter Steinberger 2026-03-17 04:07:28 +00:00
parent 08d120e706
commit 45cb02b1dd
2 changed files with 3 additions and 26 deletions

View File

@ -5,43 +5,20 @@ import type { OpenClawConfig } from "../../config/config.js";
import { applyMergePatch } from "../../config/merge-patch.js";
import type { CliBackendConfig } from "../../config/types.js";
import {
extractMcpServerMap,
loadEnabledBundleMcpConfig,
type BundleMcpConfig,
type BundleMcpServerConfig,
} from "../../plugins/bundle-mcp.js";
import { isRecord } from "../../utils.js";
type PreparedCliBundleMcpConfig = {
backend: CliBackendConfig;
cleanup?: () => Promise<void>;
};
function extractServerMap(raw: unknown): Record<string, BundleMcpServerConfig> {
if (!isRecord(raw)) {
return {};
}
const nested = isRecord(raw.mcpServers)
? raw.mcpServers
: isRecord(raw.servers)
? raw.servers
: raw;
if (!isRecord(nested)) {
return {};
}
const result: Record<string, BundleMcpServerConfig> = {};
for (const [serverName, serverRaw] of Object.entries(nested)) {
if (!isRecord(serverRaw)) {
continue;
}
result[serverName] = { ...serverRaw };
}
return result;
}
async function readExternalMcpConfig(configPath: string): Promise<BundleMcpConfig> {
try {
const raw = JSON.parse(await fs.readFile(configPath, "utf-8")) as unknown;
return { mcpServers: extractServerMap(raw) };
return { mcpServers: extractMcpServerMap(raw) };
} catch {
return { mcpServers: {} };
}

View File

@ -105,7 +105,7 @@ function resolveBundleMcpConfigPaths(params: {
return mergeUniquePathLists(defaults, declared);
}
function extractMcpServerMap(raw: unknown): Record<string, BundleMcpServerConfig> {
export function extractMcpServerMap(raw: unknown): Record<string, BundleMcpServerConfig> {
if (!isRecord(raw)) {
return {};
}