Gloucestershire have signed left-arm spinner Tom Smith on a three-year deal following a three-month loan spell at the County Ground.
Smith said: "I am really excited about joining Gloucestershire permanently.
"It has been a great three months on loan and to be staying here for the next three seasons is a fantastic challenge for me."
Smith, who was initially signed as injury cover for Gloucestershire's FLt20 campaign, has been released by Middlesex following four years at Lord's.
He added: "I have thoroughly enjoyed my loan time and to have the opportunity to join this young and exciting squad was a easy decision to make."
Gloucestershire director of cricket John Bracewell said: "I am delighted Tom has agreed to sign a contract.
"We are pleased with the progress he has made since joining us and the all-round skills that he is starting to show.
"He controls the scoreboard when he bowls in all forms of the game and I rate him highly.
"His batting has been somewhat neglected in the past, but he has played a number of promising innings for us and will be useful in that respect too."