YouTube Transcript:
Build Anything With a CUSTOM MCP Server - Python Tutorial
Skip watching entire videos - get the full transcript, search for keywords, and copy with one click.
Share:
Video Transcript
Available languages:
View:
in this video I'm going to show you how
to build a custom MCP server in Python
and connect that to an AI agent this is
going to be very beginner friendly but
it's going to illustrate what an MCP
server actually is and how you make your
own and how you can extend this to do
some really cool stuff now excuse the
shirt i don't usually wear a tank top
when I'm recording videos but it is
extremely hot here so please don't get
too distracted and let's get into the
video all right so first things first if
you don't understand MCP servers yet I
made a video on them that's been doing
really well in the past few days
explaining exactly what they are there's
some diagrams i show you how to connect
it to AI code editors for example like
cursor but the quick gist is that an MCP
server just provides various tools or
resources to something like an AI agent
so that it can utilize additional
context or it can call an API or it can
save a node or connect to a database it
can do things outside of its current
abilities so here I'm going to show you
how to make your own MCP server so that
you can write your own tools that you
provide to AI agents it could be other
AI agents or your own AI agents it
doesn't really matter and MCP servers
are just kind of the new fancy term for
essentially how LLMs and AI tools
communicate with each other okay so
let's get into it here i'll link this
video in the description in case you
want more information now we're going to
be using the MCP Python SDK which makes
this very easy to do and there are a few
setup steps so we're going to be using
UV so first thing you need to do if you
don't have this installed is install UV
this is a package manager that's just
significantly better than pip i'll have
a video coming out about this in a few
days that will talk about it more in
depth but anyways if you're on Windows
you're going to want to copy this first
command into PowerShell it will install
UV for you and if you're on Mac or Linux
you can copy any of these commands and
again install UV now you can install
this using Pi or PIP but I don't
recommend that you do that because it's
going to make it a lot more complicated
later in the video okay beyond that I am
going to suggest that you download
Claude Desktop this is free and it's
going to be one of the easiest ways to
test our MCP server so please just
download this cuz I'm going to show you
how to add your MCP server to Claude
Desktop so you can actually see an LLM
interacting with it and using those
various tools okay so once we have UV
installed we're just going to follow the
instructions here from the readme of the
uh MCP Python SDK and what we need to do
is initialize a new UV project so I'm
going to put this on the right hand side
of my screen and I'm going to open up
the code editor that I'll be using for
this video which is PyCharm now you may
be wondering why am I using PyCharm this
really is just the best IDE to be using
it has tons of tools built in makes it
really easy to work with Python code
view the documentation especially if
you're working with things like servers
or APIs it has a built-in network tabs
you can directly test them there and a
ton of other great features i also do
have a long-term partnership with
PyCharm where I'm able to give you
access to a 3month free extended trial
of their PyCharm Pro subscription you
can get access to that with the details
that are in the description check it out
see if you like it and obviously for
this video you can use anything that you
want but I do recommend PyCharm but the
first step is to just open some kind of
folder in whatever code editor you want
to use okay so I'm using PyCharm and
I've opened a folder called MCP server
tutorial to open that you typically are
going to go to the file menu you're
going to go to open you can make a new
folder or you can open an existing one
and I suggest you just make a new folder
and then open that okay so from here I'm
going to open up my terminal in my
terminal I'm in the current directory
where I want to initialize this project
and because I've already installed UV
and let me just quickly zoom in here so
you guys can actually see this i'm going
to type UV init and then dot when I do
the dot this means initialize a new UV
project in the current directory okay so
we have UVIT dot so that's kind of what
this wanted us to do here and next we
need to install the dependencies and the
dependency is going to be MCP and then
CLI so we're simply going to go here and
say UV add MCP CLI okay that's going to
install it instantly using UV and if we
look here at our directory we should see
a few files we have a uh venv this is
our virtual environment we have main.py
we have this readme we have all of these
things all right so that's installed now
the next step here is going to be to
open this main.py file if you don't have
the file again you can create one uh
don't worry about the interpreter i'm
going to fix that here in 1 second and
then we're going to start writing a
simple MCP server we're going to test it
and then we will adapt this to be
something a little bit more custom all
right all right so to get started here
we can literally just copy the code from
the quick start guide here again I'm
going to link this GitHub in the
description and you can see here that
what we do is we say from
mcp.server.fastmcp we have fastmcp we
create an MCP server we give this a name
in this case it's called demo then we
create a tool and then we have a
resource now I'm going to talk about
what these are in one second but this is
literally all you need in order to get
started with the MCP server and the next
step is to actually install this or use
it from some LLM or some AI tool so
again I literally just copied this from
the quick start but I'm going to show
you now how we install this into Claw
Desktop so we can start testing it and
then we'll change this to a more custom
example now look we have a tool okay we
have a tool that allows us to add two
numbers obviously that's pretty naive
and then we have a greeting okay or a
resource and we can greet someone based
on their name so in order to install
this we can do it manually or we can use
the MCP CLI that we just installed so
I'm going to open up my terminal again
make sure you're in the same directory
of your UV project and what you can type
is uvun and then mcp install and then
the name of your Python file and in this
case it's not server.py it's main.py
because that's what I named the file
okay so I'm going to go with
main.py now when I do that what it
should do is add this configuration to
our Claude desktop i'm going to show you
how we get that configuration and then
how we check if this is working okay so
now that we've done that we're going to
open up Claude now obviously that step
only works if you have Claude desktop
installed you don't need to use Claude
desktop but this is a good demo for this
video so we're going to open this up
here and what we can do if we want to
check the configuration is we can go
here to the three kind of uh what do you
call it bars or the menu we can go to
file and we can go to settings i'll have
my editor zoom in here so you guys can
see this from settings we're going to go
to developer and we're going to go here
to edit config okay now from edit config
it's likely going to open up some
directory for you what you're going to
want to do is open up this claw desktop
configuration file okay so I'm going to
open this up just in Sublime Text but
you can open this in anything that you
want and you're going to see that you
have some command and then you have some
arguments now what this is saying is hey
we're going to use UV the thing that you
just installed we're going to run it
with this MCP CLI and then we're going
to run the command MCP run and then this
server right here now once that's
running within Claude we should actually
be able to see this however there's some
common errors that can occur that I'm
going to show you how to get over so
really this configuration is added and
we should be able to see the MCP server
but the MCP server should show up right
here and we're not seeing it right now
now I ran into a bunch of issues with
this when I was testing and I found a
few potential fixes the first fix is
just to shut down claw desktop
completely so force quit it from your
task manager or your process list and
then reopen it so let's try that and see
if it works if it doesn't I'm going to
show you the alternative solution so I
just opened my task manager here and I'm
just going to shut down Claude desktop
okay so I'm going to end the task and
then I'm just going to wait a second and
I'm going to reopen it okay so we're
going to reopen Claude now we're hoping
that once this loads it's going to spin
up our MCP server and it looks like it
actually did so you see here now that we
have a hammer and that we have I guess
this kind of like plug here where we can
attach something from the MCP now that's
not going to work right now but we can
go to available MCP tools and we can see
that we have this add tool from the
server demo and it says it adds two
numbers so now what I can do is I can
use say something like can you add 1 + 7
and it should go ahead and use this MCP
tool this one right here that's
available okay let's see if it does that
you can see that it says running ad from
demo and we're going to say yes we will
allow this for this chat it's using the
parameter A and B and then it gets the
result which is eight okay this is a
very very simple demo of using the MCP
server but the point is if it doesn't
work restart it now if that's still not
working for you another way to fix this
is to put the absolute path to where UV
is installed now the absolute path for
where UV is installed will depend on
your operating system and how you
installed it but I'm going to show you
how you can find it so you can go to
your terminal and you can type which if
you're on Mac or Linux and then UV when
you do that it should show you the
location of where this was installed and
then what you can do is put the absolute
location to this UV file now in my case
I can also type where UV and notice it's
not showing me where this exists i can
try something like where UV.exe exe and
it's still not showing it to me so what
I had to do is look up the default
installation location and I found that
the location for this was the following
i'm just going to open it up right now
okay so again we'll zoom in here but the
default location on Windows if you
install this with the PowerShell command
is going to be C/ user/ the name of your
user in my case I called mine user kind
of silly but this would be like Tim or
Joe or like whatever your current user
is on Windows and then dot /local and
then /bin inside of here you have the UV
directory so what you could do if this
command wasn't working is you would
change the configuration here to be the
following you would put your directory
you would escape these with two
backslashes because that's just how it
works here in Windows and then you would
do
/uv.exe so you're pointing directly to
this UV executable so that you can
actually use it again same thing on Mac
you put the absolute location to the UV
executable file it won't be.exe it will
probably just be UV or UV.sh SH or
something along those lines now clearly
I don't need that because this is
working so I will just leave it as UV
and you could add other servers here as
well using the same configuration that
you see like this where essentially what
happens is once Claude boots up it runs
this command runs the server it's then
able to connect to this using the
standard input output protocol we don't
need to talk about that in too much
depth and utilize your tools okay so now
that this is working we are going to
shut down claude again and we're going
to go into our editor and we're actually
going to write a useful MCP server and
to do that we're quickly going to cover
some of the important concepts related
to MCP servers that are here on this
page so what we can do with MCP servers
is we can expose data through resources
so think of like a get endpoint like a
HTTP get endpoint where you're exposing
like a database or some context or a
memory or whatever like just anything
that is essentially retrieving data okay
that's a resource that you can expose
via MCP another thing that you can do is
provide a tool now a tool is something
that typically does something maybe it
goes and scrapes the web or it adds a
new user it performs some operation and
again that's something that you know
these AI agents like to use a lot and
that you can provide with an MCP server
you also can provide a prompt so this is
a reusable template for interacting with
LLMs and I'm going to show you all of
those in this video and how they connect
with Claude so you can see for a
resource you do something like at
mcp.resource okay for a tool you do
mcp.tool and for a prompt you do
atmc.prompt you also can connect images
and context i'm not going to show that
right now uh but that is something that
you can't do okay cool so let's get out
of that let's go back into our editor
and let me show you a real tool that
might actually be useful or at least
demonstrates how you write this out all
right so I just cleared my file here so
we just have some basic code so we're
kind of just starting from scratch and
what I'm going to do is I'm just going
to call this AI sticky notes okay and
the idea is going to be that we're going
to allow the uh AI to kind of take some
notes and store that in some external
storage in this case we'll just use like
a notepad file and this will show us how
we can kind of like read and write and
get resources and utilize tools uh it'll
be interesting enough to at least give
you like the kind of starting template
for building your own MCP so what we're
going to do is we're going to go up here
and we're going to say import OS cuz
we're going to use this in 1 second and
we're going to define a variable called
notes file and we're going to make this
equal to just like
notes.txt okay you know this is going to
be where we are storing the notes that
the MCP server will actually have access
to now we're going to make a function
we're just going to say insure file and
all this is going to do is just make
sure that this file exists before we
start trying to use it so we're going to
say if not
os.path.exists and then the notes file
you can see my autocomplete is taking
over here then what I'm going to do is
I'm going to say with open notes uh file
wf i'll explain this in one second then
I'm going to say f.right Right and I'm
just going to write an empty uh line in
here like an empty string okay you don't
really need to do this but this is what
we're going to do so we're going to say
if not os.path.exists what this is doing
is saying all right does this file exist
if it does not exist then we're going to
use this fancy Python syntax which
essentially will open up a file for us
and make sure there's no errors doing
that this is going to create a new file
because we did this in W mode this means
it will create and override any file
that currently exists and then we just
write nothing into it just so that we
have something happening inside of this
width statement okay so we're going to
run that before we do any operation on
our file just to make sure the file is
indeed there now we're going to make an
MCP tool now to make an MCP tool is
super straightforward all you have to do
is type at MCP tool and then make sure
you close your parenthesis here and this
is what's called a decorator in Python
it's something that essentially modifies
a function you don't need to worry too
much about what that means then what we
can do is we can give the tool a name
and we do that by defining a function
with the name of the tool so in this
case we say define add note okay and now
we've just made a tool called add notes
that our LLM or that our AI agent would
be able to call now for this function
we're going to specify the parameters or
the values that the AI agent would pass
to it so for adding a note we probably
have like a message that we want the AI
agent to pass so we can specify as many
parameters as we want but we need to
make sure that we specify a type for
those parameters so the AI agent knows
how to pass them so we're going to say
message and then colon string and then
we're also going to specify the return
type of this function by using a type
hint in Python so we're going to use a
dash and then the greater than sign and
then string okay now what this
effectively says is hey we have this
function we take in some message this
will be given by the LLM or by the AI
agent it's going to return some string
and then inside of here we can start
writing out the actual function so the
first thing we're going to do is we're
going to say insure file so we're going
to make sure the file does exist then
we're going to say with open and same
thing we're going to open the notes file
we're going to open this in a mode which
stands for append mode which means we're
not going to override it but we're going
to write to the end and then we're going
to say f.right write and we're going to
write the message plus a new line
character this means we will go down to
the next line in the file after we write
the message and then we're just going to
return a string that says note saved
okay so just give the LLM some output
that this actually was successful now at
this point this tool is done if we go
back to Claude and we restart it we'll
be able to start using this tool and
adding a new note but there's one more
step that you don't want to forget now
that step is adding what's called a dock
string okay and you can see in PyCharm
here it automatically populated it again
another advantage of using something
like PyCharm now a dock string is
essentially a comment so you can see
that I've done three quotation marks
here that you put at the top of a
function that describes what the
function does the parameters that it
takes and the return type or what that
return value actually is now this is
super important because the MCP server
will be able to read this and see what
this tool actually does and if it should
be using it so sure you can give it a
descriptive name like add notes but if
you want to be more specific you put
that in the dock string and this will
really dictate how well your tools work
and how often they get called so I
actually have a dock string that I'm
just going to copy in here it doesn't
follow this exact format uh but that's
fine and you can see that it says append
a new note to a sticky note file
arguments message string this note uh
the note content to be added and it says
returns a string confirmation message
indicating the note was saved okay so
now we've documented this tool so that
the AI actually knows when to use it so
let's try to test this out now before we
move on to the next step so we're going
to open up Claude and I just want to go
back here to new chat i want to look
here and see what we have and it still
has ad right it's still running that old
MCP so what we need to do again is we
need to shut this down so we're going to
go to task manager we're going to search
for claude okay we're going to end the
task and then we're just going to wait a
second reboot it and it should now run
our new MCP server and that's because
it's kind of running in the background
so here now if we look at this you can
see that we have add note so for add
note I'm going to say can you add a
note saying Tim is the best okay and
let's see what it says
and it says I'll add that note for you
and then notice it's asking us to use
this tool so I'm going to allow that for
the chat this is the message okay I
saved the note and then we can see kind
of the result here it said note saved
and then if we come here to PyCharm we
actually don't see the notes.txt file
and the reason for that is that it's
saving relative to the location where
the UV command is running or at least
where uh the MCP or sorry Claude is
running our MCP server so that's
actually something good we need to
hardcode the path where we want to save
notes.txt because right now it's kind of
saving in like a random location that we
don't have access to which I just
realized when we were running this so
what I'm going to do is quickly change
this so it's going to be relative to
where this current file is so it saves
it in this directory so we'll be able to
access it later okay so I just adjusted
this here this should now make it so
that it's always going to save it in the
same directory uh where this current
main.py file is so we did os.path.join
we got the directory name of this file
so underscore
file and then we have
notes.txt now I'm not sure if that's
going to work right now we could close
this and rerun it but let's move on to
the next function then we can test it
out after that all right so I'm going to
do this one a little bit faster i'm
going to say mcp.tool and I want this
one to be one that can read a file so
I'm going to say define read notes okay
this is going to return a string i'm
just going to copy in the dock string
i'll put all this code in the
description i just want to save us a
little bit of time here then I'm going
to say with and I'm just going to go
with my autocomplete because now it's so
damn good with notes file we're going to
open this in R mode which stands for
reading so it's not going to modify the
file we're going to say content is equal
to F read and then dot strip this is
just going to remove any new line
characters or white spaces at the end
then we're going to say return content
or we're going to say no content or no
notes yet okay so that if we don't have
any content then we'll just return no
notes yet instead okay so now we have an
add note and a read note and I want to
move on and add a resource now to add
the resource we're going to say at MCP
and this time we're going to say
resource now the resource is different
it's not a tool it's more like just
reading information so in this case we
could do something like getting the most
recent note so to do that we're going to
call this notes colon slash and then
latest okay now there's ways to embed
dynamic variables inside of your
resource for example if I go back here
let me just bring this up uh and we look
at the resource let me find where the
resource is we should be able to see
here that you can do something like this
so you can say like users user
ID/profile and then you can see that you
can put a user ID as a parameter we
don't need to do that for our notes but
that is something you can do with the
resource wanted to mention that but here
we're just saying okay you can get the
notes latest resource that's how we're
referring to it then we're going to say
get latest note this is going to return
a string and for the latest note again
we're going to ensure the file we can
actually just copy this cuz it's going
to be the same thing and this time we're
going to get the read
lines okay we're going to call this
lines and then what we're going to do is
return lines
one strip if lines else we're going to
say no notes yet okay so what this is
saying is all right we're going to get
the most recent line or the last line
we're going to strip off any of the new
line or whitespace characters then we're
only going to do that if lines so if
this list actually has at least one
value so we don't get an index error
otherwise we're just going to say no
notes yet okay so that's a resource and
then lastly I'm going to bring in a
prompt so I'm going to say atmc.prompt
prompt for the prompt we're going to say
define and then this is going to be note
summary_prompt okay if we can spell this
correctly and then this is going to be
of type string and then inside of here
what we're going to do is we're actually
just going to give a prompt that will
summarize all of the notes that
currently exist because that's something
that we might want to do so I'm going to
say with open uh notes file r uh as f
I'm going to say content equals and
again f read.strip we should really put
that into a function because it's very
repetitive but for now that's fine we're
going to say if not content then we're
going to say return there are no notes
yet okay otherwise we can return an
fstring and this is going to say
summarize the current notes and then
inside of here we are going to put the
content okay so this is just a reusable
prompt uh that again we can pull in from
the MCP server now for both of these
again I'm just going to copy in from my
other monitor here a few of the doc
strings to make them a bit clear so
generate a prompt asking the AI to
summarize all the current notes okay and
then for the latest note same thing now
I know this example is quite simple but
you can imagine that if you had an
advanced prompt with multiple values and
variables and things that you could just
put that in this function and then you
can just keep reusing that prompt over
and over and over again and it can grab
external data if it needs to same thing
for the resource again quite simple but
if you had a resource like a large
database you want to run a SQL query all
the time or something you could put that
into a resource and then your AI agent
can continue to use that similarly to a
tool okay so that is our code for our
MCP server same thing again we need to
close down claude so let's close this
down i need to get this thing here and
press end task and then let's rerun it
and test this out and make sure that
it's all working okay so we can see now
we have two tools available add note and
read notes and we have this connection
and you can see that we can get the note
summary prompt or we can get notes
latest for example and we can bring that
in as kind of extra context to our
prompt so I can say you know what are
the notes or something and let's see
what it says says based on the
information there are no notes yet okay
totally fine and you can see here now in
PyCharm this notes file got created so
let's make a few notes so let's go make
10 notes on monkeys okay and let's see
if it uses our MCP server to do that so
let's go allow for this chat and it has
a message it's using the add note
function and it's just going to keep
doing this and you can see behind me
notes are getting populated in this
notes.txt file so let's wait for that to
finish and then we can use the other
tools all right so that is done now
let's try to use maybe our prompt so
let's go to uh here and let's go the
note summary prompt and let's just go
enter here and let's just use this
prompt and see I've shared document
called note summary prompt contain 10
monkey notes I just created for you
appears to asking for a summary of these
notes would you like me to create a
concise summary and I'm going to say yes
okay so run this prompt i'll summarize
the notes about the monkeys for you and
then you can see that it's running add
note okay that's interesting that's not
what I wanted it to do but what it did
is it actually added a note to my notes
file that contained the summary so
monkey summary uh interesting kind of
proves that I guess you know you need to
be a little bit more specific here but
let's say can you get the latest note uh
and let's see if it will do that for us
okay I'll retrieve the latest note it's
getting read note okay let's do this
and let's see okay it gets the notes and
it says monkey summary this is the
latest note blah blah blah okay you guys
get the idea you can mess around with
this obviously you don't need me just to
play with this you know stupid example
um in this video but the point of this
was to show you how to make a custom MCP
server in Python and hopefully not over
complicate the process in my opinion
this is so damn cool because the types
of things you can do with this are
really just endless like even right now
when I'm running this I'm imagining all
of the times in the past where I've
needed to like manually create fake data
and add that to my database now I could
have an MCP server i could connect that
to Claude i could say "Claude make 100
fake users for me." And it can just do
that in my database i don't need to like
manually copy and paste them or run some
custom script i just connect it to a
tool that goes and does whatever I need
now that's obviously a simple example
you can get crazy with this but think
about how cool these MCP servers
actually are and really what you can do
if you have a little bit of imagination
so I hope you guys enjoyed the video if
you did make sure to leave a like
subscribe to the channel and I will see
you in the next one
[Music]
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.
Works with YouTube, Coursera, Udemy and more educational platforms
Get Instant Transcripts: Just Edit the Domain in Your Address Bar!
YouTube
←
→
↻
https://www.youtube.com/watch?v=UF8uR6Z6KLc
YoutubeToText
←
→
↻
https://youtubetotext.net/watch?v=UF8uR6Z6KLc