Skip to content

C# / .NET Debug Session Not Detected - MCP Tools Cannot Attach to Active Debugger #11

@mariogit08

Description

@mariogit08

Labels

bug, C#, .NET, debug-adapter

Description

Summary

While the documentation states that C#/.NET is "✅ Fully Supported", the DebugMCP MCP tools cannot connect to an active C# debug session in VS Code. The tools can add breakpoints to C# files but cannot read variables, evaluate expressions, or perform stepping operations when the debugger is paused.

Environment

  • VS Code Version: 1.106.3 (Build bf9252a2fb45be6893dd8870c0bf37e2e1766d61)
  • DebugMCP Version: 1.0.2 (ozzafar.debugmcpextension)
  • OS: Microsoft Windows 11 Enterprise (Build 22631)
  • C# Extension: ms-dotnettools.csharp v2.100.11
  • Project Type: ASP.NET Core 8.0 Web API
  • Debug Configuration: .NET debugger (coreclr)

MCP Configuration

File: mcp.json

{
  "servers": {
    "debugmcp": {
      "autoApprove": ["*"],
      "disabled": false,
      "timeout": 180,
      "type": "http",
      "url": "http://localhost:3001/sse"
    }
  }
}

Steps to Reproduce

  1. Install DebugMCP extension v1.0.2 in VS Code
  2. Configure MCP server in mcp.json as shown above
  3. Open a C# ASP.NET Core 8.0 project
  4. Set a breakpoint in a C# controller file (e.g., MyController.cs line 56)
  5. Start debugging with F5 (launches .NET CoreCLR debugger)
  6. Trigger an HTTP POST request to hit the breakpoint
  7. Debugger pauses at breakpoint - yellow highlight visible, Variables panel populated
  8. GitHub Copilot AI assistant attempts to use MCP debug tools

Actual Behavior

All MCP debug operations fail with the same error:

Error: Debug session is not ready. Start debugging first and ensure execution is paused.

What works:

  • ✅ MCP server starts successfully on port 3001 (verified with Test-NetConnection)
  • mcp_debugmcp_list_breakpoints() successfully lists C# breakpoints: MyController.cs:56
  • mcp_debugmcp_add_breakpoint() can successfully add new breakpoints to C# files
  • ✅ VS Code's native debug session works perfectly (Variables panel shows all values, Debug Console evaluates expressions, F10/F11 stepping works)

What doesn't work:

  • mcp_debugmcp_get_variables_values(scope: "all") - Returns "Debug session is not ready"
  • mcp_debugmcp_evaluate_expression(expression: "myVariable") - Returns "Debug session is not ready"
  • mcp_debugmcp_step_over() - Returns "Debug session is not ready. Please wait for initialization to complete."
  • mcp_debugmcp_step_into() - Returns "Debug session is not ready"
  • mcp_debugmcp_continue_execution() - Returns "Debug session is not ready"

Expected Behavior

When the .NET CoreCLR debugger is paused at a breakpoint:

  • MCP tools should detect the active debug session
  • Variables should be readable via get_variables_values() (e.g., method parameters, local variables)
  • Expressions should be evaluable via evaluate_expression() (e.g., items?.Count)
  • Stepping operations should work (step_over through lines, step_into method calls)

Comparison with TypeScript/JavaScript

The same MCP tools work perfectly with TypeScript/JavaScript debug sessions in the same workspace:

  • TypeScript test files - Variables readable ✅
  • TypeScript component files - Stepping works ✅
  • JavaScript files - Expression evaluation works ✅

This suggests the issue is specific to the C# debug adapter integration.

Root Cause Hypothesis

The DebugMCP extension appears to integrate well with VS Code's built-in Node.js/JS debugger but is not properly hooking into the C# debug adapter protocol (provided by ms-dotnettools.csharp extension). The ability to add/list breakpoints suggests partial integration exists, but runtime session state access is not working.

