Two men spent two-and-a-half months in prison in 2010.
In the precedent-setting judgement, Judge Mumbi Ngugi said both safeguarding the public from infection and upholding the patients' rights was a tricky balancing act.
But she added that a crowded prison was the worst place to try and isolate people with infectious disease.
Africa Live: BBC news updates
Ms Ngugi noted that it was within existing laws to isolate people with diseases which could spread easily.
But she added that Kenya lacked a proper isolation facility at the time the two patients were imprisoned.
She directed the Ministry of Health to issue policy guidelines within the next 90 days.
In 2010, Daniel Ng'etich and Patrick Kipngetich were sent to prison for eight months or until the satisfactory completion of their TB treatment, after originally failing to stick to their drug regime.
They two have now recovered fully from TB.
No damages were awarded "because it is not in the interest of the public and the decision was reached as a last resort", the court ruled.
The Kenya Legal and Ethical Issues Network on HIV and Aids said in a petition that putting TB patients in prison was a widespread practice in Kenya and something they sought to challenge.
A court in Kenya has said jailing patients who refused to take their TB medication is unlawful.