Transcription
Hello everyone. In this lesson, I will tell and show you what MCP is, what MCP Client is, what MCP Server is. Frequent terms that are heard now in many places. I think you hear them. From scratch, I will show you how to create your own MCP server in Python, the simplest language, and how to use it from MCP clients, even a couple of clients. All this will be, of course, on a super understandable example. Okay, let's go. MCP stands for model context protocol. It is an open source standard. What does open source mean? It means that there is source code for this thing. You can go and look at GitHub. What does this MCP protocol standard, as they say, do? It connects or integrates, however you want to call it, AI Applications, all sorts of applications that use artificial intelligence or LLM applications. it connects all these applications with external systems, some external systems. External systems can be anything. A directory on your disk, for example, is also an external system. The address of the GitHub Repository where the source code is located, here github.com model context protocol. You can go there. There are already a bunch of software developments that allow you to use this protocol, this MCP. In general, you can use it in a lot of languages and write your own MCP servers. Yes, there is a client and there is an MCP server. What is the difference between them? You will see now. So, first. MCP client. It is the one who sends requests to the MCP server. Well, like, in principle, it's understandable, right, any client sends some requests to the MCP server. Can you give an example? And the example is who is an MCP client, for example, an IDE Integrated Development Environment, that is, a program, software that you use for software development. For example, Cursor AI, it is an MCP client. For example, AW Skiro, a new shell, is also an MCP client. Cloud CD is also an MCP client. Cloud desktop is purely a thing for chat, like, just, right, it's not a development environment, it's also an MCP client. LM Studio is absolutely free. You can download it, it downloads various models offline. You don't need the internet for this. After that, you will have models on your computer. You can chat with them. That is, you will have such a local ChatGPT on your computer, for example. And this LM Studio is also an MCP client because it knows how to send requests to MCP servers. And any of your AI or LLM applications that know how to send requests to an MCP server. This was the MCP client. Now, who is the server? where are these requests sent. An MCP server is program code, really just a piece of program code that works and accepts requests from an MCP client. In principle, everything is very clear, like, right, Captain Obvious. And at the same time, this MCP client initiates actions called Tools. These Tools perform some actions, for example, what servers can be made, right, or what exists and what tools they have. Well, for example, you can make a server that sends requests to databases. And its tools will be to read this table. Another tool will be to read another table, a third to read, I don't know, or to summarize something. In general, various things with databases. You can make another server that will, say, integrate with WhatsApp, right, and send messages there, read messages. That is, its tool will be to read a message, a tool to send a message. For example, you will have another server that connects to your disk, right, to the actual disk on your computer, and its tool will be to count how many files or count how many directories or something like that. In general, so, I think that you probably don't understand anything. Or maybe, even a little bit, there is some light at the end of the tunnel. Now, so that this light at the end of the tunnel gets very close. How does all this work together? MCP client, MCP server, these models, well, uh, in short, user, requests, how does it all work together? Our user, who asks questions in an application, in some chat, let's say, our chat will be LM Studio, it, which is an MCP client because it knows how to work with MCP. By default, when you write any questions in ChatGPT, it communicates with the model that is connected, right, that you downloaded, and that's it, it sends your questions to it, and the model answers, for example, right, the model is Deepseek. It's simple. That is, the user, asking a question, what is the largest city in Africa, LM Studio will forward this request to this model. The model will answer, you will get the answer. What if the user asks a question like this: list the students in the database for me? LM Studio, of course, will try to answer this question and will even answer, by the way, but it will start asking various leading questions because it won't understand what it's all about. What database, where is this database, what students, where is this table. In general, that is, it doesn't have such context, it won't. In short, it's some external source, most likely, you are asking, and it doesn't know what it's all about. Another question, how many files in the directory? Which directory? What files? On which disk at all? That is, that is, also a question that is practically very easy, but it's impossible to answer. The model cannot answer it because it was not trained on your disks, so it knows nothing. It needs access to your disk, to your directory, to count files. Another question: send a message to dad, send a message to dad. Through what? To whom? What is dad's phone number or, in short, email? That is, how to send, how to do all this? Context, that is, the LLM model, although super smart, will not understand at all how to do what I need to do for these questions for the LLM model to be able to answer. For this, MCP exists, which can connect your client, the MCP client, in our case LM Studio, with external sources, for example, a database, WhatsApp, and Shell, purely Shell, to run commands, count files, count directories. For all this to start working, we need to create an MCP server. For example, an MCP server that will have database tools. And its tool will be to run some SQL Query, right, for example, well, another MCP server that will, say, messaging tools, right, there, which will be able to communicate via WhatsApp, right, so it has code written, program code, that connects with all tokens, passwords already, in short, in it, right, it knows how to connect to your WhatsApp or, say, Slack or, I don't know, emails, right, and knows how to send, read messages. For example, the next server that has access to your disk. That's all simple, right, really in Python, it's very simple through the OS library, and it knows how to count files, count directories, in general, all sorts of things. And all this, how does it connect to LM Studio, how does it know about these servers, it's all very, very simple. In such a file, mcp.json, in which you specify where the servers are located, or they are on the local computer, that is, where the path to them is, or they are remote. That's all. And then, when such a question is asked, listen name database, this request will first go to the LLM. The LLM will understand: "Okay, listen, I don't know what this is, but I have MCP servers connected." And one of the servers is called DB server, which has a tool to run SQL query. Oh, maybe that's it. It will run it, get the data, the answer from this execution, and, based on this answer, it will most likely be able to answer you beautifully. The same, how many files in the directory. Well, and only I have, which is a shell, in short, which is called a tool, and that's it. That is, well, it's understandable, it uses this server. Well, and another send message. It's understandable, it will understand, okay, what I have. I have some connected server with Message Tools, and there is send Message. Oh, and we will use this. That is, in short, that's how it is. So, I will now show you with a clear example how LM Studio, right, for example, answers these questions without having MCP servers. Then we will create these simple servers. Well, we will create one of the simplest with a couple of tools, which will communicate with the database and return answers to us. And we will connect this to LM Studio and see how it will answer after this MCP server is connected, that is, before the MCP server and after. Okay, enough theory, let's get to practice. So, here is my LM Studio. And I have already downloaded one model. Here it is, Deepseek. I will load it now. If you don't know how to use LM Studio, I have a whole lesson on LM Studio. You can find it and watch it. I explain in detail, in detail, how to do everything, install, download. In general, everything, everything, everything. The model is loaded. You can ask a question. Well, let's ask the same stupid question. Biggest city in Africa. Understandably, now it will, well, think and answer us, in principle, Biggest City and Africa. Well, here you see, it's a good job, Lagos, blah-blah-blah. Now let's ask this question. list students names and database. Now it will most likely give us an SQL query on how to run it. Well, I don't want an SQL query, I want it to list all the students in my database and give me, I don't know, their names or, I don't know, even ask it: "Print me the student with the best grade," something like that. Well, here you see, in short, it doesn't know what we're talking about. In short, in general, give me help. In general, I don't want any help. It needs to be able to do this. It will be able to do this if we connect an MCP server. To do this, let's make it. So, I have two files. Look, my database is an SQLite database and a Python file that will read from this database. This is not an MCP server yet. Now we will make it. MCP server. Well, let's first show what is in this database. Let's run a simple DB browser for these databases. Here it is, this is my file. We open it. I have two tables there: Courses and Students. And Browse Data. This is the Courses table. See? So, there are 10 records and here is the Students table, there are 20 records. So, this is my simple database. Now let's open this Python file and make an MCP server out of this Python file. So, here is our small, simple Python file where we use the SQLite library. Here is our database, the file, well, this one. And we have three functions, as you can see. Get students, simple. We connect to this file. There are no passwords, no usernames here. Everything is very simple. And we run an SQL query select star from students. And that's it. And we return the entire result. Get courses. The same select star from courses. And we return the results and get tables. We simply return all the tables that are in the database. Well, and we print. This is purely for testing. Let's run it, see how it works. I run it. Oh, it was able to read my database with data. In general, that's it, good job. Now, how to make a real MCP server out of this file? It's all very, very simple. Well, first, you need to add the MCP library from MCPServer.fast import fast. The MCP library needs to be installed, of course, it's understandable how it's done. Very simple. We go to Packages, right? Python. Python interpreter. And here we add, we search for MCP. Here it is. Module context protocol. We add it. All done. We are good. Next, we need to initialize our MCP server. Here, let's put this line. MCP equals fast MCP and some cool name, your name. That's all. By the way, the server, in principle, can be considered ready. Uh, well, wait a second. For these functions to become tools for our server, for this server to have several tools, several instruments, what needs to be done? A very simple thing needs to be done. Before each of these functions, put a decorator like this. This is MCP Tool. The same here. This is MCP Tool. The same here. This is MCP Tool. That's all. And now, of course, we cannot print anything. We cannot do any prints. What do we do now? We simply call MCP, that is, we start the MCP server. Like this. That's all. So, when this Python file, this server, is launched, what will be launched? The mcp run command will be launched. That is, mostly this command will be launched, and our server will start working. If I launch it now, look, let me launch it now, what will happen. The server has started. Well, it's waiting for something, for some requests to come to it, right? In general, that's all. That is, nothing happens, for the most part. That's it, you can insert this MCP server into our MCP client, and it will call it, and it will have three tools, three instruments that it will use smartly. I am sure it will do it very smartly. Let's go. So, let's start a new chat. And here, look, in LM Studio, there are user, power user, and developer modes at the bottom. Well, let's choose power user or developer, so that this menu appears in the upper right corner. Here you choose, see context, model, program. You need program. And let's install MCP edit MCP json. Here it is. And here we specify the SQL server. Let's give it a name, for example. Server SQL. Like this. That's what it will be called. In fact, here is the friendly name, as they say. You can give any. And parameters. We send two parameters. The first is the command. With what command do we launch this server? It will be launched with the Python command. Just the Python command. Very simple. The main thing is that the Python command works, by the way, right, if you run it from the command line. Python minus minus version. The Python command works. By the way, the MCP library needs to be installed immediately too. Uh, in general, because this is my hosting, as they say, of this MCP server. And Python will launch it, they need to know where to install MCP, because then we installed it in the PyCharm environment. That's for development, and this is for launching. All done, great, everything is installed. And args, arguments. Arguments are the path, the path to the server, the path to our file. Let's just copy it here, copy and paste it inside. Click, pasted. The main thing is to make these double slashes, if you have Windows, because it doesn't understand otherwise. That's it, I've done it. I save. Click. You see, MCP server immediately. If I click here again, you see, Loading Tools, it immediately loaded, it understood that I have three tools, I can use them, but they are still inactive. To activate them, you need to press this button. Well, let's close all this now. Here is our chat. Activate MCP. That's it, now besides the model, there are also various MCP tools. Server. Let's ask the same question, absolutely the same question, and see how it can answer now. And the question is list students and database. Okay, it sees that it has some tools. Get students. Td d. The first time it will always ask you if it's okay to run this thing from this MCP server. You can always. Uh, allow running from my server, SQL server. I allow it. That's it, it asks once, and that's it, it won't ask anymore. We launch. And it launches the Get Students function, gets the answer from it. And now it will process it and give us what we asked for? Names of the students. Now it should give us Names of the Students, in principle. Well, if it thought of something else, thought about something and, in general, gave more. In general, that's it. Here they are, 20 students. You can ask it now: list? Yes. List about AWS. That is, where are the courses, what courses? Right, it's hard to understand, but it knows that it has a get courses function, it can run it, get the answer from it, and based on this answer, generate an answer that you would most likely want to know. Here you see, it launches get courses, gets a reply from it. You can click here to see what it got. Here you see, it got a whole bunch of replies. Now it will filter it, in theory, give me what I need, only AWS. I have 10 different courses, but not all of them are AWS in the database. Now it should give us only what is AWS. Well, it's stupid, I'll tell you honestly, stupid. AWS related. Well, like, yes, they are all related to AWS somehow. You could have said list only AWS list only AWS courses. In general, how, in short, it can communicate with my database, as you can see, using an MCP server. That's all. Well, now it's better. You see, it only gave AWS courses. So, you can connect any other tools you want in any language, not just in Python, of course. You can also connect remote servers, not local ones. You see now how this local one is on my computer. That is, all this is done like this. All through such, if your client allows you to use these MCP json settings, where you specify your servers. That is, this is one server, you can make, comma, as they say, another server, which will not be with SQL, but, for example, Messenger, right, or Shell, right, so it has different access and all sorts of things. And this is my Cursor AI. Now, here, in principle, we will do the same. In short, let's say I add a file, I don't know, demo demo.py. Well, and here I will have students equals there for student and students. Print student. Voila, like this, right, I have such code, for example, and I want it to now fill in here, in short, help me, by taking names from the database. How do we do this? We go to Cursor AI on the right side. You see here settings, general global settings. And here there is MCP, install MCP. There are no MCP servers. Let's add the same one, as you can see. Let's call it SQL L server, for example, right, I will have Command Python and argument. This is my server, this one. Copy SP. Paste it here. You just need to make double slashes here. Save here. You can close it. And here you see, it immediately sees that everything, the SQL server is ready, installed. And it even has three tools. Good job, three tools. Everything, it's activated. In principle, everything is great. Here is the chat. And here add into students equals and all students from database. Now let's see how it does this. Vo, get table. It immediately knows what to run. Run. Here it gave the results. Planning next move. Bastard. Well, like, yes, understandable. It's smart, you could say, almost. Connects to the database, in short. Hardcore. Students names. Well, here you go, exactly what I wanted. Keep this file, it's even working now. Like this. So, you can use MCP servers in a simple chat or in an IDE where you develop your brilliant ideas. Well, I hope it was super interesting and super useful. And don't forget to like, subscribe, and see you in the next lessons.