Table of Contents

Class McpServerToolAttribute

Namespace
ModelContextProtocol.Server
Assembly
ModelContextProtocol.dll

Used to indicate that a method should be considered an McpServerTool.

[AttributeUsage(AttributeTargets.Method)]
public sealed class McpServerToolAttribute : Attribute
Inheritance
McpServerToolAttribute
Inherited Members

Remarks

This attribute is applied to methods that should be exposed as tools in the Model Context Protocol. When a class containing methods marked with this attribute is registered with McpServerBuilderExtensions, these methods become available as tools that can be called by MCP clients.

When methods are provided directly to McpServerTool.Create, the attribute is not required.

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

McpServerToolAttribute()

Initializes a new instance of the McpServerToolAttribute class.

public McpServerToolAttribute()

Properties

Destructive

Gets or sets whether the tool may perform destructive updates to its environment.

public bool Destructive { get; set; }

Property Value

bool

Remarks

If true, the tool may perform destructive updates to its environment. If false, the tool performs only additive updates. This property is most relevant when the tool modifies its environment (ReadOnly = false).

The default is true.

Idempotent

Gets or sets whether calling the tool repeatedly with the same arguments will have no additional effect on its environment.

public bool Idempotent { get; set; }

Property Value

bool

Remarks

This property is most relevant when the tool modifies its environment (ReadOnly = false).

The default is false.

Name

Gets the name of the tool.

public string? Name { get; set; }

Property Value

string

Remarks

If null, the method name will be used.

OpenWorld

Gets or sets whether this tool may interact with an "open world" of external entities.

public bool OpenWorld { get; set; }

Property Value

bool

Remarks

If true, the tool may interact with an unpredictable or dynamic set of entities (like web search). If false, the tool's domain of interaction is closed and well-defined (like memory access).

The default is true.

ReadOnly

Gets or sets whether this tool does not modify its environment.

public bool ReadOnly { get; set; }

Property Value

bool

Remarks

If true, the tool only performs read operations without changing state. If false, the tool may make modifications to its environment.

Read-only tools do not have side effects beyond computational resource usage. They don't create, update, or delete data in any system.

The default is false.

Title

Gets or sets a human-readable title for the tool that can be displayed to users.

public string? Title { get; set; }

Property Value

string

Remarks

The title provides a more descriptive, user-friendly name for the tool than the tool's programmatic name. It is intended for display purposes and to help users understand the tool's purpose at a glance.

Unlike the tool name (which follows programmatic naming conventions), the title can include spaces, special characters, and be phrased in a more natural language style.