The Secret Service director, who was hired two years ago to restore discipline and morale after a series of scandals and lapses at the agency that protects the president, said on Tuesday that he would retire next month, giving President Trump the chance to install his own choice.

Under the director, Joseph P. Clancy, the Secret Service successfully handled the challenges of a presidential campaign and a papal visit, among others; hired more agents; and began a reorganization intended to correct past failures. But morale continues to suffer. Last year, an annual survey of places to work within the federal government ranked the Secret Service dead last among more than 300 agencies.

In 2012, Secret Service agents working in Colombia to prepare for a visit by President Barack Obama brought prostitutes to their hotel rooms, leading to the dismissal or resignation of several agents. In 2014, the agency failed to prevent an armed guard working for a private security agency from riding in an elevator with Mr. Obama, a violation of Secret Service protocols. Days later, a knife-wielding man scaled the White House fence, ran across the lawn and entered the building before agents stopped him.

Subsequent investigations into the agency revealed several instances of people slipping through security screening and getting close to the president. After Representative Jason Chaffetz, Republican of Utah, became a prominent critic of the agency, it emerged that dozens of agents had improperly gained access to his 2003 application to be a Secret Service agent, and that at least one official had suggested leaking unflattering information about him.