{"record":{"id":"08f32fe0c74d3b0c","repo":"upstash/context7","slug":"toml-files-containing-multiline-strings-are-not-sa","errorCode":null,"errorMessage":"TOML files containing multiline strings are not safely editable","messagePattern":"TOML files containing multiline strings are not safely editable","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/setup/toml-editor.ts","lineNumber":186,"sourceCode":" * Updates only the `args` value of an existing Context7 stdio TOML entry.\n * Every other byte in the config is preserved. Unsupported `args` syntax\n * throws instead of allowing setup to replace a configuration it cannot read.\n * Returns false only when the requested server table is absent.\n */\nexport async function patchTomlStdioApiKey(\n  filePath: string,\n  serverName: string,\n  apiKey: string | undefined\n): Promise<boolean> {\n  let raw: string;\n  try {\n    raw = await readFile(filePath, \"utf-8\");\n  } catch {\n    return false;\n  }\n\n  if (raw.includes('\"\"\"') || raw.includes(\"'''\")) {\n    throw new Error(\"TOML files containing multiline strings are not safely editable\");\n  }\n\n  const range = findTomlServerArgs(raw, serverName);\n  if (!range) return false;\n\n  const args = range.tokens.map((token) => token.value);\n  if (!args.some(isContext7Package)) {\n    throw new Error(`Existing MCP args do not invoke ${STDIO_PACKAGE}`);\n  }\n\n  const apiKeyIndexes = args.flatMap((arg, index) => (arg === \"--api-key\" ? [index] : []));\n  let content: string;\n  if (apiKey && apiKeyIndexes.length === 1 && apiKeyIndexes[0] + 1 < range.tokens.length) {\n    const valueToken = range.tokens[apiKeyIndexes[0] + 1];\n    content = raw.slice(0, valueToken.start) + JSON.stringify(apiKey) + raw.slice(valueToken.end);\n  } else {\n    const patched = withoutApiKey(args);\n    if (apiKey) patched.push(\"--api-key\", apiKey);","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/upstash/context7/blob/edc9eeb77bbfc553e08a122ca783293fdb477daf/packages/cli/src/setup/toml-editor.ts#L168-L204","documentation":"A whole-file pre-check found triple quotes ('\"\"\"' or \"'''\") anywhere in the TOML. Because the editor rewrites by character offsets, any multiline string in the file makes offsets unsafe, so it refuses to edit rather than corrupt the file.","triggerScenarios":"Running the stdio API-key patch on a config that contains any multiline string, even in an unrelated section (e.g. a long description or certificate block).","commonSituations":"Configs with embedded PEM certificates, long prompts, or templated text stored as multiline strings; shared team configs with comment-like blocks.","solutions":["Convert multiline strings in the file to single-line strings with \\n escapes","Move large blobs (certs, prompts) to a separate file referenced by path","Then re-run the patch/setup command"],"exampleFix":"// before\ndescription = \"\"\"\nlong text\n\"\"\"\n\n// after\ndescription = \"long text\\n\"","handlingStrategy":"validation","validationCode":"if (raw.includes('\"\"\"') || raw.includes(\"'''\")) {\n  throw new Error('TOML files containing multiline strings are not safely editable');\n}","typeGuard":"function isSafelyEditableToml(raw: string): boolean {\n  return !raw.includes('\"\"\"') && !raw.includes(\"'''\");\n}","tryCatchPattern":"try { await patchTomlStdioApiKey(...); } catch (e) { if (/multiline strings are not safely editable/.test(e.message)) { /* convert triple-quoted strings to single-line, then retry */ } throw e; }","preventionTips":["Avoid multiline strings in MCP config files","Store certs/prompts in separate files referenced by path","Pre-check with isSafelyEditableToml before calling the patcher"],"tags":["toml","multiline-string","safe-edit","mcp"],"backgroundTag":"toml-unsupported-construct","analyzedSha":"edc9eeb77bbfc553e08a122ca783293fdb477daf","analyzedAt":"2026-08-28T10:18:41.476Z","contentChangedAt":"2026-08-28T10:18:41.476Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}