How to run a PHP script in the Magento 2 root directory with the access to the Magento 2 classes and database?

But how can we add products from a script which placed on root directory ?

It is unsupported, unrecommended and obviously low-quality scenario.
Please make a Magento 2 extension or use a Mapento 2 web API to manipulate a Magento 2 store.