Discuter avec les données (serveur MCP + LLM) #61

Closed
opened 2025-11-29 00:43:45 +01:00 by ColinMaudry · 7 comments
ColinMaudry commented 2025-11-29 00:43:45 +01:00 (Migrated from github.com)
Voir https://github.com/punkpeye/awesome-mcp-servers
ColinMaudry commented 2025-11-29 01:09:37 +01:00 (Migrated from github.com)

Turn Natural Language into SQL Queries! PostgreSQL MCP Server + AI Chatbot Tutorial
https://www.youtube.com/watch?v=Xj29pDlF4QI

Question to Gemini: I have a dataset stored in Parquet (but could be PosgreSQL) about public procurement contracts and I currently let my users explore this data through a Plotly Dash Web application.

The data is produced thanks to a data pipeline made of Prefect and Polars.

The data is not big (the parquet file weighs 300 MB).

What would be a straighforward approach to enable my users to ask questions to the data in natural language? For instance: "what are the to 10 suppliers of city XYZ in value?" (and more complex questions of that style).

Would using an MCP server help (in a Posgre scenario)? I heard of FastMCP.


Yes, a FastMCP server can definitely help in a PostgreSQL scenario, especially if you plan to integrate with advanced AI agents or LLM platforms that support the Model Context Protocol (MCP).

While the previous approach of using an LLM to generate a basic SQL query is effective, an MCP server provides a more structured, secure, and standardized bridge between your LLM and your PostgreSQL database.


💡 What a FastMCP Server Does for Natural Language Querying

FastMCP is a Python framework that simplifies building an MCP server. The Model Context Protocol (MCP) is a standardized way for an LLM to interact with external systems, like your database, using defined "Tools" and "Resources."

1. Structured Interaction (Tools & Resources)

Instead of giving the LLM the entire schema and asking it to write an arbitrary SQL query, you expose specific, safe actions via the MCP server:

  • Tools (Actions): You define specific Python functions, decorated as "tools," that the LLM can call. For PostgreSQL, this would include a safe execute_read_only_query(sql_query: str) tool.
  • Resources (Read-Only Data): You expose the database schema (table names, columns, and data types) as a "resource" the LLM can read.

2. Enhanced Safety and Control

A dedicated PostgreSQL MCP server (like the ones built with FastMCP) offers critical security advantages:

  • Read-Only Enforcement: You can program the execute_query tool to only accept SELECT statements, preventing accidental or malicious data modification (UPDATE, DELETE, DROP TABLE).
  • SQL Injection Prevention: The server often uses parameterized queries internally. The LLM generates the SQL with placeholders, and the server safely binds the user's values to those placeholders, eliminating the risk of SQL injection.

3. Seamless LLM Integration

MCP is designed specifically for LLM communication. Once your server is running, any LLM client that supports the MCP standard (e.g., certain AI assistants, IDEs, or custom agents) can automatically discover the database schema and the available query tools.

4. Workflow in a PostgreSQL/FastMCP Scenario

  1. User Question: "What are the top 10 suppliers of city XYZ in value?"
  2. LLM Agent (Client): The LLM uses the available Schema Resource to understand the table structure and then calls the Query Tool.
  3. MCP Server: The tool receives the request and generates a safe, parameterized SQL query (e.g., SELECT supplier_name, SUM(contract_value) AS total_value FROM contracts WHERE city_name = %s GROUP BY 1 ORDER BY 2 DESC LIMIT 10;).
  4. PostgreSQL: The server executes the query on your database.
  5. Result: The data is returned to the LLM agent, which converts the result (a table of 10 rows) back into a natural language summary or a visualization for the user in your Dash app.

Recommendation

If you decide to migrate from Parquet/Polars to PostgreSQL for your data store, then using a FastMCP server is a highly recommended way to implement NLQ.

It provides a more robust, secure, and enterprise-friendly version of the basic Text-to-SQL approach, especially because you are already familiar with Prefect (a company that has supported the FastMCP/MCP ecosystem).

