MCPcopy
hub / github.com/upstash/context7 / appendTomlServer

Function appendTomlServer

packages/cli/src/setup/mcp-writer.ts:250–299  ·  view source on GitHub ↗
(
  filePath: string,
  serverName: string,
  entry: Record<string, unknown>
)

Source from the content-addressed store, hash-verified

248}
249
250export async function appendTomlServer(
251 filePath: string,
252 serverName: string,
253 entry: Record<string, unknown>
254): Promise<{ alreadyExists: boolean }> {
255 const block = buildTomlServerBlock(serverName, entry);
256
257 let existing = "";
258 try {
259 existing = await readFile(filePath, "utf-8");
260 } catch {}
261
262 const sectionHeader = `[mcp_servers.${serverName}]`;
263 const alreadyExists = existing.includes(sectionHeader);
264
265 if (alreadyExists) {
266 const subPrefix = `[mcp_servers.${serverName}.`;
267 const startIdx = existing.indexOf(sectionHeader);
268 const rest = existing.slice(startIdx + sectionHeader.length);
269
270 let endOffset = rest.length;
271 const re = /^\[/gm;
272 let m;
273 while ((m = re.exec(rest)) !== null) {
274 const lineEnd = rest.indexOf("\n", m.index);
275 const line = rest.slice(m.index, lineEnd === -1 ? undefined : lineEnd);
276 if (!line.startsWith(subPrefix)) {
277 endOffset = m.index;
278 break;
279 }
280 }
281
282 const rawBefore = existing.slice(0, startIdx).replace(/\n+$/, "");
283 const rawAfter = existing
284 .slice(startIdx + sectionHeader.length + endOffset)
285 .replace(/^\n+/, "");
286 const before = rawBefore.length > 0 ? rawBefore + "\n\n" : "";
287 const after = rawAfter.length > 0 ? "\n" + rawAfter : "";
288 const content = before + block + after;
289 await mkdir(dirname(filePath), { recursive: true });
290 await writeFile(filePath, content, "utf-8");
291 } else {
292 const separator =
293 existing.length > 0 && !existing.endsWith("\n") ? "\n\n" : existing.length > 0 ? "\n" : "";
294 await mkdir(dirname(filePath), { recursive: true });
295 await writeFile(filePath, existing + separator + block, "utf-8");
296 }
297
298 return { alreadyExists };
299}
300
301export async function removeTomlServer(
302 filePath: string,

Callers 2

setupAgentFunction · 0.85
setup.test.tsFile · 0.85

Calls 2

buildTomlServerBlockFunction · 0.85
execMethod · 0.45

Tested by

no test coverage detected