Get startedGet started for free

Building Your First MCP Server

1. Building Your First MCP Server

Now you've begun to explore an MCP server, let's learn how to build one!

2. Converting Between Timezones

We'll build an MCP server that uses a free, public API for converting datetimes between timezones. This server will take a datetime, its timezone, and a timezone to convert to, and we receive an updated datetime in the desired timezone. This could be used in a travel application to convert between times in the user's timezone and the timezone of their destination, for instance, when booking hotels and activities.

3. MCP Server Code

Our server code will be made up of three key parts: instantiating, or booting-up, the blank server; defining the tools we want it to use, a timezone converter in this case; and finally, adding docstrings and type hints to our tools, which allow the LLM to intelligently choose which tool to use. Let's code this out!

4. Step 1: Instantiating the Server

To start, we'll instantiate the MCP server, which will be empty to start with. There are a few ways to do this, but we'll be using the official MCP Python SDK. This library contains a handy FastMCP class for us to instantiate in one line of code. We'll give this server a descriptive name to make it easier to reference.

5. Step 2: Adding the Tools

Next, we define a tool for the server to use. We only need one tool right now for the timezone conversion, but you could add more later to give it additional functionality. Tools are added using the @mcp.tool() decorator. Each time we use this decorator, the functions that are used by this tool are registered to the MCP server and become discoverable by MCP clients. A tool can be any Python function; for example, sending an API request to book a flight or a function to connect to a database to retrieve a list of available hotels. Our convert_timezone() function will take a datetime, the timezone it was recorded in, and convert it to a desired timezone.

6. Step 2: Adding the Tools

We define the url for the OpenTimezone API endpoint as a string, and create a JSON payload to send in our request. Here, containing the initial datetime, the current timezone, and the target timezone. We send our payload to the url using the post() function from requests. Then, we collect the resulting JSON, and use the .get() method to extract the new "dateTime" data. Finally, we return the data in a nicely-formatted string. Our function is now operational, but to make sure the LLM has sufficient information to choose and use this tool correctly, we'll add our final component: docstrings and type hints.

7. Step 3: Docstrings and Type Hints

Type hints and docstrings are best practice generally, but for defining tools, they're an absolute necessity. Not providing them could be the difference between a functional AI system and a broken one. Here, we clearly type each function argument, and the return object, which will all be strings in this case. The docstring contains a clear and concise usage description, along with definitions for each argument and return object.

8. MCP Server Code

And that's your server built! Remember, any Python function can become a tool for an MCP server. You just need to instantiate the server, define the tools as Python functions, use the @mcp.tool() decorator, and add comprehensive docstrings and typing.

9. Let's practice!

Time to have a go at building your own MCP server!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.