|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/facebookincubator/go-belt/tool/logger" |
| 10 | + "github.com/mark3labs/mcp-go/mcp" |
| 11 | + "github.com/mark3labs/mcp-go/server" |
| 12 | +) |
| 13 | + |
| 14 | +const defaultPhotoPath = "/data/local/tmp/photo.jpg" |
| 15 | + |
| 16 | +func registerCapturePhoto(s *server.MCPServer) { |
| 17 | + tool := mcp.NewTool("capture_photo", |
| 18 | + mcp.WithDescription( |
| 19 | + "Take a photo from a device camera and return it as a base64-encoded image. "+ |
| 20 | + "Uses the camera intent or the 'am' command to trigger the camera app. "+ |
| 21 | + "The photo is saved to a temporary file on device.", |
| 22 | + ), |
| 23 | + mcp.WithString("camera_id", |
| 24 | + mcp.Description("Camera ID to use (e.g. '0' for rear, '1' for front). Default is '0'."), |
| 25 | + ), |
| 26 | + mcp.WithString("path", |
| 27 | + mcp.Description("Output path on device (default: /data/local/tmp/photo.jpg)"), |
| 28 | + ), |
| 29 | + mcp.WithDestructiveHintAnnotation(true), |
| 30 | + mcp.WithIdempotentHintAnnotation(false), |
| 31 | + ) |
| 32 | + |
| 33 | + s.AddTool(tool, handleCapturePhoto) |
| 34 | +} |
| 35 | + |
| 36 | +func handleCapturePhoto( |
| 37 | + ctx context.Context, |
| 38 | + request mcp.CallToolRequest, |
| 39 | +) (*mcp.CallToolResult, error) { |
| 40 | + logger.Tracef(ctx, "handleCapturePhoto") |
| 41 | + defer func() { logger.Tracef(ctx, "/handleCapturePhoto") }() |
| 42 | + |
| 43 | + path := request.GetString("path", defaultPhotoPath) |
| 44 | + |
| 45 | + // Use am start to launch the camera capture intent. |
| 46 | + // On many devices, the simplest shell-based approach is to use screencap |
| 47 | + // since direct camera capture from shell requires complex IGBP setup. |
| 48 | + // For a quick shell-based approach, launch the camera app and screenshot. |
| 49 | + cmd := fmt.Sprintf( |
| 50 | + "am start -a android.media.action.IMAGE_CAPTURE --ei android.intent.extras.CAMERA_FACING 0 && "+ |
| 51 | + "sleep 2 && input keyevent KEYCODE_CAMERA && sleep 1 && "+ |
| 52 | + "screencap -p %s", |
| 53 | + shellQuote(path), |
| 54 | + ) |
| 55 | + |
| 56 | + _, err := shellExec(cmd) |
| 57 | + if err != nil { |
| 58 | + return mcp.NewToolResultError(fmt.Sprintf("capture photo: %v", err)), nil |
| 59 | + } |
| 60 | + |
| 61 | + b64, err := shellExec(fmt.Sprintf("base64 -w 0 %s", shellQuote(path))) |
| 62 | + if err != nil { |
| 63 | + return mcp.NewToolResultError(fmt.Sprintf("base64 encode: %v", err)), nil |
| 64 | + } |
| 65 | + |
| 66 | + // Clean up. |
| 67 | + _, _ = shellExec(fmt.Sprintf("rm -f %s", shellQuote(path))) |
| 68 | + |
| 69 | + return mcp.NewToolResultImage("photo", b64, "image/jpeg"), nil |
| 70 | +} |
0 commit comments