MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / writeMcpEntry

Function writeMcpEntry

src/installer/targets/opencode.ts:216–264  ·  view source on GitHub ↗
(loc: Location)

Source from the content-addressed store, hash-verified

214}
215
216function writeMcpEntry(loc: Location): WriteResult['files'][number] {
217 const file = configPath(loc);
218 const existed = fs.existsSync(file);
219 let text = readConfigText(file);
220
221 // Seed a minimal opencode config when the file is brand-new so
222 // the result is a complete, schema-tagged file (not just a bare
223 // `{ "mcp": {...} }`).
224 if (!text.trim()) {
225 text = '{\n "$schema": "https://opencode.ai/config.json"\n}\n';
226 }
227
228 const config = parseConfig(text);
229 const before = config.mcp?.servers?.codegraph;
230 const after = getOpencodeServerEntry();
231 const hasLegacy = !!config.mcp?.codegraph;
232
233 // Native entry already matches and no v1 leftover → nothing to do.
234 if (jsonDeepEqual(before, after) && !hasLegacy) {
235 return { path: file, action: 'unchanged' };
236 }
237
238 // Add $schema if the user's existing file is missing it.
239 if (!config.$schema) {
240 const schemaEdits = modify(text, ['$schema'], 'https://opencode.ai/config.json', {
241 formattingOptions: FORMATTING,
242 });
243 text = applyEdits(text, schemaEdits);
244 }
245
246 // Migrate pre-#1698 `mcp.codegraph` (+ enabled) off the file so OpenCode 2
247 // keeps only the native entry where `codemode` survives normalization.
248 if (hasLegacy) {
249 const legacyEdits = modify(text, ['mcp', 'codegraph'], undefined, {
250 formattingOptions: FORMATTING,
251 });
252 text = applyEdits(text, legacyEdits);
253 }
254
255 // Surgical edit — preserves comments, formatting, and order of
256 // every key we don't touch.
257 const edits = modify(text, ['mcp', 'servers', 'codegraph'], after, {
258 formattingOptions: FORMATTING,
259 });
260 const updated = applyEdits(text, edits);
261 atomicWriteFileSync(file, updated);
262
263 return { path: file, action: existed ? 'updated' : 'created' };
264}
265
266/**
267 * Surgically drop our CodeGraph entry from one config file — either the

Callers 1

installMethod · 0.70

Calls 7

jsonDeepEqualFunction · 0.90
atomicWriteFileSyncFunction · 0.90
getOpencodeServerEntryFunction · 0.85
applyEditsFunction · 0.85
configPathFunction · 0.70
readConfigTextFunction · 0.70
parseConfigFunction · 0.70

Tested by

no test coverage detected