MCPcopy
hub / github.com/docker/docker-agent / readImageFile

Method readImageFile

pkg/tools/builtin/filesystem/filesystem.go:1036–1083  ·  view source on GitHub ↗

readImageFile reads an image file and returns it as base64-encoded image content. The caller must ensure the file exists (e.g. via os.Stat) before calling this method.

(resolvedPath, originalPath string)

Source from the content-addressed store, hash-verified

1034// readImageFile reads an image file and returns it as base64-encoded image content.
1035// The caller must ensure the file exists (e.g. via os.Stat) before calling this method.
1036func (t *ToolSet) readImageFile(resolvedPath, originalPath string) (*tools.ToolCallResult, error) {
1037 data, err := t.readFile(resolvedPath)
1038 if err != nil {
1039 errMsg := err.Error()
1040 return &tools.ToolCallResult{
1041 Output: errMsg,
1042 IsError: true,
1043 Meta: ReadFileMeta{
1044 Error: errMsg,
1045 },
1046 }, nil
1047 }
1048
1049 mimeType := chat.DetectMimeType(resolvedPath)
1050
1051 // Resize the image if it exceeds provider limits (max 2000×2000, max 4.5MB).
1052 resized, err := chat.ResizeImage(data, mimeType)
1053 if err != nil {
1054 // Check if the original exceeds limits before falling back
1055 if len(data) > chat.MaxImageBytes {
1056 return &tools.ToolCallResult{
1057 Output: fmt.Sprintf("Error: Image file too large (%d bytes, max %d bytes)", len(data), chat.MaxImageBytes),
1058 IsError: true,
1059 Meta: ReadFileMeta{Path: originalPath, Error: "image too large"},
1060 }, nil
1061 }
1062 // Original is within limits, proceed with fallback
1063 slog.Warn("Image resize failed, sending original (within limits)", "path", originalPath, "error", err)
1064 encoded := base64.StdEncoding.EncodeToString(data)
1065 return &tools.ToolCallResult{
1066 Output: fmt.Sprintf("Read image file %s [%s] (%d bytes)", originalPath, mimeType, len(data)),
1067 Images: []tools.ImageContent{{Data: encoded, MimeType: mimeType}},
1068 Meta: ReadFileMeta{Path: originalPath},
1069 }, nil
1070 }
1071
1072 encoded := base64.StdEncoding.EncodeToString(resized.Data)
1073 output := fmt.Sprintf("Read image file %s [%s] (%d bytes)", originalPath, resized.MimeType, len(resized.Data))
1074 if note := chat.FormatDimensionNote(resized); note != "" {
1075 output += "\n" + note
1076 }
1077
1078 return &tools.ToolCallResult{
1079 Output: output,
1080 Images: []tools.ImageContent{{Data: encoded, MimeType: resized.MimeType}},
1081 Meta: ReadFileMeta{Path: originalPath},
1082 }, nil
1083}
1084
1085func (t *ToolSet) handleReadMultipleFiles(ctx context.Context, args ReadMultipleFilesArgs) (*tools.ToolCallResult, error) {
1086 annotateFilesystemSpan(ctx, "read_multiple_files", "")

Callers 1

handleReadFileMethod · 0.95

Implementers 15

StartableToolSetpkg/tools/startable.go
fakeToolSetpkg/tools/named_test.go
stubDescriberpkg/tools/startable_test.go
stubToolSetpkg/tools/startable_test.go
flappyToolSetpkg/tools/startable_test.go
listFlappyToolSetpkg/tools/startable_test.go
reportingToolSetpkg/tools/startable_test.go
reportingStartOnlyToolSetpkg/tools/startable_test.go
recoveryFailingToolSetpkg/tools/startable_test.go
codeModeToolpkg/tools/codemode/codemode.go
testToolSetpkg/tools/codemode/codemode_test.go
Toolsetpkg/tools/a2a/a2a.go

Calls 6

readFileMethod · 0.95
DetectMimeTypeFunction · 0.92
ResizeImageFunction · 0.92
FormatDimensionNoteFunction · 0.92
WarnMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected