complete fix for SampleLlmTool discovery (#102)

https://github.com/modelcontextprotocol/csharp-sdk/issues/80

Co-authored-by: Stephen Toub <stoub@microsoft.com>
This commit is contained in:
nevermore 2025-03-28 06:22:25 +08:00 • committed by GitHub
commit 9330774795
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,23 +8,17 @@ namespace TestServerWithHosting.Tools;
/// This tool uses depenency injection and async method
/// </summary>
[McpServerToolType]
public class SampleLlmTool
public static class SampleLlmTool
{
private readonly IMcpServer _server;
public SampleLlmTool(IMcpServer server)
{
_server = server ?? throw new ArgumentNullException(nameof(server));
}
[McpServerTool("sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
public async Task<string> SampleLLM(
public static async Task<string> SampleLLM(
IMcpServer thisServer,
[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);
var sampleResult = await thisServer.RequestSamplingAsync(samplingParams, cancellationToken);
return $"LLM sampling result: {sampleResult.Content.Text}";
}