MIDDLETOWN TOWNSHIP, N.J. (AP) _ A county prosecutor is investigating allegations that Police Chief Joseph McCarthy ordered a funeral director to open the grave of his former deputy chief to retrieve McCarthy's hat.

The chief's attorney, Larry S. Loigman, today denied that McCarthy had ordered the retrieval of the cap, which McCarthy loaned the family of former Deputy Chief William Schanck so it could lie in Schanck's coffin during his wake and funeral last November.

''All that the chief knows of the matter is that he loaned the hat to the Schanck family, it was on the coffin during the wake ... shortly after the funeral the hat was returned to him,'' Loigman said.

However, Irvin Beaver, a Middletown police officer who is a state delegate to the Policemen's Benevolent Association, said funeral director Cyril Sidun told him that the grave was reopened two to three hours after Schanck's funeral on Nov. 21.

''He (Sidun) said Chief McCarthy had called and indicated he didn't have his hat and wanted it back,'' said Beaver.

Sidun's attorney, Barry Albin, declined to comment on the matter.

State law says that opening a grave without a permit issued by a municipal health officer or a court order is a disorderly persons offense punishable by up to six months in jail and a fine of up to $1,000.

McCarthy allegedly sent Steven Finnegan, a part-time special police officer, with Sidun to the gravesite in Hazlet, according to a source who asked not to be identified. Sources made the same allegation to two newspapers.

Finnegan has declined comment.

Two sources said the prosecutor's office in December subpoenaed the tape recordings of police radio conversations from midnight Nov. 20 to midnight Nov. 21.

Monmouth County Prosecutor John Kaye refused to comment on the investigation except to say that his office was looking into a complaint made by members of the Schanck family.

''I would say that the whole family is incensed over the whole incident,'' said Patrolman James Eastman, Schanck's nephew.

Schanck was a 34-year veteran of the police force who retired in 1983. He died Nov. 19.