Class McpServerResource | MCP C# SDK

Namespace
ModelContextProtocol.Server
Assembly
ModelContextProtocol.Core.dll

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

public abstract class McpServerResource : IMcpServerPrimitive
Inheritance

McpServerResource

Implements
Derived
Inherited Members

Constructors

McpServerResource()

protected McpServerResource()

Properties

IsTemplated

Gets a value that indicates whether this resource is a URI template with parameters as opposed to a direct resource.

public bool IsTemplated { get; }

Property Value

bool

Metadata

Gets the metadata for this resource instance.

public abstract IReadOnlyList<object> Metadata { get; }

Property Value

IReadOnlyList<object>

ProtocolResource

Gets the protocol Resource type for this instance.

public virtual Resource? ProtocolResource { get; }

Property Value

Resource

ProtocolResourceTemplate

public abstract ResourceTemplate ProtocolResourceTemplate { get; }

Property Value

ResourceTemplate

Methods

Create(AIFunction, McpServerResourceCreateOptions?)

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

public static McpServerResource Create(AIFunction function, McpServerResourceCreateOptions? options = null)

Parameters

function AIFunction

The function to wrap.

options McpServerResourceCreateOptions

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

Returns

McpServerResource

Exceptions

ArgumentNullException

function is null.

Create(Delegate, McpServerResourceCreateOptions?)

public static McpServerResource Create(Delegate method, McpServerResourceCreateOptions? options = null)

Parameters

method Delegate

The method to be represented via the created McpServerResource.

options McpServerResourceCreateOptions

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

Returns

McpServerResource

The created McpServerResource for invoking method.

Exceptions

ArgumentNullException

method is null.

Create(MethodInfo, Func<RequestContext<ReadResourceRequestParams>, object>, McpServerResourceCreateOptions?)

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

public static McpServerResource Create(MethodInfo method, Func<RequestContext<ReadResourceRequestParams>, object> createTargetFunc, McpServerResourceCreateOptions? options = null)

Parameters

method MethodInfo

The instance method to be represented via the created McpServerResource.

createTargetFunc Func<RequestContext<ReadResourceRequestParams>, object>

The callback used on each function invocation to create an instance of the type on which the instance method method will be invoked. If the returned instance is IAsyncDisposable or IDisposable, it will be disposed of after method completes its invocation.

options McpServerResourceCreateOptions

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

Returns

McpServerResource

The created McpServerResource for invoking method.

Exceptions

ArgumentNullException

method or createTargetFunc is null.

Create(MethodInfo, object?, McpServerResourceCreateOptions?)

public static McpServerResource Create(MethodInfo method, object? target = null, McpServerResourceCreateOptions? options = null)

Parameters

method MethodInfo

The method to be represented via the created McpServerResource.

target object

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

options McpServerResourceCreateOptions

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

Returns

McpServerResource

The created McpServerResource for invoking method.

Exceptions

ArgumentNullException

method is null.

ArgumentException

method is an instance method but target is null.

IsMatch(string)

public abstract bool IsMatch(string uri)

Parameters

uri string

The URI being evaluated for this resource.

Returns

bool

true if the uri matches the ProtocolResourceTemplate; otherwise, false.

Exceptions

ArgumentNullException

uri is null.

ReadAsync(RequestContext<ReadResourceRequestParams>, CancellationToken)

Gets the resource, rendering it with the provided request parameters and returning the resource result.

public abstract ValueTask<ReadResourceResult> ReadAsync(RequestContext<ReadResourceRequestParams> request, CancellationToken cancellationToken = default)

Parameters

request RequestContext<ReadResourceRequestParams>

The request context containing information about the resource invocation, including any arguments passed to the resource. This object provides access to both the request parameters and the server context.

cancellationToken CancellationToken

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

Returns

ValueTask<ReadResourceResult>

A ValueTask<TResult> representing the asynchronous operation, containing a ReadResourceResult with the resource content and messages.

Exceptions

ArgumentNullException

request is null.

InvalidOperationException

The Uri did not match the ProtocolResourceTemplate for this resource, the resource implementation returned null, or the resource implementation returned an unsupported result type.

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.