A librarian at exclusive Trinity School says the upscale prep academy threw the book at him after he was falsely accused of dissing a teacher.

David Murphy, associate librarian at the 300-year-old Upper West Side institution, sued last week in Manhattan to get his job back.

And although he doesn’t allege it in the lawsuit, Murphy told The Post he believes he was canned because he’s gay.

Murphy, 53, said he didn’t include the discrimination claim in the suit because it would be harder to prove than the procedural violations that he alleges in the suit.

The lawyer for the school disputed Murphy’s allegations. “Trinity categorically denies that his contract was not renewed because he is gay,” said Robin Colin-Greene. “The decision not to renew Mr. Murphy’s contract was totally and completely justified.”

Murphy worked for 10 years as a middle-school librarian at Trinity – which lists Larry Hagman and John McEnroe among its alumni.

But in January, an upper-school teacher complained that during some loose cafeteria gossip, Murphy implied that a colleague “was a pedophile or even a serial killer,” according to Murphy’s suit.

Murphy was accused of saying he “understood what had happened to all the newsboys who had gone missing from [the colleague’s] front door,” the suit said. The teacher complained to headmaster Henry Moses.

Murphy denied making the comment or even having lunch with the teacher.

But the headmaster didn’t buy it.

Murphy claims Moses refused to allow him to appeal his dismissal before the school board, saying they had clashed before. The earlier dispute, Murphy said, came when Murphy recommended an Indian colleague attend a “people of color” conference instead of two white males the school had picked to attend.

“The private schools act like a plantation,” Murphy said.

Murphy said he went around Moses, directly to the appeals committee, to get his day in front of the Trinity tribunal. They looked at the facts and recommended Murphy stay on, he said.

Moses was unmoved and held fast to his decision to let his librarian go, citing a “documented pattern of unacceptable conduct,” according to the complaint.