mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 23:51:25 +08:00
refactor: modularize MCP server tools by functionality
- Split large mcp_server.go into modular files by functionality - Create dedicated files for each tool category: - mcp_tools_device.go: Device management tools - mcp_tools_touch.go: Touch operation tools - mcp_tools_swipe.go: Swipe and drag operation tools - mcp_tools_input.go: Input and IME tools - mcp_tools_button.go: Button operation tools - mcp_tools_app.go: Application management tools - mcp_tools_screen.go: Screen operation tools - mcp_tools_utility.go: Utility tools (sleep, popups) - mcp_tools_web.go: Web operation tools - mcp_tools_ai.go: AI-driven operation tools - Update mcp_server.md documentation to reflect modular architecture - Maintain pure ActionTool architecture with complete tool decoupling - Improve code organization and maintainability
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/uixt/option"
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
"github.com/mark3labs/mcp-go/server"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// ToolAIAction implements the ai_action tool call.
|
||||
type ToolAIAction struct{}
|
||||
|
||||
func (t *ToolAIAction) Name() option.ActionName {
|
||||
return option.ACTION_AIAction
|
||||
}
|
||||
|
||||
func (t *ToolAIAction) Description() string {
|
||||
return "Perform AI-driven automation actions using natural language prompts to describe the desired operation"
|
||||
}
|
||||
|
||||
func (t *ToolAIAction) Options() []mcp.ToolOption {
|
||||
unifiedReq := &option.ActionOptions{}
|
||||
return unifiedReq.GetMCPOptions(option.ACTION_AIAction)
|
||||
}
|
||||
|
||||
func (t *ToolAIAction) Implement() server.ToolHandlerFunc {
|
||||
return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
driverExt, err := setupXTDriver(ctx, request.Params.Arguments)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("setup driver failed: %w", err)
|
||||
}
|
||||
|
||||
unifiedReq, err := parseActionOptions(request.Params.Arguments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// AI action logic
|
||||
log.Info().Str("prompt", unifiedReq.Prompt).Msg("performing AI action")
|
||||
err = driverExt.AIAction(unifiedReq.Prompt)
|
||||
if err != nil {
|
||||
return mcp.NewToolResultError(fmt.Sprintf("AI action failed: %s", err.Error())), nil
|
||||
}
|
||||
|
||||
return mcp.NewToolResultText(fmt.Sprintf("Successfully performed AI action with prompt: %s", unifiedReq.Prompt)), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ToolAIAction) ConvertActionToCallToolRequest(action MobileAction) (mcp.CallToolRequest, error) {
|
||||
if prompt, ok := action.Params.(string); ok {
|
||||
arguments := map[string]any{
|
||||
"prompt": prompt,
|
||||
}
|
||||
return buildMCPCallToolRequest(t.Name(), arguments), nil
|
||||
}
|
||||
return mcp.CallToolRequest{}, fmt.Errorf("invalid AI action params: %v", action.Params)
|
||||
}
|
||||
|
||||
func (t *ToolAIAction) ReturnSchema() map[string]string {
|
||||
return map[string]string{
|
||||
"message": "string: Success message confirming AI action was performed",
|
||||
"prompt": "string: Natural language prompt that was processed",
|
||||
"actionTaken": "string: Description of the specific action that was taken by AI",
|
||||
}
|
||||
}
|
||||
|
||||
// ToolFinished implements the finished tool call.
|
||||
type ToolFinished struct{}
|
||||
|
||||
func (t *ToolFinished) Name() option.ActionName {
|
||||
return option.ACTION_Finished
|
||||
}
|
||||
|
||||
func (t *ToolFinished) Description() string {
|
||||
return "Mark the current automation task as completed with a result message"
|
||||
}
|
||||
|
||||
func (t *ToolFinished) Options() []mcp.ToolOption {
|
||||
unifiedReq := &option.ActionOptions{}
|
||||
return unifiedReq.GetMCPOptions(option.ACTION_Finished)
|
||||
}
|
||||
|
||||
func (t *ToolFinished) Implement() server.ToolHandlerFunc {
|
||||
return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
unifiedReq, err := parseActionOptions(request.Params.Arguments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Info().Str("reason", unifiedReq.Content).Msg("task finished")
|
||||
|
||||
return mcp.NewToolResultText(fmt.Sprintf("Task completed: %s", unifiedReq.Content)), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ToolFinished) ConvertActionToCallToolRequest(action MobileAction) (mcp.CallToolRequest, error) {
|
||||
if reason, ok := action.Params.(string); ok {
|
||||
arguments := map[string]any{
|
||||
"content": reason,
|
||||
}
|
||||
return buildMCPCallToolRequest(t.Name(), arguments), nil
|
||||
}
|
||||
return mcp.CallToolRequest{}, fmt.Errorf("invalid finished params: %v", action.Params)
|
||||
}
|
||||
|
||||
func (t *ToolFinished) ReturnSchema() map[string]string {
|
||||
return map[string]string{
|
||||
"message": "string: Success message confirming task completion",
|
||||
"content": "string: Completion reason or result description",
|
||||
"taskCompleted": "bool: Boolean indicating task was successfully finished",
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user