MI5 secretly planted bugs in 10 Downing Street despite repeated official denials and they remained in place for more than 10 years during the tenure of five prime ministers.

The disclosure was to have been included in the official history of MI5 by the Cambridge historian, Christopher Andrew, published last year to mark the agency's 100th anniversary. It is believed to have been suppressed by senior Whitehall officials to protect the "public interest".

Bugs are understood to have been placed in the cabinet room, the waiting room, and the prime minister's study, at the request of Harold Macmillan in July 1963. They remained there until James Callaghan removed them in 1977.

An MI5 file referring to the operation was included in the draft of Andrew's book, The Defence of the Realm, according to the Mail on Sunday. Whitehall officials did not deny the claims.

It is believed this is what Andrew was referring to when he wrote in the preface to the book to "one significant excision", which, he said, he believed was "hard to justify".

Andrew said the issue should be taken up by the parliamentary intelligence and security committee. It is not known whether it did so before the Commons was dissolved for the general election. It is believed the short MI5 file on the operation, seen by Andrew, does not explain why the bugs were planted or whether all the prime ministers during the period were told. The file was reported to contain "no product", suggesting MI5 officers did not use any information emanating from the bugs.

The bugs were installed at Macmillan's request in July 1963, a month after John Profumo, secretary of state for war, resigned over the Christine Keeler affair. It is possible Macmillan wanted to make sure no other ministers or officials were involved. MI5 was in close contact with Macmillan during the scandal.

The bugs are understood to have been removed after Macmillan left office but were reinstalled by his successor, Sir Alec Douglas-Home, who was concerned about Soviet spies. They remained during the period in office of his successors, Harold Wilson and Edward Heath. It is unclear whether they knew about the bugs.

Callaghan ordered them to be removed in 1977, the year he cleared MI5 of a plot against Wilson's Labour government. Callaghan said in a statement to MPs:

"The prime minister is satisfied that at no time has the security service or any other British intelligence or security agency, either of its own accord or at someone else's request, undertaken electronic surveillance in No 10 Downing Street".

Despite official denials, Wilson made clear he believed he was the target of MI5, sometimes gesturing to pictures on the wall or to the ceiling.

Andrew suggests that ministers were more concerned about "subversive" colleagues or MPs than MI5 ever was.

In a foreword to Andrew's book, Jonathan Evans, head of MI5, writes: "Information has only been omitted if its disclosure would damage national security or, in a small number of cases, if its publication would be inappropriate for wider public interest reasons".