Abstract Emerging evidence suggests a complex interplay among dietary habits, gut microbiota, and hepatobiliary cancers, yet the causal relationships remain unclear. Here, we conducted a comprehensive two-sample Mendelian randomization (MR) analysis using genetic instruments from large European cohorts to assess causality among 88 dietary components, 1080 microbiota traits, liver cancer (500 cases, 314,193 controls), and biliary tract cancer (1207 cases, 314,193 controls). We identified significant causal associations of 17 dietary and 101 microbial traits with hepatobiliary cancer risk, while 11 dietary and 70 microbiota traits showed evidence of reverse causality, indicating potential disease-driven alterations. Importantly, a two-step MR mediation analysis revealed that 43 microbial taxa and 6 metabolic pathways significantly mediated dietary influences on hepatobiliary cancer risk; notably, Mollicutes RF9 mediated 31% of the protective effect exerted by zinc on biliary tract cancer. These findings provide genetic evidence delineating gut microbiota as key intermediaries connecting dietary intake to hepatobiliary cancers, highlighting microbiome-targeted dietary strategies as potential preventive interventions. Further research is required to confirm these causal mechanisms and facilitate the development of targeted prevention and therapeutic approaches.