Mahbub Ali is an Afghan Muslim living in Lahore. His official job is as a horse trader, and he makes a ton of money buying and selling horses in the city… But secretly, Mahbub Ali is also a member of the huge network of information gatherers (a.k.a. spies) employed by the British Indian government to keep a secret eye on their interests in the state. His official registration number as a spy is C.25. (It doesn't have quite the same impact as 007, sadly.)
Mahbub Ali is like a talent scout for the British Indian Secret Service: long before it turns out that Kim is going to be adopted by the Irish Mavericks regiment and sent to St. Xavier's school, Mahbub Ali knows that Kim is going to be a great information gatherer for the state. As early as the first chapter, we know that Mahbub Ali can tell "the boy's value as a gossip" (1.49), and that he has been paying Kim in food to follow people and to deliver messages.
In fact, Mahbub Ali is the one to give Kim his first real mission: when Kim first sets out with the lama to travel south in search of the River of the Arrow, Mahbub Ali tells Kim to bring a secret message to an Englishman who turns out to be Colonel Creighton.
Without Mahbub Ali's help, Kim would never have been introduced to the Great Game. And Kim does thank Mahbub Ali for his assistance in keeping Kim in school (even against Kim's wishes) and bringing him to the attention of Colonel Creighton: "I say now, Hajji [a term of respect for a Muslim who has made a pilgrimage to the holy city of Mecca], that it was well done; and I see my road all clear before me to a good service" (8.46).
In a way, there is a nice balance to the fact that both Kim and Mahbub Ali have seen one another passed out at least once. In Mahbub Ali's case, it happens the night that Kim and the lama set out for Umballa in the first chapter. They are staying with Mahbub Ali's people at the local serai (a serai is a place for people living in caravans to pitch camp). Mahbub Ali has already given Kim his secret message to deliver to Colonel Creighton, and he has gone off with a prostitute, the "Flower of Delight" (1.191), to get completely drunk off his gourd.
It's a lucky thing that Mahbub Ali leaves his door open while he is drinking with this Flower, because she is also working with two men representing the rebellious northern kings who Mahbub Ali is trying to rat out to the English with the message he has given to Kim. When Mahbub Ali passes out drunk, the Flower jumps at the chance to let these two guys search all of Mahbub Ali's stuff.
Kim, of course, being the observant and nosy kid that he is, sees this all happening—Mahbub Ali's drunkenness, the search by the agents of the Five Kings, and their failure to find the message that Kim himself is carrying.
Even though Kim likes and trusts Mahbub Ali up to a point, he still doesn't tell his friend about the Flower and these two agents until long after the fact—not until Chapter 8, in fact. When Kim spots two assassins trying to kill Mahbub Ali, he recognizes one of them as the man who helped to search Mahbub Ali's things way back in Chapter 1. In exchange for a small bribe, Kim finally tells Mahbub Ali this interesting bit of information, which indicates an ongoing assassination plot against Mahbub Ali by the Five Kings up north.
While Mahbub Ali is grateful to have Kim's information, he is still annoyed that he has to pay for it. Their friendship isn't enough to overcome Kim's deeply practical desire for profit at all times. As Mahbub Ali exclaims, "Wilt thou some day sell my head for a few sweetmeats if the fit takes thee?" (8.149). In other words, will you sell my life for candy some day, kid? (Probably not, but who knows with Kim.)
When it's Kim's turn to pass out, it's totally Mahbub Ali's fault. After Kim leaves St. Xavier's and gets ready to set off on his life of wandering with the lama in Chapter 10, Mahbub Ali brings Kim to a dark place called "The Bird-Cage" (10.77)—a place that the text strongly implies is a brothel.
At first, we think that Mahbub Ali has a very different send-off in mind, but what he actually does is lead Kim to a blind woman named Huneefa to be blessed. Huneefa lights a strongly scented incense that makes Kim fall right over, and then she paints his chest with special ritual designs to protect him from evil. When Kim wakes up, the Babu tells him that Huneefa has already taken care to "[charm Kim] against all devils and all dangers" (10.122).
The reason we bring up these two incidences of unconsciousness is that they do tell us something about the relationship between Kim and Mahbub Ali. Kim likes the horse trader, but he doesn't volunteer his information about Mahbub Ali's enemies until the man actually pays him. Likewise, Mahbub Ali clearly loves Kim as an almost-son, but he still basically tricks him into being knocked out so that he can have symbols painted on his body. Both of them clearly care about each other, but there is still some distance there, distance that we don't really see in Kim's relationship with the lama.
The thing is, Kim is so deeply attached to the lama because the lama is spiritual and separate from this world. Mahbub Ali drinks alcohol, pursues women, makes lots of money (and likes it), dodges assassins, and believes in superstitions. He is very much a recognizable part of Kim's world.
Sure—he may have hidden depths (what with his secret spy identity), but he is also deeply predictable to Kim. Mahbub Ali half-jokingly complains over the fact that Kim sees "such worth in a man so little known" (8.126)—in other words, that Kim loves someone so random as the lama. But it kind of makes sense to us that Kim would attach himself to the one person in the world whose motivations and ideas Kim finds genuinely difficult to understand.