Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions DotNetMcp.Tests/Server/McpConformanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,174 @@ public async Task Server_DotnetProject_ShouldWorkWithTaskStore()

#endregion

#region OutputSchema Declaration Tests

[Fact]
public async Task Server_ToolList_StructuredTools_HaveOutputSchema()
{
// Arrange
Assert.NotNull(_client);

// These tools declare UseStructuredContent = true + OutputSchemaType so clients
// know the JSON response shape at discovery time without calling the tool first.
var toolsWithOutputSchema = new[]
{
"dotnet_server_capabilities",
"dotnet_server_metrics",
"dotnet_solution",
"dotnet_package",
"dotnet_project",
"dotnet_sdk",
};

// Act
var tools = await _client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);

// Assert
foreach (var toolName in toolsWithOutputSchema)
{
var tool = tools.FirstOrDefault(t => t.Name == toolName);
Assert.NotNull(tool);
Assert.True(
tool.ProtocolTool.OutputSchema.HasValue,
$"{toolName} should have OutputSchema declared for AI orchestration");

// The schema should be a non-empty JSON object
var schemaJson = tool.ProtocolTool.OutputSchema!.Value.ToString();
Assert.NotEmpty(schemaJson);
Assert.Contains("properties", schemaJson);
}
}

[Fact]
public async Task Server_ToolList_NonStructuredTools_DoNotHaveOutputSchema()
{
// Arrange
Assert.NotNull(_client);

// These tools do not produce structured content so they should not declare OutputSchema
var toolsWithoutOutputSchema = new[]
{
"dotnet_ef",
"dotnet_workload",
"dotnet_tool",
"dotnet_dev_certs",
"dotnet_help",
"dotnet_server_info",
};

// Act
var tools = await _client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);

// Assert
foreach (var toolName in toolsWithoutOutputSchema)
{
var tool = tools.FirstOrDefault(t => t.Name == toolName);
Assert.NotNull(tool);
Assert.False(
tool.ProtocolTool.OutputSchema.HasValue,
$"{toolName} should not have OutputSchema (it does not produce structured content)");
}
}

[Fact]
public async Task Server_ServerCapabilities_OutputSchema_ContainsExpectedFields()
{
// Arrange
Assert.NotNull(_client);

// Act
var tools = await _client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.FirstOrDefault(t => t.Name == "dotnet_server_capabilities");

// Assert
Assert.NotNull(tool);
Assert.True(tool.ProtocolTool.OutputSchema.HasValue);

var schemaJson = tool.ProtocolTool.OutputSchema!.Value.ToString();
Assert.Contains("serverVersion", schemaJson);
Assert.Contains("protocolVersion", schemaJson);
Assert.Contains("sdkVersions", schemaJson);
}

[Fact]
public async Task Server_DotnetProject_OutputSchema_ContainsBuildResultFields()
{
// Arrange
Assert.NotNull(_client);

// Act
var tools = await _client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.FirstOrDefault(t => t.Name == "dotnet_project");

// Assert
Assert.NotNull(tool);
Assert.True(tool.ProtocolTool.OutputSchema.HasValue);

var schemaJson = tool.ProtocolTool.OutputSchema!.Value.ToString();
Assert.Contains("success", schemaJson);
Assert.Contains("summary", schemaJson);
Assert.Contains("errorCount", schemaJson);
}

[Fact]
public async Task Server_DotnetSdk_OutputSchema_ContainsSdkFields()
{
// Arrange
Assert.NotNull(_client);

// Act
var tools = await _client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.FirstOrDefault(t => t.Name == "dotnet_sdk");

// Assert
Assert.NotNull(tool);
Assert.True(tool.ProtocolTool.OutputSchema.HasValue);

var schemaJson = tool.ProtocolTool.OutputSchema!.Value.ToString();
Assert.Contains("version", schemaJson);
Assert.Contains("sdks", schemaJson);
Assert.Contains("runtimes", schemaJson);
}

[Fact]
public async Task Server_DotnetSolution_OutputSchema_ContainsProjectsField()
{
// Arrange
Assert.NotNull(_client);

// Act
var tools = await _client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.FirstOrDefault(t => t.Name == "dotnet_solution");

// Assert
Assert.NotNull(tool);
Assert.True(tool.ProtocolTool.OutputSchema.HasValue);

var schemaJson = tool.ProtocolTool.OutputSchema!.Value.ToString();
Assert.Contains("projects", schemaJson);
}

