In order to identify the function of the Sermon on the Mount within Matthew’s larger purpose for his Gospel, we must keep one eye on Matthew’s theological message (the forest) even as we evaluate the Sermon within its immediate narrative context (the trees). In other words, Matthew has situated the Sermon within its narrative context in order to serve the overall theological message of his book—not merely as a literary bridge from the temptations of chapter 4 to the miracles of chapter 8. To that end, here is my summary of Matthew’s theological message: Jesus Christ is the promised Messiah who fulfills the Law and the Prophets in order to inaugurate the kingdom of heaven.
Pennington suggests that Matthew has situated the Sermon within his first discourse-narrative cycle, which is demarcated by an inclusio: Jesus “went ... teaching in their synagogues and proclaiming the gospel of the kingdom and healing every disease and every affliction” (4:23; 9:35 ESV). In chapters 5–7 Jesus teaches and preaches “the gospel of the kingdom” (the Sermon), and in chapters 8–9 he performs healings and calls more disciples. In other words, this discourse-narrative cycle offers us the first glimpses of Matthew’s gospel through kingdom ethic (ch. 5–7) and then kingdom example (ch. 8–9).
Thus, if Matthew’s overall theological message is Law-Prophetic fulfillment and heavenly kingdom inauguration in Jesus, the Sermon offers us both: (a) Jesus came to fulfill the Law and Prophets (5:17) by upholding (b) a righteousness greater than that of the scribes and Pharisees, which is the entrance requirement for the kingdom of heaven (5:20). The Sermon is a call to repentance and participation in Christ’s new covenant kingdom through radical dependence on God (ch. 6) for inner-outer (Pennington: “whole-person”) righteousness and the blessed ends (Pennington: “human flourishing”) that attend it.
 Jonathan T. Pennington, The Sermon on the Mount and Human Flourishing: A Theological Commentary (Grand Rapids: Baker Academic, 2017), 113–114.