from your condition MRTS=w/r, I believe you are solving a cost minimization problem.
Output is given in this problem, solution to this problem gives you the optimal combinations of factors inputs under different level of output, and hence you can use this to solve for the "cost curve"
MC in the condtion "P=MC" comes from the above "cost curve"
So, P=MC gives you the optimal otuput, and given this amount of output, MRTS=w/r tells you the optimal factor combinations.