Possible technical issues:

  1. Debug adapter session ID detection may only work for Node.js debug adapter
  2. C# debug adapter events (e.g., stopped, continued) may not be subscribed to
  3. Variable scopes request protocol differs between Node.js and C# debug adapters
  4. Debug session initialization detection logic may not account for CoreCLR debugger startup sequence

Code Context

File being debugged: Controllers/ApiController.cs

[HttpPost]
public async Task<IActionResult> PostDataAsync(
    [FromQuery(Name = "filter")] string? filter = null,
    [FromBody] List<DataObject>? items = null,
    CancellationToken cancellationToken = default)
{
    try
    {
        // Validate that items are provided
        if (items == null || items.Count == 0)  // ⬅️ Breakpoint at line 56
        {
            return BadRequest(new Result()
            {
                Success = false,
                Data = new List<Item>()
            });
        }

        // Process items
        var result = await _provider!.ProcessItemsAsync(
            context, 
            filter ?? string.Empty, 
            items,
            cancellationToken: cancellationToken);

        return new ObjectResult(new Result()
        {
            Success = true,
            Data = result
        });
    }
    catch (System.Exception ex)
    {
        _log?.LogError(ex, "Error processing request with {Count} items", items?.Count ?? 0);
        
        return new ObjectResult(new Result()
        {
            Success = false,
            Data = new List<Item>()
        })
        {
            StatusCode = StatusCodes.Status500InternalServerError
        };
    }
}

When paused at line 56:

  • VS Code's Variables panel shows: filter, items, cancellationToken, this
  • VS Code's Debug Console can evaluate: items?.Count, filter, etc.
  • MCP tools cannot access any of this information

Impact

This prevents AI assistants from providing automated debugging assistance for C# projects, which significantly limits the value proposition for .NET developers despite the "Fully Supported" claim in documentation.

The Debug MCP Agent workflow (as documented in the repository) requires:

  1. Variable inspection to understand state
  2. Expression evaluation to test hypotheses
  3. Stepping operations to trace execution flow

None of these work for C#/.NET debugging, making the extension essentially non-functional for the .NET ecosystem.

Additional Context

  • Port 3001 is accessible and responding (Test-NetConnection -Port 3001 confirms TcpTestSucceeded : True)
  • No firewall or network issues detected
  • Extension activates successfully (visible in Extensions panel)
  • Other MCP servers (Playwright MCP, Atlassian MCP) work correctly in the same configuration
  • Restarting VS Code does not resolve the issue
  • Restarting the debug session (Stop + F5) does not resolve the issue
  • Auto-approval is enabled ("autoApprove": ["*"]) to eliminate approval delays

Workaround

Currently, users must manually:

  1. Inspect variables via VS Code's Variables panel
  2. Copy values and paste them into chat with AI assistant
  3. Manually step through code with F10/F11
  4. Share observations with AI assistant for analysis

This defeats the purpose of automated debugging assistance and makes the "Fully Supported" C#/.NET claim misleading.

Potential Solutions to Investigate

  1. Debug Adapter Protocol Implementation: Ensure DebugMCP subscribes to C# debug adapter events (initialize, attach, stopped, continued)
  2. Session Detection Logic: Verify debug session detection supports multiple debug adapter types (not just Node.js pwa-node)
  3. Variable Scopes Protocol: C# debugger may use different scope names (Locals, Arguments vs local, global)
  4. Initialization Sequence: C# debugger may require different timing for "session ready" detection
  5. Debug Adapter Communication: Check if C# debug adapter requires different DAP command formats

Test Case for Validation

If fixed, the following sequence should work:

  1. Set breakpoint at ApiController.cs:56
  2. Start debugging ASP.NET Core app
  3. Send POST request to endpoint
  4. Execution pauses at breakpoint
  5. AI assistant calls: mcp_debugmcp_get_variables_values(scope: "all")
  6. Expected result: Returns JSON with method parameters and local variables
  7. AI assistant calls: mcp_debugmcp_evaluate_expression(expression: "items?.Count")
  8. Expected result: Returns the count value
  9. AI assistant calls: mcp_debugmcp_step_over()
  10. Expected result: Execution moves to next line, variables update

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions