-
Notifications
You must be signed in to change notification settings - Fork 469
Implement DefaultMcpEndpointRouteBuilderConfigurator for MCP endpoint configuration #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
DavidParks8
wants to merge
1
commit into
modelcontextprotocol:main
Choose a base branch
from
DavidParks8:daparks/streamableinterface
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−41
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/ModelContextProtocol.AspNetCore/DefaultMcpEndpointRouteBuilderConfigurator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Metadata; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using ModelContextProtocol.Protocol; | ||
|
||
namespace ModelContextProtocol.AspNetCore; | ||
|
||
/// <summary> | ||
/// Default implementation that wires up the MCP Streamable HTTP transport endpoints | ||
/// (and legacy SSE endpoints when applicable). | ||
/// </summary> | ||
internal sealed class DefaultMcpEndpointRouteBuilderConfigurator( | ||
StreamableHttpHandler streamableHttpHandler | ||
) : IMcpEndpointRouteBuilderConfigurator | ||
{ | ||
public IEndpointConventionBuilder Configure(IEndpointRouteBuilder endpoints, string pattern) | ||
{ | ||
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"] | ||
) | ||
); | ||
|
||
streamableHttpGroup | ||
.MapPost("", streamableHttpHandler.HandlePostRequestAsync) | ||
.WithMetadata(new AcceptsMetadata(["application/json"])) | ||
.WithMetadata( | ||
new ProducesResponseTypeMetadata( | ||
StatusCodes.Status200OK, | ||
contentTypes: ["text/event-stream"] | ||
) | ||
) | ||
.WithMetadata(new ProducesResponseTypeMetadata(StatusCodes.Status202Accepted)); | ||
|
||
if (!streamableHttpHandler.HttpServerTransportOptions.Stateless) | ||
{ | ||
// The GET and DELETE endpoints are not mapped in Stateless mode since there's no way to send unsolicited messages | ||
// for the GET to handle, and there is no server-side state for the DELETE to clean up. | ||
streamableHttpGroup | ||
.MapGet("", streamableHttpHandler.HandleGetRequestAsync) | ||
.WithMetadata( | ||
new ProducesResponseTypeMetadata( | ||
StatusCodes.Status200OK, | ||
contentTypes: ["text/event-stream"] | ||
) | ||
); | ||
streamableHttpGroup.MapDelete("", streamableHttpHandler.HandleDeleteRequestAsync); | ||
|
||
// Map legacy HTTP with SSE endpoints only if not in Stateless mode, because we cannot guarantee the /message requests | ||
// will be handled by the same process as the /sse request. | ||
var sseHandler = endpoints.ServiceProvider.GetRequiredService<SseHandler>(); | ||
var sseGroup = mcpGroup | ||
.MapGroup("") | ||
.WithDisplayName(b => $"MCP HTTP with SSE | {b.DisplayName}"); | ||
|
||
sseGroup | ||
.MapGet("/sse", sseHandler.HandleSseRequestAsync) | ||
.WithMetadata( | ||
new ProducesResponseTypeMetadata( | ||
StatusCodes.Status200OK, | ||
contentTypes: ["text/event-stream"] | ||
) | ||
); | ||
sseGroup | ||
.MapPost("/message", sseHandler.HandleMessageRequestAsync) | ||
.WithMetadata(new AcceptsMetadata(["application/json"])) | ||
.WithMetadata(new ProducesResponseTypeMetadata(StatusCodes.Status202Accepted)); | ||
} | ||
|
||
return mcpGroup; | ||
} | ||
|
||
IEndpointConventionBuilder IMcpEndpointRouteBuilderConfigurator.Configure(IEndpointRouteBuilder endpoints, string pattern) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/ModelContextProtocol.AspNetCore/IMcpEndpointRouteBuilderConfigurator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Routing; | ||
|
||
namespace ModelContextProtocol.AspNetCore; | ||
|
||
/// <summary> | ||
/// Abstraction for configuring MCP endpoints on an <see cref="IEndpointRouteBuilder"/>. | ||
/// Register an implementation in DI to override the default MapMcp behavior and enable hot swapping | ||
/// of the transport/endpoints wiring without changing application code. | ||
/// </summary> | ||
public interface IMcpEndpointRouteBuilderConfigurator | ||
{ | ||
/// <summary> | ||
/// Configures the MCP endpoints on the provided <paramref name="endpoints"/> using the given <paramref name="pattern"/>. | ||
/// Implementations should return the <see cref="IEndpointConventionBuilder"/> representing the mapped group | ||
/// to allow callers to apply additional endpoint conventions (e.g., authorization). | ||
/// </summary> | ||
/// <param name="endpoints">The endpoint route builder to attach MCP endpoints to.</param> | ||
/// <param name="pattern">The route pattern prefix to map to.</param> | ||
/// <returns>An <see cref="IEndpointConventionBuilder"/> that can be used to configure conventions.</returns> | ||
IEndpointConventionBuilder Configure(IEndpointRouteBuilder endpoints, string pattern); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should add a hook for a service to replace the implementation of
MapMcp()
entirely. If you want something that behaves radically differently, you can create your ownMapWhateverMcp()
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It isn't radically different though, It is nearly exactly the same, minus the stateless behavior. Do we really want to force people to have to learn about MapWhateverMcp calls? I can guarantee you that some people will forget to change to the other MapWhatever call and will file an issue about "MapMcp" isn't working