Microglia modulate synaptic activity, essential for context-dependent cognitive performance, allowing organism-level adaptations to different environmental scenarios. Yet, the microglial molecular drivers required for synaptic remodeling related to cognitive performance remain largely elusive. Here, combining conditional gene targeting, single-cell live imaging, RNA-seq, high-throughput proteomics, systems biology, and animal behavior, we mapped a molecular nexus between microglia and synapses that instruct cognitive performance. Specifically, we found that microglia use the RhoGTPase Rac1 as a relay switch to sense the brain microenvironment and drive synaptic remodeling required for experience-dependent sociability and learning related to memory. Targeting this microglial relay modifies context-dependent cognitive performance.