diff --git a/.cursor/mcp.json b/.cursor/mcp.json new file mode 100644 index 0000000..512b5f8 --- /dev/null +++ b/.cursor/mcp.json @@ -0,0 +1,9 @@ +{ + "mcpServers": { + "cariddi-android-adb": { + "command": "go", + "args": ["run", "./tests"], + "cwd": "${workspaceFolder}" + } + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a0a57e7 --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module CariddiAndroid + +go 1.25.6 + +require ( + github.com/electricbubble/gadb v0.1.0 + github.com/modelcontextprotocol/go-sdk v1.2.0 +) + +require ( + github.com/google/jsonschema-go v0.3.0 // indirect + github.com/yosida95/uritemplate/v3 v3.0.2 // indirect + golang.org/x/oauth2 v0.30.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..487c86a --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +github.com/electricbubble/gadb v0.1.0 h1:h7RKlToMlFtGW4rUkAd4GSiFAHioMH5Nx7jtbb2nKi4= +github.com/electricbubble/gadb v0.1.0/go.mod h1:3293YJ6OWHv/Q6NA5dwSbK43MbmYm8+Vz2d7h5J3IA8= +github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8= +github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/jsonschema-go v0.3.0 h1:6AH2TxVNtk3IlvkkhjrtbUc4S8AvO0Xii0DxIygDg+Q= +github.com/google/jsonschema-go v0.3.0/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE= +github.com/modelcontextprotocol/go-sdk v1.2.0 h1:Y23co09300CEk8iZ/tMxIX1dVmKZkzoSBZOpJwUnc/s= +github.com/modelcontextprotocol/go-sdk v1.2.0/go.mod h1:6fM3LCm3yV7pAs8isnKLn07oKtB0MP9LHd3DfAcKw10= +github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4= +github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4= +golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI= +golang.org/x/oauth2 v0.30.0/go.mod h1:B++QgG3ZKulg6sRPGD/mqlHQs5rB3Ml9erfeDY7xKlU= +golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo= +golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg= diff --git a/tests/test.go b/tests/test.go new file mode 100644 index 0000000..9f853d3 --- /dev/null +++ b/tests/test.go @@ -0,0 +1,99 @@ +package main + +import ( + "context" + "log" + + "github.com/electricbubble/gadb" + "github.com/modelcontextprotocol/go-sdk/mcp" +) + +type runAdbInput struct { + Command string `json:"command" jsonschema:"the adb shell command to run on the device"` + DeviceSerial string `json:"deviceSerial,omitempty" jsonschema:"optional device serial; uses first connected device if empty"` +} + +func runAdb(ctx context.Context, req *mcp.CallToolRequest, input runAdbInput) (*mcp.CallToolResult, any, error) { + adbClient, err := gadb.NewClient() + if err != nil { + return &mcp.CallToolResult{ + IsError: true, + Content: []mcp.Content{ + &mcp.TextContent{Text: "Failed to connect to ADB server: " + err.Error()}, + }, + }, nil, nil + } + + devices, err := adbClient.DeviceList() + if err != nil { + return &mcp.CallToolResult{ + IsError: true, + Content: []mcp.Content{ + &mcp.TextContent{Text: "Failed to list devices: " + err.Error()}, + }, + }, nil, nil + } + + if len(devices) == 0 { + return &mcp.CallToolResult{ + IsError: true, + Content: []mcp.Content{ + &mcp.TextContent{Text: "No devices connected"}, + }, + }, nil, nil + } + + var dev gadb.Device + if input.DeviceSerial != "" { + found := false + for _, d := range devices { + if d.Serial() == input.DeviceSerial { + dev = d + found = true + break + } + } + if !found { + return &mcp.CallToolResult{ + IsError: true, + Content: []mcp.Content{ + &mcp.TextContent{Text: "Device not found: " + input.DeviceSerial}, + }, + }, nil, nil + } + } else { + dev = devices[0] + } + + output, err := dev.RunShellCommand(input.Command) + if err != nil { + return &mcp.CallToolResult{ + IsError: true, + Content: []mcp.Content{ + &mcp.TextContent{Text: "Command failed: " + err.Error()}, + }, + }, nil, nil + } + + return &mcp.CallToolResult{ + Content: []mcp.Content{ + &mcp.TextContent{Text: output}, + }, + }, nil, nil +} + +func main() { + server := mcp.NewServer(&mcp.Implementation{ + Name: "cariddi-android-adb", + Version: "v1.0.0", + }, nil) + + mcp.AddTool(server, &mcp.Tool{ + Name: "runAdb", + Description: "Run an adb shell command on a connected Android device and return the output. If deviceSerial is omitted, uses the first connected device.", + }, runAdb) + + if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil { + log.Fatal(err) + } +}