Building MCP Servers with LangChain in Python

Muhammad Moin · Intermediate ·👁️ Computer Vision ·1y ago

About this lesson

In this video tutorial, we’ll build MCP servers from scratch using the LangChain and LangGraph libraries. You’ll learn how to create two different MCP servers using two differnt MCP transport protocols: Stdio and HTTP. Additionally, you'll learn how to integrate any MCP server you build with your own application or client. Code: https://github.com/MuhammadMoinFaisal/MCP-Server-LangChain *🧑🏻‍💻 My AI and Computer Vision Courses⭐* *📗Build AI Agents with LangChain v1: Deep Agents & Tools 2026 (13$)* https://www.udemy.com/course/build-ai-agents-with-langchain-v1-deep-agents-tools-2026/?couponCode=PROMOTION13DOLLARS *📗YOLO26 Bootcamp: Real-Time Detection, Segmentation & Pose (13$)* https://www.udemy.com/course/yolo26-bootcamp-real-time-detection-segmentation-pose/?couponCode=PROMOTION13DOLLARS *📘Hands-On RAG Bootcamp: Build Apps with LangGraph & LangChain (13$)* https://www.udemy.com/course/hands-on-rag-bootcamp-build-apps-with-langgraph-langchain/?couponCode=PROMOTION13DOLLARS *📙Complete Computer Vision Bootcamp: YOLO to Multimodal AI (13$)* https://www.udemy.com/course/complete-computer-vision-bootcamp-yolo-to-multimodal-ai/?couponCode=PROMOTIONJUNE *📚 Generative AI, LLM Apps & AI Agents Masterclass 2026 (13$)* https://www.udemy.com/course/ai-agents-with-n8n-automate-anything-with-no-code/?couponCode=PROMOTIONJUNE *📘 YOLOv12 & YOLO26: Custom Object Detection & Web Apps 2026 (13$)* https://www.udemy.com/course/yolov12-custom-object-detection-tracking-webapps/?couponCode=PROMOTIONJUNE *📙 Modern Computer Vision with OpenCV 2026 (13$)* https://www.udemy.com/course/modern-computer-vision-with-opencv/?couponCode=PROMOTIONJUNE *📚 YOLO11 & YOLOv12: Object Detection & Web Apps in Python 2026 (13$)* https://www.udemy.com/course/yolo11-custom-object-detection-web-apps-in-python-2024/?couponCode=PROMOTIONJUNE *📘 AI 4 Everyone: Build Generative AI & Computer Vision Apps (13$)* https://www.udemy.com/course/ai-4-everyone-dive-into-modern-ai-with-llama-31-and-ge

Full Transcript

