The Model Context Protocol (MCP) Explained (and one cool code example.)

Underfitted · Beginner ·📐 ML Fundamentals ·1y ago

Key Takeaways

The Model Context Protocol (MCP) is a standard protocol for connecting LLMs or agents to APIs and backends, reducing complexity by allowing sharing of implementation and specifications across agents. The video explains MCP, its core concepts, and demonstrates its usage with a custom MCP server and various tools such as Slack API, Gmail API, and custom database.

Full Transcript

okay so let's talk about mCP why it's important and why do I think it's here to stay and it's going to enable us to do a bunch of stuff that are just not possible today okay so first of all uh this is the way the state-of-the-art today without mCP so forget about mCP and let's say you want uh to create an agent and you want that agent to communicate with different apis so for example you want to communicate with slack uh Gmail or just a custom database so in order for you to do that you will have to build these Integrations from scratch it's not just as simple as letting the model figure out the documentation of an API because there are for example the Gmail API might allow you to delete messages but you don't want your agent to have access to delete messages so you want to restrict your agent somehow to only uh create new messages right or maybe save drafts you don't want the agent to have access to the full API so in order to allow for that to happen you're going to have to create some custom maybe prompts or some custom implementation that will restrict your agent or guide your agent to do only the things that you want to do okay the same thing happens when you want maybe custom messages you want your agent to format messages in certain ways so that communication that specification that implementation has to exist somehow so let's say you buil that as part of your agent so now your agent knows how to use slack and Gmail and the custom database but all of the sudden that agent uh you give it away or you put it in public and people want to use the same implementation with their agents so maybe this agent here is when serve the IDE when you write code and your ID now wind serve can communicate with slack and Gmail and a database and you want people now to use that agent in cursor so what's going to happen at that point is that all of the implementation that you did custom for wind serve is not going to translate to cursor now so all of that work will have to be redone for cursor right so that is this the thing that the main um idea here of NCP mCP comes to reduce complexity because now you don't have to recreate all of those connections implementations uh you know specifications for every single agent that you want to connect to all of those apis now with NCP you can have a standard that that anyone can use to connect an llm or an agent to your back end or different API so in this particular case here I have the mCP server here in the middle and I want you to imagine that this mCP server communicates or knows how to connect to slack knows how to connect to Gmail knows how to connect to the custom database and the agent up here that might be a custom agent it might be clock desktop it might be cursor it might be I don't know wind serve whatever it is that custom agent now we'll use the protocol to talk to that mCP server and you can swap that agent you can take this same thing to wind serve and the implementation the glue the C the custom specification will reside on the mCP server so it doesn't matter what agent you use you can still make the whole thing work so even though we're adding that extra layer is not to complexity we're we're actually reducing complexity because we can actually share this so now imagine a company like slack they are now providing their mCP server so you can come to your client application let's say your IDE and you can connect to the mCP server for slack and all of the sudden now cursor you can access slack with it why because you have that specification you can connect the same mCP server to Cloud desktop or your customer custom agent or windserve it doesn't matter anymore because we're using that common specification by the way that common specification uh you can Google it up here is their website and they have here all of the Core Concepts and all of the stuff that you can do uh just as a quick uh really quick overview there are three concepts here they have the host and think of a host as let's say cursor your IDE the application that implement ments the agent right then they have clients an mCP client it's basically the structure or the the tool that's going to communicate with a host and then sorry with the with the server and then you have the server which is the one that contains the implementation that you want to access so let's say slacks mCP server or Gmail's mCP server or your own custom mCP server that's going to do some functionality it's going to implement some functionality uh some of the things that you can do in those mCP servers I'm not going to go through all of them um I think the most important one might be tools so if you want to implement the calculator if you want to implement a Weather Service I'm going to show you a tool that I'm implementing right now so you can expose those tools from the mCP server so the the client now or the host application let's call it cursor now can connect to my NCP server and immediately list all of the tools that are available to it so we will know oh I can call this and I can use this tool and that tool and this is not a New Concept obviously we've been using tools with Lama index on L chain and all of that forever uh it's the same idea now for the mCP server uh you also have the ability to share prompts and those prompts the llm or the agent can use those prompts to do stuff right uh so you can share prompts you can share resources so let's say you have documentation now you can share those uh that documentation the documents through that mCP server so the agent knows how to maybe call the apis or maybe do certain things in your code like assuming it's it's cursor so there is a lot of good information here check that out I'm going to mention one more thing and then I'm going to show you the code of what I'm building uh the thing that I want to mention is that this mCP server here even though if you're just adding mCP servers from different companies every one of those mCP servers is going to be independent so you're going to have one mCP server for slack one mCP server for Gmail one mCP server maybe for your custom database you can also Implement an mCP server that does custom things that are really really cool so for example imagine that you want to access slack access Gmail and maybe write a lock in your database and you want all of that to happen at the same time so instead of hoping for the llm to do that through prompts maybe you can encapsulate that all of that uh functionality in your mCP server that mCP server will call slack will call Gmail will'll call your custom API your custom database and will do the locks and will do all of that and now cursor or your agent only needs to talk to your mCP server all of that functionality is is code Ved there you don't have to rely on an llm that is not always reliable so anyway a lot of cool stuff here I'm just learning this I don't even understand the possibilities here uh something that I can tell you is that uh check I think uh anthropic has a directory of mCP servers out there check that out I think compos has uh like more than 250 mCP servers that you can just plug and play into your clients check that out so a lot of companies are exposing their functionality through these mCP servers uh and that's super awesome so now let me show you the one that I'm currently building it's not done yet but I can show you I can show what I have and uh you can see it working and you see it's super cool so I have uh this project which I'm working on this is actually yeah this is the project and this project here is this the project yeah so this is a machine learning project and there are a lot of different pipelines that I need to run a lot of different things that I need to manage servers Etc so one of the things that I do is I have a machine learning model that I can serve and I can I need to evaluate that model right so I have standard evaluation things that I can run but let me run the model here for uh just a second because we're going to need this I'm just basically loading the model in memory and getting it ready to receive uh requests receive predictions so I'm going to show you the model is running now I'm going to open a new console and I'm going to say just invoke and this uh sent a request to the model so if I go back to the model you're going to see it's making predictions so receiv something making a prediction and you can see here the prediction coming back from the model so I can test my model like this however this is uh something that I need to do but it costs a lot of time I want to experiment with that model by maybe changing the values that I sent to the model maybe adding things hey uh don't send this specific field to see how the model react or change the values for that particular field to two standard deviations so I can see how it works so there are a bunch of different experiments and in order to run all of those experiments I will need to write scripts that do all of that for me and every time I have a new idea I will need to write a script to do that so that gets all really really quick so what if I could just use cursor to test my model I can tell cursor this is how I want you to test my model and allow cursor to just do that for me that will save me a ton of time so the implementation to do this is actually very simple so let me show you here my mCP server or the one that I'm building right now and you'll see how simple it is I'm going to only show you a tool there are more tools here in this mCP server and if you want to know how to to start uh there is documentation in the model context protocol website where you know python samples and JavaScript samples and whatnot let me just show you this one here which is really really uh easy too there is a tool and the tool I'm calling it invoke model and the tool is going to receive a payload okay so the thing that the tool is going to do is just send a request post to that particular URL so I'm going to only be running this when the model is being served locally just for testing purposes so I'm pointing uh the request to Local Host to this particular URL and I'm basically just sending whatever I received as payload in a specific Json format that's it that's what the tool does now notice that the tool has documentation here this documentation is important because it's going to be part of the protocol like the agent is going to need this documentation to understand how to format the payload and how to what is this function for right so there is documentation I also give the model I'm telling the model hey this is payload which is an argument and I'm telling the model how to format that payload like what's the specific format that I'm expecting and then the return which explains that I'm just returning the prediction from the model that's my tool that's what my NCP server is doing so now with that uh in place I can come to cursor I can go to settings in cursor and you're going to find an option is called cursor settings and under features they have a section here I'm going to make this smaller they have a section here where you can add mCP server so if you want to communicate with slack you find the mCP server you add it here or Gmail or Google or whatever and here I'm adding my mCP server is this one here I call it machine learning school so here's the command to run that mCP server that is a script that's in my computer I don't need this is not a website I'm not or or an API this is just running it's a script that's running on my computer and I'm telling cursor if you need to invoke the model or run any of the tools that you see here in this case I'm registering two tools is endpoint running and invoke model I'm telling cursor if you need to do that run this command and that is going to do the job for you okay so as you can see this greenen dot means that it's working so you know cursor was able to connect to mCP just to to get the number of the tools inside and it's ready to be used so now all of the sudden I can come here and I can ask cursor to do stuff for me using that mCP server so you're going to go to composer and just make sure it's in agent mode as of right now only agent mode in cursor supports mCP tooling okay so just make sure you are in agent mode and I'm going to do the following I have a file here you know it's just a CSV file and I'm going to ask cursor to do the following uh invoke the model remember the model is running here locally with three samples from the penguin. CSV file I'm just going to ask that nothing fancy I should have done a script for this it's maybe easier to do a script but just bear with me for a second I'm going to submit this and hopefully cursor is going to find the Penguin's file it's going to load the the samples and it's going to decide okay I'm going to use mCP to communicate with the server so look at this it searched for the file Penguins it found the file in my path it read the file so it says now that I understand the data format I'll invoke the model with three samples from the data set okay I'll use complete samples avoiding any na value so cursor decided to do all of that which is great and format them according to the expected payload structure now cursor is asking me I'm going to call the mCP tool invoke model and if I open it's going to tell me exactly what it's going to call this with notice that it's going to just use the payload argument and the payload is formatted correctly all of that was done by cursor I did not have to code this so I'm going to run the tool so look at this I've invoked the model with three samples um I didn't have it here but if we had it uh let me just go back here like obviously you should have seen here I mean I didn't have it here but you should have seen the requests coming in anyway csor called the model invoked the model with that data and now it's telling me what the predictions are so it got 99% confidence on the first one 90% okay so this is awesome so now let's do something cool now for example I'm going to say uh do that again but now change the body weight to one standard deviation okay so now I'm asking something completely custom I'm asking cursor to read the data compute what one standard devation is and resend those samples changing the value of that feature I want to see how my model reacts to penguins that are fatter they have more body weight so now it's going to run a command this command is going to it's going to compute the standard deviation so I'm going to allow it to run this command there we go so it got the mean body mass the standard deviation is 801 G now I invoke the model with the same Penguins but adjust their body mass adding one standard deviation so now it's going to call my NCP tool but now the body mass if if we go to the previous one you can see the difference like this body mass was 3750 now the new body mass is 4552 cursor is doing that by itself and it's just relying on my tool to invoke that model so let me just accept this so it does what it needs to do so now it's giving me the results back the model still predicts all penguins as adly but with lower confidence so it's telling me it's interpreting the results now I can take this way farther than this I can ask now cursor to compute an average prediction coming from the model I can start interacting with my tool thanks to artificial intelligence and all of this is happening because I have this mCP server that's telling cursor how to communicate with that model without that I had nothing but what's even better is that this mCP server I can now use with wind serve and it's going to work I can use it with Cloud desktop and it's going to work I don't have to make any changes just because I'm changing from cursor to another uh IDE so anyway hopefully this makes sense hopefully this gives you an idea of of the stuff that you can start doing with this uh I have way many more ideas for the mCP server that I'm building just to manage all of these models and to do cool stuff with the models uh but anyway I hope you enjoyed it and I'll see you in the next one bye-bye

