MCP (Model Context Protocol) is an open-source protocol introduced by Anthropic in November 2024, designed to enable large language models (LLMs) to interact with external tools or data sources. With MCP, users no longer need to manually copy and execute the output of an LLM; instead, the LLM can directly instruct the tools to perform specific actions.
OceanBase MCP Server provides the capability for LLMs to interact with OceanBase Database through the MCP protocol. It supports executing SQL statements. With the right client, you can quickly build a project prototype, and it's already open-sourced on GitHub.
Cursor is an AI-powered code editor that supports multiple operating systems, including Windows, macOS, and Linux.
This article will guide you on how to integrate Cursor with OceanBase MCP Server to quickly build a backend application.
Prerequisites
You have deployed OceanBase Database and created a MySQL-compatible mode user tenant. For more information, see Create a tenant.
Install Python 3.11 or later and the corresponding pip. If your system has a lower version of Python, you can use Miniconda to create a new environment with Python 3.11 or later. For more information, see Install Miniconda.
Install Git based on your operating system.
Install the uv package manager. After the installation is complete, run the
uv --versioncommand to verify the installation:pip install uv uv --versionDownload Cursor and install the version that matches your operating system. Note that you need to register a new account or log in with an existing account before using Cursor for the first time. After logging in, you can create a new project or open an existing project.
Step 1: Obtain the database connection information
Contact the deployment personnel or administrator of OceanBase Database to obtain the database connection string, for example:
obclient -h$host -P$port -u$user_name -p$password -D$database_name
Parameter description:
$host: the IP address for connecting to OceanBase Database. For the connection through OceanBase Database Proxy (ODP), use the IP address of the ODP. For direct connection, use the IP address of the OBServer node.$port: the port for connecting to OceanBase Database. For the connection through ODP, the default port is2883, which can be customized when ODP is deployed. For direct connection, the default port is2881, which can be customized when OceanBase Database is deployed.$database_name: the name of the database to be accessed.Notice
The user for connecting to the tenant must have the
CREATE,INSERT,DROP, andSELECTprivileges on the database. For more information about user privileges, see Privilege types in MySQL-compatible mode.$user_name: the tenant connection account. For the connection through ODP, the format isusername@tenant name#cluster nameorcluster name:tenant name:username. For direct connection, the format isusername@tenant name.$password: the password of the account.
For more information about the connection string, see Connect to an OceanBase tenant by using OBClient.
Step 2: Configure the 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/mcp-oceanbase.git
Navigate to the source code directory:
cd mcp-oceanbase
Install dependencies
In the mcp-oceanbase directory, run the following commands to create a virtual environment and install the dependencies:
uv venv
source .venv/bin/activate
uv pip install .
Create a working directory for the Cursor client and configure OceanBase MCP Server
Manually create a working directory for the Cursor client and open it with Cursor. The files generated by Cursor will be stored in this directory. An example directory name is cursor.
Use the shortcut Ctrl + L (Windows) or Command + L (MacOS) to open the chat dialog. Click the gear icon in the top right corner and select MCP Tools.

