Brain-machine interfaces (BMIs) for closed-loop control of anesthesia have the potential to automatically monitor and control brain states under anesthesia. Since a variety of anesthetic states are needed in different clinical scenarios, designing a generalizable BMI architecture that can control a wide range of anesthetic states is essential. In addition, drug dynamics are non-stationary over time and could change with the depth of anesthesia. Hence for precise control, a BMI needs to track these non-stationarities online. Here we design a BMI architecture that generalizes to control of various anesthetic states and their associated neural signatures, and is adaptive to time-varying drug dynamics. We provide a systematic approach to build general parametric models that quantify the anesthetic state and describe the drug dynamics. Based on these models, we develop an adaptive closed-loop controller within the framework of stochastic optimal feedback control. This controller tracks the non-stationarities in drug dynamics, achieves tight control in a time-varying environment, and removes the need for an offline system identification session. For robustness, the BMI also ensures small drug infusion rate variations at steady state. We test the BMI architecture for control of two common anesthetic states, i.e., burst suppression in medically-induced coma and unconsciousness in general anesthesia. Using numerical experiments, we find that the BMI generalizes to control of both these anesthetic states; in a time-varying environment, even without initial knowledge of model parameters, the BMI accurately controls these two different anesthetic states, reducing bias and error more than 70 times and 9 times, respectively, compared with a non-adaptive system.