22using System . Text . Json ;
33using Microsoft . AspNetCore . Server . Kestrel . Core ;
44using NLog . Web ;
5+ using AspNetCoreRateLimit ;
56
67using mcp_nexus . Constants ;
78using mcp_nexus . Debugger ;
@@ -551,7 +552,7 @@ private static async Task RunHttpServer(string[] args, CommandLineArguments comm
551552
552553 ConfigureLogging ( webBuilder . Logging , commandLineArgs . ServiceMode ) ;
553554 RegisterServices ( webBuilder . Services , webBuilder . Configuration , commandLineArgs . CustomCdbPath ) ;
554- ConfigureHttpServices ( webBuilder . Services ) ;
555+ ConfigureHttpServices ( webBuilder . Services , webBuilder . Configuration ) ;
555556
556557 var app = webBuilder . Build ( ) ;
557558 ConfigureHttpPipeline ( app ) ;
@@ -703,14 +704,14 @@ private static void RegisterServices(IServiceCollection services, IConfiguration
703704 Console . Error . WriteLine ( "Registered McpResourceService for MCP resources support" ) ;
704705 }
705706
706- private static void ConfigureHttpServices ( IServiceCollection services )
707+ private static void ConfigureHttpServices ( IServiceCollection services , IConfiguration configuration )
707708 {
708709 Console . WriteLine ( "Configuring MCP server for HTTP..." ) ;
709710
710711 services . AddControllers ( )
711712 . AddJsonOptions ( options =>
712713 {
713- options . JsonSerializerOptions . PropertyNamingPolicy = JsonNamingPolicy . CamelCase ;
714+ options . JsonSerializerOptions . PropertyNamingPolicy = null ; // Don't change property names - MCP protocol requires exact field names
714715 options . JsonSerializerOptions . WriteIndented = true ;
715716 } ) ;
716717
@@ -738,14 +739,22 @@ private static void ConfigureHttpServices(IServiceCollection services)
738739 } ) ;
739740 } ) ;
740741
742+ // Configure rate limiting
743+ services . AddMemoryCache ( ) ;
744+ services . Configure < IpRateLimitOptions > ( configuration . GetSection ( "IpRateLimiting" ) ) ;
745+ services . AddSingleton < IIpPolicyStore , MemoryCacheIpPolicyStore > ( ) ;
746+ services . AddSingleton < IRateLimitCounterStore , MemoryCacheRateLimitCounterStore > ( ) ;
747+ services . AddSingleton < IRateLimitConfiguration , RateLimitConfiguration > ( ) ;
748+ services . AddSingleton < IProcessingStrategy , AsyncKeyLockProcessingStrategy > ( ) ;
749+
741750 // MIGRATION: Register tool discovery without stdio transport for HTTP mode
742751 // Note: Tools are discovered automatically via [McpServerToolType] attribute
743752 // HTTP mode uses controllers instead of stdio transport
744753
745754 // Register MCP services for HTTP endpoint compatibility
746- services . AddSingleton < McpToolDefinitionService > ( ) ;
747- services . AddSingleton < McpToolExecutionService > ( ) ;
748- services . AddSingleton < McpProtocolService > ( ) ;
755+ services . AddSingleton < IMcpToolDefinitionService , McpToolDefinitionService > ( ) ;
756+ services . AddSingleton < IMcpToolExecutionService , McpToolExecutionService > ( ) ;
757+ services . AddSingleton < IMcpProtocolService , McpProtocolService > ( ) ;
749758 // Note: IMcpNotificationService now registered in shared RegisterServices() method
750759
751760 Console . WriteLine ( "MCP server configured for HTTP with controllers, CORS, and tool discovery" ) ;
@@ -756,7 +765,7 @@ private static void ConfigureStdioServices(IServiceCollection services)
756765 Console . Error . WriteLine ( "Configuring MCP server for stdio..." ) ;
757766
758767 // Add the MCP protocol service for logging comparison
759- services . AddSingleton < McpProtocolService > ( ) ;
768+ services . AddSingleton < IMcpProtocolService , McpProtocolService > ( ) ;
760769
761770 // Note: IMcpNotificationService is now registered in shared RegisterServices() method
762771
@@ -774,6 +783,7 @@ private static void ConfigureHttpPipeline(WebApplication app)
774783 {
775784 Console . WriteLine ( "Configuring HTTP request pipeline..." ) ;
776785
786+ app . UseIpRateLimiting ( ) ;
777787 app . UseCors ( ) ;
778788 app . UseRouting ( ) ;
779789 app . MapControllers ( ) ;
0 commit comments