Frank Anderson, a former American spymaster who supervised clandestine CIA operations in the Middle East, including a smuggling initiative that supplied Afghan rebels with billions of dollars in weapons during their 1980s war against the Soviet Union, died Jan. 27 at a hospice center in Sarasota, Fla. He was 77.

The cause was a stroke, said his wife, Donna Anderson.

Mr. Anderson spent the bulk of his 26-year intelligence career as a CIA officer in the Middle East, where he and his colleagues served as the tip of the spear of American foreign policy, orchestrating covert actions intended to obtain intelligence and give the country an edge during the Cold War.

A station chief for three tours of duty, he presided over the development of new gadgets as head of the CIA’s technical services wing and directed the Near East and South Asia division in the directorate of operations, the agency’s clandestine arm.

In recent years he emerged as a fierce critic of the CIA’s use of “enhanced interrogation techniques” on terrorism suspects, which he equated to torture and called “counterproductive, illegal and morally repugnant.”

“As an operations officer and leader, I learned that good guys have bad days, and that fear, anger and ambition degrade, rather than enhance, judgment and decision making,” he wrote in a 2014 op-ed for the Miami Herald. “My friends and colleagues made serious errors in just such an atmosphere.”

“Anderson really was a singular person of gravitas in the murky world of intelligence,” said author Kai Bird, who interviewed Mr. Anderson for his book “The Good Spy.” At times, he seemed to function as a diplomat as much as a spy, serving as the preeminent American envoy in countries where he was based, according to former journalist Tim Weiner, author of the CIA history “Legacy of Ashes.”

“He would shake the hand of his hosts while picking their pocket with his free hand — but the handshake would be warm and sincere,” Weiner said. “He was one of the last of a generation of CIA officers who went beyond Cold War sensibilities and saw intelligence not simply as the iron fist of the United States, but also as a way to prevent wars.”

That approach was perhaps most evident when Mr. Anderson helped maintain back-channel communications with the Palestine Liberation Organization, which Israel and the U.S. State Department deemed a terrorist organization. Mr. Anderson met frequently with Ali Hassan Salameh, whose work as the security chief for PLO Chairman Yasser Arafat made him a key CIA asset.

Their relationship, originally cultivated by CIA officer Robert Ames, infuriated Israeli intelligence officials, who considered Salameh responsible for the terrorist attack at the 1972 Olympics in Munich. Mr. Anderson was among those who believed that the CIA’s relationship with Salameh had “brought the Palestinians in from the cold” and paved the way for the Oslo peace process, Bird wrote in “The Good Spy,” an Ames biography.

By the time Arafat and Israeli Prime Minister Yitzhak Rabin met outside the White House for a historic handshake in 1993, Salameh had been dead for more than 14 years, killed by a car bomb attributed to Israel’s Mossad intelligence agency. Mr. Anderson had responded to the assassination in part by writing a condolence letter to Salameh’s eldest son, noting that he was a teenager when his own father died.

“From the memory of my past loss, and from the pain of today, I share your pain,” he wrote. “I promise to honor your father’s memory — and to stand ready to be your friend.”

Among fellow spies, Mr. Anderson was perhaps best known for his involvement in the Soviet-Afghan War, which began in 1979 when KGB forces stormed the presidential palace in Kabul and installed a Soviet loyalist. The resulting conflict raged for more than nine years, with an estimated 1 million civilians killed in addition to tens of thousands of mujahideen rebels and at least 15,000 Soviet troops.

It also became a major proxy battle of the Cold War, with the CIA providing weapons and supplies for the mujahideen, Muslim “holy warriors” who drove Soviet forces from the country. Mr. Anderson supervised the agency’s Afghan task force from 1987 until the war’s end in 1989, at a time when the U.S. government invested as much as $700 million a year in the war effort, paying for Stinger antiaircraft missiles and weapons purchased from Soviet bloc countries to disguise their origins.

By the close of the conflict, Mr. Anderson was arguing that the CIA should end its involvement in Afghanistan entirely — an opinion shared by the agency’s Langley headquarters. “The result was an Afghanistan that descended into chaos,” Mr. Anderson later said, calling the decision to pull out from the country a terrible mistake. Civil war was followed by the rise of the Taliban, which offered safe haven for Osama bin Laden.

When American forces invaded the country in October 2001, beginning the longest war in U.S. history, they faced some of the same Afghan soldiers who had been armed by the CIA. By then, however, Mr. Anderson was long gone from the agency. He announced his retirement in 1994 amid fallout from the Aldrich Ames spy case, in which a longtime CIA officer — unrelated to Robert Ames — was found to be a Russian mole.

In response to the revelations, Director of Central Intelligence R. James Woolsey Jr. disciplined Milton Bearden, a distinguished agency veteran who had supervised Ames. The day after Woolsey announced his disciplinary actions, Mr. Anderson and another senior CIA official gave an award to Bearden honoring his work during the 1980s, without prior approval from top leadership.

Their actions were seen as a slight to Woolsey, who reassigned Mr. Anderson and the other official, John MacGaffin, the No. 2 spymaster for clandestine operations. Rather than face demotion, both men resigned.

“If I was to take another job, that would mean Woolsey was right in his decision to ask us to leave,” MacGaffin said by phone, recalling his and Mr. Anderson’s mind-set. Bearden, he added, “was one of those really great legends of this place, and we hated to see him going away.”

While Mr. Anderson never regretted his decision to resign, his wife recalled, he nonetheless missed the agency that had been his home for nearly three decades, and that had offered escape from his old life as a car salesman and Illinois marina owner.

“This was an outfit that had an image that only the best and the brightest from the Ivy League schools with tremendous political connections get in the door,” he told the Baltimore Sun in 1997. “And the truth of the matter is, I was really grateful to be there.”

Frank Ray Anderson was born in Chicago on Feb. 1, 1942. His mother worked for a radio equipment manufacturer, and his father was a bouncer and onetime bar owner.

Mr. Anderson served in the Army after high school and met a CIA recruiter while studying at the University of Illinois at Chicago. He joined the agency in 1968, after receiving a bachelor’s degree, and learned Arabic from a Foreign Service Institute school in Beirut.

After leaving the CIA he was president of the Middle East Policy Council, a Washington nonprofit organization.

His marriages to Dorothy Kaehn and Barbara Virginia Krieps ended in divorce. Survivors include his wife of 39 years, the former Donna Eby; two sons from his first marriage, Frank Anderson Jr. and Mark Anderson; a daughter from his second marriage, Amy Anderson; a son from his third marriage, John Anderson; two brothers; a sister; 13 grandchildren; and 15 great-grandchildren.

As chief of the CIA’s Near East division, Mr. Anderson was keen on recognizing the contributions of fallen officers such as Robert Ames, who was killed in the 1983 U.S. Embassy bombing in Beirut. On the day when Rabin and Arafat met outside the White House before a large crowd in 1993, Mr. Anderson learned that no CIA representatives would be in attendance — a shame, he believed, given that the work of officers such as Ames had helped set the Israeli-Palestinian peace talks in motion.

According to “The Good Spy,” he requisitioned a CIA bus and organized a ceremony of his own, taking around three dozen officers to “go visit our dead” at Arlington National Cemetery. “We were at Bob’s gravesite at the moment of the handshake — as planned,” Mr. Anderson said.