Skip to content

Commit 26c3622

Browse files
authored
Use WithResources in EverythingServer sample (#400)
1 parent d86b327 commit 26c3622

File tree

2 files changed

+41
-78
lines changed

2 files changed

+41
-78
lines changed

samples/EverythingServer/Program.cs

+2-78
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using EverythingServer;
22
using EverythingServer.Prompts;
3+
using EverythingServer.Resources;
34
using EverythingServer.Tools;
45
using Microsoft.Extensions.AI;
56
using Microsoft.Extensions.DependencyInjection;
@@ -14,8 +15,6 @@
1415
using OpenTelemetry.Resources;
1516
using OpenTelemetry.Trace;
1617

17-
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
18-
1918
var builder = Host.CreateApplicationBuilder(args);
2019
builder.Logging.AddConsole(consoleLogOptions =>
2120
{
@@ -38,82 +37,7 @@
3837
.WithTools<TinyImageTool>()
3938
.WithPrompts<ComplexPromptType>()
4039
.WithPrompts<SimplePromptType>()
41-
.WithListResourcesHandler(async (ctx, ct) =>
42-
{
43-
return new ListResourcesResult
44-
{
45-
Resources =
46-
[
47-
new ModelContextProtocol.Protocol.Types.Resource { Name = "Direct Text Resource", Description = "A direct text resource", MimeType = "text/plain", Uri = "test://direct/text/resource" },
48-
]
49-
};
50-
})
51-
.WithListResourceTemplatesHandler(async (ctx, ct) =>
52-
{
53-
return new ListResourceTemplatesResult
54-
{
55-
ResourceTemplates =
56-
[
57-
new ResourceTemplate { Name = "Template Resource", Description = "A template resource with a numeric ID", UriTemplate = "test://template/resource/{id}" }
58-
]
59-
};
60-
})
61-
.WithReadResourceHandler(async (ctx, ct) =>
62-
{
63-
var uri = ctx.Params?.Uri;
64-
65-
if (uri == "test://direct/text/resource")
66-
{
67-
return new ReadResourceResult
68-
{
69-
Contents = [new TextResourceContents
70-
{
71-
Text = "This is a direct resource",
72-
MimeType = "text/plain",
73-
Uri = uri,
74-
}]
75-
};
76-
}
77-
78-
if (uri is null || !uri.StartsWith("test://template/resource/"))
79-
{
80-
throw new NotSupportedException($"Unknown resource: {uri}");
81-
}
82-
83-
int index = int.Parse(uri["test://template/resource/".Length..]) - 1;
84-
85-
if (index < 0 || index >= ResourceGenerator.Resources.Count)
86-
{
87-
throw new NotSupportedException($"Unknown resource: {uri}");
88-
}
89-
90-
var resource = ResourceGenerator.Resources[index];
91-
92-
if (resource.MimeType == "text/plain")
93-
{
94-
return new ReadResourceResult
95-
{
96-
Contents = [new TextResourceContents
97-
{
98-
Text = resource.Description!,
99-
MimeType = resource.MimeType,
100-
Uri = resource.Uri,
101-
}]
102-
};
103-
}
104-
else
105-
{
106-
return new ReadResourceResult
107-
{
108-
Contents = [new BlobResourceContents
109-
{
110-
Blob = resource.Description!,
111-
MimeType = resource.MimeType,
112-
Uri = resource.Uri,
113-
}]
114-
};
115-
}
116-
})
40+
.WithResources<SimpleResourceType>()
11741
.WithSubscribeToResourcesHandler(async (ctx, ct) =>
11842
{
11943
var uri = ctx.Params?.Uri;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using ModelContextProtocol.Protocol.Types;
2+
using ModelContextProtocol.Server;
3+
using System.ComponentModel;
4+
5+
namespace EverythingServer.Resources;
6+
7+
[McpServerResourceType]
8+
public class SimpleResourceType
9+
{
10+
[McpServerResource(UriTemplate = "test://direct/text/resource", Name = "Direct Text Resource", MimeType = "text/plain")]
11+
[Description("A direct text resource")]
12+
public static string DirectTextResource() => "This is a direct resource";
13+
14+
[McpServerResource(UriTemplate = "test://template/resource/{id}", Name = "Template Resource")]
15+
[Description("A template resource with a numeric ID")]
16+
public static ResourceContents TemplateResource(RequestContext<ReadResourceRequestParams> requestContext, int id)
17+
{
18+
int index = id - 1;
19+
if ((uint)index >= ResourceGenerator.Resources.Count)
20+
{
21+
throw new NotSupportedException($"Unknown resource: {requestContext.Params?.Uri}");
22+
}
23+
24+
var resource = ResourceGenerator.Resources[index];
25+
return resource.MimeType == "text/plain" ?
26+
new TextResourceContents
27+
{
28+
Text = resource.Description!,
29+
MimeType = resource.MimeType,
30+
Uri = resource.Uri,
31+
} :
32+
new BlobResourceContents
33+
{
34+
Blob = resource.Description!,
35+
MimeType = resource.MimeType,
36+
Uri = resource.Uri,
37+
};
38+
}
39+
}

0 commit comments

Comments
 (0)