Hello everyone. In this video tutorial, we will be building MCP servers from scratch using lang chain and lang graph. We will be creating two different MCP servers. One MCP server will have addition and multiplication tools and in the second MCP server we will call the weather API and we will be using two different MCP protocols. Like in the first MCV server which has addition and multiplication tool, we will use the MCP protocol as or MC protocol or transport protocol as stdio and the second MC server where we will be calling weather API where we will use the transport protocol or MCP protocol as HTTP. So like you can see over here MCP protocol or transport protocol like you can say that and transport protocol or MCP protocol is the way the MCP server communicate communicates with the client. Okay. So MCB protocol is basically the way the MCB server communicates with the client. So here we have an application like for example uh so in this tutorial we will learn how we can integrate our MCP servers like any kind of MC servers that we build along with our application or with MCP client. So for example I am building a chatbot application using lang chain and lang graph. Inside this application I have uh different large language models integrated. MCV servers are connected with tools like addition and multiplication tool and as well as MCV server is connected to weather API uh as well as weather API. Okay. So the user provides an input like you can see over here uh here we have an input. Okay. So here we have here we have an input the user provides Okay. So user provides an input to the large language model and based on the based on the input the large language model decides whether it has to make a call from an MCB server or not. Okay. For example, if a user asks a question, what is the weather of New York? So LLM will not be able to answer because LM does not have live information. it will make a call to the MCB server and it will use the weather call API tool. Okay. So, so to call the MCB server, it will be using MCB protocol. So, MCB protocol or transport protocol is the way that MCB server communicates with the client or your application. Okay. So we can say in short the in when the input is received uh to the large language model uh the large language model will call the MCB server uh uh it will use different MCB server tools using the MCB protocol. So as the large language model receives an input, it will uh just check the input and uh then it will uh see if we it needs to call the MCB server and if it needs to call the MCB server, it will call the MCB server using MCV protocol or transport protocol and uh then it will use uh different uh the tool uh that we have like based on the input it will decide which tool to use in the MCB server. then it will use the tool uh that are provided in the MCV server and then it will generate a response. Okay. So over here uh this is a quick intro of MCB servers that we will be building in this uh tutorial. So let's go to the cursor and create a project over there. So over here you can see I have opened cursor. So first of all I will go to file and click on open folder over here uh in cursor uh that is my previous project open. So like you can see from here or I can just close this up and start from here now. So you can see over here I have open cursor over here. So now I will just open project now. So now I will click on open project from here and over here I will just go to the directory and here you can see just I have just created an empty folder over here. I will just select this folder from here and now you can see I've just created a new project over here. So like this all you can see. Okay. So you can simply uh go over here in the terminal and just click on new terminal from here. So first of all uh we need to install the UV package. So you can install the UV package by writing pip install uv. I will just write pip install uv. So this will install the uv package. Next we need to initialize the workspace with the help of UV package. So I will just write UV in it. So this will initialize the workspace with the help of UV package. Now we need to create a virtual environment with the help of UV package. So we write UV venv. And now we can just activate this environment. So, so now you can see we have activated the environment as well. Now if I just open pi project 2ml. So now you can see there are currently no dependencies listed over here. Uh so we can just create a requirements txt file over here dot txt file. In the requirements txt file, I will add all the packages that are required. So let me add all the required packages that are required in this project. So I require the lang chain dash brock package. Uh I require the lang chain dash mcp adapters package. I require the MCP package and I require the lang chain langraph package. So in this tutorial as I told you at the start we will be creating uh MCB servers with the help of lang chain and langraph. So you can see therefore so now uh I will just install all these requirements all these packages. I will just write uv at r requirements txt. So this will install the pack list that are listed in the requirements txt file. Okay. So I not save this. So just so now you can see the packages are getting installed now. So this will take some time before these packages get installed. Okay. So that's a very speedy process. So the package installation is being done. So as I told you that I will be creating two different MCP servers. First I will build math MCB server and here I will be using uh the transport protocol or MCB protocol as std IOU std IO and this MCB server will be connected to tools like addition and multiplication. So let's read first uh MCB server which is the math server. it will contain two tools addition and multiplication tool. Okay. So now if I just open this pip project. So in the dependencies you can see we have all these four packages installed. So we can just close this up now and I will just create a new file by the name maths server dot py file. So that looks quite promising and from here I will just write from mcp dots server dotfirst mccp import fast first mcp and uh first now I'll just define the server name over here and mcp is equal to fast mcp and here is our server name which I will just pass over here server name will be math. Now I will just create first tool inside the MCP server. So I will just write add mcp. tool and I will just create a function by the name addition add which is for addition and it will take integer values and uh here I will just uh pass uh prompt and lm will uh choose which tool to based on the prompt which I am passing over here. Okay. So I will just run it over here. This tool adds two numbers together. Okay. And this will return a plus b. That looks quite promising. And now I will just create second tool inside. Second tool inside the MCP server. So I will just write MCP do. And we'll decide multiply and here we have. So now here we will multiply two numbers. Uh so we can just write this tool multiplies two numbers together and this will return me. This will also get two numbers together. And now we will use the transport protocol stdio. Std IO. So basically transport protocol but is transport protocol. So the transport is the way the MCP server communicates with the client. So std IO is the default transport for MCP server and std.io IO is a transport protocol that allows the MCB server to communicate with the client over the standard input and output strings. And this H stdio is the default transport protocol. So we can write mcgb.tr run transport is equal to std. So that looks quite promising. Okay. So no errors. Everything looks good. So we have created a mat server. Here we have two tools. One we have the addition tool and the other tool is we have the multiplication tool. So now we'll create another MCP server and we will just call weather API. I will not be calling weather API but just to show you that how you can create a weather API server as well. Okay. So we can just write uh weather.py over here and I will just write from mcp dots server dotfost mcp import fast mcb. That looks good. Then here I will just write the server name over here. And the server name here is MCP is equal to first MCP. And the server name will be vector. Okay. Now we'll create one tool. We require only one tool which is MCP. And we'll just write async get weather and location will be sat. Okay. And so this tool will give me the weather forecast for a given location. So this will return the weather in Kelly for is solid. So this is you can just call a weather API over here but to explain the concept of MCB servers then this is the aim of this tutorial and here I will just use uh the transport protocol as HTTP. So I will just say transport is equal to stream enable http. Okay. So that looks quite promising. We have created a second MCP server as well. Okay. So now we just need to create a client. So our our application where we will calling these MCB servers based on the user input that it user pass to the large language model. Okay. So let's go ahead with that as well. So now I will just create another file over here by the name line. So first of all we will import all the required libraries over here. So lang chain dash ncv- adapters dotclient port multisver mcp client and I will just write from lang chain dot pre-build board create react or not this is not lang here we have line prof and I will just write form lang chain dash croc import we will use chat proc because we are just chat uh because first we pass the input to the the user input we will pass large language model and large language model decide which MCP server it wants to use or not okay and from dot environment invoke load dash Okay. So, first thing what I will do is I will just click over here and create a env file. Inside this env file, I will just pass my gro API key over here. So you can just go to the blog site and get your own API key as well. Okay. Then I will just load the environment over here. Then I will import OS and import async. So now first I will just create main function over here and the client will be multi-erver MCP client command will be Python and we will using Mad server.py by and transport will be S3DIO. Similarly, we'll create two for the weather as well. So, let me just add that code. Okay. So here I just pass the URL over here because we are using the transport protocol as HTTP. Okay. Then I will just set up the environment over here. So I will just write over here OS dot And here we have the brock API key over here and we'll just write OS dot get environment and here we'll just get a block. Okay. Next we will just call tools and I will set await do get dash then I will just write models model is equal I will chat proc model and I will using model is equal to when dash qw 32 parameters model then I will just call the create react agent and I will just write over here modern tools then I will just write math response response is equal to await agent dot a invoke. So here we'll have messages. Yes. And here we have the content. What is 10 plus 20? Okay. And from here I will just print math response. that response. I'll just go to the messages. Okay. Similarly, we will add a weather response over here as well. And here I'm asking what is the weather in California. Okay. And lastly I will just write async io dot run. Okay. So that looks promising. We have just completed this up. So now uh we have done with the client part as well. So now I will just open terminal from here. First of all we need to just run this weather.py Pi port. So I will just write Python vendor.py. So this will give me a URL over here. So let's wait for this. So now you can see we have the URL. So unicorn running on this URL. Okay. Now I will just add new terminal over here. And now over here I will just run this client.py file code. So we can simply write python client.py. So now you can see the math respond the result of 10 + 20 is 30 and the weather response is the current weather in California is sun. So like you can see that uh using MCP protocol we are able to communicate between the MCP server and our application on the client side. So in this tutorial we have created two different MCP servers and we have used two different HTT uh two different transport protocols. Uh first MCP server has two tools. One is the uh addition tool and other is the multiplication tool. The second MCV server has the weather call API tool and uh we have we have used default MCV protocol which is stdio and we have also used the HTTP transport protocol as well. So we have used two different transport protocols and we have seen that how we can uh communicate uh how you can use uh NC MCB protocol or a transport protocol to communicate between the client and the MCV server. So that's all from this tutorial. Thank you for watching.

