Skip to content

Unqualified parameter types in generated partial tool signatures cause build failures #1134

@jongalloway

Description

@jongalloway

Describe the bug
When using ModelContextProtocol.Analyzers.XmlToDescriptionGenerator with MCP server tool methods that are partial, the generator emits a generated partial method declaration that references parameter types using unqualified type names (e.g., MyAction) even when those types live in another namespace (e.g., MyApp.Actions.MyAction). The generated file does not add the needed using directives (and does not use global::), so the generated code fails to compile (e.g., CS0246). This is easy to hit with “consolidated tool” patterns where action enums are defined in a separate namespace.

Concrete examples from our repo where this surfaced:

To Reproduce
Steps to reproduce the behavior:

  1. Create a new .NET project and reference the MCP server SDK + analyzers (version doesn’t matter as long as it includes XmlToDescriptionGenerator; we’re using ModelContextProtocol 0.5.0-preview.1).
  2. Define an enum in a different namespace than the MCP tool class.
namespace MyApp.Actions;

public enum MyAction
{
    One,
    Two
}
  1. Define a tool type in another namespace with a partial MCP tool method taking the enum type.
using ModelContextProtocol.Server;

namespace MyApp;

[McpServerToolType]
public sealed partial class Tools
{
    /// <summary>Do a thing based on an action.</summary>
    /// <param name="action">The action to perform.</param>
    [McpServerTool]
    public async partial Task<string> DoThing(MyApp.Actions.MyAction action)
        => await Task.FromResult("ok");
}
  1. Build the project.

Expected behavior
The project should build successfully. Generated source should compile without requiring consumers to add extra global using directives or move their parameter types into the same namespace as the tool class.

Logs
Build fails with errors similar to:

error CS0246: The type or namespace name 'MyAction' could not be found (are you missing a using directive or an assembly reference?)
error CS0759: No defining declaration found for implementing declaration of partial method 'Tools.DoThing(MyAction)'

(Exact wording/locations vary, but the key signal is that generated code references MyAction unqualified.)

Additional context

  • This is distinct from issue CS1066 warnings for MCP tool methods with optional parameters #1109 / PR Add CS1066 suppressor for MCP server methods with optional parameters #1110 (CS1066 warning suppression). That fix adds a diagnostic suppressor for CS1066 warnings and does not address type qualification in generated partial method signatures.
  • A robust generator-side fix would be to emit fully-qualified parameter type names with global:: in generated code (including arrays/generics/nullability), or to add the necessary using directives to the generated file.
  • Current workaround on the consumer side is to add a GlobalUsings.cs with global using MyApp.Actions;, but this feels brittle and surprising for a source generator.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions