Skip to content

Conversation

@jongalloway
Copy link
Contributor

@jongalloway jongalloway commented Dec 4, 2025

Resolves compatibility issues with the XML to Description generator when working with partial methods that use async modifiers and default parameter values.

Motivation and Context

The XML to Description generator was producing invalid C# code in two scenarios:

  1. Async methods: Generated partial method declarations included the async keyword, which is invalid for partial declarations and causes CS1994 compiler errors
  2. Default parameter values: The generator was not preserving default parameter values from the original method signatures, leading to signature mismatches between the partial
    declaration and implementation

These issues prevented developers from using the generator with common method
patterns, limiting its utility in real-world scenarios.

How Has This Been Tested?

  • Unit tests: Added comprehensive test coverage (254 new lines) covering all scenarios:
    • Methods with default parameter values (null, boolean, numeric, string literals)

    • Async methods with proper modifier filtering

    • Combined async + default parameter scenarios

    • Edge cases with complex parameter signatures

  • Build verification: Ensured all existing tests continue to pass
  • Code generation validation: Verified generated code compiles without errors for all test scenarios

Breaking Changes

None. This is a backward-compatible bug fix that improves generator output without changing existing APIs or behavior for valid scenarios.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Technical implementation details:

  • Async modifier filtering: Added logic to exclude AsyncKeyword when copying method modifiers to prevent CS1994 compilation errors
  • Default value preservation: Enhanced parameter processing to examine original ParameterSyntax and preserve the Default property in generated signatures
  • Robust parameter handling: Added safety checks for cases where parameter syntax might be missing or malformed

Files modified:

  • src/ModelContextProtocol.Analyzers/XmlToDescriptionGenerator.cs - Core generator logic fixes
  • tests/ModelContextProtocol.Analyzers.Tests/XmlToDescriptionGeneratorTests.cs - Comprehensive test coverage

The fix ensures the generator produces valid, compilable C# code for all common method signature patterns while maintaining backward compatibility.

Fixes #1057 and #1058

- Update XmlToDescriptionGenerator to use partial method syntax for better compatibility
- Add comprehensive tests for XML-to-description generation functionality
Copy link
Contributor

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

Thanks!

@stephentoub stephentoub merged commit ce5bb3c into modelcontextprotocol:main Dec 4, 2025
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XmlToDescriptionGenerator loses default parameter values when generating partial method declarations

2 participants