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
|
|
@ -0,0 +1,75 @@
|
|||
using System.ComponentModel;
|
||||
|
||||
namespace ModelContextProtocol.Server;
|
||||
|
||||
/// <summary>
|
||||
/// Provides options for controlling the creation of an <see cref="McpServerResource"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// These options allow for customizing the behavior and metadata of resources created with
|
||||
/// <see cref="M:McpServerResource.Create"/>. They provide control over naming, description,
|
||||
/// and dependency injection integration.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// When creating resources programmatically rather than using attributes, these options
|
||||
/// provide the same level of configuration flexibility.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class McpServerResourceCreateOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets optional services used in the construction of the <see cref="McpServerResource"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// These services will be used to determine which parameters should be satisifed from dependency injection. As such,
|
||||
/// what services are satisfied via this provider should match what's satisfied via the provider passed in at invocation time.
|
||||
/// </remarks>
|
||||
public IServiceProvider? Services { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the URI template of the <see cref="McpServerResource"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If <see langword="null"/>, but an <see cref="McpServerResourceAttribute"/> is applied to the member,
|
||||
/// the <see cref="McpServerResourceAttribute.UriTemplate"/> from the attribute will be used. If that's not present,
|
||||
/// a URI template will be inferred from the member's signature.
|
||||
/// </remarks>
|
||||
public string? UriTemplate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the name to use for the <see cref="McpServerResource"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If <see langword="null"/>, but an <see cref="McpServerResourceAttribute"/> is applied to the member,
|
||||
/// the name from the attribute will be used. If that's not present, a name based on the members's name will be used.
|
||||
/// </remarks>
|
||||
public string? Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or set the description to use for the <see cref="McpServerResource"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If <see langword="null"/>, but a <see cref="DescriptionAttribute"/> is applied to the member,
|
||||
/// the description from that attribute will be used.
|
||||
/// </remarks>
|
||||
public string? Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the MIME (media) type of the <see cref="McpServerResource"/>.
|
||||
/// </summary>
|
||||
public string? MimeType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a shallow clone of the current <see cref="McpServerResourceCreateOptions"/> instance.
|
||||
/// </summary>
|
||||
internal McpServerResourceCreateOptions Clone() =>
|
||||
new McpServerResourceCreateOptions
|
||||
{
|
||||
Services = Services,
|
||||
UriTemplate = UriTemplate,
|
||||
Name = Name,
|
||||
Description = Description,
|
||||
MimeType = MimeType,
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue