Homebrew
The librarian is a 12th-level spellcaster. Its spellcasting ability is Intelligence (spell save DC 18, +10 to hit with spell attacks). It has the following wizard spells prepared:
Knowledgeable. The librarian has advantage on Intelligence (History) checks and can recall any information it has read with perfect accuracy. Library Guardian. The librarian can sense if a book or scroll has been taken from the library without permission and can track the item within a range of 2 kilometers. Organize and Retrieve. As a bonus action, the librarian can retrieve any book or scroll from the library within 6 seconds, provided it knows its location. Magic Resistance. The librarian has advantage on saving throws against spells and other magical effects. Protective Aura. The librarian can emit a protective aura in a 6-square radius. All allies within this radius gain a +2 bonus to AC and saving throws. The librarian must concentrate (as if concentrating on a spell) to maintain this aura.
Multiattack. The librarian makes three melee attacks or two ranged attacks. Force Slam. Melee Weapon Attack: +9 to hit, reach 2 sq., one target. Hit: 15 ( 2d8+6 ) force damage. Arcane Bolt. Ranged Spell Attack: +10 to hit, range 24 sq., one target. Hit: 18 ( 4d6+4 ) force damage. Arcane Wave (Recharge 1d6>=5 ). The librarian releases a wave of arcane energy. Each creature in a 6-square cone must make a DC 18 Dexterity saving throw, taking 45 ( 10d8 ) force damage on a failed save, or half as much damage on a successful one.
Shield (1st Level Slot). An invisible barrier of magical force appears and protects you. Until the start of your next turn, you have a +5 bonus to AC, including against the triggering attack, and you take no damage from magic missile. Counterspell (3rd Level Slot). You attempt to interrupt a creature in the process of casting a spell. If the creature is casting a spell of 3rd level or lower, its spell fails and has no effect. If it is casting a spell of 4th level or higher, make an ability check using your spellcasting ability. The DC equals 10 + the spell's level. On a success, the creature's spell fails and has no effect.
The Eternal Librarian can take 3 legendary actions, choosing from the options below. Only one legendary action option can be used at a time and only at the end of another creature's turn. The automaton regains spent legendary actions at the start of its turn. Detect. The librarian makes a Wisdom (Perception) check. Retrieve (Costs 2 Actions). The librarian can use its Organize and Retrieve ability. Arcane Shield (Costs 3 Actions). The librarian casts Shield, increasing its AC by 5 until the start of its next turn.