Skip to content

Commit dd3e73b

Browse files
Copilotstephentoub
andcommitted
Use last content block instead of first in CallToolAsync<T>
Use result.Content[^1] instead of result.Content[0] for both error message extraction and text content fallback deserialization. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent b109728 commit dd3e73b

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ public ValueTask<CallToolResult> CallToolAsync(
933933
/// <para>
934934
/// This method calls the existing <see cref="CallToolAsync(string, IReadOnlyDictionary{string, object?}?, IProgress{ProgressNotificationValue}?, RequestOptions?, CancellationToken)"/>
935935
/// and then deserializes the result. If the result has <see cref="CallToolResult.StructuredContent"/>, that is deserialized
936-
/// as <typeparamref name="T"/>. Otherwise, if the result has text content, the text of the first <see cref="TextContentBlock"/>
936+
/// as <typeparamref name="T"/>. Otherwise, if the result has text content, the text of the last <see cref="TextContentBlock"/>
937937
/// is deserialized as <typeparamref name="T"/>.
938938
/// </para>
939939
/// <para>
@@ -952,7 +952,7 @@ public async ValueTask<T> CallToolAsync<T>(
952952

953953
if (result.IsError is true)
954954
{
955-
string errorMessage = result.Content.Count > 0 && result.Content[0] is TextContentBlock textBlock
955+
string errorMessage = result.Content.Count > 0 && result.Content[^1] is TextContentBlock textBlock
956956
? textBlock.Text
957957
: "The tool call returned an error.";
958958
throw new McpException(errorMessage);
@@ -967,7 +967,7 @@ public async ValueTask<T> CallToolAsync<T>(
967967
return JsonSerializer.Deserialize(structuredContent, typeInfo)!;
968968
}
969969

970-
if (result.Content.Count > 0 && result.Content[0] is TextContentBlock textContent)
970+
if (result.Content.Count > 0 && result.Content[^1] is TextContentBlock textContent)
971971
{
972972
return JsonSerializer.Deserialize(textContent.Text, typeInfo)!;
973973
}

0 commit comments

Comments
 (0)