AI
Backend

Building an AI-Compatible API Using MCP, Go, and Python with FastMCP

Learn how to use the MCP protocol to connect a REST API with AI agents. A complete guide with theory, key benefits, and a hands-on example using FastMCP in Python as a bridge.

C
Carlos Noronha
July 03, 202510 min read
Building an AI-Compatible API Using MCP, Go, and Python with FastMCP

Interfaces between artificial intelligences and legacy systems are evolving rapidly. The Model Context Protocol (MCP), proposed by OpenAI, allows AI agents to interact with real APIs using only natural language. In this article, we will explore MCP, its applications, advantages, future possibilities, and demonstrate a practical example of how to make your Go API compatible with AI agents using MCP and Python with FastMCP.


🤔 What Is MCP?

The Model Context Protocol (MCP) is a JSON-RPC based protocol designed to facilitate interaction between language models (LLMs) and real computational systems. With MCP, an AI agent can:

  • Discover tools and services (e.g., REST endpoints);
  • Understand how to use them (inputs, outputs, descriptions);
  • Invoke them using natural language prompts;
  • Receive standardized, interpretable results.

✅ Advantages of MCP

  • Standardizes communication between agents and APIs;
  • No need for language-specific wrappers: agents understand tools from a JSON manifest;
  • Speeds up AI integration: once MCP is exposed, any compatible agent can start using your API;
  • Decouples model and system: models interact with tools, not implementations.

🚀 The Future of MCP

MCP opens doors to a future where:

  • AI agents operate real-world applications as autonomous personal assistants;
  • Internal systems are exposed to assistants by prompts, with no need for SDKs;
  • Companies convert their APIs into conversational interfaces in a standardized way;
  • People with little technical knowledge interact with complex tools via natural language.

With a well-defined MCP layer, it's not the applications talking to AI but AI operating the applications.

⭐ Practical Applications

  • Legacy systems: create an MCP layer in front of ERP, CRM, or any backend;
  • Bots and assistants: AI that schedules meetings, queries data, and registers entities;
  • Low-code for AI: transform prompts into real operations in your current infrastructure.

The practical example and the Challenge with Go

Currently, MCP has no official library maintained by OpenAI or the community specifically for Go. Implementing the full protocol semantics manually means handling:

  • JSON-RPC 2.0;
  • Version control by date (e.g., protocolVersion like 2025-06-18);
  • Mandatory handshake fields (like initialize, capabilities, serverInfo);
  • Event streaming for agents.

Because of this, in this article we chose to use a lightweight intermediate layer in Python with the FastMCP library, which already implements these requirements and facilitates connecting any HTTP API (like our Go API) to MCP-compatible agents.

Note: You can replace this Python layer with another language like Node.js or Rust later, as long as it supports MCP requirements.

Solution Architecture

 
          AI Agent → FastMCP (Python) → Go API (Order Management)
        
mcp + rest flow

- The AI agent (e.g., GPT-4o) uses the openai-agents SDK;
- The FastMCP server exposes MCP tools based on HTTP calls to the Go API;
- The Go API is a simple REST service with /orders endpoints.

Step 1: Building the Go API

We create a simple REST API with these endpoints:

  • POST /orders to create an order;
  • GET /orders/{id} to fetch an order.

Expected JSON response example:

  {
    "id": 1,
    "customer": "Carlos",
    "product": "T-Shirt",
    "quantity": 2,
    "total_price": 199.99
  }
          

The API remains simple, unaware of MCP.

Step 2: Creating the MCP Server with FastMCP

Install required Python packages:

pip3 install fastmcp httpx

Create a file mcp_server.py:

from fastmcp import FastMCP
import httpx

API = "http://localhost:8080" # Go API URL

mcp = FastMCP("Orders-Go")

@mcp.tool(description="Create a new order")
async def create_order(customer: str, product: str, quantity: int):
    async with httpx.AsyncClient() as client:
        response = await client.post(f"{API}/orders", json={
            "customer": customer,
            "product": product,
            "quantity": quantity
        })
    return response.json()

@mcp.tool(description="Get an order by ID")
async def get_order(id: int):
    async with httpx.AsyncClient() as client:
        response = await client.get(f"{API}/orders/{id}")
    return response.json()

if __name__ == "__main__":
    mcp.run(transport="streamable-http", port=9000)
      

Now we have an MCP server that communicates with the Go API via HTTP.

Step 3: Connecting the AI Agent

Install the official OpenAI agents SDK:

pip3 install openai openai-agents openai-agents-mcp

Create a script run_agent.py:

from agents import Agent, Runner
from agents.mcp.server import MCPServerStreamableHttp
import asyncio

async def main():
    mcp_server = MCPServerStreamableHttp(
        params={"url": "http://localhost:9000/mcp/"}
    )
    await mcp_server.connect()

    agent = Agent(
        name="OrderBot",
        instructions="Use MCP tools to manage orders.",
        mcp_servers=[mcp_server],
        model="gpt-4o-mini",
    )

    result = await Runner.run(
        agent,
        "Create a new order for customer Carlos buying 3 black shirts"
    )
    print(result.final_output)

    await mcp_server.cleanup()

if __name__ == "__main__":
    asyncio.run(main())
      

In the script above, the example command is passed directly to the agent. However, in a real-world application, this message could come dynamically from user input in a chatbot interface or voice assistant — enabling natural language interaction with your systems via AI.

** The expected result is that the AI understands the prompt and calls the real Go API via MCP.

-> Future and Expansion

  • Support for authentication on endpoints;
  • Automatic versioning and support for MCP protocol updates using FastMCP;
  • Integration with other APIs without writing a single prompt;
  • Replacing the Python layer with an official or mature Go implementation when available.

Conclusion

Using MCP with Go is totally feasible by leveraging a lightweight intermediary server like FastMCP. This avoids rework, keeps up with protocol changes, and allows any existing REST API to become "AI-friendly" with minimal effort. This bridge between classic APIs and artificial intelligences is shaping the future of software development.

The complete source code is available on GitHub betonr/mcp-rest-integration.

FAQ

► Do I need to modify my API to support MCP?

No. Your API can remain a traditional REST service. MCP acts as a descriptive layer translating natural language into regular HTTP calls.

► Why use Python for the MCP server if my application is in Go?

Because there is no mature MCP implementation in Go yet. Using an external layer like FastMCP in Python allows you to validate the concept quickly.

► Is MCP compatible with GraphQL or gRPC APIs?

Not directly yet. The current focus is on HTTP-based JSON tools. You can create REST adapters to GraphQL or gRPC if needed (at least I don't know at the time of writing this article).

► Is OpenAI the only company using MCP?

Currently yes, but the protocol is open and expected to be adopted by others as the ecosystem evolves.

► How to keep compatibility with new protocol versions?

Tools like FastMCP already implement automatic support for the latest version (e.g., 2025-06-18). If you implement your own server, keep an eye on the official documentation.

MCP
IA Agents
Python
Golang
FastMCP
APIs
OpenAI
Compartilhar:
C

Carlos Noronha

Full Stack Software Engineer

Leia também