BONN, May 30— Karl Carstens, a former Nazi and German Army officer who helped rebuild postwar West Germany and was an architect of the Treaty of Rome, the 1957 pact that became a forerunner of the European Community, died today at his home in Meckenheim, the Government announced. He was 77 years old.

The cause of death was not given, but Mr. Carstens suffered a stroke two weeks ago.

Despite controversy over his wartime membership in the National Socialist party, Mr. Carstens joined Konrad Adenauer's Christian Democratic Union after the war and was a key force in shaping the political future of West Germany and of Europe. He served in a variety of Government positions until Willy Brandt was elected Chancellor, and then was a leading opposition spokesman in the West German Parliament until his election to the largely ceremonial post of President in 1979.

"Karl Carstens was one of the best German statesmen of the postwar era," President Richard von Weizsacker, his successor as President, said. Criticized for Nazi Past

Mr. Carstens served as an officer in the German Army from 1939 to 1945 and was harshly criticized later for his Nazi affiliation. Mr. Carstens said he joined the party as a formality and that he was not an active member. He was not accused of any war crimes.

Born in Bremen on Dec. 14, 1914, Mr. Carstens attended several universities in Germany and France before earning a doctor of law degree from the University of Hamburg in 1936. He opened a legal practice in Bremen soon after the war ended and was first elected to the West German Parliament as a representative from Bremen in 1949, the same year he received a master of law degree from Yale University.

Mr. Carstens was named West German representative to the Council of Europe in 1954 and was one of architects of the Treaty of Rome, the agreement that established the European Common Market, the forerunner of the current European Community. Moved to Opposition

After Mr. Adenauer's resignation, Mr. Carstens served as a Deputy Defense Minister in 1966 and as head of the Chancellor's office in 1968. He left the Government when Mr. Brandt, a Socialist, was elected Chancellor in 1969. Mr. Carstens returned to the lower house of Parliament, or Bundestag, in 1972 and served as parliamentary leader of the Christian Democrats for three years. He was also president of the Bundestag from 1976-79, leaving upon his election that year as West German President. He declined to run for a second term in 1984.

"He was a patriot who unwaveringly strived for German unity," Chancellor Helmut Kohl, a Christian Democrat, said today. "And he was just as convinced of the importance of Europe's political union." .

Mr. Carstens is survived by his wife, Veronica, whom he married in 1944. They had no children.

Photo: Karl Carstens (1983)