{"record":{"id":"1e144dd12e6c7d4f","repo":"can1357/oh-my-pi","slug":"nameerror-dynamic-message-from-validateservername","errorCode":null,"errorMessage":"nameError (dynamic message from validateServerName, e.g. \"Server name cannot be empty\")","messagePattern":"nameError \\(dynamic message from validateServerName, e\\.g\\. \"Server name cannot be empty\"\\)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/mcp/config-writer.ts","lineNumber":114,"sourceCode":"\t// sanitize them via createMCPToolName) and `/mcp reauth` writes such names back\n\t// as a user-config override that shadows the discovered entry.\n\tif (!/^[a-zA-Z0-9_.:-]+$/.test(name)) {\n\t\treturn \"Server name can only contain letters, numbers, dash, underscore, dot, and colon\";\n\t}\n\treturn undefined;\n}\n\n/**\n * Add an MCP server to a config file.\n * Validates the config before writing.\n *\n * @throws Error if server name already exists or validation fails\n */\nexport async function addMCPServer(filePath: string, name: string, config: MCPServerConfig): Promise<void> {\n\t// Validate server name\n\tconst nameError = validateServerName(name);\n\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}","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/mcp/config-writer.ts#L96-L132","documentation":"addMCPServer validates the requested server name via validateServerName before writing config; a failing name (empty, >100 chars, or containing characters outside [a-zA-Z0-9_.:-]) becomes the thrown Error message directly (e.g. 'Server name cannot be empty', 'Server name is too long (max 100 characters)', 'Server name can only contain letters, numbers, dash, underscore, dot, and colon').","triggerScenarios":"Calling addMCPServer(filePath, name, config) — directly or via #handleWizardComplete / handleAddCommand — with an empty name, a name over 100 characters, or one containing spaces/slashes/unicode outside the allowed set.","commonSituations":"Wizard submitted with a blank name field; server name copied from a URL containing '/' or '?'; names with spaces from display labels; pasting a fully-qualified command path as the server name.","solutions":["Provide a non-empty name using only letters, numbers, dash, underscore, dot, and colon.","Shorten the name to ≤100 characters.","Sanitize the name in the calling wizard/command before invoking addMCPServer (call validateServerName yourself for early feedback).","Use a short id like 'my-server-1' rather than a label or path."],"exampleFix":"// before\nawait addMCPServer(path, \"My MCP Server / v2\", config);\n// after\nawait addMCPServer(path, \"my-mcp-server-v2\", config);","handlingStrategy":"validation","validationCode":"import { validateServerName } from \"./config-writer\";\nfunction assertValidName(name: string): void {\n  const err = validateServerName(name);\n  if (err) throw new Error(`Pre-check: ${err}`);\n}","typeGuard":"function isValidServerName(name: string): boolean {\n  return name.length > 0 && name.length <= 100 && /^[a-zA-Z0-9_.:-]+$/.test(name);\n}","tryCatchPattern":"try {\n  await addMCPServer(path, name, config);\n} catch (err) {\n  if (err instanceof Error && /^(Server name cannot be empty|Server name is too long|Server name can only contain)/.test(err.message)) {\n    console.error(`${err.message}; pick a short id like 'my-server-1'`);\n    return;\n  }\n  throw err;\n}","preventionTips":["Call validateServerName in UI/wizard code before submitting the name.","Normalize names: trim whitespace, replace spaces with dashes.","Reject empty input early in forms and commands.","Document the allowed charset (letters, numbers, dash, underscore, dot, colon) next to the name field."],"tags":["mcp","validation","naming","configuration"],"backgroundTag":"invalid-server-name","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}