The district attorneys for two eastern Massachusetts counties are joining with nonprofit groups to sue the federal government to stop immigration agents from making arrests at courthouses in the state.

The suit alleges that for the last two years, Immigration and Customs Enforcement (ICE) agents have patrolled courthouses in Massachusetts to pursue immigrants, something the plaintiffs characterize as “unprecedented in American history."

Middlesex District Attorney Marian Ryan said in a press conference Monday that everyone has a right to appear in court, and with the threat of arrest on court property, immigrants are being denied that right.

But Ryan focused more on the impact of the specter of immigration arrests on her office's ability to prosecute crimes.

“Our accomplishment of [keeping the public safe] usually lies in the prosecution of criminal cases [and] in order to do that effectively we must rely on the cooperation of witnesses [and] victims,” Ryan said.

When those people fear that coming to court will “disrupt and tear apart” their families, Ryan said that “makes our prosecution of cases more difficult, and in many, many cases, impossible."

Ryan said offenders will also choose not to come to court for fear of being detained by ICE. “When we cannot hold anyone accountable for their actions … that is not justice," she said, "and not one person in this commonwealth is safer because of that practice.”

Citing the litigation, ICE spokesman John Mohan declined to comment on the case specifically.

ICE’s practice of arresting immigrants at courts has been happening for years, but in January 2018, the policy was enshrined by the agency in a directive, allowing its agents to operate in local, state and federal courthouses.

The plaintiffs are asking the U.S. District Court to issue an injunction against the practice, claiming it violates the "privilege against civil arrests for those attending court on officials business," according to the complaint.

But not everyone agrees on the merits of the case.

Bristol County Sheriff Thomas Hodgson called it a “frivolous lawsuit” that seeks to make it more difficult to "apprehend criminal illegal aliens."

“Barring ICE from a courthouse is the same as barring the FBI or the DEA,” Hodgson said in a statement. "And if anyone in that courtroom, whether they came to court voluntarily or not, has a warrant or is suspected to be part of criminal activity, why shouldn’t law enforcement be there to act?“

A similar case was heard on the state level in 2018, when a Supreme Judicial Court justice refused to block ICE's arrests, citing the failure of plaintiffs to produce individuals who were not going to court for fear of prosecution, and because the court heard just one side of the argument.

DA Ryan said in that case the judge made it clear that it needed to be heard on the federal level.

Alongside Ryan at the news conference was Suffolk County District Attorney Rachael Rollins, a fellow Democrat, who said it’s clear that the threat of ICE arrests is interfering with her prosecutions — even forcing authorities to abandon cases when those testifying refuse to appear in court.

“Since taking office [in January] I’ve watched serious criminal cases against individuals accused of violent, heinous crimes grind to a halt, because of civil arrests by ICE,” Rollins said. “Standing by silently as immigration officials … strip our justice system of its ability to function simply is not an option.”

The filing comes on the heels of another high-profile immigration case in the Boston area, regarding a judge in Newton who allegedly helped an immigrant to evade ICE custody.

The judge, and a now-retired court officer, were indicted Thursday.

Ryan said their lawsuit is unrelated to the federal charges, adding that their filing has been in the works for more than a year.

Elected on a platform of criminal justice reform, Rollins has instructed her employees to inform her when they see ICE agents patrolling courthouses.

Asked whether that could get her into trouble with federal authorities, Rollins said she doesn’t believe so. “And very candidly, if I am, it would be my honor to be, because we need to stand up and be very, very bold about this behavior our president is engaging in right now,” Rollins said.

It’s not clear to what extent ICE’s courthouse operations are affecting the justice system.

Wendy Wayne, a director at the Committee for Public Counsel Services, which oversees public defenders in the state, said her group has been receiving "at a minimum several calls per week for the past two years, from defense attorneys about their client being arrested as they come into court."

Correction: Due to an editing error, an earlier version of this story misstated when the petition was sent to the state's highest court. We regret the error.