- 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
Metadata
Gets the metadata for this resource instance.
public abstract IReadOnlyList<object> Metadata { get; }
Property Value
ProtocolResource
Gets the protocol Resource type for this instance.
public virtual Resource? ProtocolResource { get; }
Property Value
ProtocolResourceTemplate
public abstract ResourceTemplate ProtocolResourceTemplate { get; }
Property Value
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
functionAIFunctionThe function to wrap.
optionsMcpServerResourceCreateOptionsOptional options used in the creation of the McpServerResource to control its behavior.
Returns
Exceptions
- ArgumentNullException
functionis null.
Create(Delegate, McpServerResourceCreateOptions?)
public static McpServerResource Create(Delegate method, McpServerResourceCreateOptions? options = null)
Parameters
methodDelegateThe method to be represented via the created McpServerResource.
optionsMcpServerResourceCreateOptionsOptional options used in the creation of the McpServerResource to control its behavior.
Returns
- McpServerResource
The created McpServerResource for invoking
method.
Exceptions
- ArgumentNullException
methodis 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
methodMethodInfoThe instance method to be represented via the created McpServerResource.
createTargetFuncFunc<RequestContext<ReadResourceRequestParams>, object>The callback used on each function invocation to create an instance of the type on which the instance method
methodwill be invoked. If the returned instance is IAsyncDisposable or IDisposable, it will be disposed of after method completes its invocation.optionsMcpServerResourceCreateOptionsOptional options used in the creation of the McpServerResource to control its behavior.
Returns
- McpServerResource
The created McpServerResource for invoking
method.
Exceptions
- ArgumentNullException
methodorcreateTargetFuncis null.
Create(MethodInfo, object?, McpServerResourceCreateOptions?)
public static McpServerResource Create(MethodInfo method, object? target = null, McpServerResourceCreateOptions? options = null)
Parameters
methodMethodInfoThe method to be represented via the created McpServerResource.
targetobjectThe instance if
methodis an instance method; otherwise, null.optionsMcpServerResourceCreateOptionsOptional options used in the creation of the McpServerResource to control its behavior.
Returns
- McpServerResource
The created McpServerResource for invoking
method.
Exceptions
- ArgumentNullException
methodis null.- ArgumentException
methodis an instance method buttargetis null.
IsMatch(string)
public abstract bool IsMatch(string uri)
Parameters
uristringThe URI being evaluated for this resource.
Returns
- bool
true if the
urimatches the ProtocolResourceTemplate; otherwise, false.
Exceptions
- ArgumentNullException
uriis 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
requestRequestContext<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.
cancellationTokenCancellationTokenThe 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
requestis 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.