Podcast Title

Author Name

0:00
0:00
Album Art

Connecting AI to Your Python API: A 5-Minute Guide to MCP Servers

By 10xdev team August 16, 2025

In this article, you will learn how to quickly convert any Python server into an MCP (Model-Connectable Point) server, a crucial step that allows any AI model to connect with your internal systems. We'll demonstrate this process using a sample Book API built to teach how REST APIs function. This API can retrieve and create books in a database, and by the end of this guide, we will have an AI creating the books for us.

Understanding the Base API

First, let's explore how the API works without any AI involvement to get a clear picture of the project.

In the project folder, we have our REST API file. We can start the server by running the following command:

python rest_server.py

This command initiates the server on port 8,000.

To interact with the API, we can use a curl command. For instance, to retrieve all books from the database, we execute the following:

curl http://localhost:8000/books

This sends a GET request to the server, which returns a list of pre-existing books from the database:

[
  {"id": 1, "title": "The Great Gatsby", "author": "F. Scott Fitzgerald"},
  {"id": 2, "title": "To Kill a Mockingbird", "author": "Harper Lee"},
  {"id": 3, "title": "1984", "author": "George Orwell"}
]

While this is a standard REST API, an AI cannot call it directly. To enable AI interaction, we must convert this server into an MCP server.

The Conversion Process: From REST to MCP

Fortunately, since our server uses FastAPI, a popular Python framework, we can leverage existing projects to handle this conversion automatically. For this guide, we'll use FastAPI-MCP, an incredibly straightforward tool that offers an almost drag-and-drop solution.

Here’s how to configure it in just a few steps:

Step 1: Install the Library

First, we need to install the FastAPI-MCP library using pip. Stop the running server and execute this command in your terminal:

pip install fastapi-mcp

Step 2: Update the Server File

Next, we need to add a couple of lines to our server file.

  1. Import the FastAPI-MCP library at the top of your file:

    from fastapi_mcp import MCPMiddleware
    
  2. Wrap the FastAPI app instance with the MCPMiddleware. To ensure all API endpoints are included, it's best to add these lines at the very end of the file, right before the server is run.

    # Assuming your FastAPI instance is named 'app'
    app.add_middleware(MCPMiddleware)
    

With these changes, the server is ready. Let's restart it:

python rest_server.py

You won't notice any immediate changes. The REST API will function as it did before. Running the curl command again will return the same list of books, confirming that we haven't broken any existing functionality.

Connecting the AI Assistant

Now, let's connect an AI tool, such as GitHub Copilot or a cloud-based AI desktop, to our newly created MCP server. The process is generalized for any MCP-compatible tool.

In your AI tool's settings, look for an option to add a new tool or connect to an MCP server. You'll be presented with several options. When converting a REST API, most frameworks expose it as an HTTP server with server-sent events. This setup allows MCP servers to be hosted in the cloud for broader access, but for our local setup, we'll stick with a direct HTTP connection.

You'll need to provide the server's URL. The standard URL for the auto-generated MCP server is app_base_url/mcp. Since our server is running locally on port 8000, the URL will be:

http://localhost:8000/mcp

Note: We use http because the local server is not running with SSL/TLS.

After entering the URL and giving the connection a descriptive name (e.g., "Books MCP Server"), save the settings. Your AI tool will then perform tool discovery by sending requests to this endpoint to understand what tools are available.

The FastAPI-MCP framework intelligently takes your existing REST API endpoints and converts them into logical tools for the AI. For example, the GET /books endpoint we tested earlier is automatically transformed into a get_all_books tool that the AI can use.

Putting the AI to Work

Let's test this integration with a couple of practical examples.

1. Retrieving All Books

In a new session with your AI assistant (ensuring it has access to the new "Books MCP Server" tool), ask a simple question:

"retrieve all my books"

The AI will recognize that it has access to a books API and will correctly choose to run the get_all_books tool. After you approve the action, the command will execute, and the AI will present the data in a nicely formatted list based on the server's output. We can verify this is correct by seeing "To Kill a Mockingbird" in the list, which we know is part of our initial sample data.

2. Adding a New Book

Now for the exciting part: letting the AI add a new book to our database. Let's give it a creative prompt:

"add any book you like to my database"

This is a powerful example. Imagine you just finished a book; you could provide a quick summary and review, and the AI could automatically add it to your database.

The AI will identify the correct tool for creating a book and generate a request payload. For instance, it might create a request like this:

{
  "title": "The Hitchhiker's Guide to the Galaxy",
  "author": "Douglas Adams",
  "summary": "A comedic science fiction series created by Douglas Adams."
}

After the AI confirms the book has been added, we can verify this independently.

Final Confirmation

Let's go back to our terminal and run the original curl command one more time to fetch all books:

curl http://localhost:8000/books

The output will now include the new entry:

[
  {"id": 1, "title": "The Great Gatsby", "author": "F. Scott Fitzgerald"},
  {"id": 2, "title": "To Kill a Mockingbird", "author": "Harper Lee"},
  {"id": 3, "title": "1984", "author": "George Orwell"},
  {"id": 4, "title": "The Hitchhiker's Guide to the Galaxy", "author": "Douglas Adams"}
]

The presence of "The Hitchhiker's Guide to the Galaxy" confirms that the AI successfully interacted with our API to add a new entry to the database.

This is just the beginning. For real-world applications, you can explore more advanced scenarios, such as exposing only certain routes to the AI or adding authentication layers to ensure your AI can safely access internal services and even confidential data.

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.

Recommended For You

Up Next