Add server-side Streamable HTTP transport support (#330)
- Changes IJsonRpcMessage to an abstract base class so RelatedTransport will always be available - Streamable HTTP supports multiple concurrent HTTP request with their own indpendent SSE response streams - RelatedTransport indicates the source or destination of the JsonRpcMessage - Changes the default RequestId to a JSON number for better compatibility with MCP servers in the wild
This commit is contained in:
parent
7d77be68cf
commit
7dcba357d4
1 changed files with 1637 additions and 395 deletions
37
src/ModelContextProtocol/Server/DestinationBoundMcpServer.cs
Normal file
37
src/ModelContextProtocol/Server/DestinationBoundMcpServer.cs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
using ModelContextProtocol.Protocol.Messages;
|
||||
using ModelContextProtocol.Protocol.Transport;
|
||||
using ModelContextProtocol.Protocol.Types;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace ModelContextProtocol.Server;
|
||||
|
||||
internal sealed class DestinationBoundMcpServer(McpServer server, ITransport? transport) : IMcpServer
|
||||
{
|
||||
public string EndpointName => server.EndpointName;
|
||||
public ClientCapabilities? ClientCapabilities => server.ClientCapabilities;
|
||||
public Implementation? ClientInfo => server.ClientInfo;
|
||||
public McpServerOptions ServerOptions => server.ServerOptions;
|
||||
public IServiceProvider? Services => server.Services;
|
||||
public LoggingLevel? LoggingLevel => server.LoggingLevel;
|
||||
|
||||
public ValueTask DisposeAsync() => server.DisposeAsync();
|
||||
|
||||
public IAsyncDisposable RegisterNotificationHandler(string method, Func<JsonRpcNotification, CancellationToken, ValueTask> handler) => server.RegisterNotificationHandler(method, handler);
|
||||
|
||||
// This will throw because the server must already be running for this class to be constructed, but it should give us a good Exception message.
|
||||
public Task RunAsync(CancellationToken cancellationToken) => server.RunAsync(cancellationToken);
|
||||
|
||||
public Task SendMessageAsync(JsonRpcMessage message, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Debug.Assert(message.RelatedTransport is null);
|
||||
message.RelatedTransport = transport;
|
||||
return server.SendMessageAsync(message, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<JsonRpcResponse> SendRequestAsync(JsonRpcRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Debug.Assert(request.RelatedTransport is null);
|
||||
request.RelatedTransport = transport;
|
||||
return server.SendRequestAsync(request, cancellationToken);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue