feat: optimize MCP tools response format with automatic schema generation

- Remove all manual ReturnSchema() methods from tools
- Implement automatic schema generation using reflection
- Unify response format to flat structure with action/success/message fields
- Simplify tool implementation by removing MCPResponse embedding
- Update documentation to reflect new architecture
- Achieve ~70% code reduction while maintaining type safety
This commit is contained in:
lilong.129
2025-06-05 23:17:06 +08:00
parent 56831845ca
commit 6e1bd5bbe2
15 changed files with 990 additions and 797 deletions
+55 -48
View File
@@ -11,7 +11,11 @@ import (
)
// ToolTapXY implements the tap_xy tool call.
type ToolTapXY struct{}
type ToolTapXY struct {
// Return data fields - these define the structure of data returned by this tool
X float64 `json:"x" desc:"X coordinate where tap was performed"`
Y float64 `json:"y" desc:"Y coordinate where tap was performed"`
}
func (t *ToolTapXY) Name() option.ActionName {
return option.ACTION_TapXY
@@ -54,10 +58,16 @@ func (t *ToolTapXY) Implement() server.ToolHandlerFunc {
// Tap action logic
err = driverExt.TapXY(unifiedReq.X, unifiedReq.Y, opts...)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("Tap failed: %s", err.Error())), nil
return NewMCPErrorResponse(fmt.Sprintf("Tap failed: %s", err.Error())), nil
}
return mcp.NewToolResultText(fmt.Sprintf("Successfully tapped at coordinates (%.2f, %.2f)", unifiedReq.X, unifiedReq.Y)), nil
message := fmt.Sprintf("Successfully tapped at coordinates (%.2f, %.2f)", unifiedReq.X, unifiedReq.Y)
returnData := ToolTapXY{
X: unifiedReq.X,
Y: unifiedReq.Y,
}
return NewMCPSuccessResponse(message, &returnData), nil
}
}
@@ -81,14 +91,12 @@ func (t *ToolTapXY) ConvertActionToCallToolRequest(action option.MobileAction) (
return mcp.CallToolRequest{}, fmt.Errorf("invalid tap params: %v", action.Params)
}
func (t *ToolTapXY) ReturnSchema() map[string]string {
return map[string]string{
"message": "string: Success message confirming tap operation at specified coordinates",
}
}
// ToolTapAbsXY implements the tap_abs_xy tool call.
type ToolTapAbsXY struct{}
type ToolTapAbsXY struct {
// Return data fields - these define the structure of data returned by this tool
X float64 `json:"x" desc:"X coordinate where tap was performed (absolute pixels)"`
Y float64 `json:"y" desc:"Y coordinate where tap was performed (absolute pixels)"`
}
func (t *ToolTapAbsXY) Name() option.ActionName {
return option.ACTION_TapAbsXY
@@ -136,10 +144,16 @@ func (t *ToolTapAbsXY) Implement() server.ToolHandlerFunc {
// Tap absolute XY action logic
err = driverExt.TapAbsXY(unifiedReq.X, unifiedReq.Y, opts...)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("Tap absolute XY failed: %s", err.Error())), nil
return NewMCPErrorResponse(fmt.Sprintf("Tap absolute XY failed: %s", err.Error())), nil
}
return mcp.NewToolResultText(fmt.Sprintf("Successfully tapped at absolute coordinates (%.0f, %.0f)", unifiedReq.X, unifiedReq.Y)), nil
message := fmt.Sprintf("Successfully tapped at absolute coordinates (%.0f, %.0f)", unifiedReq.X, unifiedReq.Y)
returnData := ToolTapAbsXY{
X: unifiedReq.X,
Y: unifiedReq.Y,
}
return NewMCPSuccessResponse(message, &returnData), nil
}
}
@@ -163,21 +177,11 @@ func (t *ToolTapAbsXY) ConvertActionToCallToolRequest(action option.MobileAction
return mcp.CallToolRequest{}, fmt.Errorf("invalid tap abs params: %v", action.Params)
}
func (t *ToolTapAbsXY) ReturnSchema() map[string]string {
return map[string]string{
"message": "string: Success message confirming tap operation at absolute coordinates",
}
}
// defaultReturnSchema provides a standard return schema for most tools
func defaultReturnSchema() map[string]string {
return map[string]string{
"message": "string: Success message confirming the operation was completed",
}
}
// ToolTapByOCR implements the tap_ocr tool call.
type ToolTapByOCR struct{}
type ToolTapByOCR struct {
// Return data fields - these define the structure of data returned by this tool
Text string `json:"text" desc:"Text that was tapped by OCR"`
}
func (t *ToolTapByOCR) Name() option.ActionName {
return option.ACTION_TapByOCR
@@ -220,10 +224,13 @@ func (t *ToolTapByOCR) Implement() server.ToolHandlerFunc {
// Tap by OCR action logic
err = driverExt.TapByOCR(unifiedReq.Text, opts...)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("Tap by OCR failed: %s", err.Error())), nil
return NewMCPErrorResponse(fmt.Sprintf("Tap by OCR failed: %s", err.Error())), nil
}
return mcp.NewToolResultText(fmt.Sprintf("Successfully tapped on OCR text: %s", unifiedReq.Text)), nil
message := fmt.Sprintf("Successfully tapped on OCR text: %s", unifiedReq.Text)
returnData := ToolTapByOCR{Text: unifiedReq.Text}
return NewMCPSuccessResponse(message, &returnData), nil
}
}
@@ -241,14 +248,9 @@ func (t *ToolTapByOCR) ConvertActionToCallToolRequest(action option.MobileAction
return mcp.CallToolRequest{}, fmt.Errorf("invalid tap by OCR params: %v", action.Params)
}
func (t *ToolTapByOCR) ReturnSchema() map[string]string {
return map[string]string{
"message": "string: Success message confirming the operation was completed",
}
}
// ToolTapByCV implements the tap_cv tool call.
type ToolTapByCV struct{}
type ToolTapByCV struct { // Return data fields - these define the structure of data returned by this tool
}
func (t *ToolTapByCV) Name() option.ActionName {
return option.ACTION_TapByCV
@@ -288,10 +290,13 @@ func (t *ToolTapByCV) Implement() server.ToolHandlerFunc {
// We'll add a basic implementation that triggers CV recognition
err = driverExt.TapByCV(opts...)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("Tap by CV failed: %s", err.Error())), nil
return NewMCPErrorResponse(fmt.Sprintf("Tap by CV failed: %s", err.Error())), nil
}
return mcp.NewToolResultText("Successfully tapped by computer vision"), nil
message := "Successfully tapped by computer vision"
returnData := ToolTapByCV{}
return NewMCPSuccessResponse(message, &returnData), nil
}
}
@@ -307,12 +312,12 @@ func (t *ToolTapByCV) ConvertActionToCallToolRequest(action option.MobileAction)
return buildMCPCallToolRequest(t.Name(), arguments), nil
}
func (t *ToolTapByCV) ReturnSchema() map[string]string {
return defaultReturnSchema()
}
// ToolDoubleTapXY implements the double_tap_xy tool call.
type ToolDoubleTapXY struct{}
type ToolDoubleTapXY struct {
// Return data fields - these define the structure of data returned by this tool
X float64 `json:"x" desc:"X coordinate where double tap was performed"`
Y float64 `json:"y" desc:"Y coordinate where double tap was performed"`
}
func (t *ToolDoubleTapXY) Name() option.ActionName {
return option.ACTION_DoubleTapXY
@@ -347,10 +352,16 @@ func (t *ToolDoubleTapXY) Implement() server.ToolHandlerFunc {
// Double tap XY action logic
err = driverExt.DoubleTap(unifiedReq.X, unifiedReq.Y)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("Double tap failed: %s", err.Error())), nil
return NewMCPErrorResponse(fmt.Sprintf("Double tap failed: %s", err.Error())), nil
}
return mcp.NewToolResultText(fmt.Sprintf("Successfully double tapped at (%.2f, %.2f)", unifiedReq.X, unifiedReq.Y)), nil
message := fmt.Sprintf("Successfully double tapped at (%.2f, %.2f)", unifiedReq.X, unifiedReq.Y)
returnData := ToolDoubleTapXY{
X: unifiedReq.X,
Y: unifiedReq.Y,
}
return NewMCPSuccessResponse(message, &returnData), nil
}
}
@@ -365,7 +376,3 @@ func (t *ToolDoubleTapXY) ConvertActionToCallToolRequest(action option.MobileAct
}
return mcp.CallToolRequest{}, fmt.Errorf("invalid double tap params: %v", action.Params)
}
func (t *ToolDoubleTapXY) ReturnSchema() map[string]string {
return defaultReturnSchema()
}