Through fiction, a fool in a kings court seems to have the role of devils advocate. They seem to be the only ones to question a kings behavior. Is there any historical basis behind this, or is it all made up?
Yes and no. The modern image of a medieval fool is a conflation of several different court positions and roles that existed in various places at various times.
Most of these roles were pure entertainers, generally along the lines of acrobats and clowns. They were frequently quite adept at playing the fool, in the same way modern day circus clowns can be, and as such fool is a name that has become attached to the role. (However, they were rarely officially titled fool. More common was the title of Jester.)
These fools then got conflated with the religious Fools of Eastern Orthodoxy. (Some examples of Foolishness for Christ exist in Western Christianity, but they were rare, and the major examples were Saint Francis of Assisi and his religious followers.)
While an Eastern Orthodox Holy Fool was generally allowed (and sometimes expected) to act silly and ignore social norms, this was very much considered an outgrowth of their deep faith and divine favor. As such, if a Holy Fool was in a monarch's good graces, there was definitely room for such a divinely inspired person to offer criticisms that would be taken more seriously than others would have.
Especially in Russia, Holy Fools were treated quite seriously. Consider for example, Basil Fool for Christ (a serial shoplifter who gifted what he stole to the poor and needy, who reportedly also publicly chastised Tsar Ivan the Terrible for not going to church enough) being the Saint Basil for which the iconic Saint Basil's Cathedral in Moscow is named.