Getting started

Python

Let's Build a Weather Tool with FastMCP and an AI Client

Want to teach an AI how to check the weather? You're in the right place. This guide walks you through building a simple tool using FastMCP, which makes it incredibly easy to create MCP (Model Context Protocol) servers that let your code talk directly to AI assistants.

We'll use Claude Desktop as an example AI client in this guide because it has built-in support for MCP — but the tool you build can work with any AI that understands MCP, like ChatGPT Desktop or Raycast AI, for example.


Setup Hackatime

You'll need to use Hackatime to track your coding time. Hackatime works on all major code editors.

Set it up with the instructions here: https://hackatime.hackclub.com


What is MCP?

In short, MCP (Model Context Protocol) servers allow AI to connect to real tools. Think of it as USB ports for AI apps.

Here's how it works:

  • You define what inputs your tool needs
  • You write a function that does something useful with those inputs
  • MCP acts as the bridge so the AI can understand how to use it

Setting Up Your Project

We'll be writing this in Python. You can install Python from https://python.org/ (make sure to get Python 3.9 or higher).

Install FastMCP, which makes building MCP servers much easier:

pip install fastmcp

I'm going to call my project mcp-weather-server.

If python3 doesn't work on your system, try python. On some systems, both commands point to the same Python installation.


Writing Your Tool

Create a file called mcp_weather_server.py (note the underscores - this avoids import conflicts). We'll build this step-by-step.

1. Add the shebang

At the very top of your mcp_weather_server.py file, add this line:

#!/usr/bin/env python3

This tells the system how to run your script and makes it executable on all platforms.

Note: We use python3 instead of node since this is a Python project. The shebang ensures your script runs with the correct Python interpreter.

2. Import the libraries

import requests
from fastmcp import FastMCP
from pydantic import Field

3. Create the FastMCP server

mcp = FastMCP("My Weather Server")

4. Write and register your tool

With FastMCP, you can define your tool function and register it in one step using the @mcp.tool decorator:

@mcp.tool
def get_weather(city: str = Field(description="The city to get the weather for. Needs to be a big city.")) -> str:
    """Get the weather for a city."""
    response = requests.get(f"https://goweather.xyz/weather/{city.lower()}")
    if not response.ok:
        raise Exception(f"Couldn't get weather for {city}")

    data = response.json()
    return str(data)

If you need to add additional fields, just expand the parameters of the function using Field(). For example:

def get_weather(
    city: str = Field(description="The city to get weather for"),
    units: str = Field("metric", description="Temperature units (metric/imperial)")
) -> str:

5. Start the server

Add a main function and entry point to run your server:

def main():
    mcp.run()

if __name__ == "__main__":
    main()

That's it! Your complete FastMCP server is ready. Here's the full code:

#!/usr/bin/env python3

import requests
from fastmcp import FastMCP
from pydantic import Field

mcp = FastMCP("My Weather Server")

@mcp.tool
def get_weather(city: str = Field(description="The city to get the weather for. Needs to be a big city.")) -> str:
    """Get the weather for a city."""
    response = requests.get(f"https://goweather.xyz/weather/{city.lower()}")
    if not response.ok:
        raise Exception(f"Couldn't get weather for {city}")

    data = response.json()
    return str(data)

def main():
    mcp.run()

if __name__ == "__main__":
    main()

Testing it locally

MCP comes with a really cool inspector. To run it, simply enter the following command:

npx @modelcontextprotocol/inspector python3 mcp_weather_server.py

You'll get a URL in the console that you can open. Simply click the connect button on the bottom left!


Testing It in Claude Desktop

FastMCP makes it incredibly easy to install your server in Claude Desktop. Just run this single command:

fastmcp install claude-desktop mcp_weather_server.py --with requests

This command:

  • Automatically configures Claude Desktop
  • Installs your server and its dependencies

About the --with flag: This tells FastMCP to install the requests library that your weather server needs. You can add multiple dependencies: --with requests --with pandas

Need environment variables? Add them with the --env flag: --env API_KEY=your-key

Now restart Claude Desktop completely and try it out!

  1. Quit and re-open Claude Desktop
  2. Try asking: "What's the weather in Berlin?"

If all goes well, Claude should show the result returned by your tool. The FastMCP CLI handled all the configuration automatically!


Publishing Your Tool

To share your MCP server with the world, you'll need to publish it to PyPI (Python Package Index). Create a pyproject.toml file:

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "mcp-weather-server"
version = "1.0.0"
description = "A weather MCP server built with FastMCP"
authors = [{name = "Your Name"}]
dependencies = ["fastmcp", "requests"]
requires-python = ">=3.9"

[project.scripts]
mcp-weather-server = "mcp_weather_server:main"

Then publish it:

pip install build twine
python3 -m build
twine upload dist/*

Once published, others can install it directly from PyPI:

fastmcp install claude-desktop --from-pip mcp-weather-server

This makes your tool easily installable by anyone!


What's Next?

Now that you've got the basics, try branching out. Your AI client can use any tool — as long as you define inputs and return something usable.

Here are some inspiration, but you should come up with something else!

  • Pull info from other APIs (Wikipedia, Crypto prices, NASA, anything)
  • Automate stuff locally (play a sound, take a screenshot, launch apps)
  • Build utilities (unit converter, joke fetcher, motivational quote machine)
  • Fetch a random XKCD comic
  • Turn on your smart lights
  • Log thoughts to a text file
  • Update you on the most recent news stories

Submission requirements

To submit you must:

  • Have all your code in a public GitHub repo
  • Publish your server to PyPI
  • Have a README in your GitHub repo with installation instructions using the FastMCP CLI

Your README should include installation instructions like:

fastmcp install claude-desktop --from-pip your-package-name

If you need any help, reach out in the #toolsmith Slack channel!

Ready to Submit?

Once you've built your MCP tool and tested it, you can submit it to earn your $10 in AI credits!

Submit Your Tool