跳到主要内容

OceanBase MCP Server 与 Kiro 集成

MCP(Model Context Protocol) 是 Anthropic 公司于 2024 年 11 月推出并开源,旨在实现大语言模型与外部工具或数据源交互的协议。通过 MCP,用户不需要将大模型的输出手动复制执行,大模型可以直接指挥工具执行相应的动作(Action)。

MCP Server 通过 MCP 协议提供了大模型与 seekdb 交互的能力,可以执行 SQL 语句。通过合适的客户端可以快速搭建项目原型,已在 github 上开源。

Kiro 是亚马逊云科技(AWS)推出的一款专为AI Agent设计的集成开发环境(agentic IDE)。它是一个具有人工智能能力的编程工具,旨在帮助开发者从概念阶段到生产部署完成全流程开发。

本文使用 Kiro,展示如何通过 OceanBase MCP Server 快速构建一个后端应用。

前提条件

  • 您已完成部署 seekdb。

  • 安装 Python 3.11 及以上版本 和相应 pip。如果您的机器上 Python 版本较低,可以使用 Miniconda 来创建新的 Python 3.11 及以上的环境,具体可参考 Miniconda 安装指南

  • 根据所用的操作系统,安装 Git

  • 安装 Python 包管理器 uv。安装完成后,可使用 uv --version 命令验证安装是否成功:

    pip install uv
    uv --version
  • Kiro 客户端:

    可以在 kiro 下载页,根据自己的操作系统选择合适的版本进行安装。

步骤一:获取数据库连接信息

联系 seekdb 部署人员或者管理员获取相应的数据库连接串,例如:

mysql -h$host -P$port -u$user_name -p$password -D$database_name

参数说明:

  • $host:提供 seekdb 连接 IP 地址。

  • $port:提供 seekdb 连接端口,默认是 2881。

  • $database_name:需要访问的数据库名称。

    提示

    连接的用户需要拥有该数据库的 CREATEINSERTDROPSELECT 权限。

  • $user_name:提供数据库连接账户。

  • $password:提供账户密码。

步骤二:配置 OceanBase MCP Server

克隆 OceanBase MCP Server 仓库

执行下面的命令将源代码下载到本地:

git clone https://github.com/oceanbase/awesome-oceanbase-mcp.git

进入源代码目录:

cd awesome-oceanbase-mcp

安装依赖

oceanbase_mcp_server 目录下执行下面的命令创建虚拟环境,并安装依赖:

uv venv
source .venv/bin/activate
uv pip install .

在 Kiro 中配置 OceanBase MCP Server

  1. 使用快捷键 Command + L(MacOS)打开聊天对话框,点击左下角上角的齿轮,选择 Kiro Settings。

    image01

  2. 点击 Open User MCP Config (JSON)/Open Workspace MCP Config (JSON),填写 mcp 配置文件。

    image02

    image03

    填入下面的配置文件,点击确认。 /path/to/your/oceanbase_mcp_server 需要替换为 oceanbase_mcp_server 文件夹的绝对路径,OB_HOSTOB_PORTOB_USEROB_PASSWORDOB_DATABASE 需要替换为自己数据库的对应信息即可:

    {
    "mcpServers": {
    "oceanbase": {
    "command": "uv",
    "args": [
    "--directory",
    "/path/to/your/oceanbase_mcp_server/src/oceanbase_mcp_server",
    "run",
    "oceanbase_mcp_server"
    ],
    "env": {
    "OB_HOST": "***",
    "OB_PORT": "***",
    "OB_USER": "***",
    "OB_PASSWORD": "***",
    "OB_DATABASE": "***"
    }
    }
    }
    }
  3. 验证是否可以连接数据库。

    输入提示 “test 库中有多少张表”,Kiro 会展示即将执行的 SQL 语句,并输出查询结果:

    image04

    Kiro 会展示当前 test 库中的表数量,说明可以正常连接 seekdb。

步骤三:使用 FastAPI 快速创建 RESTful API 风格的项目

