A method for controlling gas composition in a surgical cavity during an endoscopic surgical procedure includes monitoring for a plurality of gas species in a gas flow from a surgical cavity of a patient. The method includes measuring the plurality of gas species in the gas flow from the surgical cavity and determining if the gas species measured in the gas flow from the surgical cavity are each present and/or within a respective desired range. The method includes adding gas into the surgical cavity if one or more gas species in the plurality of gas species is outside of the respective desired range so as to bring a composition of gas species in the surgical cavity within the respective desired range.