Skip to content

Commit 2eeb61f

Browse files
Add static resources to EverythingServer sample (#361)
* Add static resources * Apply suggestions from PR review --------- Co-authored-by: Stephen Halter <[email protected]>
1 parent 86dd11a commit 2eeb61f

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

samples/EverythingServer/Program.cs

+26-3
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,49 @@
3838
.WithTools<TinyImageTool>()
3939
.WithPrompts<ComplexPromptType>()
4040
.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+
})
4151
.WithListResourceTemplatesHandler(async (ctx, ct) =>
4252
{
4353
return new ListResourceTemplatesResult
4454
{
4555
ResourceTemplates =
4656
[
47-
new ResourceTemplate { Name = "Static Resource", Description = "A static resource with a numeric ID", UriTemplate = "test://static/resource/{id}" }
57+
new ResourceTemplate { Name = "Template Resource", Description = "A template resource with a numeric ID", UriTemplate = "test://template/resource/{id}" }
4858
]
4959
};
5060
})
5161
.WithReadResourceHandler(async (ctx, ct) =>
5262
{
5363
var uri = ctx.Params?.Uri;
5464

55-
if (uri is null || !uri.StartsWith("test://static/resource/"))
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/"))
5679
{
5780
throw new NotSupportedException($"Unknown resource: {uri}");
5881
}
5982

60-
int index = int.Parse(uri["test://static/resource/".Length..]) - 1;
83+
int index = int.Parse(uri["test://template/resource/".Length..]) - 1;
6184

6285
if (index < 0 || index >= ResourceGenerator.Resources.Count)
6386
{

samples/EverythingServer/ResourceGenerator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ static class ResourceGenerator
66
{
77
private static readonly List<Resource> _resources = Enumerable.Range(1, 100).Select(i =>
88
{
9-
var uri = $"test://static/resource/{i}";
9+
var uri = $"test://template/resource/{i}";
1010
if (i % 2 != 0)
1111
{
1212
return new Resource

0 commit comments

Comments
 (0)