mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 07:26:55 +08:00
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:
+63
-53
@@ -11,7 +11,11 @@ import (
|
||||
)
|
||||
|
||||
// ToolListPackages implements the list_packages tool call.
|
||||
type ToolListPackages struct{}
|
||||
type ToolListPackages struct {
|
||||
// Return data fields - these define the structure of data returned by this tool
|
||||
Packages []string `json:"packages" desc:"List of installed app package names on the device"`
|
||||
Count int `json:"count" desc:"Number of installed packages"`
|
||||
}
|
||||
|
||||
func (t *ToolListPackages) Name() option.ActionName {
|
||||
return option.ACTION_ListPackages
|
||||
@@ -35,9 +39,16 @@ func (t *ToolListPackages) Implement() server.ToolHandlerFunc {
|
||||
|
||||
apps, err := driverExt.IDriver.GetDevice().ListPackages()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return NewMCPErrorResponse("Failed to list packages: " + err.Error()), nil
|
||||
}
|
||||
return mcp.NewToolResultText(fmt.Sprintf("Device packages: %v", apps)), nil
|
||||
|
||||
message := fmt.Sprintf("Found %d installed packages", len(apps))
|
||||
returnData := ToolListPackages{
|
||||
Packages: apps,
|
||||
Count: len(apps),
|
||||
}
|
||||
|
||||
return NewMCPSuccessResponse(message, &returnData), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,14 +56,11 @@ func (t *ToolListPackages) ConvertActionToCallToolRequest(action option.MobileAc
|
||||
return buildMCPCallToolRequest(t.Name(), map[string]any{}), nil
|
||||
}
|
||||
|
||||
func (t *ToolListPackages) ReturnSchema() map[string]string {
|
||||
return map[string]string{
|
||||
"packages": "[]string: List of installed app package names on the device",
|
||||
}
|
||||
}
|
||||
|
||||
// ToolLaunchApp implements the launch_app tool call.
|
||||
type ToolLaunchApp struct{}
|
||||
type ToolLaunchApp struct {
|
||||
// Return data fields - these define the structure of data returned by this tool
|
||||
PackageName string `json:"packageName" desc:"Package name of the launched app"`
|
||||
}
|
||||
|
||||
func (t *ToolLaunchApp) Name() option.ActionName {
|
||||
return option.ACTION_AppLaunch
|
||||
@@ -86,10 +94,13 @@ func (t *ToolLaunchApp) Implement() server.ToolHandlerFunc {
|
||||
// Launch app action logic
|
||||
err = driverExt.AppLaunch(unifiedReq.PackageName)
|
||||
if err != nil {
|
||||
return mcp.NewToolResultError(fmt.Sprintf("Launch app failed: %s", err.Error())), nil
|
||||
return NewMCPErrorResponse(fmt.Sprintf("Launch app failed: %s", err.Error())), nil
|
||||
}
|
||||
|
||||
return mcp.NewToolResultText(fmt.Sprintf("Successfully launched app: %s", unifiedReq.PackageName)), nil
|
||||
message := fmt.Sprintf("Successfully launched app: %s", unifiedReq.PackageName)
|
||||
returnData := ToolLaunchApp{PackageName: unifiedReq.PackageName}
|
||||
|
||||
return NewMCPSuccessResponse(message, &returnData), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,12 +114,12 @@ func (t *ToolLaunchApp) ConvertActionToCallToolRequest(action option.MobileActio
|
||||
return mcp.CallToolRequest{}, fmt.Errorf("invalid app launch params: %v", action.Params)
|
||||
}
|
||||
|
||||
func (t *ToolLaunchApp) ReturnSchema() map[string]string {
|
||||
return defaultReturnSchema()
|
||||
}
|
||||
|
||||
// ToolTerminateApp implements the terminate_app tool call.
|
||||
type ToolTerminateApp struct{}
|
||||
type ToolTerminateApp struct {
|
||||
// Return data fields - these define the structure of data returned by this tool
|
||||
PackageName string `json:"packageName" desc:"Package name of the terminated app"`
|
||||
WasRunning bool `json:"wasRunning" desc:"Whether the app was actually running before termination"`
|
||||
}
|
||||
|
||||
func (t *ToolTerminateApp) Name() option.ActionName {
|
||||
return option.ACTION_AppTerminate
|
||||
@@ -142,13 +153,19 @@ func (t *ToolTerminateApp) Implement() server.ToolHandlerFunc {
|
||||
// Terminate app action logic
|
||||
success, err := driverExt.AppTerminate(unifiedReq.PackageName)
|
||||
if err != nil {
|
||||
return mcp.NewToolResultError(fmt.Sprintf("Terminate app failed: %s", err.Error())), nil
|
||||
return NewMCPErrorResponse(fmt.Sprintf("Terminate app failed: %s", err.Error())), nil
|
||||
}
|
||||
if !success {
|
||||
log.Warn().Str("packageName", unifiedReq.PackageName).Msg("app was not running")
|
||||
}
|
||||
|
||||
return mcp.NewToolResultText(fmt.Sprintf("Successfully terminated app: %s", unifiedReq.PackageName)), nil
|
||||
message := fmt.Sprintf("Successfully terminated app: %s", unifiedReq.PackageName)
|
||||
returnData := ToolTerminateApp{
|
||||
PackageName: unifiedReq.PackageName,
|
||||
WasRunning: success,
|
||||
}
|
||||
|
||||
return NewMCPSuccessResponse(message, &returnData), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,12 +179,11 @@ func (t *ToolTerminateApp) ConvertActionToCallToolRequest(action option.MobileAc
|
||||
return mcp.CallToolRequest{}, fmt.Errorf("invalid app terminate params: %v", action.Params)
|
||||
}
|
||||
|
||||
func (t *ToolTerminateApp) ReturnSchema() map[string]string {
|
||||
return defaultReturnSchema()
|
||||
}
|
||||
|
||||
// ToolAppInstall implements the app_install tool call.
|
||||
type ToolAppInstall struct{}
|
||||
type ToolAppInstall struct {
|
||||
// Return data fields - these define the structure of data returned by this tool
|
||||
Path string `json:"path" desc:"Path or URL of the installed app"`
|
||||
}
|
||||
|
||||
func (t *ToolAppInstall) Name() option.ActionName {
|
||||
return option.ACTION_AppInstall
|
||||
@@ -197,10 +213,13 @@ func (t *ToolAppInstall) Implement() server.ToolHandlerFunc {
|
||||
// App install action logic
|
||||
err = driverExt.GetDevice().Install(unifiedReq.AppUrl)
|
||||
if err != nil {
|
||||
return mcp.NewToolResultError(fmt.Sprintf("App install failed: %s", err.Error())), nil
|
||||
return NewMCPErrorResponse(fmt.Sprintf("App install failed: %s", err.Error())), nil
|
||||
}
|
||||
|
||||
return mcp.NewToolResultText(fmt.Sprintf("Successfully installed app from: %s", unifiedReq.AppUrl)), nil
|
||||
message := fmt.Sprintf("Successfully installed app from: %s", unifiedReq.AppUrl)
|
||||
returnData := ToolAppInstall{Path: unifiedReq.AppUrl}
|
||||
|
||||
return NewMCPSuccessResponse(message, &returnData), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,15 +233,11 @@ func (t *ToolAppInstall) ConvertActionToCallToolRequest(action option.MobileActi
|
||||
return mcp.CallToolRequest{}, fmt.Errorf("invalid app install params: %v", action.Params)
|
||||
}
|
||||
|
||||
func (t *ToolAppInstall) ReturnSchema() map[string]string {
|
||||
return map[string]string{
|
||||
"message": "string: Success message confirming app installation",
|
||||
"appUrl": "string: URL or path of the app that was installed",
|
||||
}
|
||||
}
|
||||
|
||||
// ToolAppUninstall implements the app_uninstall tool call.
|
||||
type ToolAppUninstall struct{}
|
||||
type ToolAppUninstall struct {
|
||||
// Return data fields - these define the structure of data returned by this tool
|
||||
PackageName string `json:"packageName" desc:"Package name of the uninstalled app"`
|
||||
}
|
||||
|
||||
func (t *ToolAppUninstall) Name() option.ActionName {
|
||||
return option.ACTION_AppUninstall
|
||||
@@ -252,10 +267,13 @@ func (t *ToolAppUninstall) Implement() server.ToolHandlerFunc {
|
||||
// App uninstall action logic
|
||||
err = driverExt.GetDevice().Uninstall(unifiedReq.PackageName)
|
||||
if err != nil {
|
||||
return mcp.NewToolResultError(fmt.Sprintf("App uninstall failed: %s", err.Error())), nil
|
||||
return NewMCPErrorResponse(fmt.Sprintf("App uninstall failed: %s", err.Error())), nil
|
||||
}
|
||||
|
||||
return mcp.NewToolResultText(fmt.Sprintf("Successfully uninstalled app: %s", unifiedReq.PackageName)), nil
|
||||
message := fmt.Sprintf("Successfully uninstalled app: %s", unifiedReq.PackageName)
|
||||
returnData := ToolAppUninstall{PackageName: unifiedReq.PackageName}
|
||||
|
||||
return NewMCPSuccessResponse(message, &returnData), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,15 +287,11 @@ func (t *ToolAppUninstall) ConvertActionToCallToolRequest(action option.MobileAc
|
||||
return mcp.CallToolRequest{}, fmt.Errorf("invalid app uninstall params: %v", action.Params)
|
||||
}
|
||||
|
||||
func (t *ToolAppUninstall) ReturnSchema() map[string]string {
|
||||
return map[string]string{
|
||||
"message": "string: Success message confirming app uninstallation",
|
||||
"packageName": "string: Package name of the app that was uninstalled",
|
||||
}
|
||||
}
|
||||
|
||||
// ToolAppClear implements the app_clear tool call.
|
||||
type ToolAppClear struct{}
|
||||
type ToolAppClear struct {
|
||||
// Return data fields - these define the structure of data returned by this tool
|
||||
PackageName string `json:"packageName" desc:"Package name of the app whose data was cleared"`
|
||||
}
|
||||
|
||||
func (t *ToolAppClear) Name() option.ActionName {
|
||||
return option.ACTION_AppClear
|
||||
@@ -307,10 +321,13 @@ func (t *ToolAppClear) Implement() server.ToolHandlerFunc {
|
||||
// App clear action logic
|
||||
err = driverExt.AppClear(unifiedReq.PackageName)
|
||||
if err != nil {
|
||||
return mcp.NewToolResultError(fmt.Sprintf("App clear failed: %s", err.Error())), nil
|
||||
return NewMCPErrorResponse(fmt.Sprintf("App clear failed: %s", err.Error())), nil
|
||||
}
|
||||
|
||||
return mcp.NewToolResultText(fmt.Sprintf("Successfully cleared app: %s", unifiedReq.PackageName)), nil
|
||||
message := fmt.Sprintf("Successfully cleared app: %s", unifiedReq.PackageName)
|
||||
returnData := ToolAppClear{PackageName: unifiedReq.PackageName}
|
||||
|
||||
return NewMCPSuccessResponse(message, &returnData), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,10 +340,3 @@ func (t *ToolAppClear) ConvertActionToCallToolRequest(action option.MobileAction
|
||||
}
|
||||
return mcp.CallToolRequest{}, fmt.Errorf("invalid app clear params: %v", action.Params)
|
||||
}
|
||||
|
||||
func (t *ToolAppClear) ReturnSchema() map[string]string {
|
||||
return map[string]string{
|
||||
"message": "string: Success message confirming app data and cache were cleared",
|
||||
"packageName": "string: Package name of the app that was cleared",
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user