add-mcp
Version:
Add MCP servers to your favorite coding agents with a single command.
88 lines (86 loc) • 2.69 kB
JavaScript
import {
agents,
detectGlobalAgents,
detectProjectAgents,
getAgentTypes,
getConfigKey,
getConfigPath2 as getConfigPath,
getNestedValue,
installServerForAgent,
listInstalledServers,
readConfig,
removeOpenCodeServer,
removeServerFromConfig,
rewriteCopilotCliConfig
} from "./chunk-JZK2SME6.js";
// src/lib.ts
import { existsSync } from "fs";
function isKnownAgent(agentType) {
return Object.prototype.hasOwnProperty.call(agents, agentType);
}
function validate(agentType, local) {
if (!isKnownAgent(agentType)) {
return `Unknown agent type: ${String(agentType)}`;
}
if (local && !agents[agentType].localConfigPath) {
return `${agentType} does not support project-level config`;
}
return null;
}
function upsertServer(agentType, serverName, serverConfig, options = {}) {
const error = validate(agentType, options.local);
if (error) return { success: false, path: "", error };
return installServerForAgent(
serverName,
serverConfig,
agentType,
options
);
}
function hasServer(serversObj, serverName) {
return !!serversObj && typeof serversObj === "object" && !Array.isArray(serversObj) && Object.prototype.hasOwnProperty.call(serversObj, serverName);
}
function doRemove(agentType, serverName, options, configPath) {
const agent = agents[agentType];
if (!existsSync(configPath)) {
return { success: true, path: configPath, removed: false };
}
if (agentType === "opencode") {
const removed = removeOpenCodeServer(configPath, serverName);
return { success: true, path: configPath, removed };
}
const configKey = getConfigKey(agent, options);
const fullConfig = readConfig(configPath, agent.format);
if (!hasServer(getNestedValue(fullConfig, configKey), serverName)) {
return { success: true, path: configPath, removed: false };
}
removeServerFromConfig(configPath, agent.format, configKey, serverName);
rewriteCopilotCliConfig(agentType, configPath);
return { success: true, path: configPath, removed: true };
}
function removeServer(agentType, serverName, options = {}) {
const error = validate(agentType, options.local);
if (error) return { success: false, path: "", removed: false, error };
const known = agentType;
let configPath = "";
try {
configPath = getConfigPath(agents[known], options);
return doRemove(known, serverName, options, configPath);
} catch (e) {
return {
success: false,
path: configPath,
removed: false,
error: e instanceof Error ? e.message : "Unknown error"
};
}
}
export {
agents,
detectGlobalAgents,
detectProjectAgents,
getAgentTypes,
listInstalledServers,
removeServer,
upsertServer
};