Abstract One of the major concerns of ecological and evolutionary research is the prediction of community function in relation to the degree of biodiversity. To clarify the relationship between biodiversity and the expression of plant defence traits, we investigated ant–plant defensive mutualism in the pioneer plant Mallotus japonicus at several sites across Japan. The plant bears ant‐attracting extrafloral nectar (EFN) and food bodies (FBs) as indirect defence traits. To reveal variations in ant–plant interactions in the field, we measured species richness of nectar‐feeding ants and interaction strength between each ant species and the plant (visiting frequency of each ant species to the plant). We also investigated the expression of EFNs and FBs in natural plant populations at the study sites. To assess the defensive quality of each ant species, we then experimentally estimated the aggressiveness of the dominant ant species. To examine the link between ant species richness and the defensive function of ants, we conducted an ant‐exclusion experiment and a common garden experiment to clarify whether the defence efficacy of ants varied in relation to ant species richness. Ant species richness differed among the study sites. We found negative relationships between ant species richness and the mean interaction strength of ant species, the interaction strength of aggressive ant species and the number of EFNs per leaf. The effectiveness of indirect defence by ants was poor at sites where ant species richness was high. When cultivated in the same environment, plants from sites with low ant species richness developed a larger number of EFNs per leaf than those from sites with high ant species richness. Our results suggest that facultative ant–plant defensive mutualism is weakened at sites where ant species richness is high, resulting in a decrease in the number of EFNs per leaf. Such a link between species richness and an indirect trait function may help us to understand the evolutionary patterns of various species traits in complex biological communities. A free Plain Language Summary can be found within the Supporting Information of this article.