ik chain and bones inside the ik, please help!

Hello..i am trying to make a script for stretch bones with ik.
i have made the general concept of how to make it but i have a problem.
the problem is that. i create a bone hierarchy and i add the hi solver,. then i want to choose the ik chain and the script look and tell me how many bones are in the ik chain. any idea of how to do that. if i found this solution i think i can create the rest of the script. thanks in advance