I think the most down-to-earth approach is to make the classical $\operatorname{Spec}$ construction constructive, interpret it in the internal logic of the topos, then externalise the result.
This is basically what you guessed – just take the frame of radical ideals of the ring – and is explained in § 12.4 of the PhD thesis of Ingo Blechschmidt.
Note especially that it is not the same thing as the relative $\operatorname{\underline{Spec}}$, which has a different universal property.
But if you only care about existence, then there is a more conceptual approach.
A ringed (Grothendieck) topos is basically the same thing as a topos equipped with a geometric morphism to the classifying topos for (commutative) rings, i.e. $\mathbf{B} (\textbf{CRing}) = \textbf{Psh} (\textbf{CRing}_\textrm{fp}^\textrm{op})$.
We even get the correct notion of morphism for free: it is a geometric morphism between the toposes together with a 2-morphism from the composite geometric morphism to the other one.
Diagrammatically:
$$\require{AMScd}
\begin{CD}
\mathcal{Y} @>{\ulcorner O_\mathcal{Y} \urcorner}>> \mathbf{B} (\textbf{CRing}) \\
@VVV \Rightarrow @| \\
\mathcal{X} @>>{\ulcorner O_\mathcal{X} \urcorner}> \mathbf{B} (\textbf{CRing})
\end{CD}$$
A locally ringed topos is basically the same thing as a topos equipped with a geometric morphism to the Zariski topos, i.e. $\mathbf{B} (\textbf{LCRing}) = \textbf{Sh} (\textbf{CRing}_\textrm{fp}^\textrm{op}, \textrm{Zar})$.
This gives the right objects, but unfortunately the natural notion of morphism suggested by this characterisation ends up being the same thing as a morphism of ringed toposes, contrary to the conventional definition.
This is probably the root cause of the failure of obvious/naïve constructions: for example, taking the pullback of the classifying morphism $\ulcorner O_\mathcal{X} \urcorner : \mathcal{X} \to \mathbf{B} (\textbf{CRing})$ and the inclusion $\mathbf{B} (\textbf{LCRing}) \hookrightarrow \mathbf{B} (\textbf{CRing})$ gives the maximal subtopos of $\mathcal{X}$ such that the restriction of $O_\mathcal{X}$ is a local ring, and taking the comma object instead gives the classifying topos for $O_\mathcal{X}$-algebras, which is also not what we want.
Nonetheless, the yoga of classifying toposes suggests a way forward.
The universal ring in $\mathbf{B} (\textbf{CRing})$ "is" simply the inclusion $\textbf{CRing}_\textrm{fp} \to \textbf{CRing}$.
If the prime spectrum is truly a natural construction, then it should be possible to relativise it to rings in arbitrary toposes by constructing it once for all for the universal ring.
There is an obvious candidate: take the classical prime spectrum functor $\textbf{CRing}_\textrm{fp} \to \textbf{Frm}$, then use the Grothendieck construction to obtain a site fibred in frames over $\textbf{CRing}_\textrm{fp}^\textrm{op}$.
Somewhat more explicitly, the underlying category has objects pairs $(A, U)$ where $A$ is a finitely presented ring and $U$ is an open subspace of $\operatorname{Spec} A$, morphisms $(A, U) \to (B, V)$ are ring homomorphisms $\phi : B \to A$ such that the image of $U$ under $\operatorname{Spec} \phi : \operatorname{Spec} A \to \operatorname{Spec} B$ is a subspace of $V$, and the Grothendieck topology is the one generated by declaring $\{ (B, V_\alpha) \to (B, V) \}$ to be covering if the underlying ring homomorphisms are all $\textrm{id}_B$ and $\bigcup_\alpha V_\alpha = V$.
Let $\mathbf{B} (\textbf{CRingP})$ be category of sheaves on this site.
By construction, the forgetful functor induces a localic geometric morphism $\mathbf{B} (\textbf{CRingP}) \to \mathbf{B} (\textbf{CRing})$, and we also have a local ring in $\mathbf{B} (\textbf{CRingP})$, namely the sheaf $(A, U) \mapsto O_{\operatorname{Spec} A} (U)$, giving a geometric morphism $\mathbf{B} (\textbf{CRingP}) \to \mathbf{B} (\textbf{LCRing})$.
Define $\mathop{\textbf{Spec}} O_\mathcal{X}$ by the following pullback diagram (in the bicategory of toposes):
$$\begin{CD}
\mathop{\textbf{Spec}} O_\mathcal{X} @>>> \mathbf{B} (\textbf{CRingP}) \\
@VVV @VVV \\
\mathcal{X} @>>{\ulcorner O_\mathcal{X} \urcorner}> \mathbf{B} (\textbf{CRing})
\end{CD}$$
Localic geometric morphisms are closed under pullback, so $\mathop{\textbf{Spec}} O_\mathcal{X}$ is localic over $\mathcal{X}$; in particular, if $\mathcal{X}$ is localic, so is $\mathop{\textbf{Spec}} O_\mathcal{X}$.
In any case, $\mathop{\textbf{Spec}} O_\mathcal{X}$ is a ringed topos in two (possibly different) ways: first, pulling back either $O_\mathcal{X}$ from $\mathcal{X}$ or $(A, U) \mapsto A$ from $\mathbf{B} (\textbf{CRingP})$ gives a (possibly non-local) ring $A_{\mathop{\textbf{Spec}} O_\mathcal{X}}$, but more importantly, pulling back the local ring from $\mathbf{B} (\textbf{CRingP})$ gives a local ring $O_{\mathop{\textbf{Spec}} O_\mathcal{X}}$.
In particular, we get a morphism from the locally ringed topos $\mathop{\textbf{Spec}} O_\mathcal{X}$ to the ringed topos $\mathcal{X}$; diagrammatically:
$$\begin{CD}
\mathop{\textbf{Spec}} O_\mathcal{X} @>{\ulcorner O_{\mathop{\textbf{Spec}} O_\mathcal{X}} \urcorner}>> \mathbf{B} (\textbf{LCRing}) \\
@VVV \Rightarrow @VVV \\
\mathcal{X} @>>{\ulcorner O_\mathcal{X} \urcorner}> \mathbf{B} (\textbf{CRing})
\end{CD}$$
It remains to be shown this is universal.
By the yoga of classifying toposes again, we start with the universal case, i.e. the inclusion $\mathbf{B} (\textbf{LCRing}) \hookrightarrow \mathbf{B} (\textbf{CRing})$.
For this, it is helpful to know that $\mathbf{B} (\textbf{CRingP})$ is the classifying topos for rings equipped with prime filters.
Here, by filter of a ring $O$ I mean a subset $F \subseteq O$ such that:
- given elements $a_0, \ldots, a_{n-1}$ of $O$, $\{ a_0, \ldots, a_{n-1} \} \subseteq F$ if and only if $a_0 \cdots a_{n-1} \in F$.
For example, the set $O^\times$ of units of $O$ is a filter of $O$ – in fact, $O^\times$ is the smallest filter of $O$, since any filter contains $1$ and is closed under factors.
A filter $F$ as above is prime if:
- if $a_0 + \cdots + a_{n-1} \in F$, then for some $i$, $a_i \in F$.
Classically, a subset is a prime filter if and only if its complement is a prime ideal, but prime filters are what matter constructively.
Also, $O^\times$ is prime if and only if $O$ is local – indeed, this is the constructively correct definition of local ring – and, more generally, $O [F^{-1}]$ is a local ring if $F$ is a prime filter of $O$.
The universal ring equipped with a prime filter in $\mathbf{B} (\textbf{CRingP})$ is the (non-local!) ring $(A, U) \mapsto A$ with the prime filter obtained by pulling back the canonical one from the local ring previously defined, i.e.:
$$(A, U) \mapsto \{ a \in A : a \text{ becomes invertible in } O_{\operatorname{Spec} A} (U) \}$$
Thus, to factorise $\mathbf{B} (\textbf{LCRing}) \hookrightarrow \mathbf{B} (\textbf{CRing})$ it suffices to give a prime filter of the universal local ring – well, there is an obvious choice: the unit filter.
Thus we get a geometric morphism $\mathbf{B} (\textbf{LCRing}) \to \mathbf{B} (\textbf{CRingP})$, and (with a bit of thought) we see that it is a right adjoint right inverse, i.e.:
- the composite $\mathbf{B} (\textbf{LCRing}) \to \mathbf{B} (\textbf{CRingP}) \to \mathbf{B} (\textbf{LCRing})$ is isomorphic to the identity,
- there is a 2-morphism from the identity to the composite $\mathbf{B} (\textbf{CRingP}) \to \mathbf{B} (\textbf{LCRing}) \to \mathbf{B} (\textbf{CRingP})$, and
- these satisfy the triangle identities.
Now we return to the original problem.
Given a locally ringed topos $\mathcal{Y}$, a ringed topos $\mathcal{X}$, and a morphism $(f, f^\sharp) : (\mathcal{Y}, O_\mathcal{Y}) \to (\mathcal{X}, O_\mathcal{X})$ of ringed toposes, we may form the following pullback diagram in $\mathcal{Y}$:
$$\begin{CD}
F_f @>>> O_\mathcal{Y}^\times \\
@VVV @VVV \\
f^{-1} O_\mathcal{X} @>>{f^\sharp}> O_\mathcal{Y}
\end{CD}$$
Since $O_\mathcal{Y}^\times$ is a prime filter of $O_\mathcal{Y}$, $F_f$ is a prime filter of $f^{-1} O_\mathcal{X}$, so we obtain a commutative square in the bicategory of toposes:
$$\begin{CD}
\mathcal{Y} @>{\ulcorner (f^{-1} O_\mathcal{X}, F_f) \urcorner}>> \mathbf{B} (\textbf{CRingP}) \\
@VVV @VVV \\
\mathcal{X} @>>{\ulcorner O_\mathcal{X} \urcorner}> \mathbf{B} (\textbf{CRing})
\end{CD}$$
By the universal property of pullbacks, we get a factorisation:
$$\begin{CD}
\mathcal{Y} @>{\ulcorner (f^{-1} O_\mathcal{X}, F_f) \urcorner}>> \mathbf{B} (\textbf{CRingP}) \\
@VVV @| \\
\mathop{\textbf{Spec}} O_\mathcal{X} @>>> \mathbf{B} (\textbf{CRingP}) \\
@VVV @VVV \\
\mathcal{X} @>>{\ulcorner O_\mathcal{X} \urcorner}> \mathbf{B} (\textbf{CRing})
\end{CD}$$
Postcomposing the top half with the 2-morphism corresponding to $f^\sharp$ and the geometric morphism $\mathbf{B} (\textbf{CRingP}) \to \mathbf{B} (\textbf{LCRing})$ gives us a diagram of the form below:
$$\begin{CD}
\mathcal{Y} @>{\ulcorner (O_\mathcal{Y}, O_\mathcal{Y}^\times) \urcorner}>> \mathbf{B} (\textbf{CRingP}) @>>> \mathbf{B} (\textbf{LCRing}) \\
@V{\tilde{f}}VV \Rightarrow @| @| \\
\mathop{\textbf{Spec}} O_\mathcal{X} @>>> \mathbf{B} (\textbf{CRingP}) @>>> \mathbf{B} (\textbf{LCRing})
\end{CD}$$
The composite of the top row is, of course, the classifying morphism $\ulcorner O_\mathcal{Y} \urcorner : \mathcal{Y} \to \mathbf{B} (\textbf{LCRing})$.
The composite of the bottom row is the classifying morphism of the canonical local ring $O_{\mathop{\textbf{Spec}} O_\mathcal{X}}$ in $\mathop{\textbf{Spec}} O_\mathcal{X}$.
Furthermore, the choice of $F_f$ and the definition of $\tilde{f}$ ensure that this corresponds to a morphism of locally ringed toposes: first, we have the following pullback square in $\mathcal{Y}$,
$$\begin{CD}
F_f @>>> \tilde{f}{}^{-1} O_{\mathop{\textbf{Spec}} O_\mathcal{X}}^\times \\
@VVV @VVV \\
f^{-1} O_\mathcal{X} @>>> \tilde{f}{}^{-1} O_{\mathop{\textbf{Spec}} O_\mathcal{X}}
\end{CD}$$
and moreover we have $(f^{-1} O_\mathcal{X}) [F_f^{-1}] \cong \tilde{f}{}^{-1} O_{\mathop{\textbf{Spec}} O_\mathcal{X}}$, hence
$$\begin{CD}
\tilde{f}{}^{-1} O_{\mathop{\textbf{Spec}} O_\mathcal{X}}^\times @>>> O_\mathcal{Y}^\times \\
@VVV @VVV \\
\tilde{f}{}^{-1} O_{\mathop{\textbf{Spec}} O_\mathcal{X}} @>>{\tilde{f}{}^\sharp}> O_\mathcal{Y}
\end{CD}$$
is also a pullback square in $\mathcal{Y}$.
We have now factorised the given morphism $(f, f^\sharp)$ as a morphism of locally ringed toposes followed by the morphism $(\mathop{\textbf{Spec}} O_\mathcal{X}, O_{\mathop{\textbf{Spec}} O_\mathcal{X}}) \to (\mathcal{X}, O_\mathcal{X})$, proving the existence part of the universality of the latter.
Uniqueness remains to be shown but I think that should be straightforward given everything I have already said.