Table of Contents

Class McpServerResourceAttribute

Namespace
ModelContextProtocol.Server
Assembly
ModelContextProtocol.Core.dll

Used to indicate that a method or property should be considered an McpServerResource.

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

Remarks

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

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

Read resource requests do not contain separate arguments, only a URI. However, for templated resources, portions of that URI may be considered as arguments and may be bound to parameters. Further, resource methods may accept parameters that will be bound to arguments based on their type.

Return values from a method are used to create the ReadResourceResult that is sent back to the client:

ResourceContentsWrapped in a list containing the single ResourceContents.
Microsoft.Extensions.AI.TextContentConverted to a list containing a single TextResourceContents.
Microsoft.Extensions.AI.DataContentConverted to a list containing a single BlobResourceContents.
stringConverted to a list containing a single TextResourceContents.
IEnumerable<T> of ResourceContentsReturned directly as a list of ResourceContents.
IEnumerable<T> of Microsoft.Extensions.AI.AIContentConverted to a list containing a TextResourceContents for each Microsoft.Extensions.AI.TextContent and a BlobResourceContents for each Microsoft.Extensions.AI.DataContent.
IEnumerable<T> of stringConverted to a list containing a TextResourceContents, one for each string.

Other returned types will result in an InvalidOperationException being thrown.

Constructors

McpServerResourceAttribute()

Initializes a new instance of the McpServerResourceAttribute class.

public McpServerResourceAttribute()

Properties

MimeType

Gets or sets the MIME (media) type of the resource.

public string? MimeType { get; set; }

Property Value

string

Name

Gets or sets the name of the resource.

public string? Name { get; set; }

Property Value

string

Remarks

If null, the method name will be used.

UriTemplate

Gets or sets the URI template of the resource.

public string? UriTemplate { get; set; }

Property Value

string

Remarks

If null, a URI will be derived from Name and the method's parameter names. This template may, but doesn't have to, include parameters; if it does, this McpServerResource will be considered a "resource template", and if it doesn't, it will be considered a "direct resource". The former will be listed with ResourcesTemplatesList requests and the latter with ResourcesList requests.