Complete Intro to MCP

Call the MCP with JSON RPC

Brian Holt
Databricks
Complete Intro to MCP

Lesson Description

The "Call the MCP with JSON RPC" Lesson is part of the full, Complete Intro to MCP course featured in this preview video. Here's what you'd learn in this lesson:

Brian creates JSON RPC payloads to send to the MCP server. One payload asks the MCP server for a list of available tools, while another calls the add tool with two numbers as arguments.

Preview
Close

Transcript from the "Call the MCP with JSON RPC" Lesson

[00:00:00]
>> Brian Holt: Okay, we are almost done with our MCP server. Let's just add our last couple lines here. Const Transport equals new standard IO server transport. Transport is just basically like how am I getting this? Like, how am I managing all the messaging out of this? We talked about there was sses, streaming HTTP and then standard IO.

[00:00:24]
We're using that. Those are all called transports. That's what that term is in that particular case means. And we're just going to say await server, connect transport. So this is just a process that you leave running or in the case of like Claude desktop, you give it the files like to go run and then it'll go run that server for you inside of its shared processes.

[00:00:51]
In our case here, we should just be able to say nodemcp JS yeah, you probably need to tell this as a type module right here as well. So in your package JSON, if you see something like this, it's because I'm using import, right? So you have to say type module and it's running and I needed to save.

[00:01:25]
Cancel, save. There we go. Okay, so you should see something like this where it's just like, it seems like it's hanging, right? That's because the server is running and it's waiting for something to be input into it, right? So let's go give it something now. Well, you know, let's do it.

[00:01:49]
Let's make you type painful JSON by hand because it's fun. We're gonna do this once. After that I'm going to let you copy and paste. So we're gonna echo something into the standard IO into the input, right? We're going to pipe that into our node server and then we're going to see what it says.

[00:02:08]
Echo. So if you're not familiar with echo, it just outputs something and then we're going to pipe that into it. So if I say echo hi, it's just going to say hi echo, single quote. Make sure adamant here that we're single quote, open and curly brace, jsonrpc:. We'll say what kind of JSON RPC we're doing.

[00:02:34]
MCP is all based on JSON RPC 2.0. We will talk in just a second about what that is. Okay. And we're going to say ID one because you have to identify what client you are or what command you are. In any case, McP requires this ID here, something that you'll never manage yourself.

[00:03:00]
All your client and your server will kinda handshake those things together. Method. If you're not Familiar with the RPC, all RPCs look like this Tools list. Okay, we'll say params. Another opening curly brace here, name add and then arguments and then opening curly brace. Closing, closing. Single quote.

[00:03:48]
Hopefully you got it. Let's just make sure that I got it there. I did. Okay, if you're not familiar with JQ or if you don't have it installed, does it come installed on Mac or do you have to install it? I don't remember. If you have Brew, you can just say brew, install jq.

[00:04:04]
It'll work. It's like a JSON query language. It's a really handy binary for working with JSON in the command line. As you can see here. It just pretty prints it for me. Okay, so that's correct. So I'm gonna take that same command here and then I'm gonna say node mcp.js, this is just a pipe.

[00:04:33]
This is saying take this and then insert that as the input into this process. You'll get some amount of results there. If you want it to as well, you can just pipe that into JQ as well, and you can get this nice pretty printed thing. Essentially I called the Tools list, which is basically saying like, tell me everything that you know about this particular function and it'll tell you this is the name, this is the addition.

[00:05:08]
It adds two numbers together, all these things. Here's the required. This is all the information that your MCP server would pass back to a client and basically how they handshake it like, here's the tools that are available, right? Cuz imagine Claude Desktop is starting up. It's going to call this MCP Server, and the MCP server is going to say, here's everything that I can do for you.

[00:05:30]
Here's all the text that you need to ingest to know that I have an addition tool, I have a subtraction tool. This is what it responds with here. Pretty cool. Most of this wiring up is all done by this model Context Protocol SDK, which is to say that we could have written all of this.

