From 34f9215ecef95f833255e3e4db0bfd9e5addc60b Mon Sep 17 00:00:00 2001 From: Easley Date: Wed, 26 Mar 2025 10:58:55 +0800 Subject: [PATCH] complete fix for SampleLlmTool discovery https://github.com/modelcontextprotocol/csharp-sdk/issues/80 --- .../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 9c8c02d0d..62ed56ec0 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}"; }