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
1 changed files with 2703 additions and 686 deletions
164
src/ModelContextProtocol/Server/McpServerToolCollection.cs
Normal file
164
src/ModelContextProtocol/Server/McpServerToolCollection.cs
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
using ModelContextProtocol.Utils;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace ModelContextProtocol.Server;
|
||||
|
||||
/// <summary>Provides a thread-safe collection of <see cref="McpServerTool"/> instances, indexed by their names.</summary>
|
||||
public class McpServerToolCollection : ICollection<McpServerTool>, IReadOnlyCollection<McpServerTool>
|
||||
{
|
||||
/// <summary>Concurrent dictionary of tools, indexed by their names.</summary>
|
||||
private readonly ConcurrentDictionary<string, McpServerTool> _tools = [];
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="McpServerToolCollection"/> class.
|
||||
/// </summary>
|
||||
public McpServerToolCollection()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>Occurs when the collection is changed.</summary>
|
||||
/// <remarks>
|
||||
/// By default, this is raised when a tool is added or removed. However, a derived implementation
|
||||
/// may raise this event for other reasons, such as when a tool is modified.
|
||||
/// </remarks>
|
||||
public event EventHandler? Changed;
|
||||
|
||||
/// <summary>Gets the number of tools in the collection.</summary>
|
||||
public int Count => _tools.Count;
|
||||
|
||||
/// <summary>Gets whether there are any tools in the collection.</summary>
|
||||
public bool IsEmpty => _tools.IsEmpty;
|
||||
|
||||
/// <summary>Raises <see cref="Changed"/> if there are registered handlers.</summary>
|
||||
protected void RaiseChanged() => Changed?.Invoke(this, EventArgs.Empty);
|
||||
|
||||
/// <summary>Gets the <see cref="McpServerTool"/> with the specified <paramref name="name"/> from the collection.</summary>
|
||||
/// <param name="name">The name of the tool to retrieve.</param>
|
||||
/// <returns>The <see cref="McpServerTool"/> with the specified name.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="KeyNotFoundException">A tool with the specified name does not exist in the collection.</exception>
|
||||
public McpServerTool this[string name]
|
||||
{
|
||||
get
|
||||
{
|
||||
Throw.IfNull(name);
|
||||
return _tools[name];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Clears all tools from the collection.</summary>
|
||||
public virtual void Clear()
|
||||
{
|
||||
_tools.Clear();
|
||||
RaiseChanged();
|
||||
}
|
||||
|
||||
/// <summary>Adds the specified <see cref="McpServerTool"/> to the collection.</summary>
|
||||
/// <param name="tool">The tool to be added.</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="tool"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentException">A tool with the same name as <paramref name="tool"/> already exists in the collection.</exception>
|
||||
public void Add(McpServerTool tool)
|
||||
{
|
||||
if (!TryAdd(tool))
|
||||
{
|
||||
throw new ArgumentException($"A tool with the same name '{tool.ProtocolTool.Name}' already exists in the collection.", nameof(tool));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Adds the specified <see cref="McpServerTool"/> to the collection.</summary>
|
||||
/// <param name="tool">The tool to be added.</param>
|
||||
/// <returns><see langword="true"/> if the tool was added; otherwise, <see langword="false"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="tool"/> is <see langword="null"/>.</exception>
|
||||
public virtual bool TryAdd(McpServerTool tool)
|
||||
{
|
||||
Throw.IfNull(tool);
|
||||
|
||||
bool added = _tools.TryAdd(tool.ProtocolTool.Name, tool);
|
||||
if (added)
|
||||
{
|
||||
RaiseChanged();
|
||||
}
|
||||
|
||||
return added;
|
||||
}
|
||||
|
||||
/// <summary>Removes the specified toolfrom the collection.</summary>
|
||||
/// <param name="tool">The tool to be removed from the collection.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true"/> if the tool was found in the collection and removed; otherwise, <see langword="false"/> if it couldn't be found.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="tool"/> is <see langword="null"/>.</exception>
|
||||
public virtual bool Remove(McpServerTool tool)
|
||||
{
|
||||
Throw.IfNull(tool);
|
||||
|
||||
bool removed = ((ICollection<KeyValuePair<string, McpServerTool>>)_tools).Remove(new(tool.ProtocolTool.Name, tool));
|
||||
if (removed)
|
||||
{
|
||||
RaiseChanged();
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
/// <summary>Attempts to get the tool with the specified name from the collection.</summary>
|
||||
/// <param name="name">The name of the tool to retrieve.</param>
|
||||
/// <param name="tool">The tool, if found; otherwise, <see langword="null"/>.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true"/> if the tool was found in the collection and return; otherwise, <see langword="false"/> if it couldn't be found.
|
||||
/// </returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
|
||||
public virtual bool TryGetTool(string name, [NotNullWhen(true)] out McpServerTool? tool)
|
||||
{
|
||||
Throw.IfNull(name);
|
||||
return _tools.TryGetValue(name, out tool);
|
||||
}
|
||||
|
||||
/// <summary>Checks if a specific tool is present in the collection of tools.</summary>
|
||||
/// <param name="tool">The tool to search for in the collection.</param>
|
||||
/// <see langword="true"/> if the tool was found in the collection and return; otherwise, <see langword="false"/> if it couldn't be found.
|
||||
/// <exception cref="ArgumentNullException"><paramref name="tool"/> is <see langword="null"/>.</exception>
|
||||
public virtual bool Contains(McpServerTool tool)
|
||||
{
|
||||
Throw.IfNull(tool);
|
||||
return ((ICollection<KeyValuePair<string, McpServerTool>>)_tools).Contains(new(tool.ProtocolTool.Name, tool));
|
||||
}
|
||||
|
||||
/// <summary>Gets the names of all of the tools in the collection.</summary>
|
||||
public virtual ICollection<string> ToolNames => _tools.Keys;
|
||||
|
||||
/// <summary>Creates an array containing all of the tools in the collection.</summary>
|
||||
/// <returns>An array containing all of the tools in the collection.</returns>
|
||||
public virtual McpServerTool[] ToArray() => _tools.Values.ToArray();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual void CopyTo(McpServerTool[] array, int arrayIndex)
|
||||
{
|
||||
Throw.IfNull(array);
|
||||
|
||||
foreach (var entry in _tools)
|
||||
{
|
||||
array[arrayIndex++] = entry.Value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual IEnumerator<McpServerTool> GetEnumerator()
|
||||
{
|
||||
foreach (var entry in _tools)
|
||||
{
|
||||
yield return entry.Value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator<McpServerTool> IEnumerable<McpServerTool>.GetEnumerator() => GetEnumerator();
|
||||
|
||||
/// <inheritdoc/>
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
/// <inheritdoc/>
|
||||
bool ICollection<McpServerTool>.IsReadOnly => false;
|
||||
}
|
||||
Loading…
Reference in a new issue