|
1 | | -using OpenTelemetry; |
2 | | -using OpenTelemetry.Metrics; |
3 | | -using OpenTelemetry.Trace; |
4 | 1 | using AspNetCoreMcpPerSessionTools.Tools; |
5 | 2 | using ModelContextProtocol.Server; |
6 | 3 | using System.Collections.Concurrent; |
|
20 | 17 | options.ConfigureSessionOptions = async (httpContext, mcpOptions, cancellationToken) => |
21 | 18 | { |
22 | 19 | // Determine tool category from route parameters |
23 | | - var toolCategory = GetToolCategoryFromRoute(httpContext); |
24 | | - |
25 | | - // Get the tool collection that we can modify per session |
26 | | - var toolCollection = mcpOptions.Capabilities?.Tools?.ToolCollection; |
27 | | - if (toolCollection == null) |
28 | | - { |
29 | | - return; |
30 | | - } |
31 | | - |
32 | | - // Clear tools (we add them to make sure the capability is initialized) |
33 | | - toolCollection.Clear(); |
| 20 | + var toolCategory = httpContext.Request.RouteValues["toolCategory"]?.ToString()?.ToLower() ?? "all"; |
34 | 21 |
|
35 | 22 | // Get pre-populated tools for the requested category |
36 | | - if (toolDictionary.TryGetValue(toolCategory.ToLower(), out var tools)) |
| 23 | + if (toolDictionary.TryGetValue(toolCategory, out var tools)) |
37 | 24 | { |
| 25 | + mcpOptions.Capabilities = new(); |
| 26 | + mcpOptions.Capabilities.Tools = new(); |
| 27 | + var toolCollection = mcpOptions.Capabilities.Tools.ToolCollection = new(); |
| 28 | + |
38 | 29 | foreach (var tool in tools) |
39 | 30 | { |
40 | 31 | toolCollection.Add(tool); |
|
46 | 37 | .WithTools<CalculatorTool>() |
47 | 38 | .WithTools<UserInfoTool>(); |
48 | 39 |
|
49 | | -// Add OpenTelemetry for observability |
50 | | -builder.Services.AddOpenTelemetry() |
51 | | - .WithTracing(b => b.AddSource("*") |
52 | | - .AddAspNetCoreInstrumentation() |
53 | | - .AddHttpClientInstrumentation()) |
54 | | - .WithMetrics(b => b.AddMeter("*") |
55 | | - .AddAspNetCoreInstrumentation() |
56 | | - .AddHttpClientInstrumentation()) |
57 | | - .WithLogging() |
58 | | - .UseOtlpExporter(); |
59 | | - |
60 | 40 | var app = builder.Build(); |
61 | 41 |
|
62 | 42 | // Map MCP with route parameter for tool category filtering |
63 | 43 | app.MapMcp("/{toolCategory?}"); |
64 | 44 |
|
65 | 45 | app.Run(); |
66 | 46 |
|
67 | | -// Helper method for route-based tool category detection |
68 | | -static string GetToolCategoryFromRoute(HttpContext context) |
69 | | -{ |
70 | | - // Try to get tool category from route values |
71 | | - if (context.Request.RouteValues.TryGetValue("toolCategory", out var categoryObj) && categoryObj is string category) |
72 | | - { |
73 | | - return string.IsNullOrEmpty(category) ? "all" : category; |
74 | | - } |
75 | | - |
76 | | - // Default to "all" if no category specified or empty |
77 | | - return "all"; |
78 | | -} |
79 | | - |
80 | 47 | // Helper method to populate the tool dictionary at startup |
81 | 48 | static void PopulateToolDictionary(ConcurrentDictionary<string, McpServerTool[]> toolDictionary) |
82 | 49 | { |
|
0 commit comments