[Fact]
public async Task Server_DotnetPackage_OutputSchema_ContainsPackagesField()
{
// Arrange
Assert.NotNull(_client);

// Act
var tools = await _client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.FirstOrDefault(t => t.Name == "dotnet_package");

// Assert
Assert.NotNull(tool);
Assert.True(tool.ProtocolTool.OutputSchema.HasValue);

var schemaJson = tool.ProtocolTool.OutputSchema!.Value.ToString();
Assert.Contains("packages", schemaJson);
}

#endregion

#region MCP Logging Notification Tests

[Fact]
Expand Down
130 changes: 130 additions & 0 deletions DotNetMcp/Helpers/StructuredContentTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System.Text.Json.Serialization;

namespace DotNetMcp;

/// <summary>
/// Represents a single installed .NET SDK entry (version + installation path).
/// </summary>
public sealed class InstalledSdkInfo
{
/// <summary>SDK version string (e.g. "10.0.100").</summary>
[JsonPropertyName("version")]
public string Version { get; init; } = string.Empty;

/// <summary>Installation path of the SDK.</summary>
[JsonPropertyName("path")]
public string? Path { get; init; }
}

/// <summary>
/// Represents a single installed .NET runtime entry.
/// </summary>
public sealed class InstalledRuntimeInfo
{
/// <summary>Runtime display name (e.g. "Microsoft.NETCore.App").</summary>
[JsonPropertyName("name")]
public string Name { get; init; } = string.Empty;

/// <summary>Runtime version string (e.g. "10.0.0").</summary>
[JsonPropertyName("version")]
public string Version { get; init; } = string.Empty;

/// <summary>Installation path of the runtime.</summary>
[JsonPropertyName("path")]
public string? Path { get; init; }
}

/// <summary>
/// Structured result for the dotnet_sdk ListSdks action.
/// The <c>runtimes</c> array is also included when available so SDK dashboard clients
/// can render both from a single tool-result notification.
/// </summary>
public sealed class SdkListResult
{
/// <summary>All installed .NET SDKs.</summary>
[JsonPropertyName("sdks")]
public InstalledSdkInfo[] Sdks { get; init; } = [];

/// <summary>
/// All installed .NET runtimes, included alongside SDKs for dashboard rendering.
/// <c>null</c> when the runtime fetch fails.
/// </summary>
[JsonPropertyName("runtimes")]
public InstalledRuntimeInfo[]? Runtimes { get; init; }
}

/// <summary>
/// Structured result for the dotnet_sdk ListRuntimes action.
/// </summary>
public sealed class RuntimeListResult
{
/// <summary>All installed .NET runtimes.</summary>
[JsonPropertyName("runtimes")]
public InstalledRuntimeInfo[] Runtimes { get; init; } = [];
}

/// <summary>
/// Union output schema for the dotnet_sdk tool.
/// Each field corresponds to the structured content returned by a specific action:
/// <list type="bullet">
/// <item><term>version</term><description>Returned by the Version action.</description></item>
/// <item><term>sdks</term><description>Returned by the ListSdks action.</description></item>
/// <item><term>runtimes</term><description>Returned by both ListSdks and ListRuntimes actions.</description></item>
/// </list>
/// Only the relevant fields are populated for each action; all others are absent.
/// </summary>
public sealed class SdkActionResult
{
/// <summary>SDK version string. Present for the Version action.</summary>
[JsonPropertyName("version")]
public string? Version { get; init; }

/// <summary>Installed SDKs. Present for the ListSdks action.</summary>
[JsonPropertyName("sdks")]
public InstalledSdkInfo[]? Sdks { get; init; }

/// <summary>Installed runtimes. Present for ListSdks and ListRuntimes actions.</summary>
[JsonPropertyName("runtimes")]
public InstalledRuntimeInfo[]? Runtimes { get; init; }
}

/// <summary>
/// Structured result for the dotnet_solution List action.
/// </summary>
public sealed class SolutionListResult
{
/// <summary>
/// Paths of all projects in the solution.
/// Paths are relative to the solution file directory, as reported by <c>dotnet solution list</c>.
/// </summary>
[JsonPropertyName("projects")]
public string[] Projects { get; init; } = [];
}

/// <summary>
/// Metadata for a single NuGet package as reported by <c>dotnet list package</c>.
/// </summary>
public sealed class PackageInfo
{
/// <summary>Package ID (e.g. "Newtonsoft.Json").</summary>
[JsonPropertyName("name")]
public string Name { get; init; } = string.Empty;

/// <summary>Requested version constraint from the project file (e.g. "13.0.1").</summary>
[JsonPropertyName("requestedVersion")]
public string? RequestedVersion { get; init; }

/// <summary>Resolved (actual) version installed (e.g. "13.0.3").</summary>
[JsonPropertyName("resolvedVersion")]
public string? ResolvedVersion { get; init; }
}

