Building an MCP Server for SQLite Database Integration

Overview

This tutorial demonstrates how to create an MCP (Model-Centric Protocol) server that interacts with a local SQLite database. The server provides functionalities to query and explore data stored in a SQLite database.

Database Structure

The example uses a simple SQLite database with a table called demo containing two fields:

  • idno - ID number
  • Name

The table contains several records with ID numbers and corresponding names.

Server Features

The MCP server provides two main functionalities:

  1. Resource listing: View the database schema, including all table structures
  2. Query tool: Execute SQL queries directly against the database

Examples of operations:

  • Viewing table schema by using list resource
  • Running SQL queries like SELECT * FROM demo
  • Applying conditions such as SELECT * FROM demo WHERE idno=3

Implementation

The code implementation requires:

  1. Importing necessary modules:
    • sqlite3 for database connectivity
    • fast_mcp for simplified MCP server creation
  2. Creating an MCP server instance:
    mcp = FAST_MCP("SQLite Explorer")
  3. Defining resources with decorators:
    @mcp.resource("schema://main") def get_schema(): # Connect to database # Execute SQL to get schema information # Return formatted results
  4. Defining tools with decorators:
    @mcp.tool def query_data(sql): # Connect to database # Execute the provided SQL query # Return results or error messages

Error Handling

The server includes basic error handling to provide appropriate error messages when queries fail (e.g., "no such table demo1" when a non-existent table is queried).

Conclusion

By using the FAST_MCP framework, this implementation allows large language models to interact with local SQLite databases, enabling deeper integration between AI systems and local data sources.

Setup Directions

  1. Install Ollama: https://ollama.com/download
  2. Download common LLM models:
    • Basic inference:
      ollama pull phi4 ollama pull mistral
    • Image inference:
      ollama pull gemma3
    • Knowledge base:
      ollama pull nomic-embed-text
  3. Download Cherry Studio: https://cherry-ai.com/

    Required files for Cherry MCP server execution, manual download locations:

  4. Sources for good MCP Servers:

    https://github.com/punkpeye/awesome-mcp-servers

  5. Fetch MCP Server:

    https://github.com/modelcontextprotocol/servers/tree/main/src/fetch

概述

本教程展示如何創建一個 MCP(Model-Centric Protocol)伺服器,用於與本地 SQLite 資料庫進行互動。該伺服器提供查詢和探索存儲在 SQLite 資料庫中的數據的功能。

資料庫結構

示例使用了一個簡單的 SQLite 資料庫,其中包含一個名為 demo 的表,該表包含兩個欄位:

  • idno - ID 號碼
  • 姓名

該表包含幾筆記錄,每筆都有 ID 號碼和對應的姓名。

伺服器功能

MCP 伺服器提供兩個主要功能:

  1. 資源列表: 查看資料庫的結構,包括所有表的結構
  2. 查詢工具: 直接對資料庫執行 SQL 查詢

操作示例:

  • 使用 list resource 查看表結構
  • 執行 SQL 查詢,如 SELECT * FROM demo
  • 應用條件,如 SELECT * FROM demo WHERE idno=3

實現方法

代碼實現需要:

  1. 導入必要的模組:
    • sqlite3 用於資料庫連接
    • fast_mcp 用於簡化 MCP 伺服器創建
  2. 創建 MCP 伺服器實例:
    mcp = FAST_MCP("SQLite Explorer")
  3. 使用裝飾器定義資源:
    @mcp.resource("schema://main") def get_schema(): # 連接資料庫 # 執行 SQL 獲取結構信息 # 返回格式化結果
  4. 使用裝飾器定義工具:
    @mcp.tool def query_data(sql): # 連接資料庫 # 執行提供的 SQL 查詢 # 返回結果或錯誤訊息

錯誤處理

伺服器包含基本的錯誤處理,以在查詢失敗時提供適當的錯誤訊息(例如,當查詢不存在的表時顯示「no such table demo1」)。

結論

通過使用 FAST_MCP 框架,此實現允許大型語言模型與本地 SQLite 資料庫進行互動,實現 AI 系統與本地數據源之間更深層次的整合。

設置指南

  1. 安裝 Ollama:https://ollama.com/download
  2. 下載常用 LLM 模型
    • 基本推理:
      ollama pull phi4 ollama pull mistral
    • 圖像推理:
      ollama pull gemma3
    • 知識庫:
      ollama pull nomic-embed-text
  3. Cherry Studio 下載:https://cherry-ai.com/

    Cherry 有關 MCP server 執行必備檔案,手動下載位置:

  4. 獲取好用 MCP Server 的地方:

    https://github.com/punkpeye/awesome-mcp-servers

  5. Fetch MCP Server:

    https://github.com/modelcontextprotocol/servers/tree/main/src/fetch