Add and configure MCP Servers
Click
Add Custom MCPand fill in the configuration file.
Fill in the configuration file and click Confirm.
Replace
path/to/your/mcp-oceanbase/src/oceanbase_mcp_serverwith the absolute path to theoceanbase_mcp_serverfolder. Also, replaceOB_HOST,OB_PORT,OB_USER,OB_PASSWORD, andOB_DATABASEwith your database's corresponding information:{ "mcpServers": { "oceanbase": { "command": "uv", "args": [ "--directory", "/path/to/your/mcp-oceanbase/src/oceanbase_mcp_server", "run", "oceanbase_mcp_server" ], "env": { "OB_HOST": "***", "OB_PORT": "***", "OB_USER": "***", "OB_PASSWORD": "***", "OB_DATABASE": "***" } } } }If the configuration is successful, the status will show as
Available.
Test the MCP Server
In the chat dialog, type the prompt:
How many tables are in the test database?. The Cursor client will display the SQL statement it is about to execute. Confirm that it is correct and click theRun toolbutton to execute the query. The Cursor client will list all the tables in thetestdatabase, indicating that we have successfully connected to the OceanBase database.
Create a RESTful API project with FastAPI
You can use FastAPI to quickly create a RESTful API project. FastAPI is a Python web framework for building RESTful APIs.
Create a customer table
Enter the following prompt in the dialog box:
Create a customer table with ID as the primary key, and fields name, age, telephone, and location. Confirm the SQL statement and click theRun toolbutton to execute the query.
Insert test data
Enter the following prompt in the dialog box:
Insert 10 records into the customer table. Confirm the SQL statement and click theRun toolbutton to execute the query. After the insertion is successful, you will see the message10 test records have been successfully inserted into the customer table....
Create a FastAPI project
Enter the following prompt in the dialog box:
Create a FastAPI project and generate a RESTful API based on the customer table. Confirm the SQL statement and click theRun toolbutton to execute the query.
This step will automatically generate two files. We recommend that you select
Accept allfor the first use, as the content of the files generated by AI may be uncertain. You can adjust them as needed based on your actual requirements.Create a virtual environment and install dependencies
Run the following commands to create a virtual environment using the uv package manager and install the dependencies:
uv venv source .venv/bin/activate uv pip install -r requirements.txtStart the FastAPI project
Run the following command to start the FastAPI project:
uvicorn main:app --reloadView the data in the table
Run the following command in the command line, or use another request tool, to view the data in the table:
curl http://127.0.0.1:8000/customersThe returned result is as follows:
[{"id":1,"name":"Alice","age":28,"telephone":"1234567890","location":"Beijing"},{"id":2,"name":"Bob","age":32,"telephone":"2345678901","location":"Shanghai"},{"id":3,"name":"Charlie","age":25,"telephone":"3456789012","location":"Guangzhou"},{"id":4,"name":"David","age":40,"telephone":"4567890123","location":"Shenzhen"},{"id":5,"name":"Eve","age":22,"telephone":"5678901234","location":"Chengdu"},{"id":6,"name":"Frank","age":35,"telephone":"6789012345","location":"Wuhan"},{"id":7,"name":"Grace","age":30,"telephone":"7890123456","location":"Hangzhou"},{"id":8,"name":"Heidi","age":27,"telephone":"8901234567","location":"Nanjing"},{"id":9,"name":"Ivan","age":29,"telephone":"9012345678","location":"Tianjin"},{"id":10,"name":"Judy","age":31,"telephone":"0123456789","location":"Chongqing"}]You can see that the RESTful APIs for adding, deleting, modifying, and querying data have been successfully generated:
from fastapi import FastAPI, HTTPException, Depends from pydantic import BaseModel from typing import List from sqlalchemy import create_engine, Column, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker, Session # OceanBase connection configuration (please modify it as needed) DATABASE_URL = "mysql://***:***@***:***/***" engine = create_engine(DATABASE_URL, echo=True) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() class Customer(Base): __tablename__ = "customer" id = Column(Integer, primary_key=True, index=True) name = Column(String(100)) age = Column(Integer) telephone = Column(String(20)) location = Column(String(100)) class CustomerCreate(BaseModel): id: int name: str age: int telephone: str location: str class CustomerUpdate(BaseModel): name: str = None age: int = None telephone: str = None location: str = None class CustomerOut(BaseModel): id: int name: str age: int telephone: str location: str class Config: orm_mode = True def get_db(): db = SessionLocal() try: yield db finally: db.close() app = FastAPI() @app.post("/customers/", response_model=CustomerOut) def create_customer(customer: CustomerCreate, db: Session = Depends(get_db)): db_customer = Customer(**customer.dict()) db.add(db_customer) try: db.commit() db.refresh(db_customer) except Exception as e: db.rollback() raise HTTPException(status_code=400, detail=str(e)) return db_customer @app.get("/customers/", response_model=List[CustomerOut]) def read_customers(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): return db.query(Customer).offset(skip).limit(limit).all() @app.get("/customers/{customer_id}", response_model=CustomerOut) def read_customer(customer_id: int, db: Session = Depends(get_db)): customer = db.query(Customer).filter(Customer.id == customer_id).first() if customer is None: raise HTTPException(status_code=404, detail="Customer not found") return customer @app.put("/customers/{customer_id}", response_model=CustomerOut) def update_customer(customer_id: int, customer: CustomerUpdate, db: Session = Depends(get_db)): db_customer = db.query(Customer).filter(Customer.id == customer_id).first() if db_customer is None: raise HTTPException(status_code=404, detail="Customer not found") for var, value in vars(customer).items(): if value is not None: setattr(db_customer, var, value) db.commit() db.refresh(db_customer) return db_customer @app.delete("/customers/{customer_id}") def delete_customer(customer_id: int, db: Session = Depends(get_db)): db_customer = db.query(Customer).filter(Customer.id == customer_id).first() if db_customer is None: raise HTTPException(status_code=404, detail="Customer not found") db.delete(db_customer) db.commit() return {"ok": True}