Table of Contents

Class McpClientTool

Namespace
ModelContextProtocol.Client
Assembly
ModelContextProtocol.dll

Provides an Microsoft.Extensions.AI.AIFunction that calls a tool via an IMcpClient.

public sealed class McpClientTool : AIFunction
Inheritance
AITool
AIFunction
McpClientTool
Inherited Members
AIFunction.UnderlyingMethod
AITool.ToString()

Remarks

The McpClientTool class encapsulates an IMcpClient along with a description of a tool available via that client, allowing it to be invoked as an Microsoft.Extensions.AI.AIFunction. 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 WithName(string) and WithDescription(string) without changing the underlying tool functionality.

Typically, you would get instances of this class by calling the ListToolsAsync(IMcpClient, JsonSerializerOptions?, CancellationToken) or EnumerateToolsAsync(IMcpClient, JsonSerializerOptions?, CancellationToken) extension methods on an IMcpClient instance.

Properties

AdditionalProperties

Gets any additional properties associated with the tool.

public override IReadOnlyDictionary<string, object?> AdditionalProperties { get; }

Property Value

IReadOnlyDictionary<string, object>

Description

Gets a description of the tool, suitable for use in describing the purpose to a model.

public override string Description { get; }

Property Value

string

JsonSchema

Gets a JSON Schema describing the function and its input parameters.

public override JsonElement JsonSchema { get; }

Property Value

JsonElement

Remarks

When specified, declares a self-contained JSON schema document that describes the function and its input parameters. A simple example of a JSON schema for a function that adds two numbers together is shown below:

{
  "title" : "addNumbers",
  "description": "A simple function that adds two numbers together.",
  "type": "object",
  "properties": {
    "a" : { "type": "number" },
    "b" : { "type": "number", "default": 1 }
  }, 
  "required" : ["a"]
}

The metadata present in the schema document plays an important role in guiding AI function invocation.

When no schema is specified, consuming chat clients should assume the "{}" or "true" schema, indicating that any JSON input is admissible.

JsonSerializerOptions

Gets a Microsoft.Extensions.AI.AIFunction.JsonSerializerOptions that can be used to marshal function parameters.

public override JsonSerializerOptions JsonSerializerOptions { get; }

Property Value

JsonSerializerOptions

Name

Gets the name of the tool.

public override string Name { get; }

Property Value

string

ProtocolTool

Gets the protocol Tool type for this instance.

public Tool ProtocolTool { get; }

Property Value

Tool

Remarks

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 WithName(string) or WithDescription(string).

Methods

InvokeCoreAsync(AIFunctionArguments, CancellationToken)

Invokes the Microsoft.Extensions.AI.AIFunction and returns its result.

protected override ValueTask<object?> InvokeCoreAsync(AIFunctionArguments arguments, CancellationToken cancellationToken)

Parameters

arguments AIFunctionArguments

The arguments to pass to the function's invocation.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests.

Returns

ValueTask<object>

The result of the function's execution.

WithDescription(string)

Creates a new instance of the tool but modified to return the specified description from its Description property.

public McpClientTool WithDescription(string description)

Parameters

description string

The description to give the tool.

Returns

McpClientTool

A new instance of McpClientTool with the provided description.

Remarks

Changing the description can help the model better understand the tool's purpose or provide more context about how the tool should be used. This is particularly useful when:

  • The original description is too technical or lacks clarity for the model
  • You want to add example usage scenarios to improve the model's understanding
  • You need to tailor the tool's description for specific model requirements

When invoking InvokeAsync(AIFunctionArguments, CancellationToken), the MCP server will still be called with the original tool description, so no mapping is required on the server side. This new description only affects the value returned from this instance's Microsoft.Extensions.AI.AITool.Description.

WithName(string)

Creates a new instance of the tool but modified to return the specified name from its Name property.

public McpClientTool WithName(string name)

Parameters

name string

The model-facing name to give the tool.

Returns

McpClientTool

A new instance of McpClientTool with the provided name.

Remarks

This is useful for optimizing the tool name for specific models or for prefixing the tool name with a namespace to avoid conflicts.

Changing the name can help with:

  • Making the tool name more intuitive for the model
  • Preventing name collisions when using tools from multiple sources
  • Creating specialized versions of a general tool for specific contexts

When invoking InvokeAsync(AIFunctionArguments, CancellationToken), the MCP server will still be called with the original tool name, so no mapping is required on the server side. This new name only affects the value returned from this instance's Microsoft.Extensions.AI.AITool.Name.