* 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
86 lines
No EOL
3 KiB
C#
86 lines
No EOL
3 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using ModelContextProtocol.Client;
|
|
using ModelContextProtocol.Configuration;
|
|
using ModelContextProtocol.Protocol.Transport;
|
|
using ModelContextProtocol.Server;
|
|
using ModelContextProtocol.Tests.Transport;
|
|
using System.IO.Pipelines;
|
|
|
|
namespace ModelContextProtocol.Tests.Client;
|
|
|
|
public class McpClientExtensionsTests
|
|
{
|
|
private Pipe _clientToServerPipe = new();
|
|
private Pipe _serverToClientPipe = new();
|
|
private readonly IMcpServer _server;
|
|
|
|
public McpClientExtensionsTests()
|
|
{
|
|
ServiceCollection sc = new();
|
|
sc.AddSingleton<IServerTransport>(new StdioServerTransport("TestServer", _clientToServerPipe.Reader.AsStream(), _serverToClientPipe.Writer.AsStream()));
|
|
sc.AddMcpServer();
|
|
for (int f = 0; f < 10; f++)
|
|
{
|
|
string name = $"Method{f}";
|
|
sc.AddSingleton(McpServerTool.Create((int i) => $"{name} Result {i}", name));
|
|
}
|
|
_server = sc.BuildServiceProvider().GetRequiredService<IMcpServer>();
|
|
}
|
|
|
|
public ValueTask DisposeAsync()
|
|
{
|
|
_clientToServerPipe.Writer.Complete();
|
|
_serverToClientPipe.Writer.Complete();
|
|
return _server.DisposeAsync();
|
|
}
|
|
|
|
private async Task<IMcpClient> CreateMcpClientForServer()
|
|
{
|
|
await _server.StartAsync(TestContext.Current.CancellationToken);
|
|
|
|
var stdin = new StreamReader(_serverToClientPipe.Reader.AsStream());
|
|
var stdout = new StreamWriter(_clientToServerPipe.Writer.AsStream());
|
|
|
|
var serverConfig = new McpServerConfig()
|
|
{
|
|
Id = "TestServer",
|
|
Name = "TestServer",
|
|
TransportType = "ignored",
|
|
};
|
|
|
|
return await McpClientFactory.CreateAsync(
|
|
serverConfig,
|
|
createTransportFunc: (_, _) => new StreamClientTransport(stdin, stdout),
|
|
cancellationToken: TestContext.Current.CancellationToken);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ListToolsAsync_AllToolsReturned()
|
|
{
|
|
IMcpClient client = await CreateMcpClientForServer();
|
|
|
|
var tools = await client.ListToolsAsync(TestContext.Current.CancellationToken);
|
|
Assert.Equal(10, tools.Count);
|
|
var echo = tools.Single(t => t.Name == "Method4");
|
|
var result = await echo.InvokeAsync(new Dictionary<string, object?>() { ["i"] = 42 }, TestContext.Current.CancellationToken);
|
|
Assert.Contains("Method4 Result 42", result?.ToString());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task EnumerateToolsAsync_AllToolsReturned()
|
|
{
|
|
IMcpClient client = await CreateMcpClientForServer();
|
|
|
|
await foreach (var tool in client.EnumerateToolsAsync(TestContext.Current.CancellationToken))
|
|
{
|
|
if (tool.Name == "Method4")
|
|
{
|
|
var result = await tool.InvokeAsync(new Dictionary<string, object?>() { ["i"] = 42 }, TestContext.Current.CancellationToken);
|
|
Assert.Contains("Method4 Result 42", result?.ToString());
|
|
return;
|
|
}
|
|
}
|
|
|
|
Assert.Fail("Couldn't find target method");
|
|
}
|
|
} |