It seems that Śākyamuni Buddha as a śramaṇa was chiefly concerned with practices aimed at liberation and as such taught such a path to his disciples. Initially the Buddhist community did not engage in divination or rites connected with magic, spirit evocation and protective functions. It seems that in the Buddha's time such things while respected were not part of the job description of a śramaṇa (Brahmans on the other hand had sophisticated forms of magic which eventually enabled them to hold power in courts and enforce their social model of caste on the Indian sub-continent). The Buddha is on record as having discussions with devas, and of course he taught a cosmology where such beings all have their real substantial place (i.e., they're independent beings with their own agendas, some benevolent and others not so much). However, in the early sangha it was just a matter of respecting such beings like any other, and not actively engaging with them.
Over time, however, perhaps largely with the development of the Mahāyāna, this changed. Some early Mahāyāna scriptures forbid practitioners from engaging in various forms of magic such as divination and so on (which indicates it was probably going on ... a lot). Later we see protective rites and a whole number of guardian deities with their places in the cosmos maintaining order and peace. For instance, Virūpākṣa in the west who rules over nāgas and pūtanas, as seen at Tōdai-ji in Nara, Japan:
There is also Vaiśravaṇa, guardian of the north who rules over yakṣas and rākṣasas:
The two dharma guardians present in the temple are two of the four celestial kings (caturmahārājakāyikāḥ) charged with protecting the six heavens of the desire realm.
One might argue this was all just a cultural development and irrelevant to the Buddha's original ideas. However, if you see the cosmos how ancients in India and East Asia did, maintaining order in the cosmos, primary in the way of placating malevolent forces, was a pressing concern for the fear that if such order was not ensured chaos would ensue creating a hell on earth. The divine cord undone many wrathful forces would be unleashed. Some actually believe this is what we are seeing today.
But it still begs the question: is it necessary for one's practice to summon guardian spirits or engage in other forms of magic? From a utilitarian point of view having such protective forces is no different than taking the necessary precautions in the physical world to ensure your personal safety from harm. On the other hand, it seems in the Buddha's time śramaṇas in the Buddhist community did not directly appeal to such forces, and instead focused on their meditative practices.
From another perspective, possessing such magic in hand was politically useful. Throughout history some magician Buddhist monks are credited with summoning rain. This is still practised today actually. Such practical applications of magic no doubt would help in being directly supported by state powers, which was useful if you were running a large institution requiring social and material support. In Buddhist history one can see a lot of concern with worldly-benefiting rites and magic as practised by Buddhist clerics of some sort or another (lay or monastic). Arguably, much of Mahāyāna Buddhism in history has been concerned with such worldly magic, liberation practices coming second. This is why monks historically were rites masters and fields of merit with a minority actively pursuing scholarship and then a smaller minority liberation.
Still, there are benefits to such an arrangement if we think of things from their cosmology and vision of the universe. Was it necessary for liberation? Not necessarily immediately, but in terms of operating an institution in the real world AND hopefully benefiting many beings, such magic had its uses (like summoning rain during a drought or placating wrathful spirits).