Add AspNetCoreSseServer sample (#81)
* Add AspNetCoreSseMcpTransport sample * Fix build errors after rebase * React to McpServerFactory changes * Make Utf8JsonWriter non thread static * Fix test errors caused by not rebasing the HttpLister changes on factory overhaul commit
This commit is contained in:
parent
b5a26e4c33
commit
90dfc35fbd
2 changed files with 635 additions and 332 deletions
14
samples/AspNetCoreSseServer/Tools/EchoTool.cs
Normal file
14
samples/AspNetCoreSseServer/Tools/EchoTool.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
using McpDotNet.Server;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace TestServerWithHosting.Tools;
|
||||
|
||||
[McpToolType]
|
||||
public static class EchoTool
|
||||
{
|
||||
[McpTool, Description("Echoes the input back to the client.")]
|
||||
public static string Echo(string message)
|
||||
{
|
||||
return "hello " + message;
|
||||
}
|
||||
}
|
||||
51
samples/AspNetCoreSseServer/Tools/SampleLlmTool.cs
Normal file
51
samples/AspNetCoreSseServer/Tools/SampleLlmTool.cs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
using McpDotNet.Protocol.Types;
|
||||
using McpDotNet.Server;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace TestServerWithHosting.Tools;
|
||||
|
||||
/// <summary>
|
||||
/// This tool uses depenency injection and async method
|
||||
/// </summary>
|
||||
[McpToolType]
|
||||
public class SampleLlmTool
|
||||
{
|
||||
private readonly IMcpServer _server;
|
||||
|
||||
public SampleLlmTool(IMcpServer server)
|
||||
{
|
||||
_server = server ?? throw new ArgumentNullException(nameof(server));
|
||||
}
|
||||
|
||||
[McpTool("sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
|
||||
public async Task<string> SampleLLM(
|
||||
[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
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue