Table of Contents

Class SamplingCapability

Namespace
ModelContextProtocol.Protocol
Assembly
ModelContextProtocol.Core.dll

Represents the capability for a client to generate text or other content using an AI model.

public class SamplingCapability
Inheritance
SamplingCapability
Inherited Members

Remarks

This capability enables the MCP client to respond to sampling requests from an MCP server.

When this capability is enabled, an MCP server can request the client to generate content using an AI model. The client must set a SamplingHandler to process these requests.

Properties

SamplingHandler

Gets or sets the handler for processing SamplingCreateMessage requests.

[JsonIgnore]
public Func<CreateMessageRequestParams?, IProgress<ProgressNotificationValue>, CancellationToken, ValueTask<CreateMessageResult>>? SamplingHandler { get; set; }

Property Value

Func<CreateMessageRequestParams, IProgress<ProgressNotificationValue>, CancellationToken, ValueTask<CreateMessageResult>>

Remarks

This handler function is called when an MCP server requests the client to generate content using an AI model. The client must set this property for the sampling capability to work.

The handler receives message parameters, a progress reporter for updates, and a cancellation token. It should return a CreateMessageResult containing the generated content.

You can create a handler using the CreateSamplingHandler(IChatClient) extension method with any implementation of Microsoft.Extensions.AI.IChatClient.