Theta and gamma oscillations are prominent features of cortical local field potentials (LFPs) and stimulation of the motor cortex at these frequencies can enhance motor learning. Phase-targeted closed-loop stimulation could provide a more precise and effective method to modulate these oscillations, particularly if stimulation parameters could harness the dynamics of the specific circuit mechanisms underpinning the generation of these activities. To investigate this, we defined the response of theta- and gamma-frequency oscillations in the motor cortex to closed-loop optogenetic stimulation of excitatory pyramidal neurons and inhibitory interneurons transfected with Channelrhodopsin-2 in awake, head-fixed RBP4-Cre (retinol-binding-protein-4) and PV-Cre (parvalbumin) mice, respectively. Phase-targeted blue-light pulses were delivered using the OscillTrack algorithm to track theta phase in the cortical LFP in real time and trigger stimulation at one of four target theta phases. Stimulation was delivered over a quarter of the target theta cycle, either as a single continuous pulse ("continuous" protocol) or three short pulses at gamma (75Hz) frequency ("gamma" protocol). Stimulation of both neuron types, using either stimulation protocol, modulated theta power in a phase-dependent manner, with continuous stimulation of excitatory neurons leading to stronger modulation. Phase-dependent amplification during stimulation of excitatory vs inhibitory neurons was offset by 90°, in line with predictions from computational models. Replay of previously recorded closed-loop stimulation patterns in an open-loop configuration failed to reproduce the same phase-specific effects, highlighting the necessity of real-time closed-loop interaction to achieve precise modulation. Additionally, stimulation of pyramidal neurons using the gamma protocol selectively amplified gamma power, independent of the target theta phase. These findings demonstrate that phase-dependent amplification of cortical theta power can be induced through targeted stimulation of local excitatory or inhibitory neurons, with the observed phase offset likely reflecting underlying circuit dynamics. This approach provides a framework for developing more effective brain stimulation strategies aimed at modulating oscillatory activity in humans.