RESPONDING TO A LAWSUIT brought by two Massachusetts district attorneys, the federal Immigration and Customs Enforcement agency on Monday said it has the legal authority to arrest undocumented immigrants at state courthouses.

The agency said in a court filing that the common law prohibition against arrests at courthouses claimed by Middlesex District Attorney Marian Ryan and her Suffolk County colleague Rachael Rollins does not exist. Even if it did exist, the legal brief says, Congress has granted the immigration agency authority that supersedes any common law prohibition.

“ICE may arrest all aliens except aliens who are in state criminal custody serving a criminal sentence,” the agency said in its filing.

Until recently, the 23-page response said, state courts in Massachusetts “generally cooperated” with federal immigration enforcement efforts. That changed in 2017, according to the ICE filing, when the Supreme Judicial Court of Massachusetts held that “state judicial and law enforcement officers lacked authority” to transfer detainees to the federal government in civil immigration matters.

Because of that ruling, ICE says, “dangerous aliens involved in criminal activity” are now being “released to the streets.”

ICE said a 2018 agency directive sought to clarify the practice of immigration enforcement officers going into court houses to civilly arrest undocumented immigrants. The filing says that arresting immigrants in courthouses is one of the “few places where “safety risks for the arresting officers, arrestee, and members of the community are substantially diminished.”

The lawsuit against ICE was filed on April 29 by Ryan, Rollins, the public defender agency Committee for Public Counsel Services, and the advocacy group Chelsea Collaborative. The lawsuit seeks to block ICE agents from arresting undocumented immigrants at local courthouses, alleging the practice violates the Tenth Amendment, which upholds state’s rights. The plaintiffs say ICE has decided to “flout” common-law privilege against civil courthouse arrests and “commandeer” the state courts for federal immigration purposes.

While ICE agents have been booted out of courthouses across the country, this is the first time a district attorney’s office has filed suit against the Department of Homeland Security and the agency over the presence of agents in courthouses.

President Donald Trump weighed in on the issue in early May, saying of the DAs (while not naming them outright), “These are people that probably don’t mind crime, they don’t mind what’s going on. You look at MS-13, they say in the world there’s nothing more evil. These are some very, very bad people. To try and protect them, I don’t think so.”

ICE agents arrested two immigrants who were in the country illegally and charged with crimes at the Chelsea and East Boston District Courts the same day the lawsuit was filed.

Ryan’s office declined to comment. She has said her office had interacted with ICE on more than a dozen occasions where agents interfered with business at local courts.