|
| 1 | +using ComplianceServer; |
| 2 | +using ComplianceServer.Prompts; |
| 3 | +using ComplianceServer.Resources; |
| 4 | +using ComplianceServer.Tools; |
| 5 | +using Microsoft.Extensions.AI; |
| 6 | +using ModelContextProtocol; |
| 7 | +using ModelContextProtocol.Protocol; |
| 8 | +using ModelContextProtocol.Server; |
| 9 | +using System.Collections.Concurrent; |
| 10 | + |
| 11 | +var builder = WebApplication.CreateBuilder(args); |
| 12 | + |
| 13 | +// Dictionary of session IDs to a set of resource URIs they are subscribed to |
| 14 | +// The value is a ConcurrentDictionary used as a thread-safe HashSet |
| 15 | +// because .NET does not have a built-in concurrent HashSet |
| 16 | +ConcurrentDictionary<string, ConcurrentDictionary<string, byte>> subscriptions = new(); |
| 17 | + |
| 18 | +builder.Services |
| 19 | + .AddMcpServer() |
| 20 | + .WithHttpTransport() |
| 21 | + .WithTools<ComplianceTools>() |
| 22 | + .WithPrompts<CompliancePrompts>() |
| 23 | + .WithResources<ComplianceResources>() |
| 24 | + .WithSubscribeToResourcesHandler(async (ctx, ct) => |
| 25 | + { |
| 26 | + if (ctx.Server.SessionId == null) |
| 27 | + { |
| 28 | + throw new McpException("Cannot add subscription for server with null SessionId"); |
| 29 | + } |
| 30 | + if (ctx.Params?.Uri is { } uri) |
| 31 | + { |
| 32 | + subscriptions[ctx.Server.SessionId].TryAdd(uri, 0); |
| 33 | + |
| 34 | + await ctx.Server.SampleAsync([ |
| 35 | + new ChatMessage(ChatRole.System, "You are a helpful test server"), |
| 36 | + new ChatMessage(ChatRole.User, $"Resource {uri}, context: A new subscription was started"), |
| 37 | + ], |
| 38 | + options: new ChatOptions |
| 39 | + { |
| 40 | + MaxOutputTokens = 100, |
| 41 | + Temperature = 0.7f, |
| 42 | + }, |
| 43 | + cancellationToken: ct); |
| 44 | + } |
| 45 | + |
| 46 | + return new EmptyResult(); |
| 47 | + }) |
| 48 | + .WithUnsubscribeFromResourcesHandler(async (ctx, ct) => |
| 49 | + { |
| 50 | + if (ctx.Server.SessionId == null) |
| 51 | + { |
| 52 | + throw new McpException("Cannot remove subscription for server with null SessionId"); |
| 53 | + } |
| 54 | + if (ctx.Params?.Uri is { } uri) |
| 55 | + { |
| 56 | + subscriptions[ctx.Server.SessionId].TryRemove(uri, out _); |
| 57 | + } |
| 58 | + return new EmptyResult(); |
| 59 | + }) |
| 60 | + .WithCompleteHandler(async (ctx, ct) => |
| 61 | + { |
| 62 | + var exampleCompletions = new Dictionary<string, IEnumerable<string>> |
| 63 | + { |
| 64 | + { "style", ["casual", "formal", "technical", "friendly"] }, |
| 65 | + { "temperature", ["0", "0.5", "0.7", "1.0"] }, |
| 66 | + { "resourceId", ["1", "2", "3", "4", "5"] } |
| 67 | + }; |
| 68 | + |
| 69 | + if (ctx.Params is not { } @params) |
| 70 | + { |
| 71 | + throw new NotSupportedException($"Params are required."); |
| 72 | + } |
| 73 | + |
| 74 | + var @ref = @params.Ref; |
| 75 | + var argument = @params.Argument; |
| 76 | + |
| 77 | + if (@ref is ResourceTemplateReference rtr) |
| 78 | + { |
| 79 | + var resourceId = rtr.Uri?.Split("/").Last(); |
| 80 | + |
| 81 | + if (resourceId is null) |
| 82 | + { |
| 83 | + return new CompleteResult(); |
| 84 | + } |
| 85 | + |
| 86 | + var values = exampleCompletions["resourceId"].Where(id => id.StartsWith(argument.Value)); |
| 87 | + |
| 88 | + return new CompleteResult |
| 89 | + { |
| 90 | + Completion = new Completion { Values = [.. values], HasMore = false, Total = values.Count() } |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + if (@ref is PromptReference pr) |
| 95 | + { |
| 96 | + if (!exampleCompletions.TryGetValue(argument.Name, out IEnumerable<string>? value)) |
| 97 | + { |
| 98 | + throw new NotSupportedException($"Unknown argument name: {argument.Name}"); |
| 99 | + } |
| 100 | + |
| 101 | + var values = value.Where(value => value.StartsWith(argument.Value)); |
| 102 | + return new CompleteResult |
| 103 | + { |
| 104 | + Completion = new Completion { Values = [.. values], HasMore = false, Total = values.Count() } |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + throw new NotSupportedException($"Unknown reference type: {@ref.Type}"); |
| 109 | + }) |
| 110 | + .WithSetLoggingLevelHandler(async (ctx, ct) => |
| 111 | + { |
| 112 | + if (ctx.Params?.Level is null) |
| 113 | + { |
| 114 | + throw new McpProtocolException("Missing required argument 'level'", McpErrorCode.InvalidParams); |
| 115 | + } |
| 116 | + |
| 117 | + // The SDK updates the LoggingLevel field of the IMcpServer |
| 118 | + |
| 119 | + await ctx.Server.SendNotificationAsync("notifications/message", new |
| 120 | + { |
| 121 | + Level = "debug", |
| 122 | + Logger = "test-server", |
| 123 | + Data = $"Logging level set to {ctx.Params.Level}", |
| 124 | + }, cancellationToken: ct); |
| 125 | + |
| 126 | + return new EmptyResult(); |
| 127 | + }); |
| 128 | + |
| 129 | +var app = builder.Build(); |
| 130 | + |
| 131 | +app.MapMcp(); |
| 132 | + |
| 133 | +app.Run(); |
0 commit comments