Skip to content

Commit e5227b2

Browse files
committed
WIP
1 parent e3f5fa6 commit e5227b2

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

samples/EverythingServer/Program.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.Hosting;
33
using ModelContextProtocol.Protocol.Types;
44
using EverythingServer.Tools;
5+
using EverythingServer;
56

67
var builder = Host.CreateEmptyApplicationBuilder(settings: null);
78

@@ -18,7 +19,7 @@
1819
new Prompt { Name= "simple_prompt", Description = "A prompt without arguments" },
1920
new Prompt { Name= "complex_prompt", Description = "A prompt with arguments", Arguments = [
2021
new PromptArgument { Name = "temperature", Description = "Temperature setting", Required = true },
21-
new PromptArgument { Name = "style", Description = "Output style", Required = false}
22+
new PromptArgument { Name = "style", Description = "Output style", Required = false }
2223
]
2324
}
2425
]
@@ -42,6 +43,47 @@
4243
{
4344
Messages = messages
4445
});
45-
});
46+
})
47+
.WithListResourceTemplatesHandler((ctx, ct) =>
48+
{
49+
return Task.FromResult(new ListResourceTemplatesResult
50+
{
51+
ResourceTemplates =
52+
[
53+
new ResourceTemplate { Name = "Static Resource", Description = "A static resource with a numeric ID", UriTemplate = "test://static/resource/{id}" }
54+
]
55+
});
56+
})
57+
.WithReadResourceHandler((ctx, ct) =>
58+
{
59+
var uri = ctx.Params?.Uri;
60+
61+
if (uri is null || !uri.StartsWith("test://static/resource/"))
62+
{
63+
throw new NotSupportedException($"Unknown resource: {uri}");
64+
}
65+
66+
int index = int.Parse(uri["test://static/resource/".Length..]) - 1;
67+
68+
if (index < 0 || index >= ResourceGenerator.Resources.Count)
69+
{
70+
throw new NotSupportedException($"Unknown resource: {uri}");
71+
}
72+
73+
var resource = ResourceGenerator.Resources[index];
74+
75+
return Task.FromResult(new ReadResourceResult
76+
{
77+
Contents = [new ResourceContents
78+
{
79+
Uri = resource.Uri,
80+
MimeType = resource.MimeType,
81+
Blob = resource.Description,
82+
Name = resource.Description
83+
}
84+
]
85+
});
86+
})
87+
;
4688

4789
await builder.Build().RunAsync();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using ModelContextProtocol.Protocol.Types;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace EverythingServer;
7+
8+
static class ResourceGenerator
9+
{
10+
private static readonly List<Resource> _resources = Enumerable.Range(1, 100).Select(i =>
11+
{
12+
var uri = $"test://static/resource/{i}";
13+
if (i % 2 != 0)
14+
{
15+
return new Resource
16+
{
17+
Uri = uri,
18+
Name = $"Resource {i}",
19+
MimeType = "text/plain",
20+
Description = $"Resource {i}: This is a plaintext resource"
21+
};
22+
}
23+
else
24+
{
25+
var buffer = System.Text.Encoding.UTF8.GetBytes($"Resource {i}: This is a base64 blob");
26+
return new Resource
27+
{
28+
Uri = uri,
29+
Name = $"Resource {i}",
30+
MimeType = "application/octet-stream",
31+
Description = Convert.ToBase64String(buffer)
32+
};
33+
}
34+
}).ToList();
35+
36+
public static IReadOnlyList<Resource> Resources => _resources;
37+
}

src/ModelContextProtocol/Protocol/Types/ResourceContents.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace ModelContextProtocol.Protocol.Types;
44

55
/// <summary>
6-
/// Represents the content of a resource.
6+
/// A known resource that the server is capable of reading.
77
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
88
/// </summary>
99
public class ResourceContents
@@ -32,4 +32,18 @@ public class ResourceContents
3232
/// </summary>
3333
[JsonPropertyName("blob")]
3434
public string? Blob { get; set; }
35+
36+
/// <summary>
37+
/// A human-readable name for this resource.\n\nThis can be used by clients to populate UI elements.
38+
/// </summary>
39+
[JsonPropertyName("name")]
40+
public string? Name { get; set; }
41+
42+
/// <summary>
43+
/// The size of the raw resource content, in bytes (i.e., before base64 encoding or any tokenization), if known.
44+
///
45+
/// This can be used by Hosts to display file sizes and estimate context window usage.
46+
/// </summary>
47+
[JsonPropertyName("size")]
48+
public long? Size { get; set; }
3549
}

0 commit comments

Comments
 (0)