From 15679deab3b2491e0045585fa4ca4b2360f02211 Mon Sep 17 00:00:00 2001 From: JeffreySu Date: Sun, 20 Apr 2025 00:47:50 +0800 Subject: [PATCH] reference https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http streamable HTTP use /mcp, and retain the customization ability of the homepage --- .../McpEndpointRouteBuilderExtensions.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/ModelContextProtocol.AspNetCore/McpEndpointRouteBuilderExtensions.cs b/src/ModelContextProtocol.AspNetCore/McpEndpointRouteBuilderExtensions.cs index 35ec90c05..f37d5fc80 100644 --- a/src/ModelContextProtocol.AspNetCore/McpEndpointRouteBuilderExtensions.cs +++ b/src/ModelContextProtocol.AspNetCore/McpEndpointRouteBuilderExtensions.cs @@ -28,12 +28,19 @@ public static IEndpointConventionBuilder MapMcp(this IEndpointRouteBuilder endpo throw new InvalidOperationException("You must call WithHttpTransport(). Unable to find required services. Call builder.Services.AddMcpServer().WithHttpTransport() in application startup code."); var mcpGroup = endpoints.MapGroup(pattern); + var streamableHttpGroup = mcpGroup.MapGroup("") - .WithDisplayName(b => $"MCP Streamable HTTP | {b.DisplayName}") - .WithMetadata(new ProducesResponseTypeMetadata(StatusCodes.Status404NotFound, typeof(JsonRpcError), contentTypes: ["application/json"])); + .WithDisplayName(b => $"MCP Streamable HTTP | {b.DisplayName}") + .WithMetadata(new ProducesResponseTypeMetadata(StatusCodes.Status404NotFound, typeof(JsonRpcError), contentTypes: ["application/json"])); + streamableHttpGroup.MapPost("mcp", streamableHttpHandler.HandlePostRequestAsync) + .WithMetadata(new AcceptsMetadata(["application/json"])) + .WithMetadata(new ProducesResponseTypeMetadata(StatusCodes.Status200OK, contentTypes: ["text/event-stream"])) + .WithMetadata(new ProducesResponseTypeMetadata(StatusCodes.Status202Accepted)); + streamableHttpGroup.MapGet("mcp", streamableHttpHandler.HandleGetRequestAsync) + .WithMetadata(new ProducesResponseTypeMetadata(StatusCodes.Status200OK, contentTypes: ["text/event-stream"])); + streamableHttpGroup.MapDelete("mcp", streamableHttpHandler.HandleDeleteRequestAsync); - var routeGroup = endpoints.MapGroup(pattern); if (occupyRootUrl) { streamableHttpGroup.MapPost("", streamableHttpHandler.HandlePostRequestAsync)