Create and Update a Fast API Server with Gemini CLI in 5 Minutes
This article explains how to create a simple Fast API MCP server, connect it to the Gemini CLI, and then use the CLI itself to dynamically add new functionality to the server.
Setting Up the Initial Server
First, we start with a basic Fast API MCP server that includes a couple of tools: add
and multiply
. The server is configured to listen on port 8,000.
To get started, we'll build and run the Docker container for our server. The following commands will handle the entire process:
# Build the docker container
docker build -t mcp-server .
# Run the docker container
docker run -p 8000:8000 mcp-server
After running the commands, you can verify that the container is active and listening on the correct port by using the docker ps
command. You should see the server running and mapped to port 8,000.
Connecting the Server to Gemini CLI
Next, we need to make the Gemini CLI aware of our new MCP server. To do this, we'll modify the settings.json
file, which can be found in your home directory's .gemini
folder.
We will add the following configuration to connect to the remote MCP server. This setup uses the mcp-remote
Node.js module for transport.
{
"mcp_servers": [
{
"transport": "node_module",
"options": {
"module": "mcp-remote",
"args": ["/path/to/your/project/a.py"]
}
}
]
}
Once this configuration is saved, starting the Gemini CLI will show a message indicating the connection to the server:
Using 1 mcp server (Ctrl+T to view)
By pressing Ctrl+T
, you can view the tools available from our server, which currently include add_two_numbers
and multiply_two_numbers
.
Let's test it with a simple prompt:
"Can you add two and three?"
The CLI will recognize the add
function from our MCP server and ask for permission to execute it. After granting permission, it will return the correct answer.
Updating the Server with AI
Here's where things get interesting. We can use the Gemini CLI to modify its own toolset by updating the server's source code.
We'll ask the CLI to add a new divide
tool to our server.
"Can you add a divide tool to our mcp server at a.py?"
The CLI will analyze the existing file (a.py
) and generate the necessary Python code to add the new tool. It will then present the proposed changes for approval.
The generated code even includes an exception to handle divide-by-zero errors, showcasing its ability to write robust code.
After applying the changes, we need to rebuild and restart our Docker container for the updates to take effect.
# Rebuild the docker image
docker build -t mcp-server .
# Relaunch the container
docker run -p 8000:8000 mcp-server
Verifying the Changes
After restarting the Gemini CLI and pressing Ctrl+T
, you will now see the newly added divide_two_numbers
tool listed alongside the original ones.
Let's test our new tool:
"Can you divide 3 by 5?"
The CLI will again ask for permission, and upon granting it, will execute the function from our updated MCP server and provide the result.
A quick look at the a.py
file confirms the changes. The CLI has successfully added the divide_two_numbers
function with the correct function name, description, and summary, making it a powerful mechanism for dynamically extending toolsets.
Here is the code that was added:
@tool
def divide_two_numbers(a: float, b: float) -> float:
"""
Divides the first number by the second number.
"""
if b == 0:
raise ValueError("Cannot divide by zero.")
return a / b
This demonstrates a powerful workflow where the AI not only consumes tools but also actively participates in creating and extending them.
Join the 10xdev Community
Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.