
How to setup MCP with UV in Python the right way!
One of the most challenging things with MCP is setting it up with uv and then testing the mcp server. This short blog covers this process.
Create a new directory and cd into it:
1
2
cd ~
mkdir mcp-example && cd mcp-example Then create a uv managed package:
1
uv init --python 3.12.9NOTE: You will need to ensure python 3.12 or later is installed.
Create a virtual environment:
1
uv venvNOTE: You can optionally give it a name
uv venv my-venv otherwise .venv will be created. Activate the virtual environment:
1
source ./venv/bin/activateInstall mcp:
1
uv add "mcp[cli]"Add this code into the main.py file that was created:
1
2
3
4
5
6
7
8
9
10
11
12
13
from mcp.server.fastmcp import FastMCP
# Create an MCP server
mcp = FastMCP("Weather Service")
@mcp.tool()
def get_hello(name: str) -> str:
"""Get hello `name` message from server."""
return f"Hello {name}. Hope you are having a great day!"
# Run the server
if __name__ == "__main__":
mcp.run()Test the server with Inspector:
1
2
3
4
5
uv run mcp dev main.py
or
npx @modelcontextprotocol/inspector uv --directory ./ "run" "main.py"
And thats it. Now you are ready to connect your MCP server to a tool like Amazon Q CLI or even to your own AI agent using the Strands Agents SDK .
Peace!
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article