I notice this is a rather old thread but I nevertheless have the same question I'm looking for an answer to. Would it be possible to share that script again? It seems that the dropbox link has stoped to work at this moment.
The MapServer that you have put in the question does not support tiles, see this MapServer in comparison https://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/ it has tile info mentioned, and thus it supports tiles.
The core misconception is the nature of the Spherical (Pseudo) Mercator projection, and the application of the Slippy Map tile grid on it.
Generally speaking[¹], given the (pseudo code) transformation (Lon/Lat to Spherical Mercator)
ERAD = 6378137.0 // Auxiliary Sphere equal axis radius in meter
D2R = PI / 180.0 // Degrees to Radians multiplying factor