MicroRNAs and other small oligonucleotides in biofluids are promising disease biomarkers, yet conventional assays require complex processing steps that are unsuitable for point-of-care testing or for implantable or wearable sensors. Single-walled carbon nanotubes are an ideal material for implantable sensors, owing to their emission in the near-infrared spectral region, photostability and exquisite sensitivity. Here, we report an engineered carbon-nanotube-based sensor capable of real-time optical quantification of hybridization events of microRNA and other oligonucleotides. The mechanism of the sensor arises from competitive effects between displacement of both oligonucleotide charge groups and water from the nanotube surface, which result in a solvatochromism-like response. The sensor, which allows for detection via single-molecule sensor elements and for multiplexing by using multiple nanotube chiralities, can monitor toehold-based strand-displacement events, which reverse the sensor response and regenerate the sensor complex. We also show that the sensor functions in whole urine and serum, and can non-invasively measure DNA and microRNA after implantation in live mice.