@forge/cli
14.2.015.0.0-next.23
out/service/skills-mcp-config.js+
out/service/skills-mcp-config.jsNew file+212
Index: package/out/service/skills-mcp-config.js
===================================================================
--- package/out/service/skills-mcp-config.js
+++ package/out/service/skills-mcp-config.js
@@ -0,0 +1,212 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.setForgeMcpServers = setForgeMcpServers;
+exports.removeForgeMcpServers = removeForgeMcpServers;
+exports.hasForgeMcpServers = hasForgeMcpServers;
+const crypto_1 = require("crypto");
+const fs_1 = require("fs");
+const path_1 = require("path");
+const util_1 = require("util");
+const jsonc_parser_1 = require("jsonc-parser");
+const toml_eslint_parser_1 = require("toml-eslint-parser");
+const cli_shared_1 = require("@forge/cli-shared");
+const skills_agents_1 = require("./skills-agents");
+const skills_download_1 = require("./skills-download");
+const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
+function setForgeMcpServers(agent, scope, servers, { dryRun = false, ...options } = {}) {
+ const files = skills_agents_1.SKILLS_AGENT_REGISTRY[agent][scope].mcp;
+ if (!files.length)
+ return undefined;
+ const file = files.find((candidate) => (0, fs_1.existsSync)((0, skills_agents_1.resolveAgentPath)(candidate.path, options))) ?? files[0];
+ return apply(agent, scope, readDocument(file, options), servers, dryRun);
+}
+function removeForgeMcpServers(agent, scope, { dryRun = false, ...options } = {}) {
+ return skills_agents_1.SKILLS_AGENT_REGISTRY[agent][scope].mcp
+ .filter((file) => (0, fs_1.existsSync)((0, skills_agents_1.resolveAgentPath)(file.path, options)))
+ .map((file) => apply(agent, scope, readDocument(file, options), undefined, dryRun));
+}
+function hasForgeMcpServers(agent, scope, options = {}) {
+ return skills_agents_1.SKILLS_AGENT_REGISTRY[agent][scope].mcp.some((file) => {
+ try {
+ const document = readDocument(file, options);
+ return document.exists && isObject(serverMap(document, serverPath(agent, scope, document))?.forge);
+ }
+ catch {
+ return false;
+ }
+ });
+}
+function readDocument(file, options) {
+ const path = (0, skills_agents_1.resolveAgentPath)(file.path, options);
+ const exists = (0, fs_1.existsSync)(path);
+ const raw = exists ? (0, fs_1.readFileSync)(path, 'utf8') : '';
+ const bom = raw.startsWith('\uFEFF');
+ const text = bom ? raw.slice(1) : raw;
+ let config;
+ try {
+ if (file.format === 'toml') {
+ config = (0, toml_eslint_parser_1.getStaticTOMLValue)((0, toml_eslint_parser_1.parseTOML)(text));
+ }
+ else {
+ const errors = [];
+ config = (0, jsonc_parser_1.parse)(text.trim() ? text : '{}', errors, { allowTrailingComma: file.format === 'jsonc' });
+ if (errors.length)
+ config = undefined;
+ }
+ }
+ catch {
+ config = undefined;
+ }
+ if (!isObject(config) || (Object.hasOwn(config, file.serverKey) && !isObject(config[file.serverKey]))) {
+ throw new cli_shared_1.UserError(cli_shared_1.Text.skills.errors.invalidMcpConfig(path));
+ }
+ return { file, path, exists, bom, text, config };
+}
+function serverPath(agent, scope, document) {
+ const key = document.file.serverKey;
+ const bare = agent === skills_agents_1.SkillsAgent.COPILOT &&
+ scope === 'project' &&
+ !Object.hasOwn(document.config, key) &&
+ Object.entries(document.config).some(([name, value]) => name !== '$schema' && isObject(value) && (typeof value.url === 'string' || typeof value.command === 'string'));
+ return bare ? [] : [key];
+}
+const serverMap = (document, prefix) => (prefix.length ? document.config[prefix[0]] : document.config);
+function serverEntry(agent, server) {
+ const headers = server.headers ? { headers: server.headers } : {};
+ switch (agent) {
+ case skills_agents_1.SkillsAgent.CODEX:
+ return { url: server.url, ...(server.headers ? { http_headers: server.headers } : {}) };
+ case skills_agents_1.SkillsAgent.GEMINI:
+ return { httpUrl: server.url, ...headers };
+ case skills_agents_1.SkillsAgent.CURSOR:
+ return { url: server.url, ...headers };
+ default:
+ return { type: 'http', url: server.url, ...headers };
+ }
+}
+function apply(agent, scope, document, servers, dryRun) {
+ const prefix = serverPath(agent, scope, document);
+ const { changes, replacing } = serverChanges(agent, serverMap(document, prefix), servers);
+ const action = changeAction(changes, servers, replacing);
+ if (action !== 'unchanged' && !dryRun) {
+ const text = document.file.format === 'toml' ? editToml(document.text, changes) : editJson(document, prefix, changes);
+ writeDocument(document, text);
+ }
+ return { path: document.path, action };
+}
+function serverChanges(agent, existing, servers) {
+ const changes = {};
+ let replacing = false;
+ for (const name of skills_download_1.FORGE_MCP_SERVER_NAMES) {
+ const present = existing !== undefined && Object.hasOwn(existing, name);
+ const desired = servers && serverEntry(agent, servers[name]);
+ const unchanged = desired ? present && (0, util_1.isDeepStrictEqual)(existing[name], desired) : !present;
+ if (unchanged)
+ continue;
+ changes[name] = desired;
+ replacing ||= present;
+ }
+ return { changes, replacing };
+}
+function changeAction(changes, servers, replacing) {
+ if (!Object.keys(changes).length)
+ return 'unchanged';
+ if (!servers)
+ return 'removed';
+ return replacing ? 'updated' : 'added';
+}
+function editJson(document, prefix, changes) {
+ const eol = document.text.includes('\r\n') ? '\r\n' : '\n';
+ let text = document.text.trim() ? document.text : '{}';
+ const formattingOptions = { insertSpaces: true, tabSize: 2, eol };
+ for (const [name, value] of Object.entries(changes).reverse()) {
+ text = (0, jsonc_parser_1.applyEdits)(text, (0, jsonc_parser_1.modify)(text, [...prefix, name], value, { formattingOptions, getInsertionIndex: () => 0 }));
+ }
+ return text;
+}
+function writeDocument(document, text) {
+ const target = document.exists ? (0, fs_1.realpathSync)(document.path) : document.path;
+ if (document.exists)
+ (0, fs_1.accessSync)(target, fs_1.constants.W_OK);
+ const mode = document.exists ? (0, fs_1.statSync)(target).mode & 0o777 : 0o600;
+ (0, fs_1.mkdirSync)((0, path_1.dirname)(target), { recursive: true });
+ const temporary = `${target}.${(0, crypto_1.randomUUID)()}.tmp`;
+ try {
+ (0, fs_1.writeFileSync)(temporary, document.bom ? `\uFEFF${text}` : text, { mode });
+ (0, fs_1.chmodSync)(temporary, mode);
+ (0, fs_1.renameSync)(temporary, target);
+ }
+ finally {
+ (0, fs_1.rmSync)(temporary, { force: true });
+ }
+}
+const tomlKeyParts = (key) => key.keys.map((part) => (part.type === 'TOMLBare' ? part.name : part.value));
+function tomlValue(value) {
+ if (typeof value === 'string') {
+ return JSON.stringify(value);
+ }
+ if (isObject(value)) {
+ return `{ ${Object.entries(value)
+ .map(([key, entry]) => `${JSON.stringify(key)} = ${tomlValue(entry)}`)
+ .join(', ')} }`;
+ }
+ throw new Error('Unsupported managed TOML value');
+}
+function managedTomlNodes(node, changes) {
+ const managed = (key) => Object.hasOwn(changes, key ?? '');
+ if (node.type === 'TOMLTable') {
+ if (node.resolvedKey[0] !== 'mcp_servers')
+ return [];
+ if (managed(node.resolvedKey[1]))
+ return [node];
+ return node.resolvedKey.length === 1 ? node.body.filter((entry) => managed(tomlKeyParts(entry.key)[0])) : [];
+ }
+ const keys = tomlKeyParts(node.key);
+ return keys[0] === 'mcp_servers' && managed(keys[1]) ? [node] : [];
+}
+function editToml(source, changes) {
+ const ast = (0, toml_eslint_parser_1.parseTOML)(source);
+ const edits = [];
+ let inlineRoot;
+ const managed = (key) => Object.hasOwn(changes, key ?? '');
+ const remove = (node) => edits.push({ start: node.range[0], end: node.range[1], text: '' });
+ for (const node of ast.body[0].body) {
+ managedTomlNodes(node, changes).forEach(remove);
+ if (node.type === 'TOMLKeyValue') {
+ const keys = tomlKeyParts(node.key);
+ if (keys.length === 1 && keys[0] === 'mcp_servers' && node.value.type === 'TOMLInlineTable') {
+ inlineRoot = node.value;
+ }
+ }
+ }
+ if (inlineRoot) {
+ const entries = inlineRoot.body
+ .filter((entry) => !managed(tomlKeyParts(entry.key)[0]))
+ .map((entry) => source.slice(...entry.range));
+ entries.push(...skills_download_1.FORGE_MCP_SERVER_NAMES.filter((name) => changes[name]).map((name) => `${JSON.stringify(name)} = ${tomlValue(changes[name])}`));
+ edits.push({ start: inlineRoot.range[0], end: inlineRoot.range[1], text: `{ ${entries.join(', ')} }` });
+ }
+ let result = source;
+ edits.sort((left, right) => right.start - left.start);
+ for (const edit of edits) {
+ result = result.slice(0, edit.start) + edit.text + result.slice(edit.end);
+ }
+ if (inlineRoot)
+ return result;
+ const eol = source.includes('\r\n') ? '\r\n' : '\n';
+ return appendTomlServers(result, changes, eol);
+}
+function appendTomlServers(source, changes, eol) {
+ let result = source;
+ for (const name of skills_download_1.FORGE_MCP_SERVER_NAMES) {
+ const entry = changes[name];
+ if (!entry)
+ continue;
+ const values = Object.entries(entry)
+ .map(([key, value]) => `${key} = ${tomlValue(value)}`)
+ .join(eol);
+ result += `${eol}[mcp_servers.${JSON.stringify(name)}]${eol}${values}${eol}`;
+ }
+ return result;
+}
+//# sourceMappingURL=skills-mcp-config.js.map
\ No newline at end of file