Original Description

Here is an explanation of what MCP is, how it works, and why I think it's awesome. I will also show you the MCP server I'm building. I teach a live, interactive program that'll help you build production-ready Machine Learning systems from the ground up. Check it out here: https://www.ml.school To keep up with my content: • Twitter/X: https://www.twitter.com/svpino • LinkedIn: https://www.linkedin.com/in/svpino 🔔 Subscribe for more stories: https://www.youtube.com/@underfitted?sub_confirmation=1
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Underfitted · Underfitted · 0 of 60

← Previous Next →
1 Test-Time Augmentation In Machine Learning.
Test-Time Augmentation In Machine Learning.
Underfitted
2 Don't Replace Missing Values In Your Dataset.
Don't Replace Missing Values In Your Dataset.
Underfitted
3 Introduction to Adversarial Validation In Machine Learning.
Introduction to Adversarial Validation In Machine Learning.
Underfitted
4 Introduction To Autoencoders In Machine Learning.
Introduction To Autoencoders In Machine Learning.
Underfitted
5 Active Learning. The Secret of Training Models Without Labels.
Active Learning. The Secret of Training Models Without Labels.
Underfitted
6 Early Stopping. The Most Popular Regularization Technique In Machine Learning.
Early Stopping. The Most Popular Regularization Technique In Machine Learning.
Underfitted
7 The Confusion Matrix in Machine Learning
The Confusion Matrix in Machine Learning
Underfitted
8 3 Tips to Build a Career in Machine Learning (Unconventional Advice)
3 Tips to Build a Career in Machine Learning (Unconventional Advice)
Underfitted
9 I can predict cars CRASHING. And it's 99% accurate!
I can predict cars CRASHING. And it's 99% accurate!
Underfitted
10 A Critical Skill People Learn Too LATE: Learning Curves In Machine Learning.
A Critical Skill People Learn Too LATE: Learning Curves In Machine Learning.
Underfitted
11 The BEST Machine Learning Interview Strategy.
The BEST Machine Learning Interview Strategy.
Underfitted
12 OpenAI’s Whisper is AMAZING!
OpenAI’s Whisper is AMAZING!
Underfitted
13 5 Lessons You’re NOT Taught in School
5 Lessons You’re NOT Taught in School
Underfitted
14 TensorFlow On Apple Silicon. Step-by-Step Instructions
TensorFlow On Apple Silicon. Step-by-Step Instructions
Underfitted
15 Generating Images From Text. Stable Diffusion, Explained
Generating Images From Text. Stable Diffusion, Explained
Underfitted
16 The Wrong Batch Size Will Ruin Your Model
The Wrong Batch Size Will Ruin Your Model
Underfitted
17 8 Mistakes Holding Your Career Back | Machine Learning
8 Mistakes Holding Your Career Back | Machine Learning
Underfitted
18 AI Just Solved a 53-Year-Old Problem! | AlphaTensor, Explained
AI Just Solved a 53-Year-Old Problem! | AlphaTensor, Explained
Underfitted
19 Bias and Variance, Simplified
Bias and Variance, Simplified
Underfitted
20 Should You Stop Splitting Your Data Like This?
Should You Stop Splitting Your Data Like This?
Underfitted
21 The Function That Changed Everything
The Function That Changed Everything
Underfitted
22 This Model Caused A Nuclear Disaster
This Model Caused A Nuclear Disaster
Underfitted
23 Will Your Code Write Itself?
Will Your Code Write Itself?
Underfitted
24 The Simplest Encoding You’ve Never Heard Of
The Simplest Encoding You’ve Never Heard Of
Underfitted
25 Superhuman AI Cracked An Impossible Game! | DeepNash, Explained
Superhuman AI Cracked An Impossible Game! | DeepNash, Explained
Underfitted
26 Can you become a Data Scientist without a Ph.D?
Can you become a Data Scientist without a Ph.D?
Underfitted
27 How to 10x your productivity with ChatGPT?
How to 10x your productivity with ChatGPT?
Underfitted
28 Cheating the Prisoner's Dilemma
Cheating the Prisoner's Dilemma
Underfitted
29 We integrated OpenAI's Whisper with Spot
We integrated OpenAI's Whisper with Spot
Underfitted
30 The Machine Learning School program
The Machine Learning School program
Underfitted
31 We integrated ChatGPT with our robots
We integrated ChatGPT with our robots
Underfitted
32 Solving complex tasks using a Large Language Model (LLM)
Solving complex tasks using a Large Language Model (LLM)
Underfitted
33 5 problems when using a Large Language Model
5 problems when using a Large Language Model
Underfitted
34 We just discovered faster sorting algorithms!
We just discovered faster sorting algorithms!
Underfitted
35 The 3 most important updates to OpenAI's API.
The 3 most important updates to OpenAI's API.
Underfitted
36 People are divided! Does GPT-4 understand what it says?
People are divided! Does GPT-4 understand what it says?
Underfitted
37 How much should you charge hourly as a Machine Learning freelancer?
How much should you charge hourly as a Machine Learning freelancer?
Underfitted
38 Building a RAG application from scratch using Python, LangChain, and the OpenAI API
Building a RAG application from scratch using Python, LangChain, and the OpenAI API
Underfitted
39 Building a RAG application using open-source models (Asking questions from a PDF using Llama2)
Building a RAG application using open-source models (Asking questions from a PDF using Llama2)
Underfitted
40 How to evaluate an LLM-powered RAG application automatically.
How to evaluate an LLM-powered RAG application automatically.
Underfitted
41 Step by step no-code RAG application using Langflow.
Step by step no-code RAG application using Langflow.
Underfitted
42 I built a simple game using Langchain. Here is a step by step tutorial.
I built a simple game using Langchain. Here is a step by step tutorial.
Underfitted
43 I used the first AI Software Engineer for a week. This is happening.
I used the first AI Software Engineer for a week. This is happening.
Underfitted
44 I deployed a recommendation model. Testing Models In Production using Interleaving Experiments.
I deployed a recommendation model. Testing Models In Production using Interleaving Experiments.
Underfitted
45 How to run PyTorch, TensorFlow, and JAX on your Mac (Apple Silicon)
How to run PyTorch, TensorFlow, and JAX on your Mac (Apple Silicon)
Underfitted
46 How to train a model to generate image embeddings from scratch
How to train a model to generate image embeddings from scratch
Underfitted
47 Building an AI assistant that listens and sees the world (Step by step tutorial)
Building an AI assistant that listens and sees the world (Step by step tutorial)
Underfitted
48 Why are vector databases so FAST?
Why are vector databases so FAST?
Underfitted
49 A Machine Learning roadmap (the one I recommend to my students)
A Machine Learning roadmap (the one I recommend to my students)
Underfitted
50 How to build a real-time AI assistant (with voice and vision)
How to build a real-time AI assistant (with voice and vision)
Underfitted
51 An introduction to Mojo (for Python developers)
An introduction to Mojo (for Python developers)
Underfitted
52 How does Lexical Scoping in Mojo 🔥 works (under 3 minutes)
How does Lexical Scoping in Mojo 🔥 works (under 3 minutes)
Underfitted
53 Building a CI workflow for those who hate it (using GitHub Actions)
Building a CI workflow for those who hate it (using GitHub Actions)
Underfitted
54 How to run Python Code in Mojo 🔥
How to run Python Code in Mojo 🔥
Underfitted
55 AI will not take your job. Here is what I think will happen instead.
AI will not take your job. Here is what I think will happen instead.
Underfitted
56 How to fine-tune a model using LoRA (step by step)
How to fine-tune a model using LoRA (step by step)
Underfitted
57 Late initialization in Mojo🔥 (Python doesn't support this)
Late initialization in Mojo🔥 (Python doesn't support this)
Underfitted
58 The $1,000,000 problem AI can't solve
The $1,000,000 problem AI can't solve
Underfitted
59 A gentle introduction to RAG (using open-source models)
A gentle introduction to RAG (using open-source models)
Underfitted
60 Automating feedback using ChatGPT and Zapier
Automating feedback using ChatGPT and Zapier
Underfitted

The Model Context Protocol (MCP) is a standard protocol for connecting LLMs or agents to APIs and backends. The video explains MCP, its core concepts, and demonstrates its usage with a custom MCP server and various tools. By following this lesson, you can learn how to use MCP to build and connect machine learning models to APIs and backends.

Key Takeaways
  1. Load a machine learning model in memory
  2. Get the model ready to receive requests and make predictions
  3. Send a POST request to the model server with a payload in a specific JSON format
  4. Document the payload format and return values for the 'invoke model' tool
  5. Run MCP server script on local computer
  6. Add MCP server to cursor settings
  7. Register tools with MCP server
  8. Invoke model with data from penguin.csv
  9. Compute standard deviation of body mass
💡 The Model Context Protocol (MCP) provides a standardized way to connect LLMs or agents to APIs and backends, reducing complexity and allowing for more efficient machine learning model development and deployment.

Related AI Lessons

10 Python Concepts You Must Know Before Calling Yourself Advanced
Learn 10 essential Python concepts to take your skills to the advanced level and stand out as a developer
Medium · AI
10 Python Concepts You Must Know Before Calling Yourself Advanced
Learn 10 crucial Python concepts to elevate your skills from intermediate to advanced and become a proficient developer
Medium · Data Science
10 Python Concepts You Must Know Before Calling Yourself Advanced
Learn 10 essential Python concepts to take your skills to the advanced level and stand out as a developer
Medium · Programming
10 Python Concepts You Must Know Before Calling Yourself Advanced
Learn 10 essential Python concepts to take your skills to the advanced level and separate yourself from beginner developers
Medium · Python
Up next
Learn Deep Learning by Hand (Beginner's Guide - Part 1)
Thu Vu
Watch →