Theresa May narrowly avoided an embarrassing defeat after MPs voted by a majority of just six not to keep the UK in a customs union with the EU if a trade deal isn't agreed by January.

The razor-thin division saw 12 pro-EU Tories vote against the government by backing the amendment during debates on key Brexit legislation in the Commons on Tuesday evening.

Ministers were able to dodge humiliation with the help of five Labour MPs as the motion was defeated by 307 votes to 301.

The amendment would have forced the government to adopt a negotiating objective of seeking to keep the UK in “a customs union” with the EU after Brexit, unless it has managed to negotiate a “frictionless free trade area for goods” by 21 January next year.

Reports have suggested Tory whips told the party’s MPs a vote of no confidence in the government would be raised if the customs union amendment had been passed.

Minutes earlier, Ms May suffered only her second-ever Brexit defeat in the Commons as MPs voted to keep the UK in the European Medicines Agency.

The government also performed a U-turn on a proposal to bring forward the start of the summer Parliamentary recess to Thursday, rather than the planned 24 July date.

But the Government did not move the motion amid growing Tory opposition to the plan, with several pro-European Tories having already indicated they would have opposed any attempt to cut short the term with so much work to do on Brexit.

Labour was also understood to have instructed its MPs to vote against the proposal.