The other mechanism is that the immune system doesn't react only to foreign antigens, there also needs to be some general activation of the immune system in that area for a real response.
The full answer is that this is really, really complex like pretty much everything involving the immune system. And auto-immune diseases indicate that the immune system doesn't always get this right.
in very loose terms both keys must be presented while the immune system is in training, the instances where protiens are expressed with out cell surface recognition molecules in proximity are invalid thus foriegn, and memory immunity to this "failed login" is maintained. when both factors [protien plus recognition] are present constituitively over the course of development the immune response is squelched.
this is known as central tolerance.
So far, nobody has been able to provide a satisfying answer.