Skip to content

Commit dfcd1ec

Browse files
Add tests for prompts and resources using server-wide JsonSerializerOptions
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
1 parent 542fa91 commit dfcd1ec

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

tests/ModelContextProtocol.Tests/Configuration/McpServerJsonSerializerOptionsTests.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Extensions.AI;
12
using Microsoft.Extensions.DependencyInjection;
23
using Microsoft.Extensions.Options;
34
using ModelContextProtocol.Protocol;
@@ -74,10 +75,90 @@ public void WithTools_UsesServerWideOptions_WhenNoExplicitOptionsProvided()
7475
Assert.False(propertiesElement.TryGetProperty("MyParameter", out _), "Schema should not have 'MyParameter' property (PascalCase)");
7576
}
7677

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+
77142
[McpServerToolType]
78143
private class TestTools
79144
{
80145
[McpServerTool]
81146
public static string ToolWithParameters(string myParameter) => myParameter;
82147
}
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+
}
83164
}

0 commit comments

Comments
 (0)