microsoft/aspire · error · InvalidOperationException
The configuration file
Error message
The configuration file '{0}' contains malformed JSON. Please fix the file manually and re-run the command. What it means
McpConfigFileHelper.ReadConfigAsync parses an MCP agent configuration file (e.g. mcp.json) with JsonNode.Parse. This error is thrown when the file contains syntactically invalid JSON, with the original JsonException attached as the inner exception. The CLI refuses to proceed so it never corrupts or guesses at a broken config, and asks the user to repair the file by hand.
Solutions
- Open the file named in the message and fix the JSON syntax (check trailing commas, quotes, and braces) — the inner JsonException line/position pinpoints it.
- Validate the file with a JSON linter or `jq . <file>` before re-running the command.
- If unsure of the correct content, delete or rename the broken file and let the CLI regenerate a fresh one, then re-apply your agent entries.
- Restore the file from backup or source control if it was accidentally corrupted.
Example fix
// before
{
"mcpServers": { "fs": { "command": "npx", }
}
// after
{
"mcpServers": { "fs": { "command": "npx" } }
} Defensive patterns
Strategy: validation
Validate before calling
if (!System.Text.Json.JsonDocument.TryParse(File.ReadAllText(mcpJsonPath), out _))
{
// fix JSON syntax before invoking the CLI command
} Try / catch
try
{
await command.ExecuteAsync();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("contains malformed JSON"))
{
// surface ex.InnerException (JsonException) position info to the user
} Prevention
- Validate mcp.json with a JSON linter or `jq . file` after every hand edit.
- Do not hand-write trailing commas or comments in strict-JSON config files.
- Let the CLI regenerate config skeletons instead of editing from scratch.
When it happens
Trigger: Thrown when JsonNode.Parse throws JsonException while parsing the contents of the config file at configFilePath — trailing commas, comments in strict JSON, truncated writes, BOM/encoding issues, or hand-edited syntax errors.
Common situations: Manually editing mcp.json and leaving a trailing comma or unquoted key; a previous crashed command left a truncated file; merging changes with a tool that emitted JSON5/JSONC; copy-pasting config with smart quotes.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- The configuration file
- The configuration file
- The configuration file
- The configuration file
- Failed to parse JSON output into type
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/e925bf764ca78964.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Cli/Agents/McpConfigFileHelper.cs:90
{
return new JsonObject();
}
var content = await File.ReadAllTextAsync(configFilePath, cancellationToken);
if (preprocessContent is not null)
{
content = preprocessContent(content);
}
JsonNode? root;
try
{
root = JsonNode.Parse(content);
}
catch (JsonException ex)
{
throw new InvalidOperationException(
string.Format(CultureInfo.CurrentCulture, AgentCommandStrings.MalformedConfigFileError, configFilePath), ex);
}
return root as JsonObject
?? throw new InvalidOperationException(
string.Format(CultureInfo.CurrentCulture, ErrorStrings.ConfigurationFileMustBeJsonObject, configFilePath));
}
}
View on GitHub (pinned to 25830f84bd)