The functions of both Chaplain and Librarian are actually remnants of the pre-heresy era and ironically traditionally at odds with each other. When Mangus the Red rejoined the legions of the emperor he brought with him the immense psychic knowledge of Prospero's centuries of experiments in sorcery. The other legions accepted the knowledge that he was willing to share with mixed levels of acceptance. The supplemental processes used to create librarians were used by chapters in order to prevent random psychic mutation but the actual acceptance of librarians was met with mixed results at best.
"Some [Sanguinius, Vulkan, Fulgrim, and Horus] desired to extend their librarians departments and recruit even more Psychers whilst other Primarchs were vehemently against the entire notion of librarians. Leman Russ argued that the librarians were simply warlocks by any other name. Corax and Rogal Dorn refused to commit their legions to battle alongside those fielding librarians." (Horus Heresy, Collected Visions p.92)
The Emperor eventually comes to the decision that the librarians are too dangerous to have in any chapter at the council of Nikaea. He decreed a ban on all usage of psychic powers and informed Mangus rather indelicately that he was being reckless in his use of psychic powers.
"Beyond the use of the Navigators and the Astropaths he was adamant that the legions did not employ psychers. Even the hint of sorcery had become too dangerous and unacceptable. He commanded that the Primarchs close their Librarius departments forthwith and ordered that the Primarchs themselves not indulge their undoubted psychic talents." (Horus Heresy, Collected Visions p.98)
However as it was difficult (if not impossible) to absolutely ensure that the chapters had decided to abide by the doctorine of Nikaea the Chaplians were created as an organization to police that. Essentially they were to be to the Space Marines what the Commissarate was to the Imperial Gaurd.
"...Malcador ordered the other Space Marine Primarchs to appoint chaplains who would ensure the spiritual well-being of the Lengion, and Enforce the Psycher ban..."(Horus Heresy, Collected Visions, p 124)
However as the Heresy itself happened soon after these edicts, and due to the unreliable nature of astronmacon, neither of these edicts were wholly enforced. Suffice it to say that once the whole and real threat of chaos was made aware to the space marine chapters the necessity of the librarian was made manifest, the institution of the chaplain was adapted to (while still serving its original purpose's spirit) focus on rooting out potentially treasonous elements of the chapter and remedying such issues.
As to their external nature within the command structure, considering the history of these offices there is little wonder why they aloof from the rest of the chapter. Librarians were created long after the creation of the chapters and the legitimacy of their continued use within a chapter is based off of their necessity rather than out of choice. Chaplains must keep themselves present but aloof from other marines as they can, and do, administer justice to those who's loyalties are wanting.