-
Notifications
You must be signed in to change notification settings - Fork 600
Description
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:
- Add consolidated dotnet_workload tool with enum-based action routing jongalloway/dotnet-mcp#222
- Add consolidated dotnet_solution tool with action-based routing jongalloway/dotnet-mcp#221
To Reproduce
Steps to reproduce the behavior:
- Create a new .NET project and reference the MCP server SDK + analyzers (version doesn’t matter as long as it includes
XmlToDescriptionGenerator; we’re usingModelContextProtocol0.5.0-preview.1). - Define an enum in a different namespace than the MCP tool class.
namespace MyApp.Actions;
public enum MyAction
{
One,
Two
}- Define a tool type in another namespace with a
partialMCP 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");
}- 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 necessaryusingdirectives to the generated file. - Current workaround on the consumer side is to add a
GlobalUsings.cswithglobal using MyApp.Actions;, but this feels brittle and surprising for a source generator.