Resource mentioned in this lesson: MCP Python SDK
Connecting the Client to the Server
00:00
You are now ready for the final step before having a bare-bones MCP client that is able to connect to an MCP server. You’re just a coroutine away. You need to implement _connect_to_server().
00:13
And this is where the library mcp will come into play. You’re going to scroll to the top and import a couple of functions and classes. From mcp you’re going to import the classes ClientSession and StdioServerParameters.
00:31
And from mcp.client.stdio you’re going to import the function stdio_client. Now you’re going to scroll down back to your _connect_to_server().
00:42
You want to create a client session. And for that you need to establish a connection to a standard I/O server through the asynchronous context manager stdio_client that you just imported.
00:57
Now your stdio_client requires a server. It needs to know what server to connect to and you specify the server through the StdioServerParameters class that you also just imported.
01:10
Now ideally what you will do here is specify the server_command that you accepted as an argument to your MCP client. But sadly this is not enough. StdioServerParameters expects the command to be given in a very specific format whereas your MCP client class will be lenient and it will accept any shell command that runs an MCP server.
01:37
So to work around that you need to tell the server parameters that the command is actually given by the shell sh and then you specify a couple of arguments.
01:48
You can delete this from here. The arguments you specify are first "-c", and -c says whatever is coming next should be interpreted as a shell command, which is precisely what self.server_command is supposed to be.
02:05
But now, just for your convenience, you’re actually going to wrap the server_command in an f-string so that you can redirect standard error to null, just to keep your terminal cleaner.
02:20
This specifies your asynchronous context manager, but you are going to need a second one in a bit. So to make it easier to manage all of your asynchronous context managers, within the context of the context manager that the MCP client itself is, you’re going to use from contextlib,
02:41
you import an AsyncExitStack. This will help you manage all of your context managers. In your method __init__, you start by instantiating your exit_stack,
02:57
and then around stdio_client you’re going to await it with the exit_stack. So you’re going to write await self.exit_stack.enter_async_context().
03:12
Make sure you open your parenthesis, close the parenthesis around the stdio_client, and indent the code appropriately. As you await this stdio_client, you get two variables, read and write.
03:27
And these are required to create your client session. read and write. Now this is also used as an asynchronous context manager, so once again you’re going to wrap this in await self.exit_stack.enter_async_context().
03:48
Make sure you indent your code correctly, and you save this in the variable client_session.
03:55
Finally, you’re going to await the initialization of the client_session. And once you’re done, you return it. And if you’re returning the client_session, it means you need to update the return type to be ClientSession.
04:14
Now this plugs back to the dunder method __aenter__, where you are specifying the attribute client_session. Now how do you properly close your asynchronous context manager? What you need to do is you need to await for your exit_stack to be closed. And this dunder method returns None.
04:40
So this should be the proper implementation of your MCP client. To test it, open your terminal and run the command uv run src/mcp_client/__init__.py.
04:55 If you wait a second, you will see it does not work because there is a typo in the code.
05:02
You can quickly fix it. initialize, this is what you get for typing and speaking at the same time.
05:11
Once you fix the typo, you can try running uv run src/mcp_client/__init__.py again. You give it a second. If you get this success message, congratulations, this was the most daunting part of the whole course.
05:27 In the upcoming lessons, you’re going to build a CLI around your MCP client so that you can connect to different servers and request different prompts, resources, and tools from the servers you connect to.
Become a Member to join the conversation.
