Catalysis of the reaction: tryptophan + O2 = N-formylkynurenine. The product of the reaction depends on the substrate; D-tryptophan produces N-formyl-D-kynurenine, and L-tryptophan produces N-formyl-L-kynurenine.