WASHINGTON — As the F.B.I.’s Russia investigation draws closer to him, President Trump on Sunday unleashed an extraordinary assault on the nation’s premier law enforcement agency, calling it a biased institution whose reputation for fairness was “in tatters.”

In a series of early-morning tweets, Mr. Trump said the F.B.I.’s standing was now the “worst in history.” The attack was one of the harshest in a generation on an independent agency that two days earlier had helped secure a guilty plea and a pledge of cooperation from the president’s first national security adviser.

Current and former F.B.I. officials, historians and lawmakers rebuked the president over his efforts to undermine the F.B.I.’s credibility as it investigates whether his campaign colluded with Russian officials to sway the 2016 election. A president who has positioned himself as devoted to law and order is now in a public dispute with the country’s top law enforcement agents.

Thomas O’Connor, the president of the association representing F.B.I. agents, defended their integrity in a statement. “F.B.I. agents are dedicated to their mission,” he said, asserting that they demonstrated “unwavering integrity and professionalism” on the job. “Suggesting otherwise is simply false,” he added.