|
4 | 4 | using ModelContextProtocol.Protocol; |
5 | 5 | using ModelContextProtocol.Server; |
6 | 6 | using Moq; |
| 7 | +using System.ComponentModel; |
7 | 8 | using System.Reflection; |
8 | 9 | using System.Runtime.InteropServices; |
9 | 10 | using System.Text.Json; |
@@ -743,6 +744,87 @@ public void SupportsToolWithoutIcons() |
743 | 744 | Assert.Null(tool.ProtocolTool.Icons); |
744 | 745 | } |
745 | 746 |
|
| 747 | + [Fact] |
| 748 | + public void ReturnDescription_StructuredOutputDisabled_IncludedInToolDescription() |
| 749 | + { |
| 750 | + // When UseStructuredContent is false (default), return description should be appended to tool description |
| 751 | + McpServerTool tool = McpServerTool.Create(ToolWithReturnDescription); |
| 752 | + |
| 753 | + Assert.Equal("Tool that returns data. Returns: The computed result", tool.ProtocolTool.Description); |
| 754 | + Assert.Null(tool.ProtocolTool.OutputSchema); |
| 755 | + } |
| 756 | + |
| 757 | + [Fact] |
| 758 | + public void ReturnDescription_StructuredOutputEnabled_NotIncludedInToolDescription() |
| 759 | + { |
| 760 | + // When UseStructuredContent is true, return description should be in the output schema, not in tool description |
| 761 | + McpServerTool tool = McpServerTool.Create(ToolWithReturnDescription, new() { UseStructuredContent = true }); |
| 762 | + |
| 763 | + Assert.Equal("Tool that returns data.", tool.ProtocolTool.Description); |
| 764 | + Assert.NotNull(tool.ProtocolTool.OutputSchema); |
| 765 | + // Verify the output schema contains the description |
| 766 | + Assert.True(tool.ProtocolTool.OutputSchema.Value.TryGetProperty("properties", out var properties)); |
| 767 | + Assert.True(properties.TryGetProperty("result", out var result)); |
| 768 | + Assert.True(result.TryGetProperty("description", out var description)); |
| 769 | + Assert.Equal("The computed result", description.GetString()); |
| 770 | + } |
| 771 | + |
| 772 | + [Fact] |
| 773 | + public void ReturnDescription_NoFunctionDescription_OnlyReturnsDescription() |
| 774 | + { |
| 775 | + // When there's no function description but there's a return description |
| 776 | + McpServerTool tool = McpServerTool.Create(ToolWithOnlyReturnDescription); |
| 777 | + |
| 778 | + Assert.Equal("Returns: The computed result", tool.ProtocolTool.Description); |
| 779 | + Assert.Null(tool.ProtocolTool.OutputSchema); |
| 780 | + } |
| 781 | + |
| 782 | + [Fact] |
| 783 | + public void ReturnDescription_ExplicitDescriptionOption_SynthesizesWithReturnDescription() |
| 784 | + { |
| 785 | + // When Description is explicitly set in options and there's a return description, |
| 786 | + // the return description should be appended since UseStructuredContent is false |
| 787 | + McpServerTool tool = McpServerTool.Create(ToolWithReturnDescription, new() { Description = "Custom description" }); |
| 788 | + |
| 789 | + Assert.Equal("Custom description Returns: The computed result", tool.ProtocolTool.Description); |
| 790 | + Assert.Null(tool.ProtocolTool.OutputSchema); |
| 791 | + } |
| 792 | + |
| 793 | + [Fact] |
| 794 | + public void ReturnDescription_NoReturnDescription_NoChange() |
| 795 | + { |
| 796 | + // When there's no return description, the tool description should remain unchanged |
| 797 | + McpServerTool tool = McpServerTool.Create(ToolWithoutReturnDescription); |
| 798 | + |
| 799 | + Assert.Equal("Tool without return description.", tool.ProtocolTool.Description); |
| 800 | + Assert.Null(tool.ProtocolTool.OutputSchema); |
| 801 | + } |
| 802 | + |
| 803 | + [Fact] |
| 804 | + public void ReturnDescription_StructuredOutputEnabled_WithExplicitDescription_NoSynthesis() |
| 805 | + { |
| 806 | + // When UseStructuredContent is true and Description is set, return description goes to output schema |
| 807 | + McpServerTool tool = McpServerTool.Create(ToolWithReturnDescription, new() |
| 808 | + { |
| 809 | + Description = "Custom description", |
| 810 | + UseStructuredContent = true |
| 811 | + }); |
| 812 | + |
| 813 | + // Description should not have the return description appended |
| 814 | + Assert.Equal("Custom description", tool.ProtocolTool.Description); |
| 815 | + Assert.NotNull(tool.ProtocolTool.OutputSchema); |
| 816 | + } |
| 817 | + |
| 818 | + [Description("Tool that returns data.")] |
| 819 | + [return: Description("The computed result")] |
| 820 | + private static string ToolWithReturnDescription() => "result"; |
| 821 | + |
| 822 | + [return: Description("The computed result")] |
| 823 | + private static string ToolWithOnlyReturnDescription() => "result"; |
| 824 | + |
| 825 | + [Description("Tool without return description.")] |
| 826 | + private static string ToolWithoutReturnDescription() => "result"; |
| 827 | + |
746 | 828 | [JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)] |
747 | 829 | [JsonSerializable(typeof(DisposableToolType))] |
748 | 830 | [JsonSerializable(typeof(AsyncDisposableToolType))] |
|
0 commit comments