Urbanization poses a major threat to biodiversity and food security, as expanding cities, especially in the Global South, increasingly compete with natural and agricultural lands. However, the impact of urban expansion on agricultural biodiversity in tropical regions is overlooked. Here we assess how urbanization affects the functional response of farmland bees, the most important pollinators for crop production. We sampled bees across three seasons in 36 conventional vegetable‐producing farms spread along an urbanization gradient in Bengaluru, an Indian megacity. We investigated how landscape and local environmental drivers affected different functional traits (sociality, nesting behavior, body size, and specialization) and functional diversity (functional dispersion) of bee communities. We found that the functional responses to urbanization were trait specific with more positive than negative effects of gray area (sealed surfaces and buildings) on species richness, functional diversity, and abundance of most functional groups. As expected, larger, solitary, cavity‐nesting, and, surprisingly, specialist bees benefited from urbanization. In contrast to temperate cities, the abundance of ground nesters increased in urban areas, presumably because larger patches of bare soil were still available beside roads and buildings. However, overall bee abundance and the abundance of social bees (85% of all bees) decreased with urbanization, threatening crop pollination. Crop diversity promotes taxonomic and functional diversity of bee communities. Locally, flower resources promote the abundance of all functional groups, and natural vegetation can maintain diverse pollinator communities throughout the year, especially during the noncropping season. However, exotic plants decrease functional diversity and bee specialization. To safeguard bees and their pollination services in urban farms, we recommend (1) preserving seminatural vegetation (hedges) around cropping fields to provide nesting opportunities for aboveground nesters, (2) promoting farm‐level crop diversification of beneficial crops (e.g., pulses, vegetables, and spices), (3) maintaining native natural vegetation along field margins, and (4) controlling and removing invasive exotic plants that disrupt native plant–pollinator interactions. Overall, our results suggest that urban agriculture can maintain functionally diverse bee communities and, if managed in a sustainable manner, be used to develop win–win solutions for biodiversity conservation of pollinators and food security in and around cities.