upstash/context7 · error

Unterminated TOML literal string in MCP args

Error message

Unterminated TOML literal string in MCP args

What it means

Thrown by parseTomlLiteralString when a single-quoted TOML literal string has no closing ' before end of input. Literal strings have no escapes, so the very next ' must terminate the string; if indexOf finds none, the string is unterminated and the parser throws.

Solutions

  1. Add the missing closing single quote.
  2. Escape the apostrophe problem by switching to a double-quoted basic string: "don't panic".
  3. Check preceding entries for unbalanced quotes that shifted where strings start and end.
  4. Run a TOML validator over the file to pinpoint the unbalanced quote.

Example fix

// before (config.toml)
args = ['don't panic']
// after
args = ["don't panic"]
Defensive patterns

Strategy: validation

Validate before calling

function balancedSingleQuotes(s) { return (s.match(/'/g) || []).length % 2 === 0; }
if (!balancedSingleQuotes(tomlSource)) console.warn('unbalanced single quotes in TOML');

Try / catch

try { parseConfig(toml); } catch (e) { if (e.message.includes('Unterminated TOML literal string')) { console.error("A '...' string is missing its closing quote — switch to double quotes for values with apostrophes"); } else throw e; }

Prevention

When it happens

Trigger: Parsing MCP config args where a '...' string is missing its closing quote, e.g. args = ['--flag, or an apostrophe used inside the value was itself a quote character terminating the string early and leaving a later quote unterminated.

Common situations: Writing a value containing an apostrophe (e.g. 'don't panic') inside a literal string; hand-editing the config and dropping a closing quote; copy-paste truncation.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of upstash/context7@4416fb855b (2026-09-16). Data as JSON: /api/errors/2517f68d2028c59d. Report an issue: GitHub.

Appendix: source

Thrown at packages/cli/src/setup/toml-editor.ts:74

    const width = escape === "u" ? 4 : 8;
    const hex = source.slice(index, index + width);
    if (!new RegExp(`^[0-9A-Fa-f]{${width}}$`).test(hex)) {
      throw new Error(`Invalid TOML Unicode escape \\${escape}${hex}`);
    }
    const codePoint = Number.parseInt(hex, 16);
    if (codePoint > 0x10ffff || (codePoint >= 0xd800 && codePoint <= 0xdfff)) {
      throw new Error(`Invalid TOML Unicode code point U+${hex}`);
    }
    value += String.fromCodePoint(codePoint);
    index += width;
  }
  throw new Error("Unterminated TOML basic string in MCP args");
}

function parseTomlLiteralString(source: string, start: number): TomlStringToken {
  const endQuote = source.indexOf("'", start + 1);
  if (endQuote === -1) throw new Error("Unterminated TOML literal string in MCP args");
  const value = source.slice(start + 1, endQuote);
  if (value.includes("\n") || value.includes("\r")) {
    throw new Error("Multiline strings are not supported in MCP args");
  }
  return { value, start, end: endQuote + 1 };
}

function parseTomlStringArray(
  source: string,
  start: number
): { tokens: TomlStringToken[]; end: number } {
  if (source[start] !== "[") throw new Error("Expected a TOML array for MCP args");

  const tokens: TomlStringToken[] = [];
  let index = start + 1;
  while (index < source.length) {
    index = skipTomlArrayTrivia(source, index);
    if (source[index] === "]") return { tokens, end: index + 1 };

View on GitHub (pinned to 4416fb855b)