"He will join the club on July 16 and be available for four County Championship matches and five Totesport League matches," Middlesex coach John Emburey told the club's website.
"We are delighted to have captured a world class bowler in Glenn, especially for such a vital period of the season."
McGrath, 34, Australia's leading paceman with 430 wickets from 95 Tests, stands in at Middlesex for all rounder Lance Klusener, who will be returning to South Africa for the tour of Sri Lanka.