Original Description

In this video tutorial, we’ll build MCP servers from scratch using the LangChain and LangGraph libraries. You’ll learn how to create two different MCP servers using two differnt MCP transport protocols: Stdio and HTTP. Additionally, you'll learn how to integrate any MCP server you build with your own application or client. Code: https://github.com/MuhammadMoinFaisal/MCP-Server-LangChain *🧑🏻‍💻 My AI and Computer Vision Courses⭐* *📗Build AI Agents with LangChain v1: Deep Agents & Tools 2026 (13$)* https://www.udemy.com/course/build-ai-agents-with-langchain-v1-deep-agents-tools-2026/?couponCode=PROMOTION13DOLLARS *📗YOLO26 Bootcamp: Real-Time Detection, Segmentation & Pose (13$)* https://www.udemy.com/course/yolo26-bootcamp-real-time-detection-segmentation-pose/?couponCode=PROMOTION13DOLLARS *📘Hands-On RAG Bootcamp: Build Apps with LangGraph & LangChain (13$)* https://www.udemy.com/course/hands-on-rag-bootcamp-build-apps-with-langgraph-langchain/?couponCode=PROMOTION13DOLLARS *📙Complete Computer Vision Bootcamp: YOLO to Multimodal AI (13$)* https://www.udemy.com/course/complete-computer-vision-bootcamp-yolo-to-multimodal-ai/?couponCode=PROMOTIONJUNE *📚 Generative AI, LLM Apps & AI Agents Masterclass 2026 (13$)* https://www.udemy.com/course/ai-agents-with-n8n-automate-anything-with-no-code/?couponCode=PROMOTIONJUNE *📘 YOLOv12 & YOLO26: Custom Object Detection & Web Apps 2026 (13$)* https://www.udemy.com/course/yolov12-custom-object-detection-tracking-webapps/?couponCode=PROMOTIONJUNE *📙 Modern Computer Vision with OpenCV 2026 (13$)* https://www.udemy.com/course/modern-computer-vision-with-opencv/?couponCode=PROMOTIONJUNE *📚 YOLO11 & YOLOv12: Object Detection & Web Apps in Python 2026 (13$)* https://www.udemy.com/course/yolo11-custom-object-detection-web-apps-in-python-2024/?couponCode=PROMOTIONJUNE *📘 AI 4 Everyone: Build Generative AI & Computer Vision Apps (13$)* https://www.udemy.com/course/ai-4-everyone-dive-into-modern-ai-with-llama-31-and-ge
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related Reads

📰
What Is mAP (Mean Average Precision)?
Learn about mAP, a metric to evaluate object detection models with a single number, and why it matters for model performance assessment
Medium · Data Science
📰
Beyond Binary: Taming Image Noise with Local Ternary Patterns (LTP)
Learn to reduce image noise using Local Ternary Patterns (LTP), a technique that goes beyond binary image processing to improve image quality
Medium · Machine Learning
📰
I Built an AI Face-Analysis Suite From Scratch — Here's What I Learned
Learn how to build an AI face-analysis suite from scratch and gain insights into computer vision and machine learning
Dev.to · Ahsan Fayyaz
📰
Your Group Chat Is One Video Away From Ruining Someone's Life
Learn how viral synthetic media can ruin lives and what developers can do about it
Dev.to · CaraComp
Up next
9-Phase Computer Vision Roadmap 2026 | AI & Deep Learning | #shorts
SCALER
Watch →