|
| 1 | +using Microsoft.AspNetCore.Http; |
| 2 | + |
| 3 | +namespace ModelContextProtocol.AspNetCore; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Handles MCP Streamable HTTP transport requests (POST, GET, DELETE) for an ASP.NET Core server. |
| 7 | +/// </summary> |
| 8 | +/// <remarks> |
| 9 | +/// <para> |
| 10 | +/// This handler is registered as a singleton service by <c>WithHttpTransport()</c> |
| 11 | +/// and is used internally by <c>MapMcp()</c> to map MCP endpoints using minimal APIs. |
| 12 | +/// </para> |
| 13 | +/// <para> |
| 14 | +/// It can also be injected directly into MVC controllers or other request-handling code |
| 15 | +/// to support scenarios where minimal APIs are not used: |
| 16 | +/// </para> |
| 17 | +/// <code> |
| 18 | +/// [ApiController] |
| 19 | +/// [Route("mcp")] |
| 20 | +/// public class McpController : ControllerBase |
| 21 | +/// { |
| 22 | +/// [HttpPost] |
| 23 | +/// public Task Post([FromServices] IStreamableHttpHandler handler) => handler.HandlePostRequestAsync(HttpContext); |
| 24 | +/// |
| 25 | +/// [HttpGet] |
| 26 | +/// public Task Get([FromServices] IStreamableHttpHandler handler) => handler.HandleGetRequestAsync(HttpContext); |
| 27 | +/// |
| 28 | +/// [HttpDelete] |
| 29 | +/// public Task Delete([FromServices] IStreamableHttpHandler handler) => handler.HandleDeleteRequestAsync(HttpContext); |
| 30 | +/// } |
| 31 | +/// </code> |
| 32 | +/// </remarks> |
| 33 | +public interface IStreamableHttpHandler |
| 34 | +{ |
| 35 | + /// <summary> |
| 36 | + /// Gets the configured <see cref="HttpServerTransportOptions"/> for the MCP server. |
| 37 | + /// </summary> |
| 38 | + HttpServerTransportOptions HttpServerTransportOptions { get; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Handles an MCP Streamable HTTP POST request containing a JSON-RPC message. |
| 42 | + /// </summary> |
| 43 | + /// <param name="context">The <see cref="HttpContext"/> for the current request.</param> |
| 44 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 45 | + /// <remarks> |
| 46 | + /// The response will be either a streamed SSE response containing JSON-RPC messages |
| 47 | + /// or a 202 Accepted response if the request contained only notifications. |
| 48 | + /// </remarks> |
| 49 | + Task HandlePostRequestAsync(HttpContext context); |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Handles an MCP Streamable HTTP GET request for receiving unsolicited server-to-client messages via SSE. |
| 53 | + /// </summary> |
| 54 | + /// <param name="context">The <see cref="HttpContext"/> for the current request.</param> |
| 55 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 56 | + /// <remarks> |
| 57 | + /// This endpoint keeps the connection open and streams SSE events to the client. |
| 58 | + /// It requires a valid <c>Mcp-Session-Id</c> header and is not available in stateless mode. |
| 59 | + /// </remarks> |
| 60 | + Task HandleGetRequestAsync(HttpContext context); |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Handles an MCP Streamable HTTP DELETE request to terminate an existing session. |
| 64 | + /// </summary> |
| 65 | + /// <param name="context">The <see cref="HttpContext"/> for the current request.</param> |
| 66 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 67 | + Task HandleDeleteRequestAsync(HttpContext context); |
| 68 | +} |
0 commit comments