"My first operation on was on Christmas Eve in 2004 and ever since then I have been battling to try and get back to full fitness without a great deal of luck," McRae told www.gloucesterrugby.
"But I have been told enough is enough because my knee simply is not responding the way we wanted it to.
"My body is telling me something is not right and I cannot go on. Everybody would love to go out on their own terms but if I don't finish now, I risk causing irreparable damage to my knee later in life and I want to be able to play with my kids."
The 31-year-old former stand-off or full-back has no cartilage remaining in his left knee and that causes severe bone bruising and swelling. However, he was able to Sale and Bristol in the Guinness Premiership this season despite the discomfort and again against Toulon in the European Challenge Cup.
"I have tried to get back for over a year but the knee is soft and could collapse - which is not a great thought at all. It all seems a little bit surreal at the moment and it is tremendously disappointing to finish my career and my Gloucester career in this way.
"But I would like to take this opportunity to thank all the staff, players and supporters for their support over the last three and a half years. I have had a great time and I am disappointed to be leaving like this."
After two seasons as Gloucester's first choice stand-off, McRae began this year as skills and development coach. He made 44 appearances in all competitions and scored four tries.
In total, he contributed 114 points and was generally one of the most versatile play-makers in the Premiership.
Head coach Dean Ryan added: "It is tremendously sad when anyone is forced into retirement because Duncan has been a very special part of this squad for a couple of years now.
"The most unfortunate thing is that it's not on his own terms but his contribution, both on and off the field, have been outstanding to the development of the team and he will be sorely missed.
"Duncan is a competitor and I am sure this decision was very hard for him to make. Whatever he decides to do in the next stage of his life, the club wish him all the best and every success in the future."