首批通过分布式安全可靠测评,为关键业务系统打造
OceanBase MCP Server 与 Augment Code 集成
更新时间:2026-04-09 14:12:04
MCP(Model Context Protocol) 是 Anthropic 公司于 2024 年 11 月推出并开源,旨在实现大语言模型与外部工具或数据源交互的协议。通过 MCP,用户不需要将大模型的输出手动复制执行,大模型可以直接指挥工具执行相应的动作(Action)。
OceanBase MCP Server 通过 MCP 协议提供了大模型与 OceanBase 数据库交互的能力,可以执行 SQL 语句。通过合适的客户端可以快速搭建项目原型,已在 github 上开源。
Augment Code 是开发人员 AI 平台,它可以帮助您理解代码、调试问题,并更快地发布,因为它了解您的代码库。使用聊天、下一步编辑和代码完成来完成更多的工作。
本文将展示如何使用 Augment Code,展示如何通过 OceanBase MCP Server 快速构建一个后端应用。
前提条件
您已完成部署 OceanBase 数据库并且创建了 MySQL 模式用户租户。创建用户租户的详细信息,参见 创建租户。
安装 Python 3.11 及以上版本 和相应 pip。如果您的机器上 Python 版本较低,可以使用 Miniconda 来创建新的 Python 3.11 及以上的环境,具体可参考 Miniconda 安装指南。
根据所用的操作系统,安装 Git,根据自己的操作系统,进行下载安装:
Windows:https://git-scm.com/downloads/win。
macOS:https://git-scm.com/download/mac。
Linux:https://git-scm.com/downloads/linux。
安装 Python 包管理器 uv。
对于 macOS/Linux 可以使用独立脚本安装:
curl -LsSf https://astral.sh/uv/install.sh | shWindows 平台,使用下面的脚本:
irm https://astral.sh/uv/install.ps1 | iex或者使用平台无关的 pip 安装方式:
pip install uv
安装完成后,可使用
uv --version命令验证安装是否成功:pip install uv uv --versionAugment Code 安装:
打开 VS Code(如无,参考官方文档进行安装)。
单击侧边栏中的扩展图标。
搜索 “Augment Code” 并点击安装。
Augment Code 账号注册登录。

步骤一:获取数据库连接信息
联系 OceanBase 数据库部署人员或者管理员获取相应的数据库连接串,例如:
obclient -h$host -P$port -u$user_name -p$password -D$database_name
参数说明:
$host:提供 OceanBase 数据库连接 IP。OceanBase 数据库代理(OceanBase Database Proxy,ODP)连接方式使用的是一个 ODP 地址;直连方式使用的是 OBServer 节点的 IP 地址。$port:提供 OceanBase 数据库连接端口。ODP 连接的方式默认是2883,在部署 ODP 时可自定义;直连方式默认是2881,在部署 OceanBase 数据库时可自定义。$database_name:需要访问的数据库名称。注意
连接租户的用户需要拥有该数据库的
CREATE、INSERT、DROP和SELECT权限。更多有关用户权限的信息,请参见 MySQL 模式下的权限分类。$user_name:提供租户的连接账户。ODP 连接的常用格式:用户名@租户名#集群名或者集群名:租户名:用户名;直连方式格式:用户名@租户名。$password:提供账户密码。
更多连接串的信息,请参见 通过 OBClient 连接 OceanBase 租户。
步骤二:配置 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 .
添加并配置 MCP Server
在 Augment Code 中配置 OceanBase MCP Server。

点击 Import from json,填写 mcp 配置文件。


填写配置文件,点击确认。
/path/to/your/oceanbase_mcp_server需要替换为oceanbase_mcp_server文件夹的绝对路径,OB_HOST、OB_PORT、OB_USER、OB_PASSWORD、OB_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": "***" } } } }验证是否可以连接数据库。
输入提示 “创建一个 customer 表,主键是 ID,包含 name,age,telephone,location 字段”:

Augment Code 会展示当前 test 库中的表数量,说明可以正常连接 OceanBase 数据库。
步骤三:使用 FastAPI 快速创建 RESTful API 风格的项目
FastAPI 是一个 Python的 Web 框架,可以快速构建 RESTful API。
创建表。
输入提示 “创建一个 customer 表,主键是 ID,包含 name,age,telephone,location 字段”:

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

创建 FastAPI 项目。
输入提示 “创建一个 FastAPI 项目,生成基于 customer 表的 RESTful API”,自动生成了多个文件,代码自动修复、启动 FastAPI 项目:


查看表中的数据。
在命令行中运行
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":45,"telephone":"15812345678","location":"西安市雁塔区","id":9},{"name":"王十二","age":27,"telephone":"18512345678","location":"重庆市渝中区","id":10},{"name":"测试用户","age":25,"telephone":"13900000000","location":"测试地址","id":11}]增、删、改、查的代码都已生成。
from sqlalchemy.orm import Session from database import Customer from schemas import CustomerCreate, CustomerUpdate from typing import List, Optional def get_customer(db: Session, customer_id: int) -> Optional[Customer]: """根据ID获取单个客户""" return db.query(Customer).filter(Customer.id == customer_id).first() def get_customers(db: Session, skip: int = 0, limit: int = 100) -> List[Customer]: """获取客户列表""" return db.query(Customer).offset(skip).limit(limit).all() def create_customer(db: Session, customer: CustomerCreate) -> Customer: """创建新客户""" db_customer = Customer(**customer.dict()) db.add(db_customer) db.commit() db.refresh(db_customer) return db_customer def update_customer(db: Session, customer_id: int, customer: CustomerUpdate) -> Optional[Customer]: """更新客户信息""" db_customer = db.query(Customer).filter(Customer.id == customer_id).first() if db_customer: update_data = customer.dict(exclude_unset=True) for field, value in update_data.items(): setattr(db_customer, field, value) db.commit() db.refresh(db_customer) return db_customer def delete_customer(db: Session, customer_id: int) -> bool: """删除客户""" db_customer = db.query(Customer).filter(Customer.id == customer_id).first() if db_customer: db.delete(db_customer) db.commit() return True return False def search_customers_by_name(db: Session, name: str) -> List[Customer]: """根据姓名搜索客户""" return db.query(Customer).filter(Customer.name.contains(name)).all() def get_customers_by_location(db: Session, location: str) -> List[Customer]: """根据地址获取客户""" return db.query(Customer).filter(Customer.location.contains(location)).all()