using ModelContextProtocol.Protocol.Types;
using ModelContextProtocol.Utils.Json;
using Microsoft.Extensions.AI;
using System.Text.Json;
using System.Collections.ObjectModel;
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 IMcpClient _client;
private readonly string _name;
private readonly string _description;
internal McpClientTool(IMcpClient client, Tool tool, JsonSerializerOptions serializerOptions, string? name = null, string? description = null)
{
_client = client;
ProtocolTool = tool;
JsonSerializerOptions = serializerOptions;
_name = name ?? tool.Name;
_description = description ?? tool.Description ?? string.Empty;
}
///
/// 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;
///
public override string Description => _description;
///
public override JsonElement JsonSchema => ProtocolTool.InputSchema;
///
public override JsonSerializerOptions JsonSerializerOptions { get; }
///
public override IReadOnlyDictionary AdditionalProperties => s_additionalProperties;
///
protected async override ValueTask