Add McpServer/ClientResource{Template} and friends (#391)
* Add McpServer/ClientResource{Template} and friends
This PR copies the same design used by tools and prompts to add McpServerResource, McpServerResourceAttribute, McpServerResourceTypeAttribute, McpClientResource, and then all of those for ResourceTemplate as well.
Unlike the others, [McpServerResource] is permitted not only on methods but also on properties. This convenience is afforded because such resources aren't parameterized, and thus can be easily modeled as properties, likely auto-props, e.g.
```C#
[McpServerResource]
public static BlobResourceContents MyAmazingData { get; } = Create();
```
Details about resources can be encoded into the attributes or provided via McpServerResource{Template}.Create methods. If not URI is explicitly defined, one is derived from the member name and arguments. For templates, this means manufacturing a URI template that includes variables for all non-DI parameters.
This also fixes a few issues discovered along the way:
- McpClientExtensions are updated to use `ValueTask<T>` instead of `Task<T>` where relevant.
- McpServerPrompt was missing handling for progress notifications; that's been rectified. It was also missing configuring the description on the ProtocolPrompt.
- Renamed IMcpServerPrimitive.Name to Id. For resources, names aren't unique.
- After going back and forth, updated McpServer to not fail for missing handlers and to instead register default handlers. It simplifies the implementation and makes it more consistent.
- Added a bunch of primitive types into the McpJsonUtilities JSON source generator context so that such primitives are usable with tools/prompts/resources without folks being required to add them into their own context for Native AOT. I think we should consider pushing this down to AIJsonUtilities, instead, and that will then flow up to McpJsonUtilities automatically.
* Address feedback
This commit is contained in:
parent
47de86ab91
commit
d86b3277d4
1 changed files with 4146 additions and 308 deletions
64
src/ModelContextProtocol/Client/McpClientResourceTemplate.cs
Normal file
64
src/ModelContextProtocol/Client/McpClientResourceTemplate.cs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
using ModelContextProtocol.Protocol.Types;
|
||||
|
||||
namespace ModelContextProtocol.Client;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a named resource template that can be retrieved from an MCP server.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This class provides a client-side wrapper around a resource template defined on an MCP server. It allows
|
||||
/// retrieving the resource template's content by sending a request to the server with the resource's URI.
|
||||
/// Instances of this class are typically obtained by calling <see cref="McpClientExtensions.ListResourceTemplatesAsync"/>
|
||||
/// or <see cref="McpClientExtensions.EnumerateResourceTemplatesAsync"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class McpClientResourceTemplate
|
||||
{
|
||||
private readonly IMcpClient _client;
|
||||
|
||||
internal McpClientResourceTemplate(IMcpClient client, ResourceTemplate resourceTemplate)
|
||||
{
|
||||
_client = client;
|
||||
ProtocolResourceTemplate = resourceTemplate;
|
||||
}
|
||||
|
||||
/// <summary>Gets the underlying protocol <see cref="ResourceTemplate"/> type for this instance.</summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This property provides direct access to the underlying protocol representation of the resource template,
|
||||
/// which can be useful for advanced scenarios or when implementing custom MCP client extensions.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// For most common use cases, you can use the more convenient <see cref="UriTemplate"/> and
|
||||
/// <see cref="Description"/> properties instead of accessing the <see cref="ProtocolResourceTemplate"/> directly.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public ResourceTemplate ProtocolResourceTemplate { get; }
|
||||
|
||||
/// <summary>Gets the URI template of the resource template.</summary>
|
||||
public string UriTemplate => ProtocolResourceTemplate.UriTemplate;
|
||||
|
||||
/// <summary>Gets the name of the resource template.</summary>
|
||||
public string Name => ProtocolResourceTemplate.Name;
|
||||
|
||||
/// <summary>Gets a description of the resource template.</summary>
|
||||
public string? Description => ProtocolResourceTemplate.Description;
|
||||
|
||||
/// <summary>Gets a media (MIME) type of the resource template.</summary>
|
||||
public string? MimeType => ProtocolResourceTemplate.MimeType;
|
||||
|
||||
/// <summary>
|
||||
/// Gets this resource template's content by formatting a URI from the template and supplied arguments
|
||||
/// and sending a request to the server.
|
||||
/// </summary>
|
||||
/// <param name="arguments">A dictionary of arguments to pass to the tool. Each key represents a parameter name,
|
||||
/// and its associated value represents the argument value.
|
||||
/// </param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="ValueTask{ReadResourceResult}"/> containing the resource template's result with content and messages.</returns>
|
||||
public ValueTask<ReadResourceResult> ReadAsync(
|
||||
IReadOnlyDictionary<string, object?> arguments,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
_client.ReadResourceAsync(UriTemplate, arguments, cancellationToken);
|
||||
}
|
||||
Loading…
Reference in a new issue