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:
Stephen Halter 2025-04-18 17:07:23 -07:00 • committed by GitHub
commit 7dcba357d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 1637 additions and 395 deletions

View file

@ -6,7 +6,7 @@ using System.Text.Json.Serialization;
namespace ModelContextProtocol.Utils.Json;
/// <summary>
/// Provides a <see cref="JsonConverter"/> for <see cref="IJsonRpcMessage"/> messages,
/// Provides a <see cref="JsonConverter"/> for <see cref="JsonRpcMessage"/> messages,
/// handling polymorphic deserialization of different message types.
/// </summary>
/// <remarks>
@ -26,10 +26,10 @@ namespace ModelContextProtocol.Utils.Json;
/// </para>
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class JsonRpcMessageConverter : JsonConverter<IJsonRpcMessage>
public sealed class JsonRpcMessageConverter : JsonConverter<JsonRpcMessage>
{
/// <inheritdoc/>
public override IJsonRpcMessage? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override JsonRpcMessage? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
{
@ -87,7 +87,7 @@ public sealed class JsonRpcMessageConverter : JsonConverter<IJsonRpcMessage>
}
/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, IJsonRpcMessage value, JsonSerializerOptions options)
public override void Write(Utf8JsonWriter writer, JsonRpcMessage value, JsonSerializerOptions options)
{
switch (value)
{

View file

@ -82,7 +82,8 @@ public static partial class McpJsonUtilities
NumberHandling = JsonNumberHandling.AllowReadingFromString)]
// JSON-RPC
[JsonSerializable(typeof(IJsonRpcMessage))]
[JsonSerializable(typeof(JsonRpcMessage))]
[JsonSerializable(typeof(JsonRpcMessage[]))]
[JsonSerializable(typeof(JsonRpcRequest))]
[JsonSerializable(typeof(JsonRpcNotification))]
[JsonSerializable(typeof(JsonRpcResponse))]