|
| 1 | +using Microsoft.Extensions.AI; |
1 | 2 | using Microsoft.Extensions.DependencyInjection; |
2 | 3 | using Microsoft.Extensions.Options; |
3 | 4 | using ModelContextProtocol.Protocol; |
@@ -74,10 +75,90 @@ public void WithTools_UsesServerWideOptions_WhenNoExplicitOptionsProvided() |
74 | 75 | Assert.False(propertiesElement.TryGetProperty("MyParameter", out _), "Schema should not have 'MyParameter' property (PascalCase)"); |
75 | 76 | } |
76 | 77 |
|
| 78 | + [Fact] |
| 79 | + public void WithPrompts_UsesServerWideOptions_WhenNoExplicitOptionsProvided() |
| 80 | + { |
| 81 | + // Arrange |
| 82 | + var services = new ServiceCollection(); |
| 83 | + var customOptions = new JsonSerializerOptions(McpJsonUtilities.DefaultOptions) |
| 84 | + { |
| 85 | + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower |
| 86 | + }; |
| 87 | + |
| 88 | + services.Configure<McpServerOptions>(options => |
| 89 | + { |
| 90 | + options.JsonSerializerOptions = customOptions; |
| 91 | + }); |
| 92 | + |
| 93 | + var builder = services.AddMcpServer(); |
| 94 | + |
| 95 | + // Act - WithPrompts should pick up the server-wide options with snake_case naming policy |
| 96 | + builder.WithPrompts<TestPrompts>(); |
| 97 | + var serviceProvider = services.BuildServiceProvider(); |
| 98 | + |
| 99 | + // Assert - Verify the prompt schema uses snake_case property naming |
| 100 | + var prompts = serviceProvider.GetServices<McpServerPrompt>().ToList(); |
| 101 | + Assert.Single(prompts); |
| 102 | + |
| 103 | + var prompt = prompts[0]; |
| 104 | + Assert.Equal("PromptWithParameters", prompt.ProtocolPrompt.Name); |
| 105 | + |
| 106 | + // Check that the arguments schema uses snake_case for property names |
| 107 | + var arguments = prompt.ProtocolPrompt.Arguments; |
| 108 | + Assert.NotNull(arguments); |
| 109 | + Assert.Single(arguments); |
| 110 | + Assert.Equal("my_argument", arguments[0].Name); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public void WithResources_UsesServerWideOptions_WhenNoExplicitOptionsProvided() |
| 115 | + { |
| 116 | + // Arrange |
| 117 | + var services = new ServiceCollection(); |
| 118 | + var customOptions = new JsonSerializerOptions(McpJsonUtilities.DefaultOptions) |
| 119 | + { |
| 120 | + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower |
| 121 | + }; |
| 122 | + |
| 123 | + services.Configure<McpServerOptions>(options => |
| 124 | + { |
| 125 | + options.JsonSerializerOptions = customOptions; |
| 126 | + }); |
| 127 | + |
| 128 | + var builder = services.AddMcpServer(); |
| 129 | + |
| 130 | + // Act - WithResources should pick up the server-wide options with snake_case naming policy |
| 131 | + builder.WithResources<TestResources>(); |
| 132 | + var serviceProvider = services.BuildServiceProvider(); |
| 133 | + |
| 134 | + // Assert - Verify the resource was registered (resources don't expose schema in the same way) |
| 135 | + var resources = serviceProvider.GetServices<McpServerResource>().ToList(); |
| 136 | + Assert.Single(resources); |
| 137 | + |
| 138 | + var resource = resources[0]; |
| 139 | + Assert.Equal("resource://test/{myParameter}", resource.ProtocolResourceTemplate.UriTemplate); |
| 140 | + } |
| 141 | + |
77 | 142 | [McpServerToolType] |
78 | 143 | private class TestTools |
79 | 144 | { |
80 | 145 | [McpServerTool] |
81 | 146 | public static string ToolWithParameters(string myParameter) => myParameter; |
82 | 147 | } |
| 148 | + |
| 149 | + [McpServerPromptType] |
| 150 | + private class TestPrompts |
| 151 | + { |
| 152 | + [McpServerPrompt] |
| 153 | + public static ChatMessage PromptWithParameters(string myArgument) => |
| 154 | + new(ChatRole.User, $"Prompt with: {myArgument}"); |
| 155 | + } |
| 156 | + |
| 157 | + [McpServerResourceType] |
| 158 | + private class TestResources |
| 159 | + { |
| 160 | + [McpServerResource(UriTemplate = "resource://test/{myParameter}")] |
| 161 | + public static string ResourceWithParameters(string myParameter) => |
| 162 | + $"Resource content: {myParameter}"; |
| 163 | + } |
83 | 164 | } |
0 commit comments