As he had done at the Pentagon and Ford, Mr. McNamara sought to remake the bank. When he arrived on April 1, 1968, the bank was lending about $1 billion a year. That figure grew until it stood at $12 billion when he left in 1981. By that time the bank oversaw some 1,600 projects valued at $100 billion in 100 nations, including hydroelectric dams, superhighways and steel factories.

The ecological effects of these developments, however, had not been taken into account. In some cases, corruption in the governments that the bank sought to help undid its good intentions. Many poor nations, overwhelmed by their debts to the bank, were not able to repay loans.

The costs of Mr. McNamara’s work thus sometimes outweighed the benefits, and that led to a concerted political attack on the bank itself during the 1980s.

Mr. McNamara saw some of these problems as they developed and shifted the emphasis of the bank’s lending toward smaller projects  irrigation, seeds and fertilizer, paving farm-to-market roads. But progress was often hard to measure. At the end of his tenure, the bank estimated that the world’s poorest numbered 800 million, an increase of 200 million over the decade.

Public Contrition

Mr. McNamara left the bank when he turned 65, after his wife died, and for a time he tried to unwind and get away, taking a 140-mile hike up to the 18,000-foot level of Mount Everest. But within two years, he began to speak out against the nuclear arms race. In 1995, 14 years after leaving public life, he published his denunciation of the Vietnam War and his role in it, “In Retrospect: The Tragedy and Lessons of Vietnam” (Times Books/Random House), for which he was denounced in turn.

Unlike any other secretary of defense, Mr. McNamara struggled in public with the morality of war and the uses of American power.

“We are the strongest nation in the world today,” Mr. McNamara said in “The Fog of War,” released at the time of the 2003 invasion of Iraq. “I do not believe that we should ever apply that economic, political, and military power unilaterally. If we had followed that rule in Vietnam, we wouldn’t have been there. None of our allies supported us. Not Japan, not Germany, not Britain or France. If we can’t persuade nations with comparable values of the merit of our cause, we’d better re-examine our reasoning.”

“War is so complex it’s beyond the ability of the human mind to comprehend,” he concluded. “Our judgment, our understanding, are not adequate. And we kill people unnecessarily.”