/// <summary>
/// Structured result for the dotnet_package List action.
/// </summary>
public sealed class PackageListResult
{
/// <summary>NuGet packages referenced by the project.</summary>
[JsonPropertyName("packages")]
public PackageInfo[] Packages { get; init; } = [];
}
2 changes: 1 addition & 1 deletion DotNetMcp/Tools/Cli/DotNetCliTools.Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed partial class DotNetCliTools
/// No PII is stored; only tool names and timing data are tracked.
/// </summary>
/// <param name="action">The metrics operation to perform: Get (return current snapshot) or Reset (clear all counters)</param>
[McpServerTool(Title = "Server Metrics", ReadOnly = false, Idempotent = false, IconSource = "https://raw.githubusercontent.com/microsoft/fluentui-emoji/62ecdc0d7ca5c6df32148c169556bc8d3782fca4/assets/Bar%20Chart/Flat/bar_chart_flat.svg")]
[McpServerTool(Title = "Server Metrics", ReadOnly = false, Idempotent = false, UseStructuredContent = true, OutputSchemaType = typeof(ServerMetricsResponse), IconSource = "https://raw.githubusercontent.com/microsoft/fluentui-emoji/62ecdc0d7ca5c6df32148c169556bc8d3782fca4/assets/Bar%20Chart/Flat/bar_chart_flat.svg")]
[McpMeta("category", "telemetry")]
[McpMeta("priority", 5.0)]
[McpMeta("consolidatedTool", true)]
Expand Down
2 changes: 1 addition & 1 deletion DotNetMcp/Tools/Cli/DotNetCliTools.Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async partial Task<CallToolResult> DotnetHelp(
/// <summary>
/// Get a machine-readable JSON snapshot of server capabilities, versions, and supported features for agent orchestration and discovery.
/// </summary>
[McpServerTool(Title = "Server Capabilities", ReadOnly = true, Idempotent = true, IconSource = "https://raw.githubusercontent.com/microsoft/fluentui-emoji/62ecdc0d7ca5c6df32148c169556bc8d3782fca4/assets/Bar%20Chart/Flat/bar_chart_flat.svg")]
[McpServerTool(Title = "Server Capabilities", ReadOnly = true, Idempotent = true, UseStructuredContent = true, OutputSchemaType = typeof(ServerCapabilities), IconSource = "https://raw.githubusercontent.com/microsoft/fluentui-emoji/62ecdc0d7ca5c6df32148c169556bc8d3782fca4/assets/Bar%20Chart/Flat/bar_chart_flat.svg")]
[McpMeta("category", "help")]
[McpMeta("priority", 8.0)]
[McpMeta("commonlyUsed", true)]
Expand Down
13 changes: 7 additions & 6 deletions DotNetMcp/Tools/Cli/DotNetCliTools.Package.Consolidated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public sealed partial class DotNetCliTools
/// <param name="referencePath">Path to referenced project for add/remove reference operations</param>
/// <param name="cacheType">Cache location to clear: all, http-cache, global-packages, temp, plugins-cache</param>
/// <param name="workingDirectory">Working directory for command execution</param>
[McpServerTool(Title = "NuGet Package Manager", Destructive = true, IconSource = "https://raw.githubusercontent.com/microsoft/fluentui-emoji/62ecdc0d7ca5c6df32148c169556bc8d3782fca4/assets/Package/Flat/package_flat.svg")]
[McpServerTool(Title = "NuGet Package Manager", Destructive = true, UseStructuredContent = true, OutputSchemaType = typeof(PackageListResult), IconSource = "https://raw.githubusercontent.com/microsoft/fluentui-emoji/62ecdc0d7ca5c6df32148c169556bc8d3782fca4/assets/Package/Flat/package_flat.svg")]
[McpMeta("category", "package")]
[McpMeta("priority", 9.0)]
[McpMeta("commonlyUsed", true)]
Expand Down Expand Up @@ -287,7 +287,7 @@ private async Task<string> HandleClearCacheAction(string? cacheType)
clear: true);
}

private static object? BuildPackageListStructuredContent(string textResult)
private static PackageListResult? BuildPackageListStructuredContent(string textResult)
{
// Parse package list from 'dotnet list package' output
var lines = textResult.Split('\n', StringSplitOptions.RemoveEmptyEntries);
Expand All @@ -298,16 +298,17 @@ private async Task<string> HandleClearCacheAction(string? cacheType)
var parts = l.Trim().TrimStart('>').Trim()
.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 3)
return new { name = parts[0], requestedVersion = (string?)parts[1], resolvedVersion = (string?)parts[2] };
return new PackageInfo { Name = parts[0], RequestedVersion = parts[1], ResolvedVersion = parts[2] };
if (parts.Length == 2)
return new { name = parts[0], requestedVersion = (string?)parts[1], resolvedVersion = (string?)null };
return new PackageInfo { Name = parts[0], RequestedVersion = parts[1] };
return parts.Length == 1
? new { name = parts[0], requestedVersion = (string?)null, resolvedVersion = (string?)null }
? new PackageInfo { Name = parts[0] }
: null;
})
.Where(p => p != null)
.Cast<PackageInfo>()
.ToArray();
return new { packages };
return new PackageListResult { Packages = packages };
}

// ===== Reference helper methods (moved from DotNetCliTools.Reference.cs) =====
Expand Down
2 changes: 1 addition & 1 deletion DotNetMcp/Tools/Cli/DotNetCliTools.Project.Consolidated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public sealed partial class DotNetCliTools
/// <param name="configFile">NuGet.config file to use for restore action</param>
/// <param name="itemType">Item type for AddItem/RemoveItem/ListItems actions (e.g., 'Using', 'Content', 'None')</param>
/// <param name="include">The Include attribute value for AddItem/RemoveItem actions</param>
[McpServerTool(Title = ".NET Project", Destructive = true, TaskSupport = ToolTaskSupport.Optional, IconSource = "https://raw.githubusercontent.com/microsoft/fluentui-emoji/62ecdc0d7ca5c6df32148c169556bc8d3782fca4/assets/File%20Folder/Flat/file_folder_flat.svg")]
[McpServerTool(Title = ".NET Project", Destructive = true, UseStructuredContent = true, OutputSchemaType = typeof(BuildResult), TaskSupport = ToolTaskSupport.Optional, IconSource = "https://raw.githubusercontent.com/microsoft/fluentui-emoji/62ecdc0d7ca5c6df32148c169556bc8d3782fca4/assets/File%20Folder/Flat/file_folder_flat.svg")]
[McpMeta("category", "project")]
[McpMeta("priority", 10.0)]
[McpMeta("commonlyUsed", true)]
Expand Down
6 changes: 3 additions & 3 deletions DotNetMcp/Tools/Cli/DotNetCliTools.Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ internal async Task<string> DotnetSolutionRemove(
/// <param name="output">Output directory for solution file (optional, used with 'create' action)</param>
/// <param name="format">Solution file format: 'sln' (classic) or 'slnx' (XML-based). Default is 'sln'. (optional, used with 'create' action)</param>
/// <param name="projects">Array of project file paths (required for 'add' and 'remove' actions)</param>
[McpServerTool(Title = ".NET Solution Manager", Destructive = true, IconSource = "https://raw.githubusercontent.com/microsoft/fluentui-emoji/62ecdc0d7ca5c6df32148c169556bc8d3782fca4/assets/Card%20File%20Box/Flat/card_file_box_flat.svg")]
[McpServerTool(Title = ".NET Solution Manager", Destructive = true, UseStructuredContent = true, OutputSchemaType = typeof(SolutionListResult), IconSource = "https://raw.githubusercontent.com/microsoft/fluentui-emoji/62ecdc0d7ca5c6df32148c169556bc8d3782fca4/assets/Card%20File%20Box/Flat/card_file_box_flat.svg")]
[McpMeta("category", "solution")]
[McpMeta("priority", 10.0)]
[McpMeta("commonlyUsed", true)]
Expand Down Expand Up @@ -265,7 +265,7 @@ private async Task<string> HandleSolutionValidateAction(string? solution)
return await SolutionAnalysisHelper.ValidateSolutionAsync(solution!, args => ExecuteDotNetCommand(args), _logger);
}

private static object? BuildSolutionListStructuredContent(string textResult)
private static SolutionListResult? BuildSolutionListStructuredContent(string textResult)
{
// Parse project paths from dotnet solution list output
// Output format: lines with .csproj/.fsproj/.vbproj paths
Expand All @@ -280,6 +280,6 @@ private async Task<string> HandleSolutionValidateAction(string? solution)
|| l.Trim().EndsWith(".vbproj", StringComparison.OrdinalIgnoreCase)))
.Select(l => l.Trim())
.ToArray();
return new { projects };
return new SolutionListResult { Projects = projects };
}
}
Loading
Loading