An obstacle in a proof of Lagrange's mean value theorem by Nested Interval theorem

I was trying to prove Lagrange's mean value theorem by Nested Interval theorem and there's step where I got stuck ; let me write down to the step

Let $f:[x_1,x_2]\to \mathbb R$ be continuous on $[x_1,x_2]$ and differentiable in $(x_1,x_2)$ . Define

$m:=\dfrac{f(x_1)-f(x_2)}{x_1-x_2} , y:=\dfrac{x_1+x_2}2 , m_1:=\dfrac {f(y)-f(x_1)}{h} , m_2:=\dfrac {f(x_2)-f(y)}{h}$ , where

$h:=\dfrac{x_2-x_1}2$ then $\min\{m_1,m_2\}\le m \le \max \{m_1,m_2\}$ so defining the continuous function

$g(x):=\dfrac {f(x+h)-f(x)}h$ , we see by intermediate value theorem , for some

$a_1 \in [x_1,x_2] , m=\dfrac{f(b_1)-f(a_1)}{b_1-a_1} ,$ where $b_1-a_1=h=\dfrac {x_2-x_1}2$ so we can repeat this process to

get a nested sequence of intervals $[a_n,b_n]$ , where $b_n-a_n=\dfrac {x_2-x_1}{2^n}$ and $m=\dfrac {f(a_n)-f(b_n)}{a_n-b_n}$ for each $n$ , so there is a unique point $x \in \cap_{n=1}^ \infty [a_n,b_n]$ I am planning to show that

$f'(x)=m=\dfrac{f(x_1)-f(x_2)}{x_1-x_2}$ , and I can except one difficulty , I am not being able to show that $x$

lies strictly between $x_1,x_2$ that is I cannot show $x \in (x_1,x_2)$ ; Is it true that $x \in (x_1,x_2)$ or is my construction not valid ?

Consider an affine linear function to see that it is possible to choose $a_n = x_1$ for all $n$, and then you have $x = x_1 \notin (x_1,x_2)$.

To get the proof working, you need to show that you can arrange it so that $x \in (x_1,x_2)$, that is, there is an $n_1$ with $a_{n_1} > x_1$, and an $n_2$ with $b_{n_2} < x_2$. Then we have $x_1 < a_n < b_n < x_2$ for all $n \geqslant \max \{n_1,n_2\}$ and the proof is done.

If $m_1 < m < m_2$ (or $m_2 < m < m_1$), then we must have $x_1 < a_1 < y$, and therefore also $b_1 < x_2$, and everything is fine.

So we need only consider the case where we have $m_1 = m$ or $m_2 = m$. But if that is the case, then

$$m = \frac{f(x_2) - f(x_1)}{x_2-x_1} = \frac{1}{2}\cdot \frac{f(x_2) - f(y) + f(y) - f(x_1)}{h} = \frac{1}{2}\left(m_2 + m_1\right)$$

shows that we have $m_1 = m = m_2$.

Then: If at the first step we have $m_1 = m = m_2$, choose $a_1 = x_1$ to get $b_1 < x_2$. If at the second step we also have $m_1 = m = m_2$, then choose $a_2 = \frac{a_1+b_1}{2}$ to get $x_1 < a_2$ and we are done. If at the second step the two slopes are different, we necessarily have $x_1 < a_2 < b_2 < x_2$ and are happy too.