MHC genes, which code for proteins responsible for presenting pathogen-derived antigens to the host immune system, show remarkable copy-number variation both between and within species. However, the evolutionary forces driving this variation are poorly understood. Here, we use computer simulations to investigate whether evolution of the number of MHC variants in the genome can be shaped by the number of pathogen species the host population encounters (pathogen richness). Our model assumed that while increasing a range of pathogens recognised, expressing additional MHC variants also incurs costs such as an increased risk of autoimmunity. We found that pathogen richness selected for high MHC copy number only when the costs were low. Furthermore, the shape of the association was modified by the rate of pathogen evolution, with faster pathogen mutation rates selecting for increased host MHC copy number, but only when pathogen richness was low to moderate. Thus, taking into account factors other than pathogen richness may help explain wide variation between vertebrate species in the number of MHC genes. Within population, variation in the number of unique MHC variants carried by individuals (INV) was observed under most parameter combinations, except at low pathogen richness. This variance gave rise to positive correlations between INV and host immunocompetence (proportion of pathogens recognised). However, within-population variation in host immunocompetence declined with pathogen richness. Thus, counterintuitively, pathogens can contribute more to genetic variance for host fitness in species exposed to fewer pathogen species, with consequences to predictions from Hamilton-Zuk theory of sexual selection.