MCP数据库查询服务

通过模型上下文协议(MCP)将LLM/客户端与SQLite数据库安全连接,提供数据表查询和SQL执行能力,适用于构建智能代理驱动的数据库交互场景。

  • by gowshik29
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 gowshik29/MCP-Dummy-DB-Integration-and-Data-Retrieval

使用说明

MCP Dummy DB Integration POC

This project demonstrates how to use the Model Context Protocol (MCP) to act as a connector between an LLM/Client and a database.

Overview

The system consists of three main components:

  1. Database: A dummy SQLite database (dummy.db) containing employees, projects, and issues.
  2. MCP Server (mcp_server.py): Exposes tools to query the database securely. It uses the mcp Python SDK to define tools like query_database and list_tables.
  3. MCP Client (client.py): Simulates an Agent/LLM that connects to the server via standard input/output (stdio) and calls tools to retrieve data.

Prerequisites

  • Python 3.10+
  • mcp package

Setup

  1. Install Dependencies:

    pip install -r requirements.txt
    
  2. Initialize Database: Run the setup script to create dummy.db and populate it with sample data.

    python setup_db.py
    

Running the Demo

Run the client script to simulate the data retrieval flow:

python client.py

Expected Output

The client will:

  1. Connect to the MCP server.
  2. List available tools.
  3. List tables in the database.
  4. Execute a SELECT query to fetch employees in the 'AI' department.
  5. Demonstrate error handling for invalid or forbidden queries.

Agentic Workflow Demo

The project includes an agent_workflow.py script that demonstrates a simulated Agentic flow:

python agent_workflow.py

How it works:

  1. User Intent: The script accepts natural language queries (e.g., "Show me all employees in the AI department").
  2. Mock LLM: A simple logic block simulates an LLM converting the intent into SQL.
  3. Tool Selection: The agent decides whether to use query_database or list_tables.
  4. Execution: The agent calls the MCP server to fetch data.
  5. Response: The agent formats and displays the results.

Architecture Flow

Client (Request) -> MCP Protocol (JSON-RPC) -> MCP Server -> SQLite DB

  • Security: The server restricts queries to SELECT only, preventing modification or deletion of data.
  • Abstraction: The client doesn't need to know the database location or credentials, only the tool definition.