According to this section of an online Kubrick FAQ (which seems to be based primarily on this New York Times article), Kubrick didn't write the script himself, but he helped shape it in close collaboration with a series of writers: Brian Aldiss (who wrote the original short story it was based on, 'Supertoys Last All Summer Long'), Bob Shaw, Ian Watson, and lastly Sara Maitland. This section of the article discusses some elements of the movie's ending that seem in particular to have been more Kubrick's vision than the writers':
Kubrick's final collaborator on the 'A.I' script was English novelist Sara Maitland whom he felt was necessary in shaping the story into a cohesive whole. "By the time I came to the project it had become enormous, unwieldy and unfocused," said Ms. Maitland. Upon perusing the piles of unfinished scripts, she concluded that the story needed to make emotional sense as a myth or fairy tale does, and believes that Kubrick realized this. In fact Kubrick also was adamant that the story should work in terms of myth. "He never referred to the film as 'A.I.'; he always called it 'Pinocchio.'"
"He decided to make this film because he wanted people to shift to a more positive view of A.I., he was quite open to me about that. Kubrick was fascinated by artificial intelligence and fond of robots, which he regarded as a more environmentally adaptable form of human being. He said, 'I think of them as I'd like to think of my great-grandchildren.' And he's very fond of his grandchildren."
It was the relationship between David and his mother that most occupied Kubrick and Sara Maitland. An alcoholic whose 'Bloody Mary' cocktails David would mix for her in a vain attempt to win her affection. The mother was the to be emotional center of the film that would eventually come full-circle.
At the story's conclusion, the robots that have inherited the Earth use David's memories to reconstruct, in virtual form, the apartment where he had lived with his parents. Because his memories are subjective, the mother is much more vividly realized than the father, and his stepsister's room is not there at all; it is just a hole in the wall.
For Ms. Maitland, the film would end with David preparing a Bloody Mary for his mother, the juice a brighter red than in real life: "He hears her voice, and that's it. We don't see him turn to see her." Kubrick, however, wanted a coda in which the new race of robots, because of a technological limitation, cannot keep the mother alive after reviving her. The movie would end with David in his mother's bedroom, watching her slowly disappear.
Ms. Maitland was displeased this scenario, and was furious with Kubrick for insisting on it. "It must have been a very strong visual thing for him," she says, "because he wasn't usually stupid about story. He hired me because I knew about fairy stories, but would not listen when I told him, 'You can have a failed quest, but you can't have an achieved quest and no reward.' "