Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Add AspNetCoreSseServer sample #81

Merged
merged 5 commits into from
Mar 18, 2025
Merged

Conversation

halter73
Copy link
Collaborator

This PR demonstrates how you could use a modified AddMcpServer API in ASP.NET Core sample app to implement a MapMcpSse() API (name tbd) that exposes tools registered using .WithTools(). It's inspired a bit by https://github.com/modelcontextprotocol/servers/blob/7034c3d13c72eaf2f188908e9cd54e6d6b091272/src/everything/sse.ts. It doesn't handle anything advanced like concurrent sessions yet.

I haven't added any projects other than the new sample app for now. However, the AspNetCoreSseMcpTransport could be renamed to something like ResponseStreamSseTransport and moved into the main mcpdotnet package. It wouldn't require adding any ASP.NET Core dependencies, but we would need to update the System.Net.ServerSentEvents dependency to be at least 10.0.0-preview.1.25080.5 which might be awkward for a non-preview package.

Contributes to #24

eiriktsarpalis
eiriktsarpalis previously approved these changes Mar 14, 2025
Copy link
Collaborator

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@PederHP
Copy link
Owner

PederHP commented Mar 15, 2025

#73 has had a bit of clean-up and the partial multi-client support has been removed to be added properly in a future PR. Feel free to merge it with yours as we discussed, otherwise I can merge the clean-up in a subsequent PR.

/// to <see cref="Console.Out"/>, as that will interfere with the transport's output.
/// </para>
/// </remarks>
public StdioServerTransport(IOptions<McpServerOptions> serverOptions, ILoggerFactory? loggerFactory = null)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the benefit of having a ctor that takes an IOptions<McpServerOptions> when there's already an overload that takes an McpServerOptions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part wasn't important to the AspNetCoreSseServer sample which doesn't use this transport. I put it in a separate "Fix TestServerWithHosting" ee96253 (#81) commit since it isn't required for this sample.

Before this change, the TestServerWithHosting sample didn't work because the McpServerFactory was unable to resolve the IServerTransport, StdioServerTransport, due to being able to find a service for McpServerOptions. IOptions<McpServerOptions> works and also allows you to call methods supported by the options pattern like .Configure<McpServerOptions>(options => ...)

byte[] buffer = Encoding.UTF8.GetBytes("Accepted");
response.OutputStream.Write(buffer, 0, buffer.Length);
#pragma warning restore CA2016 // Forward the 'CancellationToken' parameter to methods
#pragma warning disable CA1835 // Prefer the 'Memory'-based overloads for 'ReadAsync' and 'WriteAsync'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use the Memory-based overload under an ifdef for NET

@halter73 halter73 changed the base branch from feat/sse_server_support to main March 18, 2025 00:04
@halter73 halter73 dismissed eiriktsarpalis’s stale review March 18, 2025 00:04

The base branch was changed.

@halter73
Copy link
Collaborator Author

The PR approval from earlier got dismissed because I rebased on top of main. Can someone please reapprove? Thanks.


/// <summary>
/// Server capabilities to advertise to the server.
/// </summary>
public ServerCapabilities? Capabilities { get; init; }
public ServerCapabilities? Capabilities { get; set; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're making all of these mutable, can you follow-up after this to make other stuff mutable as well? e.g. seems like there's no an inconsistency between McpServerOptions and McpClientOptions. We should have the same story for all these types.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Do you want me to do that before merging or in a follow up PR?

@eiriktsarpalis eiriktsarpalis merged commit 90dfc35 into PederHP:main Mar 18, 2025
1 check passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants