unfortunately I do not have enough reputation to comment answers of other questions. So I have to start a new question.
Essentially I have the same problem as described
You can walk the sub-expressions to collect the auxiliary sort and function declarations. The following extended example contains code that parses SMTLIB2 and it has to traverse returned expressions for collecting sort and function declarations. You can browse it here
It uses the C++ API. The function collect_decls traverses the expression and collects uninterepreted sorts and functions (this function assumes there are no user-defined algebraic data-types and does not attempt to extract those).