Background Transcranial magnetic stimulation (TMS) is widely used in brain research and treatment of various brain dysfunctions. However, the optimal way to target stimulation and administer TMS therapies, for example, where and in which electric-field direction the stimuli should be given, is yet to be determined.Objective To develop an automated closed-loop system for adjusting TMS parameters online based on TMS-evoked brain activity measured with electroencephalography (EEG).Methods We developed an automated closed-loop TMS–EEG set-up. In this set-up, the stimulus parameters are electronically adjusted with multi-locus TMS. As a proof of concept, we developed an algorithm that automatically optimizes the stimulation parameters based on single-trial EEG responses. We applied the algorithm to determine the electric-field orientation that maximizes the amplitude of the TMS– EEG responses. The validation of the algorithm was performed with six healthy volunteers, repeating the search twenty times for each subject.Results The validation demonstrated that the closed-loop control worked as desired despite the large variation in the single-trial EEG responses. We were often able to get close to the orientation that maximizes the EEG amplitude with only a few tens of pulses.Conclusion Optimizing stimulation with EEG feedback in a closed-loop manner is feasible and enables effective coupling to brain activity.Competing Interest StatementP.L. has received consulting fees (unrelated to this work) from Nexstim Plc. R.J.I. is an advisor and a minority shareholder of Nexstim Plc. The other authors declare no competing interests.