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
|
|
@ -1,4 +1,5 @@
|
|||
using ModelContextProtocol.Protocol.Messages;
|
||||
using ModelContextProtocol.Protocol.Transport;
|
||||
using ModelContextProtocol.Utils;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
|
@ -30,7 +31,7 @@ internal sealed class RequestHandlers : Dictionary<string, Func<JsonRpcRequest,
|
|||
/// </remarks>
|
||||
public void Set<TRequest, TResponse>(
|
||||
string method,
|
||||
Func<TRequest?, CancellationToken, ValueTask<TResponse>> handler,
|
||||
Func<TRequest?, ITransport?, CancellationToken, ValueTask<TResponse>> handler,
|
||||
JsonTypeInfo<TRequest> requestTypeInfo,
|
||||
JsonTypeInfo<TResponse> responseTypeInfo)
|
||||
{
|
||||
|
|
@ -42,7 +43,7 @@ internal sealed class RequestHandlers : Dictionary<string, Func<JsonRpcRequest,
|
|||
this[method] = async (request, cancellationToken) =>
|
||||
{
|
||||
TRequest? typedRequest = JsonSerializer.Deserialize(request.Params, requestTypeInfo);
|
||||
object? result = await handler(typedRequest, cancellationToken).ConfigureAwait(false);
|
||||
object? result = await handler(typedRequest, request.RelatedTransport, cancellationToken).ConfigureAwait(false);
|
||||
return JsonSerializer.SerializeToNode(result, responseTypeInfo);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue