The important caveat that is that in many jurisdictions, there’s a significant difference in the tax treatment of contractors vs. employees, and so by producing such a contract (that forbids subcontracting) you are plausibly creating an employment arrangement and are on the hook for tax consequences.
Here in the UK, for example, if you wrote such a contract, it would be enforceable… but you would be considered (by the tax authority) to be trying to evade employment taxes, and hit with something starting in the range of a low 5-figure financial consequence (assuming the contract was a few months at current average rate for tech workers).
All of my contracts explicitly allow subcontracting in order to comply with tax law.