This paper describes the architecture and functionality of a generic agent that is in charge of handling a given environment in an Ambient Intelligence context, ensuring suitable contextualized and personalized support to the user's actions, adaptivity to the user's peculiarities and to changes over time, and automated management of the environment itself. The architecture is implemented in a multi-agent system, where different types of agents are endowed with different levels of reasoning and learning capabilities. In addition to controlling normal operations of the environment, the system may identify user's needs and goals and activate suitable workflows to satisfy them. Some actions in these workflow involve the execution of semantic services. When a single service is not available for fulfilling a given need, an automatic service composer is used to obtain a suitable combination of services. The architecture has been implemented in a prototypical agent-based system that works in a Smart Home Environment.