mcpResultToToolResult function
Converts a raw tools/call result map into a ToolExecutionResult.
Throws StateError when the result carries isError: true (the loop
turns the throw into an error tool result).
Implementation
ToolExecutionResult mcpResultToToolResult(Map<String, dynamic> result) {
final content = mcpContentBlocks(result['content']);
if (result['isError'] == true) {
final message = content
.whereType<TextContent>()
.map((block) => block.text)
.join('\n');
throw StateError(message.isEmpty ? 'MCP tool reported an error' : message);
}
if (content.isEmpty) {
final structured = result['structuredContent'];
if (structured != null) {
return ToolExecutionResult.text(_fitTextBudget(jsonEncode(structured)));
}
return ToolExecutionResult.text('[MCP tool returned no content]');
}
return ToolExecutionResult(content: _fitBudget(content));
}