FastAPI 是一个 Python的 Web 框架,可以快速构建 RESTful API。

  1. 创建表。

    输入提示 “创建一个 customer 表,主键是 ID,包含 name,age,telephone,location 字段”:

    image05

  2. 插入测试数据。

    输入提示 “插入 10 条测试数据”:

    image06

  3. 创建 FastAPI 项目。

    输入提示 “创建一个 FastAPI 项目,生成基于 customer 表的 RESTful API”,自动生成了多个文件,先点击全部接受。如果有问题再进行二次修改。

    因为 AI 生成的文件每次可能都是不一样的,这里不再展示修改的过程。

    image07

  4. 执行下面的命令,在当前目录下使用 uv 包管理工具创建虚拟环境,并安装依赖。

    pip install -r requirements.txt
  5. 启动 FastAPI 项目。

    python3 main.py
  6. 查看表中的数据。

    在命令行中运行 curl http://127.0.0.1:8000/customers,或者使用其他请求工具,查看表中的数据:

    curl http://127.0.0.1:8000/customers
    [{"name":"张三","age":28,"telephone":"13812345678","location":"北京市朝阳区","id":1},{"name":"李四","age":35,"telephone":"13987654321","location":"上海市浦东新区","id":2},{"name":"王五","age":42,"telephone":"15612345678","location":"广州市天河区","id":3},{"name":"赵六","age":29,"telephone":"18712345678","location":"深圳市南山区","id":4},{"name":"钱七","age":33,"telephone":"13512345678","location":"杭州市西湖区","id":5},{"name":"孙八","age":26,"telephone":"15987654321","location":"成都市锦江区","id":6},{"name":"周九","age":38,"telephone":"18612345678","location":"武汉市江汉区","id":7},{"name":"吴十","age":31,"telephone":"13712345678","location":"南京市鼓楼区","id":8},{"name":"郑十一","age":27,"telephone":"15812345678","location":"西安市雁塔区","id":9},{"name":"王十二","age":45,"telephone":"18512345678","location":"重庆市渝中区","id":10}]
  7. 增、删、改、查的代码都已生成。

    from database import db
    from models import CustomerCreate, CustomerUpdate
    from typing import List, Optional

    class CustomerCRUD:

    @staticmethod
    def get_all_customers() -> List[dict]:
    query = "SELECT * FROM customer ORDER BY id"
    return db.execute_query(query)

    @staticmethod
    def get_customer_by_id(customer_id: int) -> Optional[dict]:
    query = "SELECT * FROM customer WHERE id = %s"
    result = db.execute_query(query, (customer_id,))
    return result[0] if result else None

    @staticmethod
    def create_customer(customer: CustomerCreate) -> dict:
    query = """
    INSERT INTO customer (name, age, telephone, location)
    VALUES (%s, %s, %s, %s)
    """
    customer_id = db.execute_insert(
    query,
    (customer.name, customer.age, customer.telephone, customer.location)
    )
    return CustomerCRUD.get_customer_by_id(customer_id)

    @staticmethod
    def update_customer(customer_id: int, customer: CustomerUpdate) -> Optional[dict]:
    # 构建动态更新查询
    update_fields = []
    params = []

    if customer.name is not None:
    update_fields.append("name = %s")
    params.append(customer.name)
    if customer.age is not None:
    update_fields.append("age = %s")
    params.append(customer.age)
    if customer.telephone is not None:
    update_fields.append("telephone = %s")
    params.append(customer.telephone)
    if customer.location is not None:
    update_fields.append("location = %s")
    params.append(customer.location)

    if not update_fields:
    return CustomerCRUD.get_customer_by_id(customer_id)

    query = f"UPDATE customer SET {', '.join(update_fields)} WHERE id = %s"
    params.append(customer_id)

    db.execute_query(query, tuple(params))
    return CustomerCRUD.get_customer_by_id(customer_id)

    @staticmethod
    def delete_customer(customer_id: int) -> bool:
    query = "DELETE FROM customer WHERE id = %s"
    db.execute_query(query, (customer_id,))
    return True

    customer_crud = CustomerCRUD()