The video below offers a tutorial on using a PostgreSQL MCP server to turn natural language into SQL queries for a chatbot. [Turn Natural Language into SQL Queries! PostgreSQL MCP Server + AI Chatbot Tutorial](https://www.youtube.com/watch?v=Xj29pDlF4QI)

Turn Natural Language into SQL Queries! PostgreSQL MCP Server + AI Chatbot Tutorial https://www.youtube.com/watch?v=Xj29pDlF4QI <details><summary>Question to Gemini: I have a dataset stored in Parquet (but could be PosgreSQL) about public procurement contracts and I currently let my users explore this data through a Plotly Dash Web application. The data is produced thanks to a data pipeline made of Prefect and Polars. The data is not big (the parquet file weighs 300 MB). What would be a straighforward approach to enable my users to ask questions to the data in natural language? For instance: "what are the to 10 suppliers of city XYZ in value?" (and more complex questions of that style). Would using an MCP server help (in a Posgre scenario)? I heard of FastMCP.</summary> Yes, a **FastMCP server** can definitely help in a PostgreSQL scenario, especially if you plan to integrate with advanced AI agents or LLM platforms that support the **Model Context Protocol (MCP)**. While the previous approach of using an LLM to generate a basic SQL query is effective, an MCP server provides a more **structured, secure, and standardized bridge** between your LLM and your PostgreSQL database. --- ## 💡 What a FastMCP Server Does for Natural Language Querying FastMCP is a Python framework that simplifies building an MCP server. The **Model Context Protocol (MCP)** is a standardized way for an LLM to interact with external systems, like your database, using defined "Tools" and "Resources." ### 1. **Structured Interaction (Tools & Resources)** Instead of giving the LLM the entire schema and asking it to write an arbitrary SQL query, you expose specific, safe actions via the MCP server: * **Tools (Actions):** You define specific Python functions, decorated as "tools," that the LLM can call. For PostgreSQL, this would include a safe `execute_read_only_query(sql_query: str)` tool. * **Resources (Read-Only Data):** You expose the database schema (table names, columns, and data types) as a "resource" the LLM can read. ### 2. **Enhanced Safety and Control** A dedicated PostgreSQL MCP server (like the ones built with FastMCP) offers critical security advantages: * **Read-Only Enforcement:** You can program the `execute_query` tool to only accept `SELECT` statements, preventing accidental or malicious data modification (`UPDATE`, `DELETE`, `DROP TABLE`). * **SQL Injection Prevention:** The server often uses **parameterized queries** internally. The LLM generates the SQL with placeholders, and the server safely binds the user's values to those placeholders, eliminating the risk of SQL injection. ### 3. **Seamless LLM Integration** MCP is designed specifically for LLM communication. Once your server is running, any LLM client that supports the MCP standard (e.g., certain AI assistants, IDEs, or custom agents) can automatically discover the database schema and the available query tools. ### 4. **Workflow in a PostgreSQL/FastMCP Scenario** 1. **User Question:** "What are the top 10 suppliers of city XYZ in value?" 2. **LLM Agent (Client):** The LLM uses the available **Schema Resource** to understand the table structure and then calls the **Query Tool**. 3. **MCP Server:** The tool receives the request and generates a **safe, parameterized SQL query** (e.g., `SELECT supplier_name, SUM(contract_value) AS total_value FROM contracts WHERE city_name = %s GROUP BY 1 ORDER BY 2 DESC LIMIT 10;`). 4. **PostgreSQL:** The server executes the query on your database. 5. **Result:** The data is returned to the LLM agent, which converts the result (a table of 10 rows) back into a natural language summary or a visualization for the user in your Dash app. --- ## ✅ Recommendation If you decide to migrate from Parquet/Polars to **PostgreSQL** for your data store, then using a **FastMCP server** is a highly recommended way to implement NLQ. It provides a more robust, secure, and enterprise-friendly version of the basic Text-to-SQL approach, especially because you are already familiar with **Prefect** (a company that has supported the FastMCP/MCP ecosystem). The video below offers a tutorial on using a PostgreSQL MCP server to turn natural language into SQL queries for a chatbot. [[Turn Natural Language into SQL Queries! PostgreSQL MCP Server + AI Chatbot Tutorial](https://www.youtube.com/watch?v=Xj29pDlF4QI)](https://www.youtube.com/watch?v=Xj29pDlF4QI) </details>
ColinMaudry commented 2025-12-03 17:38:10 +01:00 (Migrated from github.com)
MCP in ChatGPT : https://platform.openai.com/docs/mcp
ColinMaudry commented 2025-12-08 15:07:15 +01:00 (Migrated from github.com)

Frontier tech club case study (thanks @jpmckinney):

Frontier tech club case study (thanks @jpmckinney): - [part 1](https://www.frontiertechhub.org/insights/building-an-llm-application-for-international-development-technical-journey-and-learnings-part1) - [part 2](https://www.frontiertechhub.org/insights/building-an-llm-application-for-international-development-technical-journeys-and-learnings-part-2) - [part 3](https://www.frontiertechhub.org/insights/building-an-llm-for-international-development-technical-journeys-and-learnings-part-3)
ColinMaudry commented 2025-12-29 15:47:02 +01:00 (Migrated from github.com)
https://docs.supavec.com
ColinMaudry commented 2026-01-20 23:33:40 +01:00 (Migrated from github.com)
Voir https://vanna.ai
ColinMaudry commented 2026-06-28 10:13:55 +02:00 (Migrated from github.com)

Ajouter un llms.txt à la racine qui explique comment utiliser l'API.

Ajouter un llms.txt à la racine qui explique comment utiliser l'API.
ColinMaudry commented 2026-07-13 14:09:49 +02:00 (Migrated from github.com)

Finalement je me suis branché sur la solution de dash #111.

Finalement je me suis branché sur la solution de dash #111.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: colin/colibre#61