Chemical warfare agents (CWAs) are toxic that pose a threat to the environment and human health, even trace amounts of CWAs can be fatal. In view of this, there is an urgent need to develop gas sensors for trace detection and ultrafast response of CWAs. Herein, an optical gas sensor has been proposed based on metal-organic frameworks (MOFs) three-dimensional (3D) photonic crystal to detect trace CWAs' simulant (dimethyl methylphosphonate, DMMP) in different atmospheric humidity (RH 20 %, RH 40 %, RH 60 %, RH 80 %). At relative humidity (RH) of 20 %, the sensor shows excellent selectivity of DMMP due to the specific interactions of van der Waals force between UiO-67 and phosphoryl oxygen (OP) group of DMMP (C3H9O3P), the ultrahigh sensitivity (42.7 ppb), ultrafast response (0.5 s) are profit from the ordered superstructure of 3D photonic crystal and its complete photonic bandgap. At higher humidity (RH 40%-80 %), the sensor shows excellent stability, long-term repeatability, and it still keeps ultrahigh sensitivity (12.1 ppb), ultrafast response (0.49 s) for DMMP at RH 80 %. Moreover, an optical gas sensor array has been prepared to solve the problem of cross-sensitive between DMMP and other CWAs at highest humidity (RH ≥ 80 %), the average classification accuracy can reach 98.6 %.