The agent runs on a local machine in this tutorial but could also be embedded as a remote application.
meta: title: Connect Generative APIs to Serverless SQL with the Model Context Protocol description: Create an intelligent agent that can analyze your Serverless SQL data using natural language queries through Model Context Protocol. content: h1: Connect Generative APIs to Serverless SQL with the Model Context Protocol paragraph: Create an intelligent agent that can analyze your Serverless SQL data using natural language queries through Model Context Protocol. tags: AI MCP SQL agent python database categories:
- generative-apis
- serverless-sql dates: validation: 2025-05-13 posted: 2025-05-13
Model Context Protocol (MCP) is an open, standardized communication protocol that enables Large Language Models to interact with external tools and services through a defined interface.
This tutorial demonstrates how to build a data analysis agent that connects Scaleway’s Generative APIs with Serverless SQL using MCP. You’ll learn how to create an AI agent that can:
- Understand your database schema automatically
- Convert natural language questions into SQL queries
- Execute queries and present results in a human-friendly format
Before you startLink to this anchor
To complete the actions presented below, you must have:
- A Scaleway account logged into the console
- Owner status or IAM permissions allowing actions in the intended Organization
- Python 3.9 or higher
- An API key from Scaleway Identity and Access Management
- Access to Scaleway Generative APIs and Serverless SQL
IntroductionLink to this anchor
The solution consists of three main components:
- Generative APIs: Provides the AI model that processes natural language queries
- Serverless SQL: Stores and manages the data
- Local AI agent: Coordinates between the AI model and database
Configuring Scaleway servicesLink to this anchor
Set up Generative APIsLink to this anchor
- Go to the Scaleway console and navigate to Generative APIs
- Select a model (Mistral Small is used for this tutorial)
- Click *View Code and note down the base URL and model name
Configure Serverless SQLLink to this anchor
- Go to Databases > Serverless SQL
- Create a new database with default settings
- Click Connect application > Connect with an existing IAM Secret Key
- Switch to the Connection string tab and copy the connection string
Seed the databaseLink to this anchor
Connect to your database and run the following SQL to create a test schema with some data:
CREATE TABLE Sales (Product VARCHAR(50),Buyer VARCHAR(50),Timestamp TIMESTAMP);INSERT INTO Sales (Product, Buyer, Timestamp) VALUES('Laptop', 'Alice', NOW() - INTERVAL '2 days'),('Tablet', 'Bob', NOW() - INTERVAL '3 days'),('Webcam', 'Ivan', NOW() - INTERVAL '30 minutes'),('Printer', 'Judy', NOW() - INTERVAL '12 hours');
Setting up the development environmentLink to this anchor
The AI Agent is built in Python using the fast-agent open-source framework which has native support for the MCP protocol.
The postgres-mcp MCP server is used to communicate with Serverless SQL.
-
Install a package manager.
uv
is the recommended package manager byfast-agent
:curl -LsSf https://astral.sh/uv/install.sh | sh -
Create and activate a virtual environment:
uv venv -
Install required libraries:
uv pip install fast-agent-mcp postgres-mcp
These libraries work together to create a secure and efficient bridge between the AI model and your database.
Creating the AI AgentLink to this anchor
Configure the agentLink to this anchor
Create a file called fastagent.config.yaml
:
default_model: "generic.<Your model name>"generic:api_key: "<Your Scaleway API Key>"base_url: "<Your Generative API Base URL>"mcp:servers:database:command: "uv"args: ["run", "postgres-mcp"]env:DATABASE_URI: "<Your Serverless SQL Connection String>"
Create the agent scriptLink to this anchor
Create a file called agent.py
which will contain your agent logic.
-
Import the necessary modules and initialize the agent:
import asynciofrom mcp_agent.core.fastagent import FastAgentfast = FastAgent("AI Agent") -
Create agents with different responsibilities:
@fast.agent("db_schema_reader",instruction="""Get the details of the 'sales' table in the 'public' schema of the database, which contains the sales data. Only return the column names and types.""",servers=["database"],)@fast.agent("query_writer",instruction="""Write a SQL query to fetch the sales data from the database (sales table) given the input constraint. Only return the SQL query, no other text.""")@fast.agent("query_executor",instruction="""Execute the following SQL statement on the database and write the results in JSON format""",servers=["database"],)NoteEach agent has a specific role. Some agents need direct database access through the MCP server (using the
servers=["database"]
parameter) while others don’t -
Create an orchestrator that coordinates the specialized agents:
@fast.orchestrator(name="sales_analyst",agents=["db_schema_reader","query_writer","query_executor"],plan_type="iterative",plan_iterations=4,instruction="You are a sales analyst given an input criteria. Describe the corresponding sales data from the database in one sentence.")The orchestrator:
- Determines which agents to use and in what order
- Sets an overall goal for the agent system
- Uses an iterative planning approach to refine the results
- Limits the number of iterations to prevent infinite loops
-
Create an entrypoint running the data analysis query Get sales from last day:
async def main() -> None:async with fast.run() as agent:await agent.sales_analyst("Get sales from last day")if __name__ == "__main__":asyncio.run(main()) -
Run the script with:
python agent.py
The fast-agent
framework provides details of all operations and outputs its data analysis:
The sales data from the last day includes two transactions: a webcam purchased by Ivan at 14:52 and a printer purchased by Judy at 03:22.
This tutorial demonstrated how to create an AI agent that can analyze your SQL database using natural language queries. By combining Scaleway Generative APIs with Serverless SQL through Model Context Protocol, you can create powerful tools that are accessible to non-technical users.