using Microsoft.Extensions.AI;
using ModelContextProtocol.Protocol;
using System.Collections.ObjectModel;
using System.Text.Json;
namespace ModelContextProtocol.Client;
///
/// Provides an that calls a tool via an .
///
///
///
/// The class encapsulates an along with a description of
/// a tool available via that client, allowing it to be invoked as an . This enables integration
/// with AI models that support function calling capabilities.
///
///
/// Tools retrieved from an MCP server can be customized for model presentation using methods like
/// and without changing the underlying tool functionality.
///
///
/// Typically, you would get instances of this class by calling the
/// or extension methods on an instance.
///
///
public sealed class McpClientTool : AIFunction
{
/// Additional properties exposed from tools.
private static readonly ReadOnlyDictionary s_additionalProperties =
new(new Dictionary()
{
["Strict"] = false, // some MCP schemas may not meet "strict" requirements
});
private readonly McpClient _client;
private readonly string _name;
private readonly string _description;
private readonly IProgress? _progress;
internal McpClientTool(
McpClient client,
Tool tool,
JsonSerializerOptions serializerOptions,
string? name = null,
string? description = null,
IProgress? progress = null)
{
_client = client;
ProtocolTool = tool;
JsonSerializerOptions = serializerOptions;
_name = name ?? tool.Name;
_description = description ?? tool.Description ?? string.Empty;
_progress = progress;
}
///
/// Gets the protocol type for this instance.
///
///
/// This property provides direct access to the underlying protocol representation of the tool,
/// which can be useful for advanced scenarios or when implementing custom MCP client extensions.
/// It contains the original metadata about the tool as provided by the server, including its
/// name, description, and schema information before any customizations applied through methods
/// like or .
///
public Tool ProtocolTool { get; }
///
public override string Name => _name;
/// Gets the tool's title.
public string? Title => ProtocolTool.Title ?? ProtocolTool.Annotations?.Title;
///
public override string Description => _description;
///
public override JsonElement JsonSchema => ProtocolTool.InputSchema;
///
public override JsonElement? ReturnJsonSchema => ProtocolTool.OutputSchema;
///
public override JsonSerializerOptions JsonSerializerOptions { get; }
///
public override IReadOnlyDictionary AdditionalProperties => s_additionalProperties;
///
protected async override ValueTask