---
title: "OceanBase MCP Server 与 Claude Code 集成 - OceanBase 数据库 V4.3.5 | OceanBase 文档中心"
description: OceanBase MCP Server 与 Claude Code 集成 MCP（Model Context Protocol） 是 Anthropic 公司于 2024 年 11 月推出并开源，旨在实现大语言模型与外部工具或数据源交互的协议。通过 MCP，用户不需要将大模型的输出手动复制执行，大模型可以直接指挥工…
image: https://mdn.alipayobjects.com/huamei_22khvb/afts/img/A*OSPzQ6GUQF4AAAAAQHAAAAgAeiGDAQ/original
---
切换语言

- 中文站 - 简体中文
- International - English
- 日本站 - 日本語

文档反馈![](https://mdn.alipayobjects.com/huamei_22khvb/afts/img/A*P8CuR4UJ_FkAAAAAAAAAAAAADiGDAQ/original) OceanBase 数据库分布式版 - V 4.3.5 LTS

# OceanBase MCP Server 与 Claude Code 集成

更新时间：2026-04-09 14:12:04

[编辑](https://github.com/oceanbase/oceanbase-doc/edit/V4.3.5/zh-CN/680.ecological-integration/1100.AI/5000.claude-code.md)  

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

[OceanBase MCP Server](https://github.com/oceanbase/awesome-oceanbase-mcp/tree/main/src/oceanbase_mcp_server) 通过 MCP 协议提供了大模型与 OceanBase 数据库交互的能力，可以执行 SQL 语句。通过合适的客户端可以快速搭建项目原型，已在 github 上开源。

[Claude Code](https://docs.claude.com/en/home) 是 Anthropic 公司推出的一款 AI 编程工具，它是一个运行在终端中的智能编码助手，可以帮助开发者快速的将想法转化为高质量代码。

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

## 前提条件

- 您已完成部署 OceanBase 数据库并且创建了 MySQL 模式用户租户。创建用户租户的详细信息，参见 [创建租户](https://www.oceanbase.com/docs/common-oceanbase-database-cn-1000000002013128)。
 - 安装 [Python 3.11 及以上版本](https://www.python.org/downloads/) 和相应 [pip](https://pip.pypa.io/en/stable/installation/)。如果您的机器上 Python 版本较低，可以使用 Miniconda 来创建新的 Python 3.11 及以上的环境，具体可参考 [Miniconda 安装指南](https://docs.anaconda.com/miniconda/install/)。
 - 根据所用的操作系统，安装 [Git](https://git-scm.com//downloads)，根据自己的操作系统，进行下载安装：

     - 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 可以使用独立脚本安装：

      ```shell
      curl -LsSf https://astral.sh/uv/install.sh | sh

      ```
     - Windows 平台，使用下面的脚本：

      ```shell
      irm https://astral.sh/uv/install.ps1 | iex

      ```
     - 或者使用平台无关的 pip 安装方式：

      ```shell
      pip install uv

      ```
 - 安装完成后，可使用 `uv --version` 命令验证安装是否成功：

  ```shell
  pip install uv
  uv --version

  ```
 - Augment Code 安装：

     1. 打开 [VS Code](https://code.visualstudio.com/)（如无，参考官方文档进行安装）。
     2. 单击侧边栏中的扩展图标。
     3. 搜索 “Claude Code” 并点击安装。

       ![image01](https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/doc/img/cloud/integrations/AI/claude01.png)
     4. 配置 Claude Code 环境变量。

       ```shell
       cat >> ~/.zshrc << 'EOF'
       export ANTHROPIC_BASE_URL=*****
       export ANTHROPIC_API_KEY=*******
       export ANTHROPIC_MODEL=*******
       EOF
       source ~/.zshrc

       ```
     5. 测试链接。

       ![image02](https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/doc/img/cloud/integrations/AI/claude02.png)
     6. Anthropic 模型选择。

       ![image03](https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/doc/img/cloud/integrations/AI/claude03.png)

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

联系 OceanBase 数据库部署人员或者管理员获取相应的数据库连接串，例如：

```sql
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 模式下的权限分类](https://www.oceanbase.com/docs/common-oceanbase-database-cn-1000000002016100)。
 - `$user_name`：提供租户的连接账户。ODP 连接的常用格式：`用户名@租户名#集群名` 或者 `集群名:租户名:用户名`；直连方式格式：`用户名@租户名`。
 - `$password`：提供账户密码。

更多连接串的信息，请参见 [通过 OBClient 连接 OceanBase 租户](https://www.oceanbase.com/docs/common-oceanbase-database-cn-1000000002013251)。

## 步骤二：配置 OceanBase MCP Server

### 克隆 OceanBase MCP Server 仓库

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

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

```

进入源代码目录：

```shell
cd awesome-oceanbase-mcp

```

### 安装依赖

在 `oceanbase_mcp_server` 目录下执行下面的命令创建虚拟环境，并安装依赖：

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

```

### 配置 OceanBase MCP Server

1. 打开 VS Code 工具终端，执行 claude mcp add-json 命令。

   ```json
   claude mcp add-json 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": "***"
    }
   }'

   ```

   `/path/to/your/oceanbase_mcp_server` 需要替换为 `oceanbase_mcp_server` 文件夹的绝对路径，`OB_HOST`、`OB_PORT`、`OB_USER`、`OB_PASSWORD`、`OB_DATABASE` 需要替换为自己数据库的对应信息即可：

   ![image04](https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/doc/img/cloud/integrations/AI/claude04.png)
 2. 验证是否可以连接数据库。

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

   ![image05](https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/doc/img/cloud/integrations/AI/claude05.png)

   Claude Code 会展示当前 test 库中的表数量，说明可以正常连接 OceanBase 数据库。

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

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

1. 创建表。

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

   ![image06](https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/doc/img/cloud/integrations/AI/claude06.png)
 2. 插入测试数据。

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

   ![image07](https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/doc/img/cloud/integrations/AI/claude07.png)
 3. 创建 FastAPI 项目。

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

   ![image08](https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/doc/img/cloud/integrations/AI/claude08.png)
 4. 执行下面的命令安装依赖。

   ```shell
   cd customer_api
   pip install -r requirements.txt

   ```
 5. 启动 FastAPI 项目。

   ```python
   python3 main.py

   ```
 6. 查看表中的数据。

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

   ```json
   curl http://127.0.0.1:8000/customers
   [{"name":"张三","age":28,"telephone":"13800138001","location":"北京市朝阳区","ID":1},{"name":"李四","age":32,"telephone":"13900139002","location":"上海市浦东新区","ID":2},{"name":"王五","age":25,"telephone":"13700137003","location":"广州市天河区","ID":3},{"name":"赵六","age":45,"telephone":"13600136004","location":"深圳市南山区","ID":4},{"name":"陈七","age":38,"telephone":"13500135005","location":"杭州市西湖区","ID":5},{"name":"刘八","age":29,"telephone":"13400134006","location":"成都市武侯区","ID":6},{"name":"周九","age":35,"telephone":"13300133007","location":"南京市江宁区","ID":7},{"name":"吴十","age":41,"telephone":"13200132008","location":"武汉市洪山区","ID":8},{"name":"郑十一","age":27,"telephone":"13100131009","location":"重庆市渝北区","ID":9},{"name":"马十二","age":33,"telephone":"13000130010","location":"西安市雁塔区","ID":10}]

   ```
 7. 增、删、改、查的代码都已生成。

   ```python
   from sqlalchemy.orm import Session
   from . import models, schemas
   from typing import List, Optional

   def get_customer(db: Session, customer_id: int):
    return db.query(models.Customer).filter(models.Customer.ID == customer_id).first()

   def get_customers(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.Customer).offset(skip).limit(limit).all()

   def create_customer(db: Session, customer: schemas.CustomerCreate):
    db_customer = models.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: schemas.CustomerUpdate):
    db_customer = db.query(models.Customer).filter(models.Customer.ID == customer_id).first()
    if db_customer:
        update_data = customer.dict(exclude_unset=True)
        for key, value in update_data.items():
            setattr(db_customer, key, value)
        db.commit()
        db.refresh(db_customer)
    return db_customer

   def delete_customer(db: Session, customer_id: int):
    db_customer = db.query(models.Customer).filter(models.Customer.ID == customer_id).first()
    if db_customer:
        db.delete(db_customer)
        db.commit()
        return True
    return False

   def search_customers(db: Session, name: Optional[str] = None, location: Optional[str] = None):
    query = db.query(models.Customer)
    if name:
        query = query.filter(models.Customer.name.contains(name))
    if location:
        query = query.filter(models.Customer.location.contains(location))
    return query.all()

   ```

 上一篇 下一篇 ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ)[AI](https://www.oceanbase.com/obi) 咨询热线
