Skip to content

Commit 68525ef

Browse files
committed
feat: add image content type support for read file tool
1 parent 23a4bca commit 68525ef

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

main.go

+42-1
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ func isTextFile(mimeType string) bool {
325325
strings.Contains(mimeType, "+json")
326326
}
327327

328+
// isImageFile determines if a file is an image based on MIME type
329+
func isImageFile(mimeType string) bool {
330+
return strings.HasPrefix(mimeType, "image/")
331+
}
332+
328333
// pathToResourceURI converts a file path to a resource URI
329334
func pathToResourceURI(path string) string {
330335
return "file://" + path
@@ -575,8 +580,44 @@ func (s *FilesystemServer) handleReadFile(
575580
},
576581
},
577582
}, nil
583+
} else if isImageFile(mimeType) {
584+
// It's an image file, return as image content
585+
if info.Size() <= MAX_BASE64_SIZE {
586+
return &mcp.CallToolResult{
587+
Content: []mcp.Content{
588+
mcp.TextContent{
589+
Type: "text",
590+
Text: fmt.Sprintf("Image file: %s (%s, %d bytes)", validPath, mimeType, info.Size()),
591+
},
592+
mcp.ImageContent{
593+
Type: "image",
594+
Data: base64.StdEncoding.EncodeToString(content),
595+
MIMEType: mimeType,
596+
},
597+
},
598+
}, nil
599+
} else {
600+
// Too large for base64, return a reference
601+
resourceURI := pathToResourceURI(validPath)
602+
return &mcp.CallToolResult{
603+
Content: []mcp.Content{
604+
mcp.TextContent{
605+
Type: "text",
606+
Text: fmt.Sprintf("Image file is too large to display inline (%d bytes). Access it via resource URI: %s", info.Size(), resourceURI),
607+
},
608+
mcp.EmbeddedResource{
609+
Type: "resource",
610+
Resource: mcp.TextResourceContents{
611+
URI: resourceURI,
612+
MIMEType: "text/plain",
613+
Text: fmt.Sprintf("Large image: %s (%s, %d bytes)", validPath, mimeType, info.Size()),
614+
},
615+
},
616+
},
617+
}, nil
618+
}
578619
} else {
579-
// It's a binary file
620+
// It's another type of binary file
580621
resourceURI := pathToResourceURI(validPath)
581622

582623
if info.Size() <= MAX_BASE64_SIZE {

0 commit comments

Comments
 (0)