Table of Contents

Class McpClientResource

Namespace
ModelContextProtocol.Client
Assembly
ModelContextProtocol.Core.dll

Represents a named resource that can be retrieved from an MCP server.

public sealed class McpClientResource
Inheritance
McpClientResource
Inherited Members

Remarks

This class provides a client-side wrapper around a resource defined on an MCP server. It allows retrieving the resource's content by sending a request to the server with the resource's URI. Instances of this class are typically obtained by calling ListResourcesAsync(RequestOptions?, CancellationToken).

Constructors

McpClientResource(McpClient, Resource)

Initializes a new instance of the McpClientResource class.

public McpClientResource(McpClient client, Resource resource)

Parameters

client McpClient

The McpClient instance to use for reading the resource.

resource Resource

The protocol Resource definition describing the resource's metadata.

Remarks

This constructor enables reusing cached resource definitions across different McpClient instances without needing to call ListResourcesAsync(RequestOptions?, CancellationToken) on every reconnect. This is particularly useful in scenarios where resource definitions are stable and network round-trips should be minimized.

The provided resource must represent a resource that is actually available on the server associated with the client. Attempting to read a resource that doesn't exist on the server will result in an McpException.

Exceptions

ArgumentNullException

client or resource is null.

Properties

Description

Gets the description of the resource.

public string? Description { get; }

Property Value

string

MimeType

Gets the media (MIME) type of the resource.

public string? MimeType { get; }

Property Value

string

Name

Gets the name of the resource.

public string Name { get; }

Property Value

string

ProtocolResource

Gets the underlying protocol Resource type for this instance.

public Resource ProtocolResource { get; }

Property Value

Resource

Remarks

This property provides direct access to the underlying protocol representation of the resource, which can be useful for advanced scenarios or when implementing custom MCP client extensions.

For most common use cases, you can use the more convenient Name and Description properties instead of accessing the ProtocolResource directly.

Title

Gets the title of the resource.

public string? Title { get; }

Property Value

string

Uri

Gets the URI of the resource.

public string Uri { get; }

Property Value

string

Methods

ReadAsync(CancellationToken)

Gets this resource's content by sending a request to the server.

public ValueTask<ReadResourceResult> ReadAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

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

Returns

ValueTask<ReadResourceResult>

A ValueTask<TResult> containing the resource's result with content and messages.

Remarks

This is a convenience method that internally calls ReadResourceAsync(string, RequestOptions?, CancellationToken).