Add EverythingServer sample (#151)
This commit is contained in:
parent
d21d933876
commit
01410df5ad
7 changed files with 620 additions and 1 deletions
11
samples/EverythingServer/Tools/AddTool.cs
Normal file
11
samples/EverythingServer/Tools/AddTool.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
using ModelContextProtocol.Server;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace EverythingServer.Tools;
|
||||
|
||||
[McpServerToolType]
|
||||
public class AddTool
|
||||
{
|
||||
[McpServerTool(Name = "add"), Description("Adds two numbers.")]
|
||||
public static string Add(int a, int b) => $"The sum of {a} and {b} is {a + b}";
|
||||
}
|
||||
56
samples/EverythingServer/Tools/AnnotatedMessageTool.cs
Normal file
56
samples/EverythingServer/Tools/AnnotatedMessageTool.cs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
using ModelContextProtocol.Protocol.Types;
|
||||
using ModelContextProtocol.Server;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace EverythingServer.Tools;
|
||||
|
||||
[McpServerToolType]
|
||||
public class AnnotatedMessageTool
|
||||
{
|
||||
public enum MessageType
|
||||
{
|
||||
Error,
|
||||
Success,
|
||||
Debug,
|
||||
}
|
||||
|
||||
[McpServerTool(Name = "annotatedMessage"), Description("Generates an annotated message")]
|
||||
public static IEnumerable<Content> AnnotatedMessage(MessageType messageType, bool includeImage = true)
|
||||
{
|
||||
List<Content> contents = messageType switch
|
||||
{
|
||||
MessageType.Error => [new()
|
||||
{
|
||||
Type = "text",
|
||||
Text = "Error: Operation failed",
|
||||
Annotations = new() { Audience = [Role.User, Role.Assistant], Priority = 1.0f }
|
||||
}],
|
||||
MessageType.Success => [new()
|
||||
{
|
||||
Type = "text",
|
||||
Text = "Operation completed successfully",
|
||||
Annotations = new() { Audience = [Role.User], Priority = 0.7f }
|
||||
}],
|
||||
MessageType.Debug => [new()
|
||||
{
|
||||
Type = "text",
|
||||
Text = "Debug: Cache hit ratio 0.95, latency 150ms",
|
||||
Annotations = new() { Audience = [Role.Assistant], Priority = 0.3f }
|
||||
}],
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(messageType), messageType, null)
|
||||
};
|
||||
|
||||
if (includeImage)
|
||||
{
|
||||
contents.Add(new()
|
||||
{
|
||||
Type = "image",
|
||||
Data = TinyImageTool.MCP_TINY_IMAGE.Split(",").Last(),
|
||||
MimeType = "image/png",
|
||||
Annotations = new() { Audience = [Role.User], Priority = 0.5f }
|
||||
});
|
||||
}
|
||||
|
||||
return contents;
|
||||
}
|
||||
}
|
||||
11
samples/EverythingServer/Tools/EchoTool.cs
Normal file
11
samples/EverythingServer/Tools/EchoTool.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
using ModelContextProtocol.Server;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace EverythingServer.Tools;
|
||||
|
||||
[McpServerToolType]
|
||||
public class EchoTool
|
||||
{
|
||||
[McpServerTool(Name = "echo"), Description("Echoes the message back to the client.")]
|
||||
public static string Echo(string message) => $"Echo: {message}";
|
||||
}
|
||||
39
samples/EverythingServer/Tools/LongRunningTool.cs
Normal file
39
samples/EverythingServer/Tools/LongRunningTool.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using ModelContextProtocol;
|
||||
using ModelContextProtocol.Protocol.Messages;
|
||||
using ModelContextProtocol.Protocol.Types;
|
||||
using ModelContextProtocol.Server;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace EverythingServer.Tools;
|
||||
|
||||
[McpServerToolType]
|
||||
public class LongRunningTool
|
||||
{
|
||||
[McpServerTool(Name = "longRunningOperation"), Description("Demonstrates a long running operation with progress updates")]
|
||||
public static async Task<string> LongRunningOperation(
|
||||
IMcpServer server,
|
||||
RequestContext<CallToolRequestParams> context,
|
||||
int duration = 10,
|
||||
int steps = 5)
|
||||
{
|
||||
var progressToken = context.Params?.Meta?.ProgressToken;
|
||||
var stepDuration = duration / steps;
|
||||
|
||||
for (int i = 1; i <= steps + 1; i++)
|
||||
{
|
||||
await Task.Delay(stepDuration * 1000);
|
||||
|
||||
if (progressToken is not null)
|
||||
{
|
||||
await server.SendNotificationAsync("notifications/progress", new
|
||||
{
|
||||
Progress = i,
|
||||
Total = steps,
|
||||
progressToken
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return $"Long running operation completed. Duration: {duration} seconds. Steps: {steps}.";
|
||||
}
|
||||
}
|
||||
18
samples/EverythingServer/Tools/PrintEnvTool.cs
Normal file
18
samples/EverythingServer/Tools/PrintEnvTool.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
using ModelContextProtocol.Server;
|
||||
using System.ComponentModel;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace EverythingServer.Tools;
|
||||
|
||||
[McpServerToolType]
|
||||
public class PrintEnvTool
|
||||
{
|
||||
private static readonly JsonSerializerOptions options = new()
|
||||
{
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
[McpServerTool(Name = "printEnv"), Description("Prints all environment variables, helpful for debugging MCP server configuration")]
|
||||
public static string PrintEnv() =>
|
||||
JsonSerializer.Serialize(Environment.GetEnvironmentVariables(), options);
|
||||
}
|
||||
42
samples/EverythingServer/Tools/SampleLlmTool.cs
Normal file
42
samples/EverythingServer/Tools/SampleLlmTool.cs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
using ModelContextProtocol.Protocol.Types;
|
||||
using ModelContextProtocol.Server;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace EverythingServer.Tools;
|
||||
|
||||
[McpServerToolType]
|
||||
public class SampleLlmTool
|
||||
{
|
||||
[McpServerTool(Name = "sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
|
||||
public static async Task<string> SampleLLM(
|
||||
IMcpServer server,
|
||||
[Description("The prompt to send to the LLM")] string prompt,
|
||||
[Description("Maximum number of tokens to generate")] int maxTokens,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var samplingParams = CreateRequestSamplingParams(prompt ?? string.Empty, "sampleLLM", maxTokens);
|
||||
var sampleResult = await server.RequestSamplingAsync(samplingParams, cancellationToken);
|
||||
|
||||
return $"LLM sampling result: {sampleResult.Content.Text}";
|
||||
}
|
||||
|
||||
private static CreateMessageRequestParams CreateRequestSamplingParams(string context, string uri, int maxTokens = 100)
|
||||
{
|
||||
return new CreateMessageRequestParams()
|
||||
{
|
||||
Messages = [new SamplingMessage()
|
||||
{
|
||||
Role = Role.User,
|
||||
Content = new Content()
|
||||
{
|
||||
Type = "text",
|
||||
Text = $"Resource {uri} context: {context}"
|
||||
}
|
||||
}],
|
||||
SystemPrompt = "You are a helpful test server.",
|
||||
MaxTokens = maxTokens,
|
||||
Temperature = 0.7f,
|
||||
IncludeContext = ContextInclusion.ThisServer
|
||||
};
|
||||
}
|
||||
}
|
||||
19
samples/EverythingServer/Tools/TinyImageTool.cs
Normal file
19
samples/EverythingServer/Tools/TinyImageTool.cs
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue