Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Specifying the Server to Connect to Through a CLI

00:00 In this lesson, you’re going to specify the server you want to connect to through a CLI instead of hardcoding it in your code. And for that, you’re going to write a basic CLI that takes the server as an argument.

00:14 Go ahead and open your terminal, and make sure to create a new file in src/mcp_client and call it cli.py. This file will define a CLI built with the package click, so you’ll want to run the command uv add click to add click as a dependency.

00:31 Once you’ve done that, open the file cli.py in your favorite code editor.

00:36 Go ahead and import asyncio because you’re going to need it to run your coroutines and import click, which you’re going to use to build your CLI.

00:46 Also from this package, hence the dot, from . you’re going to import your MCPClient class that you defined in the previous lessons.

00:56 Now in preparation for all of the upcoming lessons, you’re going to write a series of subcommands that allow you to interact with servers in different ways.

01:05 In order to be able to define subcommands, you need to define a click group that you’re going to call the mcp_cli. And this takes a short docstring saying that it’s the CLI for your MCP client.

01:20 Now that you’ve defined your mcp_cli group, you can use it as a decorator to create commands. So these commands will test the connection to the given MCP server.

01:37 It takes a single argument that you specify with the decorator click.argument, which is the server_command,

01:46 and then you want to write something like async def connect, this coroutine accepts the server_command that is a string and it returns None.

01:58 And you want to have the exact same code from before that uses an asynchronous context manager with your MCP client. Something like async with MCPClient the given server_command, print("Connected to the server!").

02:16 Now the problem is click does not work with coroutines, so you cannot make a click command out of a coroutine. The simplest workaround is to have a synchronous function that acts as an intermediary layer and that uses asyncio to run your coroutine.

02:33 What does this look like? You create a command called connect that accepts your server_command. It still returns None, and then it uses asyncio to run your underscore connect that accepts your server_command, and you just add a leading underscore here to distinguish the asynchronous function from the synchronous command.

03:00 Once this is in place, open the file pyproject.toml and look for the section that says project.scripts. In here, you’re going to take the :main, you’re going to delete it, and you’re going to write mcp_client.cli:mcp_cli.

03:18 This is telling uv that when you run the command mcp-client, it should look in the file cli of the package mcp_client and run the object called mcp_cli.

03:31 To test your client,

03:36 open your terminal and run the command uv run mcp-client --help.

03:42 You give it a second, and you should see some standard help message for your mcp-client commands. You will see in particular that at the bottom, it mentions the command connect that you can use to test the connection to the given server.

03:56 To take this for a spin, you run the command uv run mcp-client. This is what runs the CLI, and then you specify the subcommand connect, and finally the server you want to connect to, for example mcp-data.

04:12 You give it a second and you should see the successful connection message. You can now connect to arbitrary MCP servers as long as you can type the command that runs the server.

04:25 Now in the next lesson, you’re going to write a subcommand to list all of the prompts, resources, and tools that the given server exposes.

Become a Member to join the conversation.