Being 'where you are at when you are there' is the trick.
What I mean is taking steps to consciously tell yourself, "I am playing this song. It is fun and I enjoy it. I don't need to think of other things - only playing this song and having the best time I can while I am playing this song."
You don't have to 'run the chords' in your head. If you don't know them now, you are not going to teach yourself them in the seconds before you play. Just say to yourself that you are focusing all your energy on playing the song to the best of your ability, relax and enjoy yourself.
Sounds simplistic but it helps remind you that your mind should not be concerned with 'other things'. Doing nothing leaves the possibility wide open that you may start thinking of other things. The only thing you are there to do it jam on your song. If you can go 'inside' and live it - become one with the idea - you stand much more of a chance to really play the song without being interrupted by 'stray thoughts'. You can enjoy the act of playing the song much more because you are focused and not distracted.
But also know that you are human and the thing that make you human is fallibility. If you were infallible, well, that would imply you are perfect. But there are conscious steps you can take to 'orient' or 'center' yourself in all activities - so taking a conscious step to tell yourself to be where you are while you are there can go a long way to ensuring you are focused.
Finally, and I don't mean to imply anything, but there is a thread here about
bass players with ADD and AD/HD. One of the symptoms of those conditions is being constantly interrupted by 'stray thoughts'. I am not suggesting that you have ADD or AD/HD, but if you find that being constantly distracted is something that you experience in areas other then playing music, you may want to look into being tested.