A knife-wielding psychotic who stabbed a stranger in the street has been locked up indefinitely in a hospital for the mentally ill after a judge said he feared he could kill someone.

Muhammed Ali was seen sharpening a big kitchen knife in Manningham Library, Bradford, three weeks before he plunged the weapon into Jurgen McDonald on April 20 last year.

Ali, 37, of Lilycroft Lane, Manningham, was held in prison after the attack while psychiatrists decided what was wrong with him.

Yesterday, Judge Jonathan Durham Hall QC made a Hospital Order that means Ali will be moved from Leeds Prison to Lynfield Mount Hospital, Bradford, within 28 days.

He will not be freed until a tribunal of top doctors agrees that his illness can be safely managed in the community.

Judge Durham Hall said Ali suffered from a serious mental illness that was made worse by his abuse of illegal drugs. “All the doctors, the probation service and myself find that you present a very serious risk to the general public and that you could or may kill someone,” he told him.

The judge said it was inevitable that Ali would relapse into illegal drug taking if his condition was not closely managed.

At an earlier hearing, he told Ali he could have killed Mr McDonald, 50, who was running errands for his mother on Lilycroft Lane.

Ali pleaded guilty to possession of a bladed article in Manningham Library on March 30, 2012. He admitted racially aggravated threatening behaviour when he abused and terrified a librarian, threatening to rip her head off.

Ali pleaded guilty to unlawfully wounding Mr McDonald and possession of an offensive weapon. He was charged with intending to cause him grievous bodily harm but, because he had a mental illness, fuelled by abuse of cannabis and cocaine, the charge could not be made out.

Ali was dragged out of the library by a police officer after staff saw him sharpening an eight-inch bladed kitchen knife on the premises. He was a regular and staff were wary of him.

Three weeks later, Ali dashed into the street from a barber’s shop, where he had been acting strangely.

He ran at Mr McDonald and stabbed him in the lower back, lacerating his liver.

The judge said that without the prompt medical intervention that he received “the consequences could have been very dire indeed”.