Skip to content

Commit eb31afb

Browse files
Remove unused duplicate CreateSamplingHandler and helper methods from McpClient.Methods.cs
Co-authored-by: eiriktsarpalis <[email protected]>
1 parent 3d20111 commit eb31afb

File tree

1 file changed

+0
-112
lines changed

1 file changed

+0
-112
lines changed

src/ModelContextProtocol.Core/Client/McpClient.Methods.cs

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -555,118 +555,6 @@ async ValueTask<CallToolResult> SendRequestWithProgressAsync(
555555
}
556556
}
557557

558-
/// <summary>
559-
/// Converts the contents of a <see cref="CreateMessageRequestParams"/> into a pair of
560-
/// <see cref="IEnumerable{ChatMessage}"/> and <see cref="ChatOptions"/> instances to use
561-
/// as inputs into a <see cref="IChatClient"/> operation.
562-
/// </summary>
563-
/// <param name="requestParams"></param>
564-
/// <returns>The created pair of messages and options.</returns>
565-
/// <exception cref="ArgumentNullException"><paramref name="requestParams"/> is <see langword="null"/>.</exception>
566-
internal static (IList<ChatMessage> Messages, ChatOptions? Options) ToChatClientArguments(
567-
CreateMessageRequestParams requestParams)
568-
{
569-
Throw.IfNull(requestParams);
570-
571-
ChatOptions? options = null;
572-
573-
if (requestParams.MaxTokens is int maxTokens)
574-
{
575-
(options ??= new()).MaxOutputTokens = maxTokens;
576-
}
577-
578-
if (requestParams.Temperature is float temperature)
579-
{
580-
(options ??= new()).Temperature = temperature;
581-
}
582-
583-
if (requestParams.StopSequences is { } stopSequences)
584-
{
585-
(options ??= new()).StopSequences = stopSequences.ToArray();
586-
}
587-
588-
List<ChatMessage> messages =
589-
(from sm in requestParams.Messages
590-
let aiContent = sm.Content.ToAIContent()
591-
where aiContent is not null
592-
select new ChatMessage(sm.Role == Role.Assistant ? ChatRole.Assistant : ChatRole.User, [aiContent]))
593-
.ToList();
594-
595-
return (messages, options);
596-
}
597-
598-
/// <summary>Converts the contents of a <see cref="ChatResponse"/> into a <see cref="CreateMessageResult"/>.</summary>
599-
/// <param name="chatResponse">The <see cref="ChatResponse"/> whose contents should be extracted.</param>
600-
/// <returns>The created <see cref="CreateMessageResult"/>.</returns>
601-
/// <exception cref="ArgumentNullException"><paramref name="chatResponse"/> is <see langword="null"/>.</exception>
602-
internal static CreateMessageResult ToCreateMessageResult(ChatResponse chatResponse)
603-
{
604-
Throw.IfNull(chatResponse);
605-
606-
// The ChatResponse can include multiple messages, of varying modalities, but CreateMessageResult supports
607-
// only either a single blob of text or a single image. Heuristically, we'll use an image if there is one
608-
// in any of the response messages, or we'll use all the text from them concatenated, otherwise.
609-
610-
ChatMessage? lastMessage = chatResponse.Messages.LastOrDefault();
611-
612-
ContentBlock? content = null;
613-
if (lastMessage is not null)
614-
{
615-
foreach (var lmc in lastMessage.Contents)
616-
{
617-
if (lmc is DataContent dc && (dc.HasTopLevelMediaType("image") || dc.HasTopLevelMediaType("audio")))
618-
{
619-
content = dc.ToContent();
620-
}
621-
}
622-
}
623-
624-
return new()
625-
{
626-
Content = content ?? new TextContentBlock { Text = lastMessage?.Text ?? string.Empty },
627-
Model = chatResponse.ModelId ?? "unknown",
628-
Role = lastMessage?.Role == ChatRole.User ? Role.User : Role.Assistant,
629-
StopReason = chatResponse.FinishReason == ChatFinishReason.Length ? "maxTokens" : "endTurn",
630-
};
631-
}
632-
633-
/// <summary>
634-
/// Creates a sampling handler for use with <see cref="McpClientHandlers.SamplingHandler"/> that will
635-
/// satisfy sampling requests using the specified <see cref="IChatClient"/>.
636-
/// </summary>
637-
/// <param name="chatClient">The <see cref="IChatClient"/> with which to satisfy sampling requests.</param>
638-
/// <returns>The created handler delegate that can be assigned to <see cref="McpClientHandlers.SamplingHandler"/>.</returns>
639-
/// <exception cref="ArgumentNullException"><paramref name="chatClient"/> is <see langword="null"/>.</exception>
640-
public static Func<CreateMessageRequestParams?, IProgress<ProgressNotificationValue>, CancellationToken, ValueTask<CreateMessageResult>> CreateSamplingHandler(
641-
IChatClient chatClient)
642-
{
643-
Throw.IfNull(chatClient);
644-
645-
return async (requestParams, progress, cancellationToken) =>
646-
{
647-
Throw.IfNull(requestParams);
648-
649-
var (messages, options) = ToChatClientArguments(requestParams);
650-
var progressToken = requestParams.ProgressToken;
651-
652-
List<ChatResponseUpdate> updates = [];
653-
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, options, cancellationToken).ConfigureAwait(false))
654-
{
655-
updates.Add(update);
656-
657-
if (progressToken is not null)
658-
{
659-
progress.Report(new()
660-
{
661-
Progress = updates.Count,
662-
});
663-
}
664-
}
665-
666-
return ToCreateMessageResult(updates.ToChatResponse());
667-
};
668-
}
669-
670558
/// <summary>
671559
/// Sets the logging level for the server to control which log messages are sent to the client.
672560
/// </summary>

0 commit comments

Comments
 (0)