Merge 2 vertices at LAST and at CURSOR in Python
This can be done by manipulating the mesh itself using the Bmesh module:

Would it be possible to get list of index of components BY SELECTION ORDER in Edit Mode?

You can access the selection history and proceed with the bmesh.ops calls for merging vertices:

bmesh.ops.pointmerge(bm, verts, merge_co)


Or do it entirely manually by modifying the mesh data itself.

If you have any UV's or vertex colors, you will have to take care of them too.