Skip to content

Commit 48bd4ea

Browse files
committed
Simplify sample
1 parent a2ffda9 commit 48bd4ea

File tree

2 files changed

+6
-40
lines changed

2 files changed

+6
-40
lines changed

samples/AspNetCoreMcpPerSessionTools/AspNetCoreMcpPerSessionTools.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
1615
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
1716
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
1817
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />

samples/AspNetCoreMcpPerSessionTools/Program.cs

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using OpenTelemetry;
2-
using OpenTelemetry.Metrics;
3-
using OpenTelemetry.Trace;
41
using AspNetCoreMcpPerSessionTools.Tools;
52
using ModelContextProtocol.Server;
63
using System.Collections.Concurrent;
@@ -20,21 +17,15 @@
2017
options.ConfigureSessionOptions = async (httpContext, mcpOptions, cancellationToken) =>
2118
{
2219
// 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";
3421

3522
// 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))
3724
{
25+
mcpOptions.Capabilities = new();
26+
mcpOptions.Capabilities.Tools = new();
27+
var toolCollection = mcpOptions.Capabilities.Tools.ToolCollection = new();
28+
3829
foreach (var tool in tools)
3930
{
4031
toolCollection.Add(tool);
@@ -46,37 +37,13 @@
4637
.WithTools<CalculatorTool>()
4738
.WithTools<UserInfoTool>();
4839

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-
6040
var app = builder.Build();
6141

6242
// Map MCP with route parameter for tool category filtering
6343
app.MapMcp("/{toolCategory?}");
6444

6545
app.Run();
6646

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-
8047
// Helper method to populate the tool dictionary at startup
8148
static void PopulateToolDictionary(ConcurrentDictionary<string, McpServerTool[]> toolDictionary)
8249
{

0 commit comments

Comments
 (0)