{"record":{"id":"bc2599ce1470a68d","repo":"can1357/oh-my-pi","slug":"server-name-already-exists-in-filepath","errorCode":null,"errorMessage":"Server \"${name}\" already exists in ${filePath}","messagePattern":"Server \"(.+?)\" already exists in (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/mcp/config-writer.ts","lineNumber":131,"sourceCode":"\tif (nameError) {\n\t\tthrow new Error(nameError);\n\t}\n\n\t// Validate the config\n\tconst errors = validateServerConfig(name, config);\n\tif (errors.length > 0) {\n\t\tthrow new Error(`Invalid server config: ${errors.join(\"; \")}`);\n\t}\n\n\t// Serialize the read-modify-write under a per-file lock so a concurrent\n\t// mutation cannot overwrite this one (lost update). The lock also guards\n\t// against cross-process writers sharing the same config file.\n\tawait withConfigLock(filePath, async () => {\n\t\tconst existing = await readMCPConfigFile(filePath);\n\n\t\t// Check for duplicate name\n\t\tif (existing.mcpServers?.[name]) {\n\t\t\tthrow new Error(`Server \"${name}\" already exists in ${filePath}`);\n\t\t}\n\n\t\tconst updated: MCPConfigFile = {\n\t\t\t...existing,\n\t\t\tmcpServers: {\n\t\t\t\t...existing.mcpServers,\n\t\t\t\t[name]: config,\n\t\t\t},\n\t\t};\n\t\tawait writeMCPConfigFile(filePath, updated);\n\t});\n}\n\n/**\n * Update an existing MCP server in a config file.\n * If the server doesn't exist, this will add it.\n *\n * @throws Error if validation fails","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/mcp/config-writer.ts#L113-L149","documentation":"addMCPServer() refuses to create a server entry whose name already exists in the target config file. Inside a per-file config lock it reads the existing config and throws if existing.mcpServers[name] is present, preventing silent overwrite of an existing server definition.","triggerScenarios":"Calling addMCPServer(filePath, name, config) when a server with the same name already exists in that file — e.g. running the add command or completing the wizard twice for the same server name.","commonSituations":"Re-running an install/setup script that adds an MCP server idempotently; accidentally reusing a name like \"filesystem\" that already exists; copying dotfiles between machines where the entry was already merged.","solutions":["Use updateMCPServer(filePath, name, config) to modify an existing entry instead of addMCPServer.","Choose a different, unique server name.","If replacement is intended, call removeMCPServer first, then addMCPServer.","Make scripts idempotent: read the config and skip add when the name already exists."],"exampleFix":"// before\nawait addMCPServer(cfgPath, \"github\", cfg); // throws if already present\n// after\nconst existing = await readMCPConfigFile(cfgPath);\nif (existing.mcpServers?.github) {\n  await updateMCPServer(cfgPath, \"github\", cfg);\n} else {\n  await addMCPServer(cfgPath, \"github\", cfg);\n}","handlingStrategy":"validation","validationCode":"import { readMCPConfigFile } from \"@oh-my-pi/pi-coding-agent/mcp/config\";\nconst cfg = await readMCPConfigFile(filePath);\nif (cfg.mcpServers?.[name]) {\n  await updateMCPServer(filePath, name, config);\n} else {\n  await addMCPServer(filePath, name, config);\n}","typeGuard":"function serverExists(cfg: { mcpServers?: Record<string, unknown> }, name: string): boolean {\n  return Boolean(cfg.mcpServers?.[name]);\n}","tryCatchPattern":"try {\n  await addMCPServer(cfgPath, name, config);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('already exists in')) {\n    await updateMCPServer(cfgPath, name, config); // upsert fallback\n    return;\n  }\n  throw e;\n}","preventionTips":["Implement upsert (check-then-add-or-update) in any script that adds servers.","Use unique, prefixed server names for tooling-managed entries.","Read the current config before programmatic writes.","Handle re-runs: treat 'already exists' as success in idempotent setup scripts."],"tags":["mcp","config","duplicate"],"backgroundTag":"duplicate-config-entry","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}