Enterprise Java with Spring Boot

Model Context Protocol

Josh Long
Broadcom
Enterprise Java with Spring Boot

Lesson Description

The "Model Context Protocol" Lesson is part of the full, Enterprise Java with Spring Boot course featured in this preview video. Here's what you'd learn in this lesson:

Josh extracts the scheduler tool from the application and puts it into an MCP server. The application is then refactored to use the MPC server for its tool calling.

Preview
Close

Transcript from the "Model Context Protocol" Lesson

[00:00:00]
>> Josh Long: Okay, this is a front end master's course. So what does this have to do with the front end? Well, like I said, this is all in the JVM right here, right? My tool calling is another function, another module, another object, another thing in the same jvm. But like I said earlier, the front end today is AI models.

[00:00:18]
It's chat clients, right? It's a chat assistance, it's assistants in general. And they can do increasingly powerful things. Back in November, Anthropic, the makers of Claude Claude Desktop, C L A U D E, they announced this new thing called MCP model Context protocol. And MCP is a protocol you can use to give CLAUDE access to your tools on your local machine.

[00:00:46]
It's a network protocol. It's a protocol. Rather the first version wasn't a network protocol so much as it was a standard in, standard out protocol protocol. But the point is two separate processes on the same host could now talk to each other, right? So you have Cloud desktop.

[00:01:00]
It's like ChatGPT except it's a. And ChatGPT has a ChatGPT.app, which is your Mac OS native binary that wraps the the web view for ChatGPT. There's also ChatGPT.exe. Well, the same thing for Claude. There's Claude.app and Claude.exe, right? And they both, I imagine one assumes they just wrap the webview and make it so that you can install them in your local operating systems.

[00:01:23]
But Claude Desktop ships with a new feature where you can configure a JSON config file and list enumerate the paths of binaries that if Claude Desktop wants to, it can invoke to get some responses. So what protocol do these binaries speak? Like I said, they speak this MCP protocol.

[00:01:44]
Well, I mean, that's awesome, right? There's a protocol you can actually talk to things. So people have done thousands. There's thousands of MCP services out there. This thing is taken off like wildfire, right? There's actually directories you can go to that just list really cool MCP services. There's actually, I saw this the other week, a directory of directories of other MCP services that you can download and use.

[00:02:06]
Every big vendor, every cloud, hyperscaler, or almost all of them at least have MCP services now, right? Zapier just announced a cloud like a MCP service. Everybody has an MCP service now and eventually that host to host. Sorry, in host intro host process to process standard IO Standard out based protocol evolved to also support HTTP.

[00:02:31]
This is where it gets really interesting for us because it's a front-end master thing. So now you can build a service speaking MCP on top of just regular Spring right on top of the web stack. And you can do McP with standard IO as well. We on the Spring AI team really love this protocol.

[00:02:47]
In the weeks that followed the announcement of it, we built a Java SDK implementation of the protocol. We did it so early that in fact Anthropic reached out to us and that we donated. We ended up donating the core implementation from Spring AI support for MCP to the Anthropic Model Context Protocol IO foundation thing, right?

[00:03:07]
Their effort, their open-source thing, right. So the core Java SDK is code that we wrote. And so if you want to write MCP services using the jvm. And why wouldn't you? Well, you're using our code anyway, even if you're not using Spring AI on top of it, right?

[00:03:23]
So that's what we're going to do. We're going to turn this admittedly bit of a trivial thing into an MCP service. I've got the scheduler here. I'm going to stand up a new MCP service. We'll go over here, start Spring IO and I'm going to call this Scheduler.

[00:03:39]
I'm going to say MCP Server Web Enter UAO Server Scheduler rather. And I'm going to run this in port 8081 because I need a port 8081 and I'll say Scheduler application. I'm going to cut and paste this proprietary patent pending business logic for my adoption service over here to my Scheduler thing.

[00:04:08]
And I need to tell the auto configuration here in my MCP server to export that tool. So I'll just do that with this auto config. I'll say tool objects and I'll inject the old, dogged option scheduler, okay. Okay, so let me start that up. I don't know. Yeah, that'll be fine.

[00:04:30]
So that'll start up on tomcat on port 8081. There it is right there, port 8081. So now we go back over here and now that I've left a little dog adoption scheduler shape hole in my code. I got to replace it and tell it to instead get that functionality from the service that we just stood up instead of from the local Java object.

[00:04:48]
So I'll do that by configuring an MCP client. Okay, so Bean mcp sync client, mcp sync client return mcp client.sync. and then it's a. This one changed recently, so I have to like, remember, okay. Yeah, localhost8081.build, .build. Okay and actually technically you're supposed to do this. Okay, so restart and I need to now tell Spring's chat client to then talk to that thing.

[00:05:30]
So I'll inject that here. McP sync client. McP sync client. Put that here and new sync that that Restart. So I had to add this one object that wrapped the thing I passed in. I had to define this one bean, kind of like a data source, telling it to source the MCP tools from that service.

[00:05:53]
And I could have more than one. In this case, I've just got the one scheduler. But you could have thousands, right? Okay, so whoops, it's not going to know what to do. Control C. Do you have any direct dogs? Okay, great. We have Prancer. Okay, when can I adopt Prancer from the Minneapolis location?

[00:06:17]
April 26th. So this is the adoption service. Nothing on the console there. Here's the scheduler service. The model talked to the scheduler via mcp, right. And it got the response back and it incorporated that response. You could write these MCP services in all sorts of other languages. Just find me one that'll be as productive and efficient and wide scoping and fast and scalable as the one in Java.

[00:06:42]
It's amazing. This is an amazing time. Now, when I say that there are thousands of MCP services, I might be under exaggerating. What's the opposite of exaggerating? I might be underselling it. There might be tens of thousands at this point. There are countless. I mean, I saw one again, you have to be careful because there's mcphtp, MCP standard in, standard out.

[00:07:02]
There's an MCP service that does like, it uses blender, the 3D tool. So you go to Claude desktop, you point it to one of these standard, standard in, standard out, MCP services and say render a scene of 1920s Parisian Cafe. Parisian cafe. And it'll just do it, right?

[00:07:19]
I mean people are. It's like Star Trek computer, make me a cup of coffee. I mean, people are doing amazing things with this now. And the interface for all this is just a chat. A chat box, right? So if you can build your. If you can export your business logic, your organization's functionality in terms of these MCP services, you can do all sorts of amazing things.

[00:07:38]
I showed you spring batch yesterday. We built an MCP service that allows you to ask questions about the state of your spring batch jobs. How many jobs are running, what state are they in? Right, just really amazing things. Cloud hyperscalers are letting you create and manipulate stuff. GitHub, they released an MCP service just recently, the MCP service.

[00:07:58]
I built a demo on my stream, my live stream last week with Microsoft's vice president of developer advocacy or whatever, Patrick Shannazol. In that we talked about how to use Spring AI to talk to the MCP service to ask questions and or mutate state on my GitHub account.

[00:08:15]
Like I could commit stuff via text. I could say, hey, add a file, add a readme that analyzes the source code of this Git repository and it would do it. We live in an amazing time. An amazing time. And of course, this is all AI. And guess what this is doing?

[00:08:31]
It's just doing network I O. And guess what? You don't want to do network I O without virtual threads.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now