Skip to main content

Integrate OceanBase MCP Server with Claude Code

MCP (Model Context Protocol) is an open-source protocol introduced by Anthropic in November 2024. It enables large language models (LLMs) to interact with external tools or data sources. With MCP, users can directly instruct tools to perform specific actions based on the LLM's output, eliminating the need for manual execution.

MCP Server allows LLMs to interact with seekdb and execute SQL statements. It is open-sourced on GitHub and can be quickly integrated with suitable clients.

Claude Code is an AI-powered coding assistant developed by Anthropic. It operates in the terminal and helps developers convert ideas into high-quality code efficiently.

This topic demonstrates how to use Claude Code with OceanBase MCP Server to quickly build a backend application.

Prerequisites

  • You have deployed seekdb.

  • You have installed Python 3.11 or later and the corresponding pip. If your machine has a lower Python version, you can use Miniconda to create a new Python 3.11 or later environment. For more information, see Miniconda installation guide.

  • You have installed Git based on your operating system.

  • You have installed the Python package manager uv. After the installation is complete, run the uv --version command to verify whether the installation is successful:

    pip install uv
    uv --version
  • Install Claude Code:

    1. Open VS Code (if not installed, refer to the official documentation for installation).

    2. Click the Extensions icon in the sidebar.

    3. Search for "Claude Code" and click Install.

      image01

    4. Configure the environment variables for Claude Code.

      cat >> ~/.zshrc << 'EOF'
      export ANTHROPIC_BASE_URL=*****
      export ANTHROPIC_API_KEY=*******
      export ANTHROPIC_MODEL=*******
      EOF
      source ~/.zshrc
    5. Test the connection.

      image02

    6. Select the Anthropic model.

      image03

Step 1: Obtain the database connection information

Contact the seekdb deployment personnel or administrator to obtain the corresponding database connection string, for example:

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

Parameter description:

  • $host: the IP address for connecting to seekdb.

  • $port: the port for connecting to seekdb, which is 2881 by default.

  • $database_name: the name of the database to be accessed.

    tip

    The user for connecting to the database must have the CREATE, INSERT, DROP, and SELECT permissions on the database.

  • $user_name: the database connection account.

  • $password: the account password.

Step 2: Configure OceanBase MCP Server

Clone the OceanBase MCP Server repository

Run the following command to download the source code to your local machine:

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

Go to the source code directory:

cd awesome-oceanbase-mcp

Install dependencies

Run the following command in the oceanbase_mcp_server directory to create a virtual environment and install the dependencies:

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

Configure OceanBase MCP Server

  1. Open the VS Code terminal and run the claude mcp add-json command.

    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": "***"
    }
    }'

    Replace /path/to/your/oceanbase_mcp_server with the absolute path of the oceanbase_mcp_server folder, and replace OB_HOST, OB_PORT, OB_USER, OB_PASSWORD, and OB_DATABASE with the corresponding information of your database:

    image04

  2. Verify whether you can connect to the database.

    Enter the prompt "How many tables are there in the test database?" Claude Code will display the SQL statement to be executed and output the query result:

    image05

    If the number of tables in the test database is displayed, it indicates that you can connect to seekdb.

Step 3: Use FastAPI to create a RESTful API project

FastAPI is a Python web framework that allows you to quickly build RESTful APIs.

  1. Create a table.

    Enter the prompt "Create a customer table with the ID as the primary key and the name, age, telephone, and location fields" in the command line:

    image06

  2. Insert test data.

    Enter the prompt "Insert 10 test data records" in the command line:

    image07

  3. Create a FastAPI project.

    Enter the prompt "Create a FastAPI project and generate a RESTful API based on the customer table" in the command line. Multiple files are automatically generated. If there are any issues, you can modify them later.

    image08

  4. Run the following command to install the dependencies:

    cd customer_api
    pip install -r requirements.txt
  5. Start the FastAPI project.

    python3 main.py
  6. View the data in the table.

    Run the following command in the command line: curl http://127.0.0.1:8000/customers. You can also use other request tools to view the data in the table:

    curl http://127.0.0.1:8000/customers
    [{"name":"Zhang San","age":28,"telephone":"13800138001","location":"Chaoyang District, Beijing","ID":1},{"name":"Li Si","age":32,"telephone":"13900139002","location":"Pudong New Area, Shanghai","ID":2},{"name":"Wang Wu","age":25,"telephone":"13700137003","location":"Tianhe District, Guangzhou","ID":3},{"name":"Zhao Liu","age":45,"telephone":"13600136004","location":"Nanshan District, Shenzhen","ID":4},{"name":"Chen Qi","age":38,"telephone":"13500135005","location":"Xihu District, Hangzhou","ID":5},{"name":"Liu Ba","age":29,"telephone":"13400134006","location":"Wuhou District, Chengdu","ID":6},{"name":"Zhou Jiǔ","age":35,"telephone":"13300133007","location":"Jiangning District, Nanjing","ID":7},{"name":"Wu Shi","age":41,"telephone":"13200132008","location":"Hongshan District, Wuhan","ID":8},{"name":"Zheng Shiyi","age":27,"telephone":"13100131009","location":"Yubei District, Chongqing","ID":9},{"name":"Ma Shier","age":33,"telephone":"13000130010","location":"Yanta District, Xi'an","ID":10}]
  7. The code for adding, deleting, modifying, and querying data has been generated.

    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()