change: check call tool result error

This commit is contained in:
lilong.129
2025-05-26 15:30:51 +08:00
parent 5eafcc8a2b
commit 7045a9d452
4 changed files with 19 additions and 22 deletions

View File

@@ -1 +1 @@
v5.0.0-beta-2505261453
v5.0.0-beta-2505261530

View File

@@ -218,7 +218,7 @@ func (h *MCPHost) GetTool(ctx context.Context, serverName, toolName string) (*mc
}
}
if !found {
return nil, fmt.Errorf("no connection found for server %s", serverName)
return nil, fmt.Errorf("no connection found for MCP server %s", serverName)
}
if serverTools.Err != nil {
return nil, serverTools.Err
@@ -231,7 +231,7 @@ func (h *MCPHost) GetTool(ctx context.Context, serverName, toolName string) (*mc
}
}
return nil, fmt.Errorf("tool %s not found", toolName)
return nil, fmt.Errorf("MCP tool %s/%s not found", serverName, toolName)
}
// InvokeTool calls a tool with the given arguments
@@ -271,9 +271,12 @@ func (h *MCPHost) InvokeTool(ctx context.Context,
return nil, errors.Wrapf(err,
"call tool %s/%s failed", serverName, toolName)
}
if err := handleToolError(result); err != nil {
return nil, err
if result.IsError {
if len(result.Content) > 0 {
return nil, fmt.Errorf("invoke tool %s/%s failed: %v",
serverName, toolName, result.Content)
}
return nil, fmt.Errorf("invoke tool %s/%s failed", serverName, toolName)
}
return result, nil
@@ -383,14 +386,3 @@ func prepareClientInitRequest() mcp.InitializeRequest {
},
}
}
// handleToolError handles tool execution errors
func handleToolError(result *mcp.CallToolResult) error {
if !result.IsError {
return nil
}
if len(result.Content) > 0 {
return fmt.Errorf("tool error: %v", result.Content[0])
}
return fmt.Errorf("tool error: unknown error")
}

View File

@@ -12,6 +12,7 @@ import (
"strings"
"github.com/maja42/goval"
"github.com/mark3labs/mcp-go/mcp"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
@@ -20,7 +21,6 @@ import (
"github.com/httprunner/httprunner/v5/code"
"github.com/httprunner/httprunner/v5/internal/builtin"
"github.com/httprunner/httprunner/v5/mcphost"
mcp2 "github.com/mark3labs/mcp-go/mcp"
)
func NewParser() *Parser {
@@ -316,13 +316,17 @@ func (p *Parser) CallMCPTool(ctx context.Context, serverName,
return nil, errors.Wrapf(err, "invoke tool %s/%s failed", serverName, funcName)
}
if result.IsError {
return nil, fmt.Errorf("invoke tool %s/%s failed: %v", serverName, funcName, result.Content)
if len(result.Content) > 0 {
return nil, fmt.Errorf("invoke tool %s/%s failed: %v",
serverName, funcName, result.Content)
}
return nil, fmt.Errorf("invoke tool %s/%s failed", serverName, funcName)
}
// extract text content
var resultText string
for _, item := range result.Content {
if contentMap, ok := item.(mcp2.TextContent); ok {
if contentMap, ok := item.(mcp.TextContent); ok {
resultText += fmt.Sprintf("%v ", contentMap.Text)
}
}

View File

@@ -101,9 +101,10 @@ func (dExt *XTDriver) ExecuteAction(action MobileAction) (err error) {
// Check if the tool execution had business logic errors
if result.IsError {
if len(result.Content) > 0 {
return fmt.Errorf("tool execution failed: %s", result.Content[0])
return fmt.Errorf("invoke tool %s failed: %v",
tool.Name(), result.Content)
}
return fmt.Errorf("tool execution failed")
return fmt.Errorf("invoke tool %s failed", tool.Name())
}
log.Debug().Str("method", string(action.Method)).