csharp-sdk/tests/ModelContextProtocol.Tests/Utils/TestServerTransport.cs
Stephen Toub 2c75acded8
Improve progress reporting (#145)
* Improve progress reporting

- Enable injecting an `IProgress<>` into a tool call to report progress when the client has supplied a progress token
- Add a custom type for ProgressToken and ProgressNotification to map to the schema
- Simplify RequestId to match ProgressToken's shape
- Serialize StdioClientTransport's SendMessageAsync implementation
- Add strongly-typed names for all request methods
- Change all request IDs created by McpJsonRpcEndpoint to include a guid for that endpoint

* Address PR feedback
2025-03-31 09:33:16 -04:00

82 lines
2.6 KiB
C#

using System.Threading.Channels;
using ModelContextProtocol.Protocol.Messages;
using ModelContextProtocol.Protocol.Transport;
using ModelContextProtocol.Protocol.Types;
namespace ModelContextProtocol.Tests.Utils;
public class TestServerTransport : ITransport
{
private readonly Channel<IJsonRpcMessage> _messageChannel;
public bool IsConnected { get; set; }
public ChannelReader<IJsonRpcMessage> MessageReader => _messageChannel;
public List<IJsonRpcMessage> SentMessages { get; } = [];
public Action<IJsonRpcMessage>? OnMessageSent { get; set; }
public TestServerTransport()
{
_messageChannel = Channel.CreateUnbounded<IJsonRpcMessage>(new UnboundedChannelOptions
{
SingleReader = true,
SingleWriter = true,
});
IsConnected = true;
}
public ValueTask DisposeAsync()
{
_messageChannel.Writer.TryComplete();
IsConnected = false;
return ValueTask.CompletedTask;
}
public async Task SendMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default)
{
SentMessages.Add(message);
if (message is JsonRpcRequest request)
{
if (request.Method == RequestMethods.RootsList)
await ListRoots(request, cancellationToken);
else if (request.Method == RequestMethods.SamplingCreateMessage)
await Sampling(request, cancellationToken);
else
await WriteMessageAsync(request, cancellationToken);
}
else if (message is JsonRpcNotification notification)
{
await WriteMessageAsync(notification, cancellationToken);
}
OnMessageSent?.Invoke(message);
}
private async Task ListRoots(JsonRpcRequest request, CancellationToken cancellationToken)
{
await WriteMessageAsync(new JsonRpcResponse
{
Id = request.Id,
Result = new ModelContextProtocol.Protocol.Types.ListRootsResult
{
Roots = []
}
}, cancellationToken);
}
private async Task Sampling(JsonRpcRequest request, CancellationToken cancellationToken)
{
await WriteMessageAsync(new JsonRpcResponse
{
Id = request.Id,
Result = new CreateMessageResult { Content = new(), Model = "model", Role = "role" }
}, cancellationToken);
}
private async Task WriteMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default)
{
await _messageChannel.Writer.WriteAsync(message, cancellationToken);
}
}