Mobile ad hoc networks (MANETs) are characterized by their heterogeneity and the diverse capabilities of their nodes given that almost any device with a wireless network interface can join such a network. In such an environment it is difficult to dynamically deploy services and protocols without a common understanding among the participating nodes and their capabilities. A deployment/provisioning framework must cope with the high-level of device heterogeneity, degree of mobility, and should also take into account the potentially limited device resources. This paper presents a context-based programmable framework for dynamic service/protocol deployment that allows the nodes of a mobile ad hoc network to download and safely activate required service/protocol software dynamically. Downloading and activation can be triggered through preconditions evaluated according to available contextual information. This strategy leads to the alignment of the nodes' capabilities so that common services and protocols can be deployed even if they are not available at every node. In addition, dynamic context-driven deployment may lead to a degree of network self-optimization. We present the programmable framework and functionality and evaluate its various aspects through testbed experimentation, simulation and analytical modeling. The results demonstrate good performance with respect to the supported functionality.