Improve tool handling (#28)
* Improve tool handling * Rework stuff with new hosting options * Update readme * Fix build * Try run client tests * Fix build * Fix issues on linux * Try to run testserver on linux * Fix test issue * Fix code smells
This commit is contained in:
parent
7d69499ad5
commit
ec9bdfcf5d
3 changed files with 1036 additions and 235 deletions
|
|
@ -19,6 +19,7 @@ try
|
|||
builder.Services.AddSerilog();
|
||||
builder.Services.AddMcpServer()
|
||||
.WithStdioServerTransport()
|
||||
.WithTools()
|
||||
.WithCallToolHandler((r, ct) => Task.FromResult(new McpDotNet.Protocol.Types.CallToolResponse()));
|
||||
|
||||
var app = builder.Build();
|
||||
|
|
|
|||
13
samples/TestServerWithHosting/Tools/EchoTool.cs
Normal file
13
samples/TestServerWithHosting/Tools/EchoTool.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using McpDotNet.Server;
|
||||
|
||||
namespace TestServerWithHosting.Tools;
|
||||
|
||||
[McpToolType]
|
||||
public static class EchoTool
|
||||
{
|
||||
[McpTool(Description = "Echoes the input back to the client.")]
|
||||
public static string Echo([McpParameter(true)] string message)
|
||||
{
|
||||
return "hello " + message;
|
||||
}
|
||||
}
|
||||
47
samples/TestServerWithHosting/Tools/SampleLlmTool.cs
Normal file
47
samples/TestServerWithHosting/Tools/SampleLlmTool.cs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
using McpDotNet.Protocol.Types;
|
||||
using McpDotNet.Server;
|
||||
|
||||
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(Name = "sampleLLM", Description = "Samples from an LLM using MCP's sampling feature")]
|
||||
public async Task<string> SampleLLM([McpParameter(true, Description = "The prompt to send to the LLM")] string prompt, [McpParameter(true, 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