In a festive outdoor ceremony at CIA headquarters in McLean yesterday, President Reagan signed into law a bill making it a crime to disclose the names of U.S. intelligence agents, even if the information is obtained from public records.

Reagan said the law would send a "signal to the world that while we and this democratic nation remain tolerant and flexible, we also retain our good sense and our resolve to protect our own security."

Hundreds of CIA analysts, secretaries and other personnel gathered on a grassy hill to hear Reagan.

While they waited, the U.S. Army Band filled the air with patriotic songs, and CIA employes served punch to the press, giving the event at the normally closed and secretive compound something of the feel of a Fourth of July picnic.

Reagan lauded controversial CIA Director William J. Casey, whom he referred to affectionately as "Director Bill," and he praised the employes, calling them "heroes of a grim twilight struggle."

"You are the tripwire across which the forces of repression and tyranny must stumble in the quest for global domination," he said.

Covert agents stayed in the "twilight" yesterday because of concern that they might be caught by the television cameras.

Before the bill-signing ceremony, Reagan spoke at a closed session to a number of them--precisely how many was kept secret. One CIA spokesman said there were "several hundred"; another said, "We wouldn't acknowledge how many there are," but Casey acknowledged that the room where Reagan spoke holds about 1,000 and it was full.

The new law was prompted by the practices of former agent Philip Agee and publications such as the Covert Action Information Bulletin, which regularly published the names of CIA officers stationed overseas with the avowed aim of destroying their effectiveness.

The law, called the Intelligence Identities Protection Act, imposes maximum penalties of 10 years in prison and fines of $50,000 for unauthorized disclosure by government employes with access to classified information.

Private citizens, such as journalists and scholars, could be sentenced to three years in prison and fined $15,000 if they "name names" in the course of a "pattern of activities intended to identify and expose covert agents" and if they have "reason to believe that such activities would impair or impede the foreign intelligence activities of the United States."

Reagan said the measure was necessary to help rebuild the CIA--after "nearly a decade of neglect and sometimes overzealous criticism"--in order for the nation to meet the "grave challenges" of an era "fraught with danger."

The American Civil Liberties Union has criticized the law as a "clearly unconstitutional infringement on the right of free speech."

Morton H. Halperin, director of ACLU's Center for National Security Studies, said the organization would provide legal assistance to "those whose ability to speak or write is threatened by this legislation or effort to enforce it by the Justice Department."