
Lesson Description
The "Register Your First Tool" 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 uses the @modelcontextprotocol/sdk library to register an "add" tool for the MCP server. The add tool will receive two numbers as input and return their sum as a string.
Transcript from the "Register Your First Tool" Lesson
[00:00:00]
>> Brian Holt: First thing here is we're going to create a server. So equals new Mcp Server name: "add server". You can call whatever you want and you have to give it a version. This is just telling it like, hey, I have version 1.0.1. So if you have cached internally 1.0.0, please invalidate your cache and repull it.
[00:00:31]
Okay, so now we have this MCP server. We're gonna say server.registerTool. So add. There we go. And then we're going to give it a title. So this is really important because this is how the AI is going to decide do I want to use this tool or not.
[00:00:59]
So we're going to call this the addition tool. We're going to give it a description, add two numbers together, and then we're going to give it an input schema. Input schema. And then here's where we're going to use Zod. So we're going to say A is going to be Z number and B is going to be a Z number.
[00:01:35]
So we're building an MCP server that all it does is it takes in two numbers, adds them together, returns them back to the LLM. Okay, so after that you're going to give it a function async. It's going to get an A and B. So this is the object that it's going to get from the LLM.
[00:01:58]
It's gonna get a and b. That's going to be function and it's going to return content, type text, text colon string A plus B. I guess we probably could have given a number back, but everything's going to be put into a string anyway. So I put it into a string.
[00:02:49]
So essentially your MCP server can expose many tools. We're exposing one tool here, the add tool. We've given the title of an addition tool and the description is that it adds two numbers together. So that's going to give the hint to the LLM is like, hey, if the user is asking you to add things, I'm a tool that does exactly what that user wants you to do.
[00:03:11]
It says that if you want to use this tool, give me A, give me B and then run this function by passing A and B into it. I'll do some sort of deterministic code based thing and then I'll give you back an answer. This is the power of the MCP server is adding two numbers together.
[00:03:29]
No, I'm just kidding. The power of MCP is that you can define inputs into a system and then you can deterministically Accomplish a thing and then give it back to the LLM. Because that's the problem with LLMs is that they are just wildly inconsistent. Even if you give a ton of prompting and say, if a user asks to do this, do this, this, this, this and this, and then return this, sometimes it's just like, well, I know better than what this user is asking for, therefore I'm not going to do it.
[00:04:01]
My favorite example of this is we make a MCP server for databases, right. And we used to give the database, was here, just run whatever SQL you feel like against the database. And so one of my favorite things is like, hey, put this information into the database. And what the agent decided to do is, this doesn't fit in your database.
[00:04:25]
I'm going to drop your database and recreate it. And so it just dropped everything, recreated everything from scratch and dropped all the data with it. But it didn't care because that was outside of its prompting, right? Made total sense to this agent. It's like, yeah, you don't want this database anymore.
[00:04:40]
You want a database where this fits, right? So now we just don't give it the capability of doing that. We say, here's a tool for adding things to the database, here's a tool for migrating databases. And by the way, there's no tool for dropping database. Stop asking me to drop databases, right?
[00:04:55]
>> Speaker 2: So it knows to use this tool based on the description.
>> Brian Holt: Yep.
>> Speaker 2: How much context should you put in that description?
>> Brian Holt: That's a good question. I don't necessarily think I have a great answer for you. The real answer to your question is enough. In this particular case, putting anything else here would just be superfluous.
[00:05:22]
You wouldn't want any much more than this. So you don't want to put too much context there because then you start getting the LLM to hallucinate ideas like, you use this word to real numbers, right? And then it's going to start really hinging on this word real and going off in weird directions.
[00:05:42]
You didn't really anticipate it for. Despite the fact that it is a real number, right? But it's superfluous in that context. Whereas the prompts for, I mean, I wonder if I can bring up the neon MCP server, GitHub. Yeah, some of these would be really good. An optional id, like, you can see that they're not terribly long here.
[00:06:08]
These are ones that are actually describing parameters. They're not describing tools. But you can see some of these get a little bit longer, but you can see like they're not paragraphs. Right. They're several sentences. And it's enough to get the LLM familiar with what you want from it or what you want it to do with it, and not anything more than that, because it usually causes more problems than it solves.
[00:06:33]
Is that dodgy enough of an answer for you? Okay.
>> Speaker 2: That works, yeah, thanks.
>> Brian Holt: Cool, definitely check out this one. This one's a very, like I would venture to say it's one of the best made ones. We spent a lot of time trying to get this. I was talking to someone at Microsoft the other day.
[00:06:51]
They actually say that they use ours as an example to all their customers, as well as how to make and ship an MCP server.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops