Implementing Tools
00:00 Remember that a couple of lessons ago you learned that an MCP server can expose your LLM to tools. What are these tools?
00:10 Tools in the context of MCP are dynamic functions that interact with the environment, with your environment, and that an LLM might call when it determines that such a tool would be useful. For example, in the context of your sales data, you’re going to develop a tool that provides the LLM with the sales information for a specific customer. So, whenever you’re interacting with the LLM and you ask queries, you write prompts that are related to specific sales of specific customers, the LLM might determine that calling this function, so a tool really is just a Python function in the end, calling this function might be useful because it will provide extra information. So, let’s see how you can implement a tool in an MCP server in Python.
00:59
Go ahead and open the project that you set up previously and open the file main.py from the uv project you created. If you created the project with uv, you will have a couple of lines of code already, and if you created the project with some other tool, then the file main.py might be different.
01:19
What you want to do is delete the function main() and you’ll want to import a couple of modules that you’re going to need for this lesson, namely the module csv from the standard library and something from the package mcp, the dependency you installed when you set up the project. Specifically, you’ll want to go into mcp.server.fastmcp and you’re going to import the FastMCP class.
01:48
This is the class that you’re going to use to create your server. And go ahead, just instantiate the FastMCP class, give it a readable name, this is going to be your Sales MCP server, and you can save that as the variable mcp.
02:05
To create a tool in an MCP server, you need two things. You need the function that implements the functionality you care about and you need a decorator from the variable mcp, a decorator from your server.
02:18 What does that look like? Well, let’s start with the function. You’re going to write a function that’s going to get sales from a specific customer and that function accepts a customer name, which is a string.
02:32 It’s going to return a list of integers that represents all of the sales for that customer. It’s important that you use type hints for the return value and also a docstring because that’s the meta information that the LLM uses to determine whether or not to use your function.
02:48 So in here, a short docstring will be fine. You can write something like get a list of all sales totals for a given customer. And then you write the code that implements this function.
03:01 First, you’re going to initialize an empty list
03:05
and then you’re going to open your data file. Now this file, if you follow the setup instructions from a couple of lessons ago, should live in a subfolder called data. And you’re going to open the file for reading and then you’re going to use the module csv from the standard library to create a CSV reader, which provides a convenient way of working with CSV files in Python.
03:32
Now you’re going to go through the rows of the CSV and you’re going to split them into the name of the customer and the amount they paid. So you’re splitting the rows into the two columns. You’re going to check if the name of the current row matches the name that was passed in as an argument. And if it did match, you’re going to append the value paid to your list sales, which is the resulting list.
03:59 And once you’re done, you want to return those sales. The idea is that this function will potentially be called by an LLM when an LLM needs information from the sales of a specific customer.
04:12
But this is just a function for now. In order to turn it into a tool, you need to decorate the function with @mcp.tool. The @mcp should be the variable name of your MCP server.
04:30
You instantiated your FastMCP class on line six, so you reuse that variable. And the decorator .tool, make sure you call it with parentheses. You open and close parentheses, even though you’re not passing anything in.
04:46
This is how you define a tool in your MCP server. And in the end, when you run the file main.py, you want your MCP server to run, so you don’t want to run the function main(), that function no longer exists.
04:59
You want to run mcp.run().
05:03 This defines your tool. In the next lesson, you’re going to set up Cursor so that you can connect to your MCP server and test it out.
Become a Member to join the conversation.
