This sample demonstrates how to create an MCP (Model Context Protocol) server that provides different sets of tools based on route-based session configuration. This showcases the technique of using ConfigureSessionOptions to dynamically modify the ToolCollection based on route parameters for each MCP session.
The sample demonstrates route-based tool filtering using the SDK's ConfigureSessionOptions callback. You could use any mechanism, routing is just one way to achieve this. The point of the sample is to show how an MCP server can dynamically adjust the available tools for each session based on arbitrary criteria, in this case, the URL route.
The server uses route parameters to determine which tools to make available:
GET /clock- MCP server with only clock/time toolsGET /calculator- MCP server with only calculation toolsGET /userinfo- MCP server with only session/system info toolsGET /allorGET /- MCP server with all tools (default)
-
Navigate to the sample directory:
cd samples/AspNetCoreMcpPerSessionTools -
Run the server:
dotnet run
-
The server will start on
https://localhost:5001(or the port shown in the console)
Connect your MCP client to: https://localhost:5001/clock
- Available tools: GetTime, GetDate, ConvertTimeZone
Connect your MCP client to: https://localhost:5001/calculator
- Available tools: Calculate, CalculatePercentage, SquareRoot
Connect your MCP client to: https://localhost:5001/userinfo
- Available tools: GetUserInfo
Connect your MCP client to: https://localhost:5001/all or https://localhost:5001/
- Available tools: All tools from all categories
All tools are registered during startup using the normal MCP tool registration:
builder.Services.AddMcpServer()
.WithTools<ClockTool>()
.WithTools<CalculatorTool>()
.WithTools<UserInfoTool>();The key technique is using ConfigureSessionOptions to modify the tool collection per session based on the route:
.WithHttpTransport(options =>
{
// Per-session tool filtering requires stateful mode. Set Stateless = false
// explicitly for forward compatibility in case the default changes.
options.Stateless = false;
options.ConfigureSessionOptions = async (httpContext, mcpOptions, cancellationToken) =>
{
var toolCategory = GetToolCategoryFromRoute(httpContext);
var toolCollection = mcpOptions.Capabilities?.Tools?.ToolCollection;
if (toolCollection != null)
{
// Clear all tools and add back only those for this category
toolCollection.Clear();
switch (toolCategory?.ToLower())
{
case "clock":
AddToolsForType<ClockTool>(toolCollection);
break;
case "calculator":
AddToolsForType<CalculatorTool>(toolCollection);
break;
case "userinfo":
AddToolsForType<UserInfoTool>(toolCollection);
break;
default:
// All tools for default/all category
AddToolsForType<ClockTool>(toolCollection);
AddToolsForType<CalculatorTool>(toolCollection);
AddToolsForType<UserInfoTool>(toolCollection);
break;
}
}
};
})The GetToolCategoryFromRoute method extracts the tool category from the URL route:
static string? GetToolCategoryFromRoute(HttpContext context)
{
if (context.Request.RouteValues.TryGetValue("toolCategory", out var categoryObj) && categoryObj is string category)
{
return string.IsNullOrEmpty(category) ? "all" : category;
}
return "all"; // Default
}