Calling Tools
00:00
In this lesson, you’re going to be calling tools from MCP servers. For that, you’re going to create a subcommand call. call will accept the arguments to the tool as an argument, and it’s then going to call the tool with those given inputs, and it will display the final result to the user.
00:18
Open the file cli.py, and at the top, you’re going to create a new command. Using the decorator mcp_cli.command, the help text for this command could be something like, call a tool, pretty simple, and then you’re going to accept a couple of different arguments this time. You’re going to accept the server command, as usual.
00:39 You’re going to accept the tool name, so you know what tool to call, and then you’re going to accept the arguments that are going to be passed into the tool.
00:49
All of these are strings that the function call must accept.
01:00
Now this function returns None, and as with every other subcommand, you’re going to use asyncio to run the coroutine _call that will be responsible for connecting to the server, getting the tool, and calling it.
01:18 you’re going to accept the same arguments that the function accepted,
01:24 and you’re going to use your asynchronous context manager to connect to the server.
01:37
Once you have your client, you’re going to access the client session, and from it, the function call_tool that accepts the tool name and the arguments.
01:49
At this point, arguments is a string, but if you go back to your terminal and you run uv run mcp-client ls mcp-data, you will see in the section about tools, you get this dictionary, which is the schema of the input for the tool.
02:06 And this shows that the input has to have a specific shape. So you can assume, for simplicity, that the string arguments already represents the input in the correct schema, you just need to convert it to a dictionary.
02:22
To do that, you can leverage the module json. So go to the top of your script, import json, and now go back to your coroutine, and before creating the client, create an arguments dictionary by using json.loads.
02:38
And this arguments dictionary is what gets passed to call_tool. Now call_tool is a coroutine, but you must await it. You can grab the result, and then you can print it for the user to see.
02:52
If you go back to the terminal, you will see that the tool that’s given will get a list of all sales totals for a given customer. Now the customers come from the file data/sales.csv.
03:05
If you run mcp-client fetch mcp-data, and then the given file, you can, if you squint, you can see the CSV that contains the customers Alice, Bob, and Charlie. Now Alice shows up twice with two sales of 100. So if you run mcp-client call from mcp-data, you’re going to run the
03:34
function get_sales_from_customer. And then you want to use quotes to be able to define the string that represents the input in the correct schema.
03:43
So that’s a dictionary that maps argument names like customer to their values. In this case, let’s say Alice. You can close the dictionary, you can close the single quotes.
04:01
in this case, you got an error because you have to be very careful with the input schema. For simplicity, your CLI expects the schema to be correct. Looking at the schema returned by the subcommand ls, you can look at the properties.
04:16
There’s a property called customer_name. And this is the correct name of the arguments, not customer. So you need to rewrite your command to call from the mcp-data server, get_sales_from_customer.
04:32
Now a single quote around the dictionary, the argument is called customer_name. Let’s go with Alice. You can close the dictionary, close the single quotes.
04:43
And when you run it, you get the answer. You can see towards the end, it says structuredContent, result: 100 100, the two sales from Alice.
04:54 So this is correct. And congratulations, you just finished writing an MCP client that through a CLI can test the connection to an arbitrary server, can list the members of that server, can get prompts, you can fetch resources and can call tools. And all of this allows you to test the MCP server that you connect to.
05:15 And in the next lesson, you’re going to recap everything you’ve learned so far.
Become a Member to join the conversation.