[00:05:51]
It just wouldn't have been very fun nor useful to anybody here, right? But this results call all that kind of stuff, like keeping track of all your tools and all that kind of stuff is what this package does for you. Okay, so it's listing stuff now. Let's actually make it add, right?

[00:06:13]
We've come so far. What is two plus three? I don't know. Let's ask. There's no other way I could figure this out. So we're going to come back to the same call. It looks really similar. Instead of saying Tools list, we're going to say Tools call. And we're going to give it two arguments here.

[00:06:37]
We're going to give it A two and B three. So I changed two things. Instead of Tools call, it's Tools list. Other way around, instead of Tools list, it's Tools call. And then I gave it two params. So now we should be able to call this. And through all of this machinery, all this code, we have discovered that 23 is indeed 5.

[00:07:16]
What's cool about this though is we now have this server that we can hand over to an LLM and say run this. And whenever you run into a problem shaped of this nature that this tool looks like it can solve, pass in all of your parameters that the user has given you or that you have invented yourself.

[00:07:35]
And then I deterministically, using software, will discover the answer for you and then hand it back to you. The addition was just a good way for us to get started here. So let's talk for just a hot second about JSON rpc because I think it's useful for you to know that the language that's being spoken here because inevitably you're going to have to read some of these.

[00:08:04]
Yeah, we showed you Tools list. This is just like, hey, show me everything that you can do. There's also Tools initialize. In fact, I think we do that down here. Yeah, I'll show you Tools Initialize here in just a second or just initialize. In general, there's also like resources and prompts and all these other capabilities.

[00:08:25]
But that's what this method is. Essentially, a RP stands for Remote Protocol. No, I forgot what it says.
>> Student: Remote Procedure call.
>> Brian Holt: Remote Procedure call. Thank you. Where basically you're telling something remotely. I want you to run this function on your side. That's what the RPC parts of it stand for.

[00:08:46]
And then JSON's, obviously, and I'm going to do this with you using JSON, obviously there is an XML rpc. There's other ways of doing it as well. I mean that's really it. JSON, rpc, if you look here, has been out. So we're using 2.0 since 2009. That's longer than at least some of our careers.

[00:09:09]
Is it longer than my career? It's basically as old as my career. I think I got my first Dev job in 2009. We're reusing old technology here. So I mean, you can think of RPC as like an alternative to rest. So rest, essentially you have like a resource that exists out in the cloud and you're saying, hey, I'm going to patch this, Use this information to patch this resource.

[00:09:36]
RPC is less around resources, which is what REST cares about. RPC is more around like, hey, you do something for me using these parameters? Does that make sense? We don't really use it too much outside of like, it's really common for infrastructure like DevOps kind of stuff to use RPC for like web dev.

[00:09:59]
We don't use RPC because we're in love with like GraphQL or rest or some of these other things. That's it. That is all that JSON RPC is. There's actually nothing AI specific about this. So let's just run this command for fun. I'm not gonna make you type it, that sounds way too hard.

[00:10:31]
So this is initializing. You're telling it what version of the protocol that you're going to be using. This is MCP protocol. Has a list changed? Yes. There's a bunch of stuff you can basically ask it. Like, I have these things cached. Are these things okay to keep cached?

[00:10:48]
It tells you what it is. Then at that point you say, okay, you have an AD server. Tell me everything about the AD server. Tell me all your tools, tell me all your prompts, tell me all your resources. And this does all the handshaking and figuring out, here's everything that this MCP server purports that it can do.

[00:11:03]
Everything that we're going to do with writing MCP servers after this is just variations on this. At this point, I'm very okay if you just choose to copy and paste code, because rewriting this code over and over again is not really going to teach you too much. It's actually more just about the semantics that I care about than the actual individual pieces of code when I write MCP servers because I somewhat frequently do it for like.

[00:11:25]
Well, here's a good example. I was working with Vercel the other day. Vercel doesn't have a streamable HTTP MCP server. They have a local one, but they don't have one that works remotely. I just had my claude code write one for me and then I started using it.

[00:11:44]
I actually rarely author them myself. I just make all of my coding tools author their own tools and then use the tools.

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