A prominent Boston-based political consulting firm that worked for Mayor Marion Barry earlier this year signed on yesterday with Barry's principal opponent in the Democratic primary, Patricia Roberts Harris.

The move infuriated Barry and his chief political adviser, Ivanhoe Donaldson, who said the firm's action was unethical, worthy of a nationwide boycott by Democratic politicians and proved that Harris was unethical, too.

John Marttila, a principal partner in the firm of Marttila-Kiley, said Donaldson and Barry were unprofessional and unethical themselves--refusing to return telephone calls, breaking verbal agreements and campaigning from the mayor's office when they should have been taking to the streets.

"It's highly unusual, it's highly unethical and it's immoral for them to go to work for Harris," Donaldson said yesterday.

"If they do it, we are considering legal action. . . They're also in the business of making money as pollsters and consultants, and we are going to make a point of telling people what they are like," Donaldson said before learning that the agreement between the Harris campaign and the firm had been signed.

"If I was in Pat's shoes, I wouldn't do it," Barry said. "It tells you something about the morality and ethics of the person who did it."

Marttila, who has advised several mayoral campaigns in other parts of the country, said there was nothing wrong with the decision to work for Harris.

He said the firm's contract with Barry was only to do a poll. Barry and Donaldson never followed up a verbal agreement for the firm to act as political consultant to the incumbent's campaign, he said.

"I told Marion in a Friday phone call from Barry that either he was trying to stiff us on the contract and that was unethical," said Marttila, "or they were trying to tie us up so we couldn't go to work for Mrs. Harris and that was cowardly."

"These guys believe they can push anyone around, but they're not going to push us around. They picked on the wrong guys this time," he said. "Barry is the last one to be preaching about ethics."

Donaldson said yesterday that even though the Barry organization had been slow to follow up on the verbal understanding with Marttila-Kiley, he had, in fact, planned to hire the firm as political consultant for the campaign. Since Marttila-Kiley has now signed with Harris, Donaldson said, the campaign will not hire a new consulting firm but will rely solely on advertising consultant David Abramson and political consultant Paul Lutzker, who already advise the Barry team.

The Harris campaign said little, except to express happiness that the firm had become a part of their effort to defeat Barry and three other major candidates in the Sept. 14 Democratic primary.

"We feel like the Redskins would feel if Tony Dorsett was made a free agent by the Dallas Cowboys," said Sharon Pratt Dixon, the director of the Harris campaign.

Although Harris has shown strength as a candidate since she entered the race in April, her campaign operation has been slow to organize the details and strategy of the race efficiently. Marttila-Kiley, which advised recent successful mayoral campaigns for Coleman Young in Detroit and Ernest (Dutch) Morial in New Orleans, has a reputation for expertise in the day-to-day operation of big city elections.

Marttila-Kiley consultants will work under campaign director Dixon to help Harris organize her headquarters and to establish her campaign more solidly in wards and precints. Marttila said his firm will be paid about $25,000 for its services and that he expects to be in Washington regularly to take a direct hand in advising the campaign.

The highly regarded firm of Peter Hart Research Associates Inc. has contracted to do the polling for the Harris campaign, and "The Campaign Group," a Philadelphia firm specializing in political advertising, will handle the campaign's television ads.

Marttila's partner, Tom Kiley, who did the February poll for Barry, said he had only one two-hour meeting with Barry to present the results of the poll and Barry had proprietary rights to all materials from the Febrary poll, which cost about $16,000.

"We weren't hired by Harris to make use of any poll results," said Kiley. "We aren't going to attempt to make use of anything we learned on his Barry's behalf.

"More to the point, poll results have a life, and stale results are of no practical use . . . The only place we would have learned confidential information would have been in private sessions and that did not take place."

According to sources in both campaigns, the results of polls done for Harris by Peter Hart last fall and for Barry by Marttila-Kiley have been circulating freely among both camps.

Both polls, according to the sources, show that Harris and Barry are locked in a tight race, with each candidate having about 30 percent of the vote and more than 20 percent undecided.

Harris campaign director Dixon said Marttila-Kiley would be unlikely to jeopardize its national reputation by engaging in behavior considered unethical, and that the Harris campaign would not hire a group whose ethics it questioned.