Nowadays, Wireless Sensor Networks (WSNs) are widely used in many areas, especially in military operations and monitoring applications. Their wireless nature makes them very attractive to attackers, so its security system plays a vital role. Due to the limitations on resources, such as energy and storage, the security mechanism of WSNs have to be considered differently from traditional networks. Over the past years researchers have encouraged the use of mobile agents as a new and smart paradigm for distributed applications to overcome the limitations of sensor nodes. In this paper a defensive mechanism will be proposed against sinkhole attacks using mobile agents. We use mobile agents to aware every node from its trusted neighbors through a three-step negotiation so they do not listen to the traffics generated by malicious nodes. We evaluate our work in terms of energy consumption, packet loss rate, throughput and agent overhead caused by mobility and communication.