The manager of the Leeds taxi firm was shocked to discover the Russian semi-automatic pistol when the bag was handed in as lost property.
Leeds Crown Court heard Louis Dhers and two other men got into the taxi on Russell Street, Dewsbury, and were driven to Burley Road, Leeds, on February 7 this year.
Carmel Pearson, prosecuting, said the taxi driver noticed a bag on the back seat after the men had left the vehicle.
The bag was taken to the taxi firm's base where the weapon was discovered inside the bag.
Miss Pearson said the gun contained a magazine which had a bullet inside it.
The weapon had been modified and the barrel replaced with a rifle barrel.
West Yorkshire Police were contacted and police executed a search warrant at the house on Russell Street.
Dhers was found hiding in the loft and was arrested.
The property was searched and large quantities of cannabis were found in different rooms.
Officers also found a smaller amount of amphetamines which Dhers said was for his own use.
The prosecutor said the total street value of the cannabis was £8,000.
The gun and ammunition was examined and found to contain traces of the defendant's DNA.
Dhers, of no fixed address, pleaded guilty to possession of a prohibited firearm, possession of ammunition without a certificate, possession of cannabis with intent to supply and possession of amphetamines.
The 37-year-old has a previous conviction for possessing weapons.
He was jailed in 2007 for possession of a sub-machine gun and ammunition.
Dhers also has convictions for kidnap, supplying class A drugs and escaping from custody.
Kara Frith, mitigating, said Dhers had pleaded guilty at an early stage and had the weapon because of his involvement in drug dealing.
Miss Frith said the ammunition was not capable of being fired by the gun and Dhers had never used the weapon.
Judge Rodney Jameson, QC, jailed Dhers for seven-and-a-half years.