Skip to content

Commit cab54dd

Browse files
committed
Moving to using the PromptType attribute
1 parent bcb195e commit cab54dd

File tree

11 files changed

+60
-64
lines changed

11 files changed

+60
-64
lines changed

samples/EverythingServer/Program.cs

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using ModelContextProtocol;
2-
using Microsoft.Extensions.Hosting;
1+
using Microsoft.Extensions.Hosting;
32
using ModelContextProtocol.Protocol.Types;
4-
using EverythingServer.Tools;
53
using EverythingServer;
64
using ModelContextProtocol.Server;
75
using Microsoft.Extensions.AI;
86
using Microsoft.Extensions.DependencyInjection;
7+
using EverythingServer.Prompts;
8+
using EverythingServer.Tools;
99

1010
var builder = Host.CreateEmptyApplicationBuilder(settings: null);
1111

@@ -14,41 +14,15 @@
1414
builder.Services
1515
.AddMcpServer()
1616
.WithStdioServerTransport()
17-
.WithToolsFromAssembly()
18-
.WithListPromptsHandler((ctx, ct) =>
19-
{
20-
return Task.FromResult(new ListPromptsResult
21-
{
22-
Prompts =
23-
[
24-
new Prompt { Name= "simple_prompt", Description = "A prompt without arguments" },
25-
new Prompt { Name= "complex_prompt", Description = "A prompt with arguments", Arguments = [
26-
new PromptArgument { Name = "temperature", Description = "Temperature setting", Required = true },
27-
new PromptArgument { Name = "style", Description = "Output style", Required = false }
28-
]
29-
}
30-
]
31-
});
32-
})
33-
.WithGetPromptHandler((args, ct) =>
34-
{
35-
List<PromptMessage> messages = args.Params?.Name switch
36-
{
37-
"simple_prompt" => [new PromptMessage { Role = Role.User, Content = new Content { Type = "text", Text = "This is a simple prompt without arguments" } }],
38-
"complex_prompt" => [
39-
new PromptMessage { Role = Role.User, Content = new Content { Type = "text", Text = $"This is a complex prompt with arguments: temperature={args.Params?.Arguments?["temperature"]}, style={(args.Params?.Arguments?.ContainsKey("style") == true ? args.Params?.Arguments?["style"] : "")}" } },
40-
new PromptMessage { Role = Role.Assistant, Content = new Content { Type = "text", Text = "I understand. You've provided a complex prompt with temperature and style arguments. How would you like me to proceed?" } },
41-
new PromptMessage { Role = Role.User, Content = new Content { Type = "image", Data = TinyImageTool.MCP_TINY_IMAGE.Split(",").Last(), MimeType = "image/png" } }
42-
]
43-
,
44-
_ => throw new NotSupportedException($"Unknown prompt name: {args.Params?.Name}")
45-
};
46-
47-
return Task.FromResult(new GetPromptResult
48-
{
49-
Messages = messages
50-
});
51-
})
17+
.WithTools<AddTool>()
18+
.WithTools<AnnotatedMessageTool>()
19+
.WithTools<EchoTool>()
20+
.WithTools<LongRunningTool>()
21+
.WithTools<PrintEnvTool>()
22+
.WithTools<SampleLlmTool>()
23+
.WithTools<TinyImageTool>()
24+
.WithPrompts<ComplexPromptType>()
25+
.WithPrompts<SimplePromptType>()
5226
.WithListResourceTemplatesHandler((ctx, ct) =>
5327
{
5428
return Task.FromResult(new ListResourceTemplatesResult
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using EverythingServer.Tools;
2+
using Microsoft.Extensions.AI;
3+
using ModelContextProtocol.Server;
4+
using System.ComponentModel;
5+
6+
namespace EverythingServer.Prompts;
7+
8+
[McpServerPromptType]
9+
public class ComplexPromptType
10+
{
11+
[McpServerPrompt(Name = "complex_prompt"), Description("A prompt with arguments")]
12+
public static IEnumerable<ChatMessage> ComplexPrompt(
13+
[Description("Temperature setting")] int temperature,
14+
[Description("Output style")] string? style = null)
15+
{
16+
return [
17+
new ChatMessage(ChatRole.User,$"This is a complex prompt with arguments: temperature={temperature}, style={style}"),
18+
new ChatMessage(ChatRole.Assistant, "I understand. You've provided a complex prompt with temperature and style arguments. How would you like me to proceed?"),
19+
new ChatMessage(ChatRole.User, [new DataContent(TinyImageTool.MCP_TINY_IMAGE)])
20+
];
21+
}
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using ModelContextProtocol.Server;
2+
using System.ComponentModel;
3+
4+
namespace EverythingServer.Prompts;
5+
6+
[McpServerPromptType]
7+
public class SimplePromptType
8+
{
9+
[McpServerPrompt(Name = "simple_prompt"), Description("A prompt without arguments")]
10+
public static string SimplePrompt() => "This is a simple prompt without arguments";
11+
}

samples/EverythingServer/Tools/AddTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace EverythingServer.Tools;
55

66
[McpServerToolType]
7-
public static class AddTool
7+
public class AddTool
88
{
99
[McpServerTool(Name = "add"), Description("Adds two numbers.")]
1010
public static string Add(int a, int b) => $"The sum of {a} and {b} is {a + b}";

samples/EverythingServer/Tools/AnnotatedMessageTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace EverythingServer.Tools;
55

66
[McpServerToolType]
7-
public static class AnnotatedMessageTool
7+
public class AnnotatedMessageTool
88
{
99
public enum MessageType
1010
{

samples/EverythingServer/Tools/EchoTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace EverythingServer.Tools;
55

66
[McpServerToolType]
7-
public static class EchoTool
7+
public class EchoTool
88
{
99
[McpServerTool(Name = "echo"), Description("Echoes the message back to the client.")]
1010
public static string Echo(string message) => $"Echo: {message}";

samples/EverythingServer/Tools/LongRunningTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace EverythingServer.Tools;
77

88
[McpServerToolType]
9-
public static class LongRunningTool
9+
public class LongRunningTool
1010
{
1111
[McpServerTool(Name = "longRunningOperation"), Description("Demonstrates a long running operation with progress updates")]
1212
public static async Task<string> LongRunningOperation(
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
using ModelContextProtocol.Server;
22
using System.ComponentModel;
3-
using System.Diagnostics;
43
using System.Text.Json;
54

65
namespace EverythingServer.Tools;
76

87
[McpServerToolType]
9-
public static class PrintEnvTool
8+
public class PrintEnvTool
109
{
1110
private static readonly JsonSerializerOptions options = new()
1211
{
1312
WriteIndented = true
1413
};
1514

1615
[McpServerTool(Name = "printEnv"), Description("Prints all environment variables, helpful for debugging MCP server configuration")]
17-
public static string PrintEnv()
18-
{
19-
Debugger.Launch();
20-
var envVars = Environment.GetEnvironmentVariables();
21-
return JsonSerializer.Serialize(envVars, options);
22-
}
16+
public static string PrintEnv() =>
17+
JsonSerializer.Serialize(Environment.GetEnvironmentVariables(), options);
2318
}

samples/EverythingServer/Tools/SampleLlmTool.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
namespace EverythingServer.Tools;
66

77
[McpServerToolType]
8-
public class SampleLlmTool(IMcpServer server)
8+
public class SampleLlmTool
99
{
10-
private readonly IMcpServer _server = server ?? throw new ArgumentNullException(nameof(server));
11-
1210
[McpServerTool(Name = "sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
13-
public async Task<string> SampleLLM(
11+
public static async Task<string> SampleLLM(
12+
IMcpServer server,
1413
[Description("The prompt to send to the LLM")] string prompt,
1514
[Description("Maximum number of tokens to generate")] int maxTokens,
1615
CancellationToken cancellationToken)
1716
{
1817
var samplingParams = CreateRequestSamplingParams(prompt ?? string.Empty, "sampleLLM", maxTokens);
19-
var sampleResult = await _server.RequestSamplingAsync(samplingParams, cancellationToken);
18+
var sampleResult = await server.RequestSamplingAsync(samplingParams, cancellationToken);
2019

2120
return $"LLM sampling result: {sampleResult.Content.Text}";
2221
}

samples/EverythingServer/Tools/TinyImageTool.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
using Microsoft.Extensions.AI;
22
using ModelContextProtocol.Server;
33
using System.ComponentModel;
4-
using System.Diagnostics;
54

65
namespace EverythingServer.Tools;
76

87
[McpServerToolType]
9-
public static class TinyImageTool
8+
public class TinyImageTool
109
{
1110
[McpServerTool(Name = "getTinyImage"), Description("Get a tiny image from the server")]
12-
public static IEnumerable<AIContent> GetTinyImage()
13-
{
14-
Debugger.Launch();
15-
return [
11+
public static IEnumerable<AIContent> GetTinyImage() => [
1612
new TextContent("This is a tiny image:"),
1713
new DataContent(MCP_TINY_IMAGE),
1814
new TextContent("The image above is the MCP tiny image.")
1915
];
20-
}
2116

2217
internal const string MCP_TINY_IMAGE =
2318
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAKsGlDQ1BJQ0MgUHJvZmlsZQAASImVlwdUU+kSgOfe9JDQEiIgJfQmSCeAlBBaAAXpYCMkAUKJMRBU7MriClZURLCs6KqIgo0idizYFsWC3QVZBNR1sWDDlXeBQ9jdd9575805c+a7c+efmf+e/z9nLgCdKZDJMlF1gCxpjjwyyI8dn5DIJvUABRiY0kBdIMyWcSMiwgCTUft3+dgGyJC9YzuU69/f/1fREImzhQBIBMbJomxhFsbHMe0TyuQ5ALg9mN9kbo5siK9gzJRjDWL8ZIhTR7hviJOHGY8fjomO5GGsDUCmCQTyVACaKeZn5wpTsTw0f4ztpSKJFGPsGbyzsmaLMMbqgiUWI8N4KD8n+S95Uv+WM1mZUyBIVfLIXoaF7C/JlmUK5v+fn+N/S1amYrSGOaa0NHlwJGaxvpAHGbNDlSxNnhI+yhLRcPwwpymCY0ZZmM1LHGWRwD9UuTZzStgop0gC+co8OfzoURZnB0SNsnx2pLJWipzHHWWBfKyuIiNG6U8T85X589Ki40Y5VxI7ZZSzM6JCx2J4Sr9cEansXywN8hurG6jce1b2X/Yr4SvX5qRFByv3LhjrXyzljuXMjlf2JhL7B4zFxCjjZTl+ylqyzAhlvDgzSOnPzo1Srs3BDuTY2gjlN0wXhESMMoRBELAhBjIhB+QggECQgBTEOeJ5Q2cUeLNl8+WS1LQcNhe7ZWI2Xyq0m8B2tHd0Bhi6syNH4j1r+C4irGtjvhWVAF4nBgcHT475Qm4BHEkCoNaO+SxnAKh3A1w5JVTIc0d8Q9cJCEAFNWCCDhi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