convertToMCPResult 将自定义的 MCPToolResult 转换为官方 SDK 的格式
(result *MCPToolResult)
| 448 | |
| 449 | // convertToMCPResult 将自定义的 MCPToolResult 转换为官方 SDK 的格式 |
| 450 | func convertToMCPResult(result *MCPToolResult) *mcp.CallToolResult { |
| 451 | var contents []mcp.Content |
| 452 | for _, c := range result.Content { |
| 453 | switch c.Type { |
| 454 | case "text": |
| 455 | contents = append(contents, &mcp.TextContent{Text: c.Text}) |
| 456 | case "image": |
| 457 | // 解码 base64 字符串为 []byte |
| 458 | imageData, err := base64.StdEncoding.DecodeString(c.Data) |
| 459 | if err != nil { |
| 460 | logrus.WithError(err).Error("Failed to decode base64 image data") |
| 461 | // 如果解码失败,添加错误文本 |
| 462 | contents = append(contents, &mcp.TextContent{ |
| 463 | Text: "图片数据解码失败: " + err.Error(), |
| 464 | }) |
| 465 | } else { |
| 466 | contents = append(contents, &mcp.ImageContent{ |
| 467 | Data: imageData, |
| 468 | MIMEType: c.MimeType, |
| 469 | }) |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | return &mcp.CallToolResult{ |
| 475 | Content: contents, |
| 476 | IsError: result.IsError, |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // convertStringsToInterfaces 辅助函数:将 []string 转换为 []interface{} |
| 481 | func convertStringsToInterfaces(strs []string) []interface{} { |