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.
- CancellationToken parameters are automatically bound to a CancellationToken provided by the IMcpServer and that respects any CancelledNotifications sent by the client for this operation's RequestId.
- IServiceProvider parameters are bound from the RequestContext<TParams> for this request.
- IMcpServer parameters are bound directly to the IMcpServer instance associated with this request's RequestContext<TParams>. Such parameters may be used to understand what server is being used to process the request, and to interact with the client issuing the request to that server.
- IProgress<T> parameters accepting ProgressNotificationValue values are bound to an IProgress<T> instance manufactured to forward progress notifications from the resource to the client. If the client included a ProgressToken in their request, progress reports issued to this instance will propagate to the client as ProgressNotification notifications with that token. If the client did not include a ProgressToken, the instance will ignore any progress reports issued to it.
- When the McpServerResource is constructed, it may be passed an IServiceProvider via Services. Any parameter that can be satisfied by that IServiceProvider according to IServiceProviderIsService will be resolved from the IServiceProvider provided to the resource invocation rather than from the argument collection.
- Any parameter attributed with FromKeyedServicesAttribute will similarly be resolved from the IServiceProvider provided to the resource invocation rather than from the argument collection.
- All other parameters are bound from the data in the URI.
Return values from a method are used to create the ReadResourceResult that is sent back to the client:
ResourceContents | Wrapped in a list containing the single ResourceContents. |
Microsoft.Extensions.AI.TextContent | Converted to a list containing a single TextResourceContents. |
Microsoft.Extensions.AI.DataContent | Converted to a list containing a single BlobResourceContents. |
string | Converted to a list containing a single TextResourceContents. |
IEnumerable<T> of ResourceContents | Returned directly as a list of ResourceContents. |
IEnumerable<T> of Microsoft.Extensions.AI.AIContent | Converted to a list containing a TextResourceContents for each Microsoft.Extensions.AI.TextContent and a BlobResourceContents for each Microsoft.Extensions.AI.DataContent. |
IEnumerable<T> of string | Converted 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
Name
Gets or sets the name of the resource.
public string? Name { get; set; }
Property Value
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
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.