diff --git a/samples/EverythingServer/Program.cs b/samples/EverythingServer/Program.cs index 92a5211f..22a290e6 100644 --- a/samples/EverythingServer/Program.cs +++ b/samples/EverythingServer/Program.cs @@ -1,5 +1,6 @@ using EverythingServer; using EverythingServer.Prompts; +using EverythingServer.Resources; using EverythingServer.Tools; using Microsoft.Extensions.AI; using Microsoft.Extensions.DependencyInjection; @@ -14,8 +15,6 @@ using OpenTelemetry.Resources; using OpenTelemetry.Trace; -#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously - var builder = Host.CreateApplicationBuilder(args); builder.Logging.AddConsole(consoleLogOptions => { @@ -38,82 +37,7 @@ .WithTools() .WithPrompts() .WithPrompts() - .WithListResourcesHandler(async (ctx, ct) => - { - return new ListResourcesResult - { - Resources = - [ - new ModelContextProtocol.Protocol.Types.Resource { Name = "Direct Text Resource", Description = "A direct text resource", MimeType = "text/plain", Uri = "test://direct/text/resource" }, - ] - }; - }) - .WithListResourceTemplatesHandler(async (ctx, ct) => - { - return new ListResourceTemplatesResult - { - ResourceTemplates = - [ - new ResourceTemplate { Name = "Template Resource", Description = "A template resource with a numeric ID", UriTemplate = "test://template/resource/{id}" } - ] - }; - }) - .WithReadResourceHandler(async (ctx, ct) => - { - var uri = ctx.Params?.Uri; - - if (uri == "test://direct/text/resource") - { - return new ReadResourceResult - { - Contents = [new TextResourceContents - { - Text = "This is a direct resource", - MimeType = "text/plain", - Uri = uri, - }] - }; - } - - if (uri is null || !uri.StartsWith("test://template/resource/")) - { - throw new NotSupportedException($"Unknown resource: {uri}"); - } - - int index = int.Parse(uri["test://template/resource/".Length..]) - 1; - - if (index < 0 || index >= ResourceGenerator.Resources.Count) - { - throw new NotSupportedException($"Unknown resource: {uri}"); - } - - var resource = ResourceGenerator.Resources[index]; - - if (resource.MimeType == "text/plain") - { - return new ReadResourceResult - { - Contents = [new TextResourceContents - { - Text = resource.Description!, - MimeType = resource.MimeType, - Uri = resource.Uri, - }] - }; - } - else - { - return new ReadResourceResult - { - Contents = [new BlobResourceContents - { - Blob = resource.Description!, - MimeType = resource.MimeType, - Uri = resource.Uri, - }] - }; - } - }) + .WithResources() .WithSubscribeToResourcesHandler(async (ctx, ct) => { var uri = ctx.Params?.Uri; diff --git a/samples/EverythingServer/Resources/SimpleResourceType.cs b/samples/EverythingServer/Resources/SimpleResourceType.cs new file mode 100644 index 00000000..045af2f3 --- /dev/null +++ b/samples/EverythingServer/Resources/SimpleResourceType.cs @@ -0,0 +1,39 @@ +using ModelContextProtocol.Protocol.Types; +using ModelContextProtocol.Server; +using System.ComponentModel; + +namespace EverythingServer.Resources; + +[McpServerResourceType] +public class SimpleResourceType +{ + [McpServerResource(UriTemplate = "test://direct/text/resource", Name = "Direct Text Resource", MimeType = "text/plain")] + [Description("A direct text resource")] + public static string DirectTextResource() => "This is a direct resource"; + + [McpServerResource(UriTemplate = "test://template/resource/{id}", Name = "Template Resource")] + [Description("A template resource with a numeric ID")] + public static ResourceContents TemplateResource(RequestContext requestContext, int id) + { + int index = id - 1; + if ((uint)index >= ResourceGenerator.Resources.Count) + { + throw new NotSupportedException($"Unknown resource: {requestContext.Params?.Uri}"); + } + + var resource = ResourceGenerator.Resources[index]; + return resource.MimeType == "text/plain" ? + new TextResourceContents + { + Text = resource.Description!, + MimeType = resource.MimeType, + Uri = resource.Uri, + } : + new BlobResourceContents + { + Blob = resource.Description!, + MimeType = resource.MimeType, + Uri = resource.Uri, + }; + } +}