|
15 | 15 | { |
16 | 16 | // Determine tool category from route parameters |
17 | 17 | var toolCategory = GetToolCategoryFromRoute(httpContext); |
18 | | - var sessionInfo = GetSessionInfo(httpContext); |
19 | 18 |
|
20 | 19 | // Get the tool collection that we can modify per session |
21 | 20 | var toolCollection = mcpOptions.Capabilities?.Tools?.ToolCollection; |
|
51 | 50 | break; |
52 | 51 | } |
53 | 52 | } |
54 | | - |
55 | | - // Optional: Log the session configuration for debugging |
56 | | - var logger = httpContext.RequestServices.GetRequiredService<ILogger<Program>>(); |
57 | | - logger.LogInformation("Configured MCP session for category '{ToolCategory}' from {SessionInfo}, {ToolCount} tools available", |
58 | | - toolCategory, sessionInfo, toolCollection?.Count ?? 0); |
59 | 53 | }; |
60 | 54 | }) |
61 | 55 | .WithTools<ClockTool>() |
|
75 | 69 |
|
76 | 70 | var app = builder.Build(); |
77 | 71 |
|
78 | | -// Add middleware to log requests for demo purposes |
79 | | -app.Use(async (context, next) => |
80 | | -{ |
81 | | - var logger = context.RequestServices.GetRequiredService<ILogger<Program>>(); |
82 | | - var toolCategory = GetToolCategoryFromRoute(context); |
83 | | - var sessionInfo = GetSessionInfo(context); |
84 | | - |
85 | | - logger.LogInformation("Request for category '{ToolCategory}' from {SessionInfo}: {Method} {Path}", |
86 | | - toolCategory, sessionInfo, context.Request.Method, context.Request.Path); |
87 | | - |
88 | | - await next(); |
89 | | -}); |
90 | | - |
91 | 72 | // Map MCP with route parameter for tool category filtering |
92 | 73 | app.MapMcp("/{toolCategory?}"); |
93 | 74 |
|
94 | | -// Add endpoints to test different tool categories |
95 | | -app.MapGet("/", () => Results.Text( |
96 | | - "MCP Per-Session Tools Demo\n" + |
97 | | - "=========================\n" + |
98 | | - "Available endpoints:\n" + |
99 | | - "- /clock - MCP server with clock/time tools\n" + |
100 | | - "- /calculator - MCP server with calculation tools\n" + |
101 | | - "- /userinfo - MCP server with session/system info tools\n" + |
102 | | - "- /all - MCP server with all tools (default)\n" + |
103 | | - "\n" + |
104 | | - "Test routes:\n" + |
105 | | - "- /test-category/{category} - Test category detection\n" |
106 | | -)); |
107 | | - |
108 | | -app.MapGet("/test-category/{toolCategory?}", (string? toolCategory, HttpContext context) => |
109 | | -{ |
110 | | - var detectedCategory = GetToolCategoryFromRoute(context); |
111 | | - var sessionInfo = GetSessionInfo(context); |
112 | | - |
113 | | - return Results.Text($"Tool Category: {detectedCategory ?? "all (default)"}\n" + |
114 | | - $"Session Info: {sessionInfo}\n" + |
115 | | - $"Route Parameter: {toolCategory ?? "none"}\n" + |
116 | | - $"Message: MCP session would be configured for '{detectedCategory ?? "all"}' tools"); |
117 | | -}); |
118 | | - |
119 | 75 | app.Run(); |
120 | 76 |
|
121 | 77 | // Helper methods for route-based tool category detection |
|
126 | 82 | { |
127 | 83 | return string.IsNullOrEmpty(category) ? "all" : category; |
128 | 84 | } |
129 | | - |
130 | | - // Fallback: try to extract from path |
131 | | - var path = context.Request.Path.Value?.Trim('/'); |
132 | | - if (!string.IsNullOrEmpty(path)) |
133 | | - { |
134 | | - var segments = path.Split('/'); |
135 | | - if (segments.Length > 0) |
136 | | - { |
137 | | - var firstSegment = segments[0].ToLower(); |
138 | | - if (firstSegment is "clock" or "calculator" or "userinfo" or "all") |
139 | | - { |
140 | | - return firstSegment; |
141 | | - } |
142 | | - } |
143 | | - } |
144 | | - |
145 | | - // Default to "all" if no category specified |
146 | | - return "all"; |
147 | | -} |
148 | 85 |
|
149 | | -static string GetSessionInfo(HttpContext context) |
150 | | -{ |
151 | | - var userAgent = context.Request.Headers.UserAgent.ToString(); |
152 | | - var clientInfo = !string.IsNullOrEmpty(userAgent) ? userAgent[..Math.Min(userAgent.Length, 20)] + "..." : "Unknown"; |
153 | | - var remoteIp = context.Connection.RemoteIpAddress?.ToString() ?? "unknown"; |
154 | | - |
155 | | - return $"{clientInfo} ({remoteIp})"; |
| 86 | + // Default to "all" if no category specified or empty |
| 87 | + return "all"; |
156 | 88 | } |
157 | 89 |
|
158 | 90 | static void AddToolsForType<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers( |
|
0 commit comments