forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
86 lines (73 loc) · 3.14 KB
/
Program.cs
File metadata and controls
86 lines (73 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using AspNetCoreMcpPerSessionTools.Tools;
using ModelContextProtocol.Server;
using System.Collections.Concurrent;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// Create and populate the tool dictionary at startup
var toolDictionary = new ConcurrentDictionary<string, McpServerTool[]>();
PopulateToolDictionary(toolDictionary);
// Register all MCP server tools - they will be filtered per session based on route
builder.Services.AddMcpServer()
.WithHttpTransport(options =>
{
// Configure per-session options to filter tools based on route category
options.ConfigureSessionOptions = async (httpContext, mcpOptions, cancellationToken) =>
{
// Determine tool category from route parameters
var toolCategory = httpContext.Request.RouteValues["toolCategory"]?.ToString()?.ToLower() ?? "all";
// Get pre-populated tools for the requested category
if (toolDictionary.TryGetValue(toolCategory, out var tools))
{
mcpOptions.Capabilities = new();
mcpOptions.Capabilities.Tools = new();
var toolCollection = mcpOptions.ToolCollection = new();
foreach (var tool in tools)
{
toolCollection.Add(tool);
}
}
};
});
var app = builder.Build();
// Map MCP with route parameter for tool category filtering
app.MapMcp("/{toolCategory?}");
app.Run();
// Helper method to populate the tool dictionary at startup
static void PopulateToolDictionary(ConcurrentDictionary<string, McpServerTool[]> toolDictionary)
{
// Get tools for each category
var clockTools = GetToolsForType<ClockTool>();
var calculatorTools = GetToolsForType<CalculatorTool>();
var userInfoTools = GetToolsForType<UserInfoTool>();
McpServerTool[] allTools = [.. clockTools,
.. calculatorTools,
.. userInfoTools];
// Populate the dictionary with tools for each category
toolDictionary.TryAdd("clock", clockTools);
toolDictionary.TryAdd("calculator", calculatorTools);
toolDictionary.TryAdd("userinfo", userInfoTools);
toolDictionary.TryAdd("all", allTools);
}
// Helper method to get tools for a specific type using reflection
static McpServerTool[] GetToolsForType<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(
System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)] T>()
{
var tools = new List<McpServerTool>();
var toolType = typeof(T);
var methods = toolType.GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(m => m.GetCustomAttributes(typeof(McpServerToolAttribute), false).Any());
foreach (var method in methods)
{
try
{
var tool = McpServerTool.Create(method, target: null, new McpServerToolCreateOptions());
tools.Add(tool);
}
catch (Exception ex)
{
// Log error but continue with other tools
Console.WriteLine($"Failed to add tool {toolType.Name}.{method.Name}: {ex.Message}");
}
}
return [.. tools];
}