Closed-loop neuroprostheses show promise in restoring motion in individuals with neurological conditions. However, conventional activation strategies based on functional electrical stimulation (FES) fail to accurately modulate muscle force and exhibit rapid fatigue because of its unphysiological recruitment mechanism. Here we present a closed-loop control framework that leverages physiological force modulation under functional optogenetic stimulation (FOS) to enable high-fidelity muscle control for extended periods of time (>60 minutes) in vivo. We first uncovered the force modulation characteristic of FOS, showing more physiological recruitment and significantly higher modulation ranges (>320%) compared to FES. Second, we developed a neuromuscular model that accurately describes the highly non-linear dynamics of optogenetically-stimulated muscle. Third, based on the optogenetic model, we demonstrate real-time control of muscle force with improved performance and fatigue resistance compared to FES. This work lays the foundation for fatigue-resistant neuroprostheses and optogenetically-controlled biohybrid robots with high-fidelity force modulation.