Table of Contents

Class McpServerTool

Namespace
ModelContextProtocol.Server
Assembly
ModelContextProtocol.dll

Represents an invocable tool used by Model Context Protocol clients and servers.

public abstract class McpServerTool : IMcpServerPrimitive
Inheritance
McpServerTool
Implements
Derived
Inherited Members

Remarks

McpServerTool is an abstract base class that represents an MCP tool for use in the server (as opposed to Tool, which provides the protocol representation of a tool, and McpClientTool, which provides a client-side representation of a tool). Instances of McpServerTool can be added into a IServiceCollection to be picked up automatically when McpServerFactory is used to create an IMcpServer, or added into a McpServerPrimitiveCollection<T>.

Most commonly, McpServerTool instances are created using the static McpServerTool.Create methods. These methods enable creating an McpServerTool for a method, specified via a Delegate or MethodInfo, and are what are used implicitly by WithToolsFromAssembly(IMcpServerBuilder, Assembly?, JsonSerializerOptions?) and McpServerBuilderExtensions.WithTools. The McpServerTool.Create methods create McpServerTool instances capable of working with a large variety of .NET method signatures, automatically handling how parameters are marshaled into the method from the JSON received from the MCP client, and how the return value is marshaled back into the CallToolResponse that's then serialized and sent back to the client.

By default, parameters are sourced from the Arguments dictionary, which is a collection of key/value pairs, and are represented in the JSON schema for the function, as exposed in the returned McpServerTool's ProtocolTool's InputSchema. Those parameters are deserialized from the JsonElement values in that collection. There are a few exceptions to this:

All other parameters are deserialized from the JsonElements in the Arguments dictionary, using the JsonSerializerOptions supplied in SerializerOptions, or if none was provided, using DefaultOptions.

In general, the data supplied via the Arguments's dictionary is passed along from the caller and should thus be considered unvalidated and untrusted. To provide validated and trusted data to the invocation of the tool, consider having the tool be an instance method, referring to data stored in the instance, or using an instance or parameters resolved from the IServiceProvider to provide data to the method.

Return values from a method are used to create the CallToolResponse that is sent back to the client:

nullReturns an empty Content list.
Microsoft.Extensions.AI.AIContentConverted to a single Content object using ModelContextProtocol.AIContentExtensions.ToContent(Microsoft.Extensions.AI.AIContent).
stringConverted to a single Content object with Text set to the string value and Type set to "text".
ContentReturned as a single-item Content list.
IEnumerable<T> of stringEach string is converted to a Content object with Text set to the string value and Type set to "text".
IEnumerable<T> of Microsoft.Extensions.AI.AIContentEach Microsoft.Extensions.AI.AIContent is converted to a Content object using ModelContextProtocol.AIContentExtensions.ToContent(Microsoft.Extensions.AI.AIContent).
IEnumerable<T> of ContentReturned as the Content list.
CallToolResponseReturned directly without modification.
Other typesSerialized to JSON and returned as a single Content object with Type set to "text".

Constructors

McpServerTool()

Initializes a new instance of the McpServerTool class.

protected McpServerTool()

Properties

ProtocolTool

Gets the protocol Tool type for this instance.

public abstract Tool ProtocolTool { get; }

Property Value

Tool

Methods

Create(AIFunction, McpServerToolCreateOptions?)

Creates an McpServerTool that wraps the specified Microsoft.Extensions.AI.AIFunction.

public static McpServerTool Create(AIFunction function, McpServerToolCreateOptions? options = null)

Parameters

function AIFunction

The function to wrap.

options McpServerToolCreateOptions

Optional options used in the creation of the McpServerTool to control its behavior.

Returns

McpServerTool

Remarks

Unlike the other overloads of Create, the McpServerTool created by Create(AIFunction, McpServerToolCreateOptions?) does not provide all of the special parameter handling for MCP-specific concepts, like IMcpServer.

Exceptions

ArgumentNullException

function is null.

Create(Delegate, McpServerToolCreateOptions?)

Creates an McpServerTool instance for a method, specified via a Delegate instance.

public static McpServerTool Create(Delegate method, McpServerToolCreateOptions? options = null)

Parameters

method Delegate

The method to be represented via the created McpServerTool.

options McpServerToolCreateOptions

Optional options used in the creation of the McpServerTool to control its behavior.

Returns

McpServerTool

The created McpServerTool for invoking method.

Exceptions

ArgumentNullException

method is null.

Create(MethodInfo, object?, McpServerToolCreateOptions?)

Creates an McpServerTool instance for a method, specified via a Delegate instance.

public static McpServerTool Create(MethodInfo method, object? target = null, McpServerToolCreateOptions? options = null)

Parameters

method MethodInfo

The method to be represented via the created McpServerTool.

target object

The instance if method is an instance method; otherwise, null.

options McpServerToolCreateOptions

Optional options used in the creation of the McpServerTool to control its behavior.

Returns

McpServerTool

The created McpServerTool for invoking method.

Exceptions

ArgumentNullException

method is null.

ArgumentException

method is an instance method but target is null.

Create(MethodInfo, Type, McpServerToolCreateOptions?)

Creates an McpServerTool instance for a method, specified via an MethodInfo for and instance method, along with a Type representing the type of the target object to instantiate each time the method is invoked.

public static McpServerTool Create(MethodInfo method, Type targetType, McpServerToolCreateOptions? options = null)

Parameters

method MethodInfo

The instance method to be represented via the created Microsoft.Extensions.AI.AIFunction.

targetType Type

The Type to construct an instance of on which to invoke method when the resulting Microsoft.Extensions.AI.AIFunction is invoked. If services are provided, ActivatorUtilities.CreateInstance will be used to construct the instance using those services; otherwise, CreateInstance(Type) is used, utilizing the type's public parameterless constructor. If an instance can't be constructed, an exception is thrown during the function's invocation.

options McpServerToolCreateOptions

Optional options used in the creation of the McpServerTool to control its behavior.

Returns

McpServerTool

The created Microsoft.Extensions.AI.AIFunction for invoking method.

Exceptions

ArgumentNullException

method is null.

InvokeAsync(RequestContext<CallToolRequestParams>, CancellationToken)

Invokes the McpServerTool.

public abstract Task<CallToolResponse> InvokeAsync(RequestContext<CallToolRequestParams> request, CancellationToken cancellationToken = default)

Parameters

request RequestContext<CallToolRequestParams>

The request information resulting in the invocation of this tool.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

Task<CallToolResponse>

The call response from invoking the tool.

Exceptions

ArgumentNullException

request is null.

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.