Optimizing Lagrangian Function Subject to 4 Input/Output Constraints: The objective function:

$$\text{utility}=U\left(x_{c}, y_{c}\right)$$

subject to, 


$x_{o}=f\left(y_{i}\right)$
$y_{o}=g\left(x_{i}, x_{o}\right)$
$x_{c}+x_{i}=x_{o}+x^{*}$
$y_{c}+y_{i}=y_{o}+y^{*}$


where o represents output, i is inputs, c is consumed, and * represents initial quantity stocks. Lagrangian is combined and the first-order conditions are as follows:
$$
\begin{array}{l}{\partial \mathscr{L} / \partial x_{c}=U_{1}+\lambda_{3}=0} \\ {\partial \mathscr{L} / \partial y_{c}=U_{2}+\lambda_{4}=0} \\ {\partial \mathscr{L} / \partial x_{i}=\lambda_{2} g_{1}+\lambda_{3}=0} \\ {\partial \mathscr{L} / \partial y_{i}=\lambda_{1} f_{y}+\lambda_{4}=0} \\ {\partial \mathscr{L} / \partial x_{o}=-\lambda_{1}+\lambda_{2} g_{2}-\lambda_{3}=0} \\ {\partial \mathscr{L} / \partial y_{o}=-\lambda_{2}-\lambda_{4}=0}\end{array}
$$

Simplification leads to the following expressions:
$$
M R S=\frac{U_{1}}{U_{2}}=\frac{\lambda_{3}}{\lambda_{4}}
$$
$$
M R S=\frac{\lambda_{3}}{\lambda_{4}}=\frac{\lambda_{2} g_{1}}{\lambda_{2}}=g_{1}
$$
$$
M R S=\frac{\lambda_{3}}{\lambda_{4}}=\frac{-\lambda_{1}+\lambda_{2} g_{2}}{\lambda_{4}}=\frac{-\lambda_{1}}{\lambda_{4}}+\frac{\lambda_{2} g_{2}}{\lambda_{4}}
$$
$$
=\frac{1}{f_{y}}-g_{2}
$$

Q: The text states that optimality in y production requires the individual's MRS in consumption equal the marginal productivity of x in the production of y. I do not understand this statement. We are optimizing utility not the production function?
I think what it’s referring to optimal $y$ (i.e. what would be normally in calculus denoted as $y^*$- but you already use that for the initial stock). 

It makes sense that to maximize the utility MRS should be equal to marginal productivity since MRS should be equal to ratio of prices and in case like this where the person produces goods for themselves the prices will equal to the marginal costs of production which in turn will also equal to the marginal product (essentially producing for yourself is like being in a perfectly competitive market). The intuition is that person will produce good only to the extent the marginal utility of consumption becomes equal to marginal disutility of production, as if you are producing less you can increase utility by producing more and if you are producing more than that you can increase utility by producing less. At least that’s my understanding of the intuition behind this kind of model.