Skip to content

Use WithResources in EverythingServer sample #400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 2 additions & 78 deletions samples/EverythingServer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using EverythingServer;
using EverythingServer.Prompts;
using EverythingServer.Resources;
using EverythingServer.Tools;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -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 =>
{
Expand All @@ -38,82 +37,7 @@
.WithTools<TinyImageTool>()
.WithPrompts<ComplexPromptType>()
.WithPrompts<SimplePromptType>()
.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<SimpleResourceType>()
.WithSubscribeToResourcesHandler(async (ctx, ct) =>
{
var uri = ctx.Params?.Uri;
Expand Down
39 changes: 39 additions & 0 deletions samples/EverythingServer/Resources/SimpleResourceType.cs
Original file line number Diff line number Diff line change
@@ -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<ReadResourceRequestParams> 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,
};
}
}