There's no such thing as a 100% sure identification of what 'the key' of a song is if you aren't taking notation as your reference - sometimes different people hear the same song as being in different keys. But if you have MIDI files, you have most of the same information that someone listening to a song would do - i.e. you have all the notes - so yes, you can make a fairly good go at finding out the key.
Basically all you need to do is work out how you'd work out the key of a piece by ear, and then turn that into an algorithm. How can you find the key of a song by ear? Mentions some relevant information, and you may find other useful techniques for finding the key of the song by ear around this site and the web.
As well as the Krumhansl-Schmuckler algorithm mentioned by Dom, if you're a coder (I see that you have a stack overflow account), this could be a great problem to solve using machine learning, assuming you have a suitable training set of MIDI files with the key already correctly identified. You'd probably want to distil down the data in the song - for example, you could do an analysis that worked out
- how many times each note of the octave occurs in the piece
- how many times each note is playing on the strong beats
- the average midi velocity of each note
...and then pass that in as the inputs to a suitable neural network and training algorithm. You could also consider the algorithm that Dom mentioned when deciding how to slice up the data. You might only end up with a trained NN that generates similar answers to to the Krumhansl-Schmuckler algorithm though!
You might also want to consider how you're going to deal with pieces that identifiably change key over the course of their development.
One more thing - MIDI does have the concept of a Key Signature meta message. This might not often be present or reliable but it could be another data point.