mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-28 19:47:14 +08:00
change: check call tool result error
This commit is contained in:
@@ -1 +1 @@
|
|||||||
v5.0.0-beta-2505261453
|
v5.0.0-beta-2505261530
|
||||||
|
|||||||
+8
-16
@@ -218,7 +218,7 @@ func (h *MCPHost) GetTool(ctx context.Context, serverName, toolName string) (*mc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
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 {
|
if serverTools.Err != nil {
|
||||||
return nil, serverTools.Err
|
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
|
// InvokeTool calls a tool with the given arguments
|
||||||
@@ -271,9 +271,12 @@ func (h *MCPHost) InvokeTool(ctx context.Context,
|
|||||||
return nil, errors.Wrapf(err,
|
return nil, errors.Wrapf(err,
|
||||||
"call tool %s/%s failed", serverName, toolName)
|
"call tool %s/%s failed", serverName, toolName)
|
||||||
}
|
}
|
||||||
|
if result.IsError {
|
||||||
if err := handleToolError(result); err != nil {
|
if len(result.Content) > 0 {
|
||||||
return nil, err
|
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
|
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")
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/maja42/goval"
|
"github.com/maja42/goval"
|
||||||
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
@@ -20,7 +21,6 @@ import (
|
|||||||
"github.com/httprunner/httprunner/v5/code"
|
"github.com/httprunner/httprunner/v5/code"
|
||||||
"github.com/httprunner/httprunner/v5/internal/builtin"
|
"github.com/httprunner/httprunner/v5/internal/builtin"
|
||||||
"github.com/httprunner/httprunner/v5/mcphost"
|
"github.com/httprunner/httprunner/v5/mcphost"
|
||||||
mcp2 "github.com/mark3labs/mcp-go/mcp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewParser() *Parser {
|
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)
|
return nil, errors.Wrapf(err, "invoke tool %s/%s failed", serverName, funcName)
|
||||||
}
|
}
|
||||||
if result.IsError {
|
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
|
// extract text content
|
||||||
var resultText string
|
var resultText string
|
||||||
for _, item := range result.Content {
|
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)
|
resultText += fmt.Sprintf("%v ", contentMap.Text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -101,9 +101,10 @@ func (dExt *XTDriver) ExecuteAction(action MobileAction) (err error) {
|
|||||||
// Check if the tool execution had business logic errors
|
// Check if the tool execution had business logic errors
|
||||||
if result.IsError {
|
if result.IsError {
|
||||||
if len(result.Content) > 0 {
|
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)).
|
log.Debug().Str("method", string(action.Method)).
|
||||||
|
|||||||
Reference in New Issue
Block a user