Skip to content

Handle malformed MCP JSON config files gracefully#14537

Merged
mitchdenny merged 7 commits intorelease/13.2from
fix/handle-malformed-mcp-json
Feb 23, 2026
Merged

Handle malformed MCP JSON config files gracefully#14537
mitchdenny merged 7 commits intorelease/13.2from
fix/handle-malformed-mcp-json

Conversation

@mitchdenny
Copy link
Copy Markdown
Member

Summary

When aspire mcp init or aspire agent init encounters an MCP config file (e.g., .vscode/mcp.json) containing empty or malformed JSON, the CLI previously crashed with an unhandled JsonReaderException.

Changes

  • 4 agent environment scanners (VsCode, CopilotCli, ClaudeCode, OpenCode): Wrapped JsonNode.Parse() calls in Apply*ConfigurationAsync methods with try-catch for JsonException. On malformed JSON, a descriptive InvalidOperationException is thrown with the file path.
  • AgentInitCommand: Catches InvalidOperationException from ApplyAsync and displays a user-friendly error message, then continues with remaining applicators.
  • Resource string: Added MalformedConfigFileError to AgentCommandStrings for the error message.
  • Tests: Added 3 test cases in VsCodeAgentEnvironmentScannerTests covering malformed JSON, empty files, and verifying the file is not overwritten.

Key design decision

The malformed file is NOT overwritten. The user may have valuable content they accidentally broke (e.g., a missing quote). The error message tells them which file has invalid JSON so they can fix it manually.

Fixes #14394

Copilot AI review requested due to automatic review settings February 18, 2026 02:05
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 18, 2026

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 14537

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 14537"

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds graceful error handling for malformed or empty JSON configuration files in the Aspire CLI's agent initialization commands (aspire mcp init and aspire agent init). Previously, encountering malformed JSON in MCP configuration files (e.g., .vscode/mcp.json) would crash the CLI with an unhandled JsonReaderException.

Changes:

  • Added error handling to wrap JsonNode.Parse() calls in Apply*ConfigurationAsync methods across 4 agent environment scanners with descriptive error messages
  • Modified AgentInitCommand to catch InvalidOperationException and display user-friendly error messages while continuing with remaining applicators
  • Added localized error message resource string MalformedConfigFileError in English with pending translations for 13 languages

Reviewed changes

Copilot reviewed 20 out of 21 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Aspire.Cli/Agents/VsCode/VsCodeAgentEnvironmentScanner.cs Added try-catch blocks around JsonNode.Parse in ApplyAspireMcpConfigurationAsync and ApplyPlaywrightMcpConfigurationAsync
src/Aspire.Cli/Agents/CopilotCli/CopilotCliAgentEnvironmentScanner.cs Added try-catch blocks around JsonNode.Parse in ApplyMcpConfigurationAsync and ApplyPlaywrightMcpConfigurationAsync
src/Aspire.Cli/Agents/ClaudeCode/ClaudeCodeAgentEnvironmentScanner.cs Added try-catch blocks around JsonNode.Parse in ApplyAspireMcpConfigurationAsync and ApplyPlaywrightMcpConfigurationAsync
src/Aspire.Cli/Agents/OpenCode/OpenCodeAgentEnvironmentScanner.cs Added try-catch blocks around JsonNode.Parse in ApplyMcpConfigurationAsync and ApplyPlaywrightMcpConfigurationAsync
src/Aspire.Cli/Commands/AgentInitCommand.cs Added try-catch for InvalidOperationException in applicator loop to display errors and continue
src/Aspire.Cli/Resources/AgentCommandStrings.resx Added MalformedConfigFileError resource string
src/Aspire.Cli/Resources/AgentCommandStrings.Designer.cs Generated property accessor for MalformedConfigFileError
src/Aspire.Cli/Resources/xlf/*.xlf (13 files) Added MalformedConfigFileError entries marked as "new" (pending translation)
tests/Aspire.Cli.Tests/Agents/VsCodeAgentEnvironmentScannerTests.cs Added 3 test cases covering malformed JSON, empty files, and file preservation
Files not reviewed (1)
  • src/Aspire.Cli/Resources/AgentCommandStrings.Designer.cs: Language not supported

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 18, 2026

🎬 CLI E2E Test Recordings

The following terminal recordings are available for commit 887216b:

Test Recording
AddPackageInteractiveWhileAppHostRunningDetached ▶️ View Recording
AddPackageWhileAppHostRunningDetached ▶️ View Recording
AgentCommands_AllHelpOutputs_AreCorrect ▶️ View Recording
AgentInitCommand_MigratesDeprecatedConfig ▶️ View Recording
AgentInitCommand_WithMalformedMcpJson_ShowsErrorAndExitsNonZero ▶️ View Recording
AspireUpdateRemovesAppHostPackageVersionFromDirectoryPackagesProps ▶️ View Recording
Banner_DisplayedOnFirstRun ▶️ View Recording
Banner_DisplayedWithExplicitFlag ▶️ View Recording
CreateAndDeployToDockerCompose ▶️ View Recording
CreateAndDeployToDockerComposeInteractive ▶️ View Recording
CreateAndPublishToKubernetes ▶️ View Recording
CreateAndRunAspireStarterProject ▶️ View Recording
CreateAndRunAspireStarterProjectWithBundle ▶️ View Recording
CreateAndRunJsReactProject ▶️ View Recording
CreateAndRunPythonReactProject ▶️ View Recording
CreateEmptyAppHostProject ▶️ View Recording
CreateStartAndStopAspireProject ▶️ View Recording
CreateStartWaitAndStopAspireProject ▶️ View Recording
CreateTypeScriptAppHostWithViteApp ▶️ View Recording
DoctorCommand_DetectsDeprecatedAgentConfig ▶️ View Recording
DoctorCommand_WithSslCertDir_ShowsTrusted ▶️ View Recording
DoctorCommand_WithoutSslCertDir_ShowsPartiallyTrusted ▶️ View Recording
LogsCommandShowsResourceLogs ▶️ View Recording
PsCommandListsRunningAppHost ▶️ View Recording
ResourcesCommandShowsRunningResources ▶️ View Recording
StagingChannel_ConfigureAndVerifySettings_ThenSwitchChannels ▶️ View Recording
StopAllAppHostsFromAppHostDirectory ▶️ View Recording
StopAllAppHostsFromUnrelatedDirectory ▶️ View Recording
StopNonInteractiveMultipleAppHostsShowsError ▶️ View Recording
StopNonInteractiveSingleAppHost ▶️ View Recording
StopWithNoRunningAppHostExitsSuccessfully ▶️ View Recording

📹 Recordings uploaded automatically from CI run #22322998782

Copy link
Copy Markdown
Member

@JamesNK JamesNK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t like the duplication. There should a shared method for reading MCP file.

@dotnet-policy-service dotnet-policy-service bot added the needs-author-action An issue or pull request that requires more info or actions from the author. label Feb 18, 2026
@JamesNK
Copy link
Copy Markdown
Member

JamesNK commented Feb 18, 2026

What does the user see after this change? Review showed a success message after catching error.

Mitch Denny and others added 6 commits February 24, 2026 07:05
When 'aspire mcp init' or 'aspire agent init' encounters an MCP config
file (e.g., .vscode/mcp.json) containing empty or malformed JSON, the
CLI previously crashed with an unhandled JsonReaderException.

This change wraps JsonNode.Parse() calls in try-catch blocks in all four
agent environment scanner Apply methods (VsCode, CopilotCli, ClaudeCode,
OpenCode). On malformed JSON, a descriptive InvalidOperationException is
thrown with the file path, which is caught by AgentInitCommand and
displayed as a user-friendly error. The malformed file is NOT
overwritten, preserving the user's content so they can fix it manually.

Fixes #14394

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… files

When a malformed JSON config file is encountered, now displays:
- The error message identifying the file
- An explicit 'Skipping update of ...' message
- Returns a non-zero exit code (1)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Adds AgentInitCommand_WithMalformedMcpJson_ShowsErrorAndExitsNonZero
which:
1. Creates a .vscode folder with a malformed mcp.json
2. Runs 'aspire agent init' and selects VS Code configuration
3. Verifies the error message about malformed JSON appears
4. Verifies the 'Skipping' message appears
5. Verifies the command exits with non-zero exit code
6. Verifies the original malformed file was NOT overwritten

Also adds WaitForErrorPrompt and CreateMalformedMcpConfig helpers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The agent init command shows two multi-select prompts:
1. Agent environments (VS Code, OpenCode, Claude Code)
2. Additional options (skill files, Playwright)

The test was missing handling for the second prompt, causing a
timeout. Spectre.Console MultiSelectionPrompt requires at least
one selection, so we select the first skill file option.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Move duplicated JSON config file reading and server-check logic
from 4 scanner files into a shared McpConfigFileHelper class with
two methods:

- HasServerConfigured: sync read + parse + check (for Has* methods)
- ReadConfigAsync: async read + parse with error handling (for Apply*)

Both accept an optional preprocessContent delegate for JSONC support
(used by OpenCode scanner). This removes ~360 lines of duplicated
boilerplate across VsCode, CopilotCli, ClaudeCode, and OpenCode
scanners.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
When some applicators fail due to malformed JSON config files, display a
warning message 'Configuration completed with errors' instead of silently
returning a non-zero exit code. The success message is only shown when all
applicators succeed.

Also update the E2E test to verify the new warning message is displayed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mitchdenny mitchdenny force-pushed the fix/handle-malformed-mcp-json branch from 32e1f2b to 6145644 Compare February 23, 2026 20:07
…nners

Address review feedback to add test coverage for malformed JSON handling
across all agent environment scanners, not just VsCode. Each scanner now
has tests for malformed JSON, empty files, and file preservation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mitchdenny
Copy link
Copy Markdown
Member Author

/deployment-test

@dotnet-policy-service dotnet-policy-service bot removed the needs-author-action An issue or pull request that requires more info or actions from the author. label Feb 23, 2026
@github-actions
Copy link
Copy Markdown
Contributor

🚀 Deployment tests starting on PR #14537...

This will deploy to real Azure infrastructure. Results will be posted here when complete.

View workflow run

@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions github-actions bot temporarily deployed to deployment-testing February 23, 2026 20:21 Inactive
@github-actions
Copy link
Copy Markdown
Contributor

Deployment E2E Tests passed

Summary: 23 passed, 0 failed, 0 cancelled

View workflow run

Passed Tests

  • ✅ AksStarterWithRedisDeploymentTests
  • ✅ AcaCustomRegistryDeploymentTests
  • ✅ AcaCompactNamingUpgradeDeploymentTests
  • ✅ AcaExistingRegistryDeploymentTests
  • ✅ AcaStarterDeploymentTests
  • ✅ AuthenticationTests
  • ✅ AppServiceReactDeploymentTests
  • ✅ AcaCompactNamingDeploymentTests
  • ✅ AppServicePythonDeploymentTests
  • ✅ AzureContainerRegistryDeploymentTests
  • ✅ AksStarterDeploymentTests
  • ✅ AzureAppConfigDeploymentTests
  • ✅ AzureEventHubsDeploymentTests
  • ✅ AzureStorageDeploymentTests
  • ✅ AzureLogAnalyticsDeploymentTests
  • ✅ AzureKeyVaultDeploymentTests
  • ✅ VnetStorageBlobConnectivityDeploymentTests
  • ✅ VnetKeyVaultConnectivityDeploymentTests
  • ✅ VnetStorageBlobInfraDeploymentTests
  • ✅ AzureServiceBusDeploymentTests
  • ✅ VnetSqlServerInfraDeploymentTests
  • ✅ VnetKeyVaultInfraDeploymentTests
  • ✅ PythonFastApiDeploymentTests

🎬 Terminal Recordings

Test Recording
DeployAzureAppConfigResource ▶️ View Recording
DeployAzureContainerRegistryResource ▶️ View Recording
DeployAzureEventHubsResource ▶️ View Recording
DeployAzureKeyVaultResource ▶️ View Recording
DeployAzureLogAnalyticsResource ▶️ View Recording
DeployAzureServiceBusResource ▶️ View Recording
DeployAzureStorageResource ▶️ View Recording
DeployPythonFastApiTemplateToAzureAppService ▶️ View Recording
DeployPythonFastApiTemplateToAzureContainerApps ▶️ View Recording
DeployReactTemplateToAzureAppService ▶️ View Recording
DeployStarterTemplateToAks ▶️ View Recording
DeployStarterTemplateToAzureContainerApps ▶️ View Recording
DeployStarterTemplateWithCustomRegistry ▶️ View Recording
DeployStarterTemplateWithExistingRegistry ▶️ View Recording
DeployStarterTemplateWithKeyVaultPrivateEndpoint ▶️ View Recording
DeployStarterTemplateWithRedisToAks ▶️ View Recording
DeployStarterTemplateWithStorageBlobPrivateEndpoint ▶️ View Recording
DeployVnetKeyVaultInfrastructure ▶️ View Recording
DeployVnetSqlServerInfrastructure ▶️ View Recording
DeployVnetStorageBlobInfrastructure ▶️ View Recording
DeployWithCompactNamingFixesStorageCollision ▶️ View Recording
UpgradeFromGaToDevDoesNotDuplicateStorageAccounts ▶️ View Recording

@mitchdenny
Copy link
Copy Markdown
Member Author

image

@mitchdenny
Copy link
Copy Markdown
Member Author

Also:
image

@mitchdenny mitchdenny enabled auto-merge (squash) February 23, 2026 21:42
@mitchdenny mitchdenny merged commit c258da6 into release/13.2 Feb 23, 2026
373 checks passed
@mitchdenny mitchdenny deleted the fix/handle-malformed-mcp-json branch February 23, 2026 21:52
@dotnet-policy-service dotnet-policy-service bot added this to the 13.2 milestone Feb 23, 2026
return new JsonObject();
}

var content = await File.ReadAllTextAsync(configFilePath, cancellationToken);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a chance of a race condition here where the file get's deleted right after the above check? Should this be wrapped in a try catch?


try
{
var content = File.ReadAllText(configFilePath);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as the one bellow. Is it possible to have a race condition here?

Copilot AI pushed a commit that referenced this pull request Mar 10, 2026
* Handle malformed MCP JSON config files gracefully

When 'aspire mcp init' or 'aspire agent init' encounters an MCP config
file (e.g., .vscode/mcp.json) containing empty or malformed JSON, the
CLI previously crashed with an unhandled JsonReaderException.

This change wraps JsonNode.Parse() calls in try-catch blocks in all four
agent environment scanner Apply methods (VsCode, CopilotCli, ClaudeCode,
OpenCode). On malformed JSON, a descriptive InvalidOperationException is
thrown with the file path, which is caught by AgentInitCommand and
displayed as a user-friendly error. The malformed file is NOT
overwritten, preserving the user's content so they can fix it manually.

Fixes #14394

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add explicit skip message and non-zero exit code for malformed config files

When a malformed JSON config file is encountered, now displays:
- The error message identifying the file
- An explicit 'Skipping update of ...' message
- Returns a non-zero exit code (1)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add E2E test for malformed MCP JSON config handling

Adds AgentInitCommand_WithMalformedMcpJson_ShowsErrorAndExitsNonZero
which:
1. Creates a .vscode folder with a malformed mcp.json
2. Runs 'aspire agent init' and selects VS Code configuration
3. Verifies the error message about malformed JSON appears
4. Verifies the 'Skipping' message appears
5. Verifies the command exits with non-zero exit code
6. Verifies the original malformed file was NOT overwritten

Also adds WaitForErrorPrompt and CreateMalformedMcpConfig helpers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Fix E2E test: handle additional options prompt in agent init

The agent init command shows two multi-select prompts:
1. Agent environments (VS Code, OpenCode, Claude Code)
2. Additional options (skill files, Playwright)

The test was missing handling for the second prompt, causing a
timeout. Spectre.Console MultiSelectionPrompt requires at least
one selection, so we select the first skill file option.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Extract shared McpConfigFileHelper to reduce duplication

Move duplicated JSON config file reading and server-check logic
from 4 scanner files into a shared McpConfigFileHelper class with
two methods:

- HasServerConfigured: sync read + parse + check (for Has* methods)
- ReadConfigAsync: async read + parse with error handling (for Apply*)

Both accept an optional preprocessContent delegate for JSONC support
(used by OpenCode scanner). This removes ~360 lines of duplicated
boilerplate across VsCode, CopilotCli, ClaudeCode, and OpenCode
scanners.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Display partial success warning when agent init encounters errors

When some applicators fail due to malformed JSON config files, display a
warning message 'Configuration completed with errors' instead of silently
returning a non-zero exit code. The success message is only shown when all
applicators succeed.

Also update the E2E test to verify the new warning message is displayed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add malformed JSON tests for CopilotCli, ClaudeCode, and OpenCode scanners

Address review feedback to add test coverage for malformed JSON handling
across all agent environment scanners, not just VsCode. Each scanner now
has tests for malformed JSON, empty files, and file preservation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Mitch Denny <mitch@mitchdeny.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions bot locked and limited conversation to collaborators Mar 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

aspire mcp init doesn't work

4 participants