Overhaul tool handling (#89)
* 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
This commit is contained in:
parent
aafa9d929d
commit
caca2ae61d
5 changed files with 2703 additions and 686 deletions
|
|
@ -25,6 +25,7 @@ public sealed class StdioServerTransport : TransportBase, IServerTransport
|
|||
private readonly TextReader _stdInReader;
|
||||
private readonly Stream _stdOutStream;
|
||||
|
||||
private SemaphoreSlim _sendLock = new(1, 1);
|
||||
private Task? _readTask;
|
||||
private CancellationTokenSource? _shutdownCts;
|
||||
|
||||
|
|
@ -137,6 +138,8 @@ public sealed class StdioServerTransport : TransportBase, IServerTransport
|
|||
/// <inheritdoc/>
|
||||
public override async Task SendMessageAsync(IJsonRpcMessage message, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var _ = await _sendLock.LockAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (!IsConnected)
|
||||
{
|
||||
_logger.TransportNotConnected(EndpointName);
|
||||
|
|
|
|||
|
|
@ -16,5 +16,5 @@ public class CallToolRequestParams
|
|||
/// Optional arguments to pass to the tool.
|
||||
/// </summary>
|
||||
[System.Text.Json.Serialization.JsonPropertyName("arguments")]
|
||||
public Dictionary<string,object>? Arguments { get; init; }
|
||||
public Dictionary<string,object>? Arguments { get; init; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -155,7 +155,7 @@ public record ResourcesCapability
|
|||
public record ToolsCapability
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether this server supports notifications for changes to the tool list.
|
||||
/// Gets or sets whether this server supports notifications for changes to the tool list.
|
||||
/// </summary>
|
||||
[JsonPropertyName("listChanged")]
|
||||
public bool? ListChanged { get; init; }
|
||||
|
|
@ -171,4 +171,16 @@ public record ToolsCapability
|
|||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public Func<RequestContext<CallToolRequestParams>, CancellationToken, Task<CallToolResponse>>? CallToolHandler { get; init; }
|
||||
|
||||
/// <summary>Gets or sets a collection of tools served by the server.</summary>
|
||||
/// <remarks>
|
||||
/// Tools will specified via <see cref="ToolCollection"/> augment the <see cref="ListToolsHandler"/> and
|
||||
/// <see cref="CallToolHandler"/>, if provided. ListTools requests will output information about every tool
|
||||
/// in <see cref="ToolCollection"/> and then also any tools output by <see cref="ListToolsHandler"/>, if it's
|
||||
/// non-<see langword="null"/>. CallTool requests will first check <see cref="ToolCollection"/> for the tool
|
||||
/// being requested, and if the tool is not found in the <see cref="ToolCollection"/>, any specified <see cref="CallToolHandler"/>
|
||||
/// will be invoked as a fallback.
|
||||
/// </remarks>
|
||||
[JsonIgnore]
|
||||
public McpServerToolCollection? ToolCollection { get; init; }
|
||||
}
|
||||
|
|
@ -1,6 +1,4 @@
|
|||
using ModelContextProtocol.Protocol.Types;
|
||||
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ModelContextProtocol.Protocol.Types;
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ namespace ModelContextProtocol.Protocol.Types;
|
|||
/// </summary>
|
||||
public class Tool
|
||||
{
|
||||
private JsonElement _inputSchema = McpJsonUtilities.DefaultMcpToolSchema;
|
||||
|
||||
/// <summary>
|
||||
/// The name of the tool.
|
||||
/// </summary>
|
||||
|
|
@ -42,6 +44,4 @@ public class Tool
|
|||
_inputSchema = value;
|
||||
}
|
||||
}
|
||||
|
||||
private JsonElement _inputSchema = McpJsonUtilities.DefaultMcpToolSchema;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue