* Overhaul tool handling
- Renames [McpTool{Type}] to [McpServerTool{Type}], in order to distinguish representations of tools on the server from tools on the client.
- Enables [McpServerTool] methods to have arguments injected from DI, as well as a specific set of types from the implementation directly, like IMcpServer.
- Renames WithTools to WithToolsFromAssembly.
- All of the WithToolsXx methods now publish each individual McpServerTool into DI; other code can do so as well. The options setup code gathers all of the tools from DI and combines them into a collection which is then stored in McpServerOptions and used to construct the server.
- The server tools specified via DI as well as manually-provided handlers, using CallToolHandler as a fallback in case the requested tool doesn't exist in the tools collection, and ListToolsHandler augments the information for whatever tools exist.
- The tools are stored in McpServerOptions in a new McpServerToolCollection type, which is a thread-safe container that exposes add/removal notification. Adding/removing tools triggers a change notification that in turn sends a notification to the client about a tools update.
- The ServerOptions are exposed from the server instance.
- Removed cursor-based APIs from McpClientExtensions.
- Changed McpClientExtensions APIs to return `Task<IList<...>` rather than `IAsyncEnumerable<...>`.
I'm sure this will need to evolve further before we're "done", but it's a significant improvement from where we are now. One area I'm not super happy with is the notifying collection; that might work ok for a stdio server, where you can grab the created collection from the server's options and mutate it, but I don't know how well that's going to work for sse servers.
* Make McpServerTool{Collection} extensible
* Fix README.md example
* Add notifications test
* Address feedback
83 lines
2.6 KiB
C#
83 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 : IServerTransport
|
|
{
|
|
private readonly Channel<IJsonRpcMessage> _messageChannel;
|
|
private bool _isStarted;
|
|
|
|
public bool IsConnected => _isStarted;
|
|
|
|
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,
|
|
});
|
|
}
|
|
|
|
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
|
|
|
public async Task SendMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default)
|
|
{
|
|
SentMessages.Add(message);
|
|
if (message is JsonRpcRequest request)
|
|
{
|
|
if (request.Method == "roots/list")
|
|
await ListRoots(request, cancellationToken);
|
|
else if (request.Method == "sampling/createMessage")
|
|
await Sampling(request, cancellationToken);
|
|
else
|
|
await WriteMessageAsync(request, cancellationToken);
|
|
}
|
|
else if (message is JsonRpcNotification notification)
|
|
{
|
|
await WriteMessageAsync(notification, cancellationToken);
|
|
}
|
|
|
|
OnMessageSent?.Invoke(message);
|
|
}
|
|
|
|
public Task StartListeningAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
_isStarted = true;
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
protected async Task WriteMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default)
|
|
{
|
|
await _messageChannel.Writer.WriteAsync(message, cancellationToken);
|
|
}
|
|
}
|