9. (16 points) Find all positive integers $n$ and real numbers
$$
x\left(x \in\left[-\frac{\pi}{2}, \frac{\pi}{2}\right]\right) \text {, }
$$

such that $n \tan x+\sqrt{3}$ and $\cot x+\sqrt{3}$ are both rational numbers.
Given that $n \tan x+\sqrt{3}$ and $\cot x+\sqrt{3}$ are both rational numbers, we have
$$
\begin{array}{l}
(n \tan x+\sqrt{3})+(\cot x+\sqrt{3}) \\
=(n \tan x+\cot x)+2 \sqrt{3} \in \mathbf{Q} \\
(n \tan x+\sqrt{3})(\cot x+\sqrt{3}) \\
=n+(n \tan x+\cot x) \sqrt{3}+3 \in \mathbf{Q}
\end{array}
$$

From equation (1), there exists a rational number $q$ such that
$n \tan x+\cot x=q-2 \sqrt{3}$.
From equation (2), we know $(n \tan x+\cot x) \sqrt{3} \in \mathbf{Q}$, i.e.,
$$
(q-2 \sqrt{3}) \sqrt{3} \in \mathbf{Q} \Rightarrow q \sqrt{3} \in \mathbf{Q} \Rightarrow q=0 \text {. }
$$

Thus, $n \tan x+\cot x=-2 \sqrt{3}$.
Let $\tan x=y$. Then
$$
\begin{array}{l}
n y+\frac{1}{y}=-2 \sqrt{3} \\
\Rightarrow n y^{2}+2 \sqrt{3} y+1=0 \\
\Rightarrow y=\frac{-\sqrt{3} \pm \sqrt{3-n}}{n} .
\end{array}
$$

Since $n y+\sqrt{3}= \pm \sqrt{3-n} \in \mathbf{Q}$, we know $n=2$ or 3.
When $n=2$, $y=\frac{-\sqrt{3} \pm 1}{2}$. In this case, $x=\arctan \frac{-\sqrt{3}+1}{2}$ or $\arctan \frac{-\sqrt{3}-1}{2}$.

When $n=3$, $y=-\frac{\sqrt{3}}{3}$. In this case,
$$
x=\arctan \left(-\frac{\sqrt{3}}{3}\right)=-\frac{\pi}{6} \text {. }
$$