* Remove `McpEndpoint` & `IMcpClient` * Revise naming, add abstract classes * Fix missing renames * Move extension methods to instance methods * Add back old API, but obsoleted * Add back obsoleted factory classes * Move deprecation attributes * `SseClientTransport` -> `HttpClientTransport` * Update src/ModelContextProtocol.Core/Server/AugmentedServiceProvider.cs Co-authored-by: Stephen Halter <halter73@gmail.com> * PR feedback * PR feedback * Add back dispose lock * Fix docs projects * PR feedback * Undo `docs/*.md` changes --------- Co-authored-by: Stephen Halter <halter73@gmail.com>
41 lines
1.5 KiB
C#
41 lines
1.5 KiB
C#
using ModelContextProtocol.Protocol;
|
|
using ModelContextProtocol.Server;
|
|
using System.ComponentModel;
|
|
|
|
namespace TestServerWithHosting.Tools;
|
|
|
|
/// <summary>
|
|
/// This tool uses depenency injection and async method
|
|
/// </summary>
|
|
[McpServerToolType]
|
|
public sealed class SampleLlmTool
|
|
{
|
|
[McpServerTool(Name = "sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
|
|
public static async Task<string> SampleLLM(
|
|
McpServer 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 thisServer.SampleAsync(samplingParams, cancellationToken);
|
|
|
|
return $"LLM sampling result: {(sampleResult.Content as TextContentBlock)?.Text}";
|
|
}
|
|
|
|
private static CreateMessageRequestParams CreateRequestSamplingParams(string context, string uri, int maxTokens = 100)
|
|
{
|
|
return new CreateMessageRequestParams
|
|
{
|
|
Messages = [new SamplingMessage
|
|
{
|
|
Role = Role.User,
|
|
Content = new TextContentBlock { Text = $"Resource {uri} context: {context}" },
|
|
}],
|
|
SystemPrompt = "You are a helpful test server.",
|
|
MaxTokens = maxTokens,
|
|
Temperature = 0.7f,
|
|
IncludeContext = ContextInclusion.ThisServer
|
|
};
|
|
}
|
|
}
|