From 9330774795e0544940e6ad25721da7732b52fd73 Mon Sep 17 00:00:00 2001 From: nevermore Date: Fri, 28 Mar 2025 06:22:25 +0800 Subject: [PATCH] complete fix for SampleLlmTool discovery (#102) https://github.com/modelcontextprotocol/csharp-sdk/issues/80 Co-authored-by: Stephen Toub --- .../TestServerWithHosting/Tools/SampleLlmTool.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/samples/TestServerWithHosting/Tools/SampleLlmTool.cs b/samples/TestServerWithHosting/Tools/SampleLlmTool.cs index 9c8c02d..62ed56e 100644 --- a/samples/TestServerWithHosting/Tools/SampleLlmTool.cs +++ b/samples/TestServerWithHosting/Tools/SampleLlmTool.cs @@ -8,23 +8,17 @@ namespace TestServerWithHosting.Tools; /// This tool uses depenency injection and async method /// [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 SampleLLM( + public static async Task 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}"; }