MSPs have refused consent for the UK Government's controversial "spy cops" legislation, which would allow undercover agents to break the law.

Justice Secretary Humza Yousaf said insufficient safeguards have been put in place despite "limited" improvements. 

The move means Scotland will be removed from the provisions of the Covert Human Intelligence Sources (Criminal Conduct) Bill.

Critics said this could jeopardise public safety.

Yousaf acknowledged it could mean Holyrood has to pass emergency legislation in future to "ensure the security of police operations".

The controversial bill would allow undercover agents to commit crimes in the course of their work, creating a new class of permission called a Criminal Conduct Authorisation.

This could be used by a range of bodies including Police Scotland.

CCAs make specific conduct “lawful for all purposes” and protect the person doing it from any liability.

The conduct must be deemed necessary and proportionate, but there is no limit on the offence it could cover, so that criminals could not devise tests to uncover any moles.

Speaking in Holyrood, Scottish Labour MSP Neil Findlay said it would be a "human rights scandal waiting to happen" and an "affront to our democracy".

Scottish Liberal Democrat MSP Liam McArthur said the bill places "no limits on the type of crime that can be authorised, not rape, not torture, not even murder" and includes no requirement for prior judicial approval.

But Scottish Conservative MSP Liam Kerr said the ability to carry out vital covert work "presently lacks statutory foundation". 

He said a court case has been brought against the UK Government relating to covert sources, with an appeal judgement due soon.

He added: "It is possible that the court will find that the current system of covert surveillance is unlawful. 

"In such an event, the UK would have no basis for undertaking covert surveillance into, for example, terrorism, cyber crime, people trafficking or drugs running.

"The UK bill anticipates this, setting a statutory framework allowing our security services and our police to continue to protect us.

"That bill contains provisions to ensure such practices can continue in Scotland, hence the request for legislative consent today."

Kerr said if Holyrood does not consent, the UK Government will remove Scotland from the bill's provisions. 

This means that if the court ruling finds against current practices, they will have to cease in Scotland immediately, he said.

He said Holyrood would then be required to pass emergency legislation in the middle of a pandemic to ensure operations can continue.

Kerr told MSPs: "This is vital legislation which provides a clear and consistent statutory basis for public authorities to keep the public safe.

"Failure to give consent risks leaving Scotland's people exposed. 

"That is deeply irresponsible, and I cannot believe that any MSP would vote to countenance such a situation."

Yousaf said the UK Government has not brought forward amendments to the bill that satisfy his concerns. 

He said "independent judicial scrutiny" before any activity takes place would go "a long way" towards addressing worries.

However he acknowledged the forthcoming Court of Appeal judgement may require "emergency or expedited legislation" to be passed in Scotland.

He said: "That would, of necessity, be no more than just a sticking plaster to ensure the security of police operations. 

"If such a measure is required, it would only be a stopgap to allow a full assessment of the Court of Appeal judgement. 

"That assessment will, in turn, inform any subsequent measures that may have to be put in place by this Government or indeed by any future Government post elections."

MSPs voted to refuse consent by 92 votes to 27, with only the Tories backing the Bill.

Scottish Labour's decision to reject it contrasts with the party's policy in Westminster, where UK Labour leader Sir Keir Starmer has told MPs and peers to abstain.

Two Labour frontbenchers resigned last year after voting against the legislation.