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 no longer need to manually copy and execute the output of an LLM. Instead, the LLM can directly instruct the tool to perform the corresponding action (Action).
OceanBase MCP Server provides the capability for large models to interact with OceanBase Database through the MCP protocol. It allows the execution of SQL statements. With the right client, you can quickly build a project prototype. The source code is available on GitHub.
Cline is an open-source AI coding assistant that supports the MCP protocol.
This article demonstrates how to use Cline to quickly build a backend application with OceanBase MCP Server.
Prerequisites
You have deployed OceanBase Database and created a MySQL user tenant. For more information about how to create a user tenant, see Create a tenant.
Install Python 3.11 or later and the corresponding pip. If your system has a low version of Python, you can use Miniconda to create a new Python 3.11 or later environment. For more information, see Install Miniconda.
Install Git based on the operating system you use.
Install the Python package manager uv. After the installation is complete, run the
uv --versioncommand to verify the installation:pip install uv uv --versionInstall Cline:
If you use the Visual Studio Code IDE, search for and install the Cline plugin in the Extensions section. The plugin is named
Cline. After the installation is complete, click the Settings icon to configure the large model API for Cline.If you do not use an IDE, download Cline from Cline and follow the installation instructions in the Cline installation guide.
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
Parameters:
$host: the IP address for connecting to OceanBase Database. For 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 connection through ODP, the default port is2883, which can be customized during ODP deployment. For direct connection, the default port is2881, which can be customized during OceanBase Database deployment.$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 connection through ODP, the common 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
This example uses Visual Studio Code to show you how to 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 Visual Studio Code workspace
Manually create a Visual Studio Code workspace on your local machine and open it with Visual Studio Code. The files generated by Cline will be stored in this directory. An example directory name is cline-generate.
Configure OceanBase MCP Server in the Cline plugin
Click the Cline icon in the sidebar to open the Cline dialog box.
Add and configure MCP servers
Click the
MCP Serversicon.Manually configure OceanBase MCP servers.
Edit the configuration file.
In the
cline_mcp_settings.jsonfile opened in the previous step, enter the following configuration information and save the file. Replace/path/to/your/mcp-oceanbase/src/oceanbase_mcp_serverwith the absolute path of theoceanbase_mcp_serverfolder, and replaceOB_HOST,OB_PORT,OB_USER,OB_PASSWORD, andOB_DATABASEwith the corresponding information of your database.Here is an example of the configuration file:
{ "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 change to
Available, and theMCP toolsandresourcesinformation will be displayed.Click the switch button to enable it for Cline.
Test the MCP server
Open the Cline session box and enter the prompt "How many tables are in the test database?" Cline will display the SQL statement to be executed. If there are no issues, click Run.
Cline will display the names of the tables in the test database, indicating a successful connection to the OceanBase database.
Use FastAPI to quickly create a RESTful API project
You can use FastAPI to quickly create a RESTful API project. FastAPI is a Python web framework that allows you to build RESTful APIs quickly.
Create a customer table
Enter the prompt:
Create a customer table with ID as the primary key, and fields name, age, telephone, and locationin the dialog box. Confirm the SQL statement and click theRunbutton to execute the query.Insert test data
Enter the prompt:
Insert 10 recordsin the dialog box. Confirm the SQL statement and click theRunbutton to execute the query.After the insertion is successful, the execution result will be displayed:
Create a FastAPI project
Enter the prompt:
Create a FastAPI project and generate a RESTful API based on the customer tablein the dialog box. Confirm the SQL statement and click theRunbutton to execute the query.This step will automatically generate three files. It is recommended to select "Accept All" for the first use, as the content of the AI-generated files may be uncertain. You can adjust them as needed based on your actual requirements later.
Create a virtual environment and install dependencies
Execute the following commands to create a virtual environment using the uv package manager and install the required packages in the current directory:
uv venv source .venv/bin/activate uv pip install -r requirements.txtStart the FastAPI project
Execute 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 result will be as follows:
[{"telephone":"11111111101","id":1,"name":"Zhang San","location":"Beijing","age":25},{"telephone":"11111111102","id":2,"name":"Li Si","location":"Shanghai","age":30},{"telephone":"11111111103","id":3,"name":"Wang Wu","location":"Guangzhou","age":22},{"telephone":"11111111104","id":4,"name":"Zhao Liu","location":"Shenzhen","age":28},{"telephone":"11111111105","id":5,"name":"Qian Qi","location":"Hangzhou","age":35},{"telephone":"11111111106","id":6,"name":"Sun Ba","location":"Nanjing","age":40},{"telephone":"11111111107","id":7,"name":"Zhou Jiu","location":"Chengdu","age":27},{"telephone":"11111111108","id":8,"name":"Wu Shi","location":"Wuhan","age":33},{"telephone":"11111111109","id":9,"name":"Zheng Shiyi","location":"Xi'an","age":29},{"telephone":"11111111110","id":10,"name":"Wang Shier","location":"Chongqing","age":31}]We can see that the RESTful APIs for creating, reading, updating, and deleting customers have been successfully generated:
from fastapi import FastAPI, Depends, HTTPException from sqlalchemy.orm import Session from models import Customer from database import SessionLocal, engine from pydantic import BaseModel app = FastAPI() # Database dependency def get_db(): db = SessionLocal() try: yield db finally: db.close() # Request model class CustomerCreate(BaseModel): name: str age: int telephone: str location: str # Response model class CustomerResponse(CustomerCreate): id: int class Config: from_attributes = True @app.post("/customers/") def create_customer(customer: CustomerCreate, db: Session = Depends(get_db)): db_customer = Customer(**customer.model_dump()) db.add(db_customer) db.commit() db.refresh(db_customer) return db_customer @app.get("/customers/{customer_id}") 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.get("/customers/") def read_customers(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)): return db.query(Customer).offset(skip).limit(limit).all() @app.put("/customers/{customer_id}") def update_customer(customer_id: int, customer: CustomerCreate, 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 field, value in customer.model_dump().items(): setattr(db_customer, field, 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)): customer = db.query(Customer).filter(Customer.id == customer_id).first() if customer is None: raise HTTPException(status_code=404, detail="Customer not found") db.delete(customer) db.commit() return {"message": "Customer deleted successfully"}