7 Applying Model Context Protocol (MCP)
This chapter covers
- Using tools from a predefined MCP Server
- Creating a custom MCP Server
- Optimizing an MCP Server
- Enabling MCP’s STDIO and HTTP+SSE transports
In the movie Wreck it Ralph, the character Fix-It Felix, Jr. carries an incredibly powerful hammer. With this one tool, Fix-It Felix, Jr. is able to repair anything that’s broken, by just hitting it once. Got a cracked wall? Hit it with a hammer. Bent streetlights? Hit it with a hammer. Broken window? Defying all logic and everything you know about glass and hammers, hit it with a hammer and it will be fixed. Fix-It Felix, Jr.'s hammer is rivaled only by Thor’s Mjolnir as the most amazing hammer that has ever been wielded.
But in the real world, real hammers can’t be used to fix everything. A hammer is the perfect tool for driving nails, but I don’t recommend using it to fix a window. In the real world, there are many tools, each with its own best use case. And while Fix-It Felix, Jr. always had his hammer at the ready in his hand, it’s more convenient to keep a set of many tools in a toolbox that you can carry around to where it’s needed.
In the previous chapter, you saw how employ tools in a Spring AI application, coding them as part of the application itself. In this chapter, we’ll see how to apply Model Context Protocol (MCP), a way to collect sets of associated tools so that they can be shared and used in any AI application that may make use of them.