Skip to content

XmlToDescriptionGenerator loses default parameter values when generating partial method declarations #1057

@jongalloway

Description

@jongalloway

The XmlToDescriptionGenerator copies method signatures to generate partial method declarations, but doesn't preserve default parameter values. This breaks calling code that relies on optional parameters.

Reproduction

[McpServerToolType]
public partial class Tools
{
    /// <summary>Test tool</summary>
    [McpServerTool]
    public partial Task<string> TestMethod(
        string? project = null,
        bool flag = false);
}

Generated code

// Defaults are lost
public partial Task<string> TestMethod(
    string? project,    // No longer optional
    bool flag);         // No longer optional

Impact

  • Breaks calling code: TestMethod(flag: true) now requires all preceding parameters
  • Compilation errors in existing codebases after adopting the generator
  • Forces breaking changes to method signatures

Expected behavior

The generator should preserve default parameter values on the generated partial declaration.

Source location

The parameter emission logic doesn't include default values from the original method syntax:
https://github.com/modelcontextprotocol/csharp-sdk/blob/main/src/ModelContextProtocol.Analyzers/XmlToDescriptionGenerator.cs#L329-L356

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions