Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/architecture/agentConfigurationManager.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ For AI agents to use DebugMCP, they need MCP server configuration in their setti
- Detect supported AI agents and their config file paths
- Show post-install popup for agent selection
- Write MCP server configuration to agent settings files
- Preserve existing JSON configuration files when they cannot be parsed
- Handle cross-platform config path differences (Windows, macOS, Linux)
- Track whether onboarding popup has been shown

Expand Down
30 changes: 29 additions & 1 deletion src/test/agentConfigurationManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
// Copyright (c) Microsoft Corporation.

import * as assert from 'assert';
import { upsertCodexDebugMCPConfig } from '../utils/agentConfigurationManager';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import {
upsertCodexDebugMCPConfig,
upsertJsonDebugMCPConfigFile
} from '../utils/agentConfigurationManager';

suite('AgentConfigurationManager JSON configuration', () => {
test('upsertJsonDebugMCPConfigFile should preserve an existing file if it contains malformed JSON', async () => {
const tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'debugmcp-agent-config-'));
const configPath = path.join(tempDir, 'mcp.json');
const malformedConfig = '{\n "servers": {\n "other": true,\n';
await fs.promises.writeFile(configPath, malformedConfig, 'utf8');

try {
await assert.rejects(
upsertJsonDebugMCPConfigFile(configPath, 'servers', {
type: 'streamableHttp',
url: 'http://localhost:3001/mcp'
}),
SyntaxError
);
assert.strictEqual(await fs.promises.readFile(configPath, 'utf8'), malformedConfig);
} finally {
await fs.promises.rm(tempDir, { recursive: true, force: true });
}
});
});

suite('AgentConfigurationManager Codex TOML configuration', () => {
const mcpServerUrl = 'http://localhost:3001/mcp';
Expand Down
65 changes: 41 additions & 24 deletions src/utils/agentConfigurationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ export interface MCPServerConfig {
tools?: string[];
}

export async function upsertJsonDebugMCPConfigFile(
configPath: string,
fieldName: string,
debugMCPConfig: MCPServerConfig
): Promise<void> {
let config: any = {};

if (fs.existsSync(configPath)) {
const configContent = await fs.promises.readFile(configPath, 'utf8');
config = JSON.parse(configContent);
}

if (!config[fieldName]) {
config[fieldName] = {};
}

config[fieldName].debugmcp = debugMCPConfig;
await fs.promises.writeFile(configPath, JSON.stringify(config, null, 2), 'utf8');
}

export function upsertCodexDebugMCPConfig(configContent: string, mcpServerUrl: string): string {
const normalizedConfigContent = configContent.replace(/\r\n/g, '\n');
const lines = normalizedConfigContent.split('\n');
Expand Down Expand Up @@ -528,34 +548,31 @@ export class AgentConfigurationManager {
return { success: true, skillPath };
}

let config: any = {};

// Read existing config if it exists
if (fs.existsSync(agent.configPath)) {
const configContent = await fs.promises.readFile(agent.configPath, 'utf8');
try {
config = JSON.parse(configContent);
} catch (parseError) {
console.warn(`Failed to parse existing config for ${agent.name}, creating new config`);
config = {};
const fieldName = agent.mcpServerFieldName;
try {
await upsertJsonDebugMCPConfigFile(
agent.configPath,
fieldName,
this.getDebugMCPConfig(agent)
);
} catch (error) {
if (!(error instanceof SyntaxError)) {
throw error;
}
}

// Ensure the correct MCP servers object exists for this agent
const fieldName = agent.mcpServerFieldName;
if (!config[fieldName]) {
config[fieldName] = {};
}
console.error(`Failed to parse existing config for ${agent.name}:`, error);
const openConfigButton = 'Open Config';
const result = await vscode.window.showErrorMessage(
`Failed to configure DebugMCP for ${agent.displayName}: the existing configuration contains invalid JSON.`,
openConfigButton
);

// Add or update DebugMCP configuration with current settings
config[fieldName].debugmcp = this.getDebugMCPConfig(agent);
if (result === openConfigButton) {
await vscode.commands.executeCommand('vscode.open', vscode.Uri.file(agent.configPath));
}

// Write the updated config back to file
await fs.promises.writeFile(
agent.configPath,
JSON.stringify(config, null, 2),
'utf8'
);
return { success: false, skillPath: null };
}

console.log(`Successfully added DebugMCP configuration to ${agent.name}`);
const skillPath = await this.installDebugMCPSkill();
Expand Down