  Context. In order to understand the formation of the first stars, which set
the transition between the Dark Ages and Cosmic Dawn epochs, it is necessary to
provide a detailed description of the physics at work within the first clouds
of gas which, during their gravitational collapse, will set the conditions for
stars to be form through the mechanism of thermal instability.
  Aims. Our objective is to study in detail the molecular cooling of gas in the
halos preceding the formation of the first stars. We are furthermore assessing
the sensitivity of the 21cm hydrogen line to this cooling channel.
  Results. We present the CHEMFAST code, that we developed to compute the
cosmological 21cm neutral hydrogen line inside collapsing matter overdensity.
We precisely track evolution in the abundances of ions, atoms and molecules
through a network of chemical reactions. Computing the molecular thermal
function due to the excitation of the rotational levels of the H2 molecule, we
find it strongly affects the gas temperature inside collapsing clouds of $10^8$
M$_\odot$. The gas temperature falls at the end of the collapse, when the
molecular cooling takes over the heating due to gravitation.
  Conclusions. We find that the 21cm brightness temperature inside the
collapsing cloud presents an emission feature, different from the one predicted
in expansion scenario. It moreover follows the same behavior as the gas
temperature, as it is also strongly affected by the molecular cooling. This
makes it a promising probe in order to map the collapsing halos and thermal
processes at work inside them.
