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

@@ -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")
}