-
Notifications
You must be signed in to change notification settings - Fork 577
Add Conformance tests for the server #983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mikekistler
merged 15 commits into
modelcontextprotocol:main
from
mikekistler:mdk/conformance-server
Nov 26, 2025
+981
−2
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
51a3ac6
Add ComplianceServer sample
mikekistler 03b678f
ComplianceServer now passing all tests (with one fix)
mikekistler 73cbe0f
Address PR review comments
mikekistler dc4757f
Move ConformanceServer to tests and fix naming
mikekistler 6689ec0
Add test project for conformance tests
mikekistler 7f75706
Fix conformance tests
mikekistler effa3ce
Skip the Conformance tests if Node is not installed
mikekistler ed0adcf
Restucture and add testing for .net8 and .net9
mikekistler 33059b5
A few more little fixes
mikekistler fbbc807
Fix Conformance tests in Release configuration
mikekistler f2ba02d
Attempt fix for server conformance tests in CI
mikekistler cd3d3fb
Another fix attempt for server conformance tests in CI
mikekistler 769fb69
Launch Conformance Server in-process for better debugging
mikekistler ef4198c
Move ConformanceTests into AspNetCore test project
mikekistler 49b1c4a
Merge branch 'main' into mdk/conformance-server
mikekistler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/ModelContextProtocol.ConformanceServer/ModelContextProtocol.ConformanceServer.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="../../src/ModelContextProtocol.AspNetCore/ModelContextProtocol.AspNetCore.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
75 changes: 75 additions & 0 deletions
75
tests/ModelContextProtocol.ConformanceServer/ModelContextProtocol.ConformanceServer.http
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| @HostAddress = http://localhost:3001 | ||
|
|
||
| POST {{HostAddress}}/ | ||
| Accept: application/json, text/event-stream | ||
| Content-Type: application/json | ||
|
|
||
| { | ||
| "jsonrpc": "2.0", | ||
| "id": 1, | ||
| "method": "ping" | ||
| } | ||
|
|
||
| ### | ||
|
|
||
| POST {{HostAddress}}/ | ||
| Accept: application/json, text/event-stream | ||
| Content-Type: application/json | ||
|
|
||
| { | ||
| "jsonrpc": "2.0", | ||
| "id": 2, | ||
| "method": "initialize", | ||
| "params": { | ||
| "clientInfo": { | ||
| "name": "RestClient", | ||
| "version": "0.1.0" | ||
| }, | ||
| "capabilities": {}, | ||
| "protocolVersion": "2025-06-18" | ||
| } | ||
| } | ||
|
|
||
| ### | ||
|
|
||
| @SessionId = XxIXkrK210aKVnZxD8Iu_g | ||
|
|
||
| POST {{HostAddress}}/ | ||
| Accept: application/json, text/event-stream | ||
| Content-Type: application/json | ||
| MCP-Protocol-Version: 2025-06-18 | ||
| Mcp-Session-Id: {{SessionId}} | ||
|
|
||
| { | ||
| "jsonrpc": "2.0", | ||
| "id": 3, | ||
| "method": "tools/list" | ||
| } | ||
|
|
||
| ### | ||
|
|
||
| POST {{HostAddress}}/ | ||
| Accept: application/json, text/event-stream | ||
| Content-Type: application/json | ||
| MCP-Protocol-Version: 2025-06-18 | ||
| Mcp-Session-Id: {{SessionId}} | ||
|
|
||
| { | ||
| "jsonrpc": "2.0", | ||
| "id": 4, | ||
| "method": "resources/list" | ||
| } | ||
|
|
||
| ### | ||
|
|
||
| POST {{HostAddress}}/ | ||
| Accept: application/json, text/event-stream | ||
| Content-Type: application/json | ||
| MCP-Protocol-Version: 2025-06-18 | ||
| Mcp-Session-Id: {{SessionId}} | ||
|
|
||
| { | ||
| "jsonrpc": "2.0", | ||
| "id": 5, | ||
| "method": "prompts/list" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| using ConformanceServer.Prompts; | ||
| using ConformanceServer.Resources; | ||
| using ConformanceServer.Tools; | ||
| using Microsoft.Extensions.AI; | ||
| using ModelContextProtocol; | ||
| using ModelContextProtocol.Protocol; | ||
| using System.Collections.Concurrent; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| // Dictionary of session IDs to a set of resource URIs they are subscribed to | ||
| // The value is a ConcurrentDictionary used as a thread-safe HashSet | ||
| // because .NET does not have a built-in concurrent HashSet | ||
| ConcurrentDictionary<string, ConcurrentDictionary<string, byte>> subscriptions = new(); | ||
|
|
||
| builder.Services | ||
| .AddMcpServer() | ||
| .WithHttpTransport() | ||
| .WithTools<ConformanceTools>() | ||
| .WithPrompts<ConformancePrompts>() | ||
| .WithResources<ConformanceResources>() | ||
| .WithSubscribeToResourcesHandler(async (ctx, ct) => | ||
| { | ||
| if (ctx.Server.SessionId == null) | ||
| { | ||
| throw new McpException("Cannot add subscription for server with null SessionId"); | ||
| } | ||
| if (ctx.Params?.Uri is { } uri) | ||
| { | ||
| subscriptions[ctx.Server.SessionId].TryAdd(uri, 0); | ||
mikekistler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| await ctx.Server.SampleAsync([ | ||
| new ChatMessage(ChatRole.System, "You are a helpful test server"), | ||
| new ChatMessage(ChatRole.User, $"Resource {uri}, context: A new subscription was started"), | ||
| ], | ||
| options: new ChatOptions | ||
| { | ||
| MaxOutputTokens = 100, | ||
| Temperature = 0.7f, | ||
| }, | ||
| cancellationToken: ct); | ||
| } | ||
|
|
||
| return new EmptyResult(); | ||
| }) | ||
| .WithUnsubscribeFromResourcesHandler(async (ctx, ct) => | ||
mikekistler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (ctx.Server.SessionId == null) | ||
| { | ||
| throw new McpException("Cannot remove subscription for server with null SessionId"); | ||
| } | ||
| if (ctx.Params?.Uri is { } uri) | ||
| { | ||
| subscriptions[ctx.Server.SessionId].TryRemove(uri, out _); | ||
mikekistler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return new EmptyResult(); | ||
| }) | ||
| .WithCompleteHandler(async (ctx, ct) => | ||
mikekistler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| // Basic completion support - returns empty array for conformance | ||
| // Real implementations would provide contextual suggestions | ||
| return new CompleteResult | ||
| { | ||
| Completion = new Completion | ||
| { | ||
| Values = [], | ||
| HasMore = false, | ||
| Total = 0 | ||
| } | ||
| }; | ||
| }) | ||
| .WithSetLoggingLevelHandler(async (ctx, ct) => | ||
| { | ||
| if (ctx.Params?.Level is null) | ||
| { | ||
| throw new McpProtocolException("Missing required argument 'level'", McpErrorCode.InvalidParams); | ||
| } | ||
|
|
||
| // The SDK updates the LoggingLevel field of the McpServer | ||
| // Send a log notification to confirm the level was set | ||
| await ctx.Server.SendNotificationAsync("notifications/message", new | ||
| { | ||
| Level = "info", | ||
| Logger = "conformance-test-server", | ||
| Data = $"Log level set to: {ctx.Params.Level}", | ||
| }, cancellationToken: ct); | ||
mikekistler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return new EmptyResult(); | ||
| }); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.MapMcp(); | ||
|
|
||
| app.MapGet("/health", () => TypedResults.Ok("Healthy")); | ||
|
|
||
| app.Run(); | ||
67 changes: 67 additions & 0 deletions
67
tests/ModelContextProtocol.ConformanceServer/Prompts/ConformancePrompts.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| using ModelContextProtocol.Server; | ||
| using ModelContextProtocol.Protocol; | ||
| using Microsoft.Extensions.AI; | ||
| using System.ComponentModel; | ||
|
|
||
| namespace ConformanceServer.Prompts; | ||
|
|
||
| public class ConformancePrompts | ||
mikekistler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| // Sample base64 encoded 1x1 red PNG pixel for testing | ||
| private const string TestImageBase64 = | ||
| "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg=="; | ||
|
|
||
| [McpServerPrompt(Name = "test_simple_prompt"), Description("Simple prompt without arguments")] | ||
| public static string SimplePrompt() => "This is a simple prompt without arguments"; | ||
|
|
||
| [McpServerPrompt(Name = "test_prompt_with_arguments"), Description("Parameterized prompt")] | ||
| public static string ParameterizedPrompt( | ||
| [Description("First test argument")] string arg1, | ||
| [Description("Second test argument")] string arg2) | ||
| { | ||
| return $"Prompt with arguments: arg1={arg1}, arg2={arg2}"; | ||
| } | ||
|
|
||
| [McpServerPrompt(Name = "test_prompt_with_embedded_resource"), Description("Prompt with embedded resource")] | ||
| public static IEnumerable<PromptMessage> PromptWithEmbeddedResource( | ||
| [Description("URI of the resource to embed")] string resourceUri) | ||
| { | ||
| return [ | ||
mikekistler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| new PromptMessage | ||
| { | ||
| Role = Role.User, | ||
| Content = new EmbeddedResourceBlock | ||
| { | ||
| Resource = new TextResourceContents | ||
| { | ||
| Uri = resourceUri, | ||
| Text = "Embedded resource content for testing.", | ||
| MimeType = "text/plain" | ||
| } | ||
| } | ||
| }, | ||
| new PromptMessage { Role = Role.User, Content = new TextContentBlock { Text = "Please process the embedded resource above." } }, | ||
| ]; | ||
| } | ||
|
|
||
| [McpServerPrompt(Name = "test_prompt_with_image"), Description("Prompt with image")] | ||
| public static IEnumerable<PromptMessage> PromptWithImage() | ||
| { | ||
| return [ | ||
| new PromptMessage | ||
| { | ||
| Role = Role.User, | ||
| Content = new ImageContentBlock | ||
| { | ||
| MimeType = "image/png", | ||
| Data = TestImageBase64 | ||
| } | ||
| }, | ||
| new PromptMessage | ||
| { | ||
| Role = Role.User, | ||
| Content = new TextContentBlock { Text = "Please analyze the image above." } | ||
| }, | ||
| ]; | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
tests/ModelContextProtocol.ConformanceServer/Properties/launchSettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/launchsettings.json", | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": false, | ||
| "applicationUrl": "http://localhost:3001", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": false, | ||
| "applicationUrl": "https://localhost:7292;http://localhost:3001", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
tests/ModelContextProtocol.ConformanceServer/Resources/ConformanceResources.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| using ModelContextProtocol.Protocol; | ||
| using ModelContextProtocol.Server; | ||
| using System.ComponentModel; | ||
| using System.Text.Json; | ||
|
|
||
| namespace ConformanceServer.Resources; | ||
|
|
||
| [McpServerResourceType] | ||
| public class ConformanceResources | ||
| { | ||
| // Sample base64 encoded 1x1 red PNG pixel for testing | ||
| private const string TestImageBase64 = | ||
| "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg=="; | ||
|
|
||
| /// <summary> | ||
| /// Static text resource for testing | ||
| /// </summary> | ||
| [McpServerResource(UriTemplate = "test://static-text", Name = "Static Text Resource", MimeType = "text/plain")] | ||
| [Description("A static text resource for testing")] | ||
| public static string StaticText() | ||
| { | ||
| return "This is the content of the static text resource."; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Static binary resource (image) for testing | ||
| /// </summary> | ||
| [McpServerResource(UriTemplate = "test://static-binary", Name = "Static Binary Resource", MimeType = "image/png")] | ||
| [Description("A static binary resource (image) for testing")] | ||
| public static BlobResourceContents StaticBinary() | ||
| { | ||
| return new BlobResourceContents | ||
| { | ||
| Uri = "test://static-binary", | ||
| MimeType = "image/png", | ||
| Blob = TestImageBase64 | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Resource template with parameter substitution | ||
| /// </summary> | ||
| [McpServerResource(UriTemplate = "test://template/{id}/data", Name = "Resource Template", MimeType = "application/json")] | ||
| [Description("A resource template with parameter substitution")] | ||
| public static TextResourceContents TemplateResource(string id) | ||
| { | ||
| var data = new | ||
| { | ||
| id = id, | ||
| templateTest = true, | ||
| data = $"Data for ID: {id}" | ||
| }; | ||
|
|
||
| return new TextResourceContents | ||
| { | ||
| Uri = $"test://template/{id}/data", | ||
| MimeType = "application/json", | ||
| Text = JsonSerializer.Serialize(data) | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Subscribable resource that can send updates | ||
| /// </summary> | ||
| [McpServerResource(UriTemplate = "test://watched-resource", Name = "Watched Resource", MimeType = "text/plain")] | ||
| [Description("A resource that auto-updates every 3 seconds")] | ||
| public static string WatchedResource() | ||
| { | ||
| return "Watched resource content"; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.