Initial commit
This commit is contained in:
@@ -0,0 +1,284 @@
|
||||
---
|
||||
sidebar_label: Cursor
|
||||
slug: /cursor
|
||||
---
|
||||
|
||||
# Integrate OceanBase MCP Server with Cursor
|
||||
|
||||
[MCP (Model Context Protocol)](https://modelcontextprotocol.io/introduction) is an open-source protocol introduced by Anthropic in November 2024. It allows large language models to interact with external tools or data sources. With MCP, you do not need to manually copy and execute the output of large language models. Instead, the large language model can directly instruct tools to perform specific actions.
|
||||
|
||||
[MCP Server](https://github.com/oceanbase/mcp-oceanbase/tree/main/src/oceanbase_mcp_server) enables large language models to interact with OceanBase Database through the MCP protocol and execute SQL statements. With the right client, you can quickly build project prototypes. The server has been open-sourced on GitHub.
|
||||
|
||||
[Cursor](https://cursordocs.com) is an AI-powered code editor that supports multiple operating systems, including Windows, macOS, and Linux.
|
||||
|
||||
This topic demonstrates how to integrate Cursor with OceanBase MCP Server to quickly build a backend application.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* You have deployed seekdb.
|
||||
|
||||
* You have installed [Python 3.11 or later](https://www.python.org/downloads/) and the corresponding [pip](https://pip.pypa.io/en/stable/installation/). If your machine has an older version of Python, you can use Miniconda to create a new environment with Python 3.11 or above. For more information, see [Miniconda installation guide](https://docs.anaconda.com/miniconda/install/).
|
||||
|
||||
* You have installed [Git](https://git-scm.com//downloads) based on your operating system.
|
||||
|
||||
* You have installed the Python package manager uv. After the installation, run the `uv --version` command to verify whether the installation is successful:
|
||||
|
||||
```shell
|
||||
pip install uv
|
||||
uv --version
|
||||
```
|
||||
|
||||
* You have downloaded [Cursor](https://cursor.com/cn/downloads) and installed the version that matches your operating system. When you use Cursor for the first time, you need to register a new account or log in with an existing one. After logging in, you can create a new project or open an existing project.
|
||||
|
||||
## Step 1: Obtain the database connection information
|
||||
|
||||
Contact your seekdb deployment engineer or administrator to obtain the database connection string. For example:
|
||||
|
||||
```sql
|
||||
obclient -h$host -P$port -u$user_name -p$password -D$database_name
|
||||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
* `$host`: The IP address for connecting to seekdb.
|
||||
* `$port`: The port number for connecting to seekdb. Default is `2881`.
|
||||
* `$database_name`: The name of the database to access.
|
||||
|
||||
:::tip
|
||||
The connected user must have <code>CREATE</code>, <code>INSERT</code>, <code>DROP</code>, and <code>SELECT</code> privileges on the database.
|
||||
:::
|
||||
|
||||
* `$user_name`: The username for connecting to the database.
|
||||
* `$password`: The password for the account.
|
||||
|
||||
## 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 device:
|
||||
|
||||
```shell
|
||||
git clone https://github.com/oceanbase/mcp-oceanbase.git
|
||||
```
|
||||
|
||||
Go to the source code directory:
|
||||
|
||||
```shell
|
||||
cd mcp-oceanbase
|
||||
```
|
||||
|
||||
### Install dependencies
|
||||
|
||||
Run the following command in the `mcp-oceanbase` directory to create a virtual environment and install dependencies:
|
||||
|
||||
```shell
|
||||
uv venv
|
||||
source .venv/bin/activate
|
||||
uv pip install .
|
||||
```
|
||||
|
||||
### Create a working directory for the Cursor client
|
||||
|
||||
Manually create a working directory (such as `cursor`) for the Cursor client and open it with Cursor. The files generated by Cursor will be stored in this directory.
|
||||
|
||||
### Add and configure the OceanBase MCP Server
|
||||
|
||||
1. Use Cursor V2.0.64 as an example. Click the **Open Settings** icon in the upper-right corner, select **Tools & MCP**, and click **New MCP Server**.
|
||||
|
||||

|
||||
|
||||
2. Edit the `mcp.json` configuration file.
|
||||
|
||||

|
||||
|
||||
Replace `path/to/your/mcp-oceanbase/src/oceanbase_mcp_server` with the absolute path of the `oceanbase_mcp_server` folder. Replace `OB_HOST`, `OB_PORT`, `OB_USER`, `OB_PASSWORD`, and `OB_DATABASE` with the corresponding information of your database:
|
||||
|
||||
```json
|
||||
{
|
||||
"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": "***"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3. If the configuration is successful, the MCP Server is displayed in ready status.
|
||||
|
||||

|
||||
|
||||
### Test the MCP Server
|
||||
|
||||
1. In the chat dialog box, enter the prompt: `How many tables are there in the dataanalysis_english database?`. The Cursor client will display the SQL statement to be executed. Confirm that it is correct and click the `Run` button to execute the query. The Cursor client will display all the table names in the `dataanalysis_english` database, indicating that we have successfully connected to seekdb.
|
||||
|
||||

|
||||
|
||||
### 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 for building RESTful APIs.
|
||||
|
||||
1. Create a customer table
|
||||
|
||||
In the dialog box, enter the prompt: `Create a customer table with the ID as the primary key and name, age, telephone, and location as fields`, confirm the SQL statement, and click `Run` to execute the query.
|
||||
|
||||

|
||||
|
||||
2. Insert test data
|
||||
|
||||
In the dialog box, enter the prompt: `Insert 10 rows of data into the customer table`, confirm the SQL statement, and click `Run` to execute the query. After the data is inserted, a message will be displayed: `Inserted 10 rows into the customer table. The data includes...`.
|
||||
|
||||

|
||||
|
||||
3. Create a FastAPI project
|
||||
|
||||
In the dialog box, enter the prompt: `Create a FastAPI project and generate a RESTful API based on the customer table`, confirm the SQL statement, and click `Run` to execute the query.
|
||||
|
||||

|
||||
|
||||
This step will automatically generate necessary files. It is recommended to select `Accept All` for the first use, because the content of the files generated by AI may be uncertain, and you can adjust them as needed later.
|
||||
|
||||
4. Create a virtual environment and install dependencies
|
||||
|
||||
Execute the following command to use the uv package manager to create a virtual environment and install the dependencies in the current directory:
|
||||
|
||||
```shell
|
||||
uv venv
|
||||
source .venv/bin/activate
|
||||
uv pip install -r requirements.txt
|
||||
```
|
||||
|
||||
5. Start the FastAPI project
|
||||
|
||||
Execute the following command to start the FastAPI project:
|
||||
|
||||
```shell
|
||||
uvicorn main:app --reload
|
||||
```
|
||||
|
||||
6. View the data in the table
|
||||
|
||||
Run the following command in the command line or use other request tools to view the data in the table:
|
||||
|
||||
```shell
|
||||
curl http://127.0.0.1:8000/customers
|
||||
```
|
||||
|
||||
The return result is as follows:
|
||||
|
||||
```json
|
||||
[{"ID":1,"name":"John Smith","age":28,"telephone":"555-0101","location":"New York, NY"},{"ID":2,"name":"Emily Johnson","age":35,"telephone":"555-0102","location":"Los Angeles, CA"},{"ID":3,"name":"Michael Brown","age":42,"telephone":"555-0103","location":"Chicago, IL"},{"ID":4,"name":"Sarah Davis","age":29,"telephone":"555-0104","location":"Houston, TX"},{"ID":5,"name":"David Wilson","age":51,"telephone":"555-0105","location":"Phoenix, AZ"},{"ID":6,"name":"Jessica Martinez","age":33,"telephone":"555-0106","location":"Philadelphia, PA"},{"ID":7,"name":"Robert Taylor","age":45,"telephone":"555-0107","location":"San Antonio, TX"},{"ID":8,"name":"Amanda Anderson","age":27,"telephone":"555-0108","location":"San Diego, CA"},{"ID":9,"name":"James Thomas","age":38,"telephone":"555-0109","location":"Dallas, TX"},{"ID":10,"name":"Lisa Jackson","age":31,"telephone":"555-0110","location":"San Jose, CA"}]
|
||||
```
|
||||
|
||||
You can see that the RESTful APIs for creating, deleting, updating, and querying data have been successfully generated:
|
||||
|
||||
```shell
|
||||
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
|
||||
|
||||
# seekdb connection configuration (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}
|
||||
```
|
||||
@@ -0,0 +1,288 @@
|
||||
---
|
||||
sidebar_label: Cline
|
||||
slug: /cline
|
||||
---
|
||||
|
||||
# Integrate OceanBase MCP Server with Cline
|
||||
|
||||
seekdb supports vector data storage, vector indexing, and embedding-based vector search. You can store vectorized data in seekdb for further search.
|
||||
|
||||
[Cline](https://cline.bot/) is an open-source AI coding assistant that supports the MCP protocol.
|
||||
|
||||
This topic uses Cline to demonstrate how to quickly build a backend application using OceanBase MCP Server.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* You have deployed seekdb.
|
||||
|
||||
* You have an existing MySQL database and account available in your environment, and the database account has been granted read and write privileges.
|
||||
|
||||
* You have installed [Python 3.11 or later](https://www.python.org/downloads/) and the corresponding [pip](https://pip.pypa.io/en/stable/installation/). If your machine has a low Python version, you can use Miniconda to create a new Python 3.11 or later environment. For more information, see [Miniconda installation guide](https://docs.anaconda.com/miniconda/install/).
|
||||
|
||||
* Install [Git](https://git-scm.com//downloads) based on your operating system.
|
||||
|
||||
* Install uv, a Python package manager. After the installation, run the `uv --version` command to verify the installation:
|
||||
|
||||
```shell
|
||||
pip install uv
|
||||
uv --version
|
||||
```
|
||||
|
||||
* Install Cline:
|
||||
|
||||
* If you are using Visual Studio Code IDE, search for the Cline extension and install it in the `Extensions` section. The extension name is `Cline`. After the installation, click the settings icon to configure the large model API for Cline as follows:
|
||||
|
||||

|
||||
|
||||
* If you do not have an IDE, download Cline from [Cline](https://cline.bot/) and follow the [installation guide](https://docs.cline.bot/getting-started/installing-cline).
|
||||
|
||||
## Step 1: Obtain the database connection information
|
||||
|
||||
Contact your seekdb deployment engineer or administrator to obtain the database connection string. For example:
|
||||
|
||||
```sql
|
||||
obclient -h$host -P$port -u$user_name -p$password -D$database_name
|
||||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
* `$host`: The IP address for connecting to seekdb.
|
||||
* `$port`: The port number for connecting to seekdb. Default is `2881`.
|
||||
* `$database_name`: The name of the database to access.
|
||||
|
||||
:::tip
|
||||
The connected user must have <code>CREATE</code>, <code>INSERT</code>, <code>DROP</code>, and <code>SELECT</code> privileges on the database.
|
||||
:::
|
||||
|
||||
* `$user_name`: The username for connecting to the database.
|
||||
* `$password`: The password for the account.
|
||||
|
||||
## Step 2: Configure the OceanBase MCP Server
|
||||
|
||||
This example uses Visual Studio Code to demonstrate 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 device:
|
||||
|
||||
```shell
|
||||
git clone https://github.com/oceanbase/mcp-oceanbase.git
|
||||
```
|
||||
|
||||
Go to the source code directory:
|
||||
|
||||
```shell
|
||||
cd mcp-oceanbase
|
||||
```
|
||||
|
||||
### Install dependencies
|
||||
|
||||
Run the following command in the `mcp-oceanbase` directory to create a virtual environment and install dependencies:
|
||||
|
||||
```shell
|
||||
uv venv
|
||||
source .venv/bin/activate
|
||||
uv pip install .
|
||||
```
|
||||
|
||||
### Create a working directory for Visual Studio Code
|
||||
|
||||
Manually create a working directory for Visual Studio Code on your local device and open it with Visual Studio Code. The files generated by Cline will be placed in this directory. The name of the sample directory is `cline-generate`.
|
||||
|
||||
<!--  -->
|
||||
|
||||
### Configure the OceanBase MCP Server in Cline
|
||||
|
||||
Click the Cline icon on the left-side navigation pane to open the Cline dialog box.
|
||||
|
||||

|
||||
|
||||
### Add and configure MCP servers
|
||||
|
||||
1. Click the **MCP Servers** icon as shown in the following figure.
|
||||
|
||||

|
||||
|
||||
2. Manually configure the OceanBase MCP Server according to the numbered instructions in the figure below.
|
||||
|
||||

|
||||
|
||||
3. Edit the configuration file.
|
||||
|
||||
In the `cline_mcp_settings.json` file that was opened in the previous step, enter the following configuration information and save the file. Replace `/path/to/your/mcp-oceanbase/src/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 your database information.
|
||||
|
||||
The configuration file is as follows:
|
||||
|
||||
```json
|
||||
{
|
||||
"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": "***"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
4. If the configuration is successful, the MCP Server is displayed in ready status, and the MCP `Tools` and `Resources` information will be displayed, as shown in the following figure:
|
||||
|
||||

|
||||
|
||||
5. Click the switch button in the following figure to enable the MCP Server so that Cline can use it:
|
||||
|
||||

|
||||
|
||||
### Test the MCP Server
|
||||
|
||||
Open the Cline session dialog box and enter the prompt `How many tables are there in the dataanalysis_english database`. Cline will display the SQL statement about to be executed. Confirm the SQL statement and click the `Act` button.
|
||||
|
||||

|
||||
|
||||
Cline will display the table names in the `dataanalysis_english` database, indicating that it can properly connect to seekdb.
|
||||
|
||||

|
||||
|
||||
### Create a RESTful API project using FastAPI
|
||||
|
||||
You can use FastAPI to quickly create a RESTful API project. FastAPI is a Python web framework that allows you to build RESTful APIs efficiently.
|
||||
|
||||
1. Create the customer table
|
||||
|
||||
In the dialog box, enter the prompt: `Create a "customer" table with "ID" as the primary key, including the fields "name", "age", "telephone", and "location"`. Confirm the SQL statement and click the `Act` button.
|
||||
|
||||

|
||||
|
||||
2. Insert test data
|
||||
|
||||
In the dialog box, enter the prompt: `Insert 10 rows of test data`. Confirm the SQL statement and click the `Act` button.
|
||||
|
||||

|
||||
|
||||
After the data is inserted, the execution result will be displayed.
|
||||
|
||||

|
||||
|
||||
3. Create a FastAPI project
|
||||
|
||||
In the dialog box, enter the prompt: `Create a FastAPI project and generate a RESTful API based on the "customer" table`. Confirm the SQL statement and click the `Act` button.
|
||||
|
||||

|
||||
|
||||
This step will automatically generate files. We recommend selecting "Accept All" the first time, because AI-generated files may be uncertain; you can adjust them later as needed.
|
||||
|
||||
4. Create a virtual environment and install dependencies
|
||||
|
||||
Run the following command to create a virtual environment using the uv package manager and install the dependency packages in the current directory:
|
||||
|
||||
```shell
|
||||
uv venv
|
||||
source .venv/bin/activate
|
||||
uv pip install -r requirements.txt
|
||||
```
|
||||
|
||||
5. Start the FastAPI project
|
||||
|
||||
Run the following command to start the FastAPI project:
|
||||
|
||||
```shell
|
||||
uvicorn main:app --reload
|
||||
```
|
||||
|
||||
6. View data in the table
|
||||
|
||||
Run the following command in the command line, or use another request tool, to view the data in the table:
|
||||
|
||||
```shell
|
||||
curl http://127.0.0.1:8000/customers
|
||||
```
|
||||
|
||||
The return result is as follows:
|
||||
|
||||
```json
|
||||
[{"ID":1,"name":"Alice Johnson","age":28,"telephone":"123-456-7890","location":"New York"},{"ID":2,"name":"Bob Smith","age":34,"telephone":"234-567-8901","location":"Los Angeles"},{"ID":3,"name":"Charlie Brown","age":45,"telephone":"345-678-9012","location":"Chicago"},{"ID":4,"name":"David Wilson","age":56,"telephone":"456-789-0123","location":"Houston"},{"ID":5,"name":"Eve Davis","age":67,"telephone":"567-890-1234","location":"Phoenix"},{"ID":6,"name":"Frank Garcia","age":78,"telephone":"678-901-2345","location":"Philadelphia"},{"ID":7,"name":"Grace Martinez","age":89,"telephone":"789-012-3456","location":"San Antonio"},{"ID":8,"name":"Hannah Robinson","age":19,"telephone":"890-123-4567","location":"San Diego"},{"ID":9,"name":"Ian Clark","age":23,"telephone":"901-234-5678","location":"Dallas"},{"ID":10,"name":"Julia Lewis","age":31,"telephone":"012-345-6789","location":"San Jose"}]
|
||||
```
|
||||
|
||||
You can see that the RESTful APIs for creating, reading, updating, and deleting data have been successfully generated:
|
||||
|
||||
```python
|
||||
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"}
|
||||
```
|
||||
@@ -0,0 +1,154 @@
|
||||
---
|
||||
sidebar_label: Continue
|
||||
slug: /continue
|
||||
---
|
||||
|
||||
# Integrate OceanBase MCP Server with Continue
|
||||
|
||||
[MCP (Model Context Protocol)](https://modelcontextprotocol.io/introduction) is an open-source protocol released by Anthropic in November 2024. It enables large language models to interact with external tools or data sources. With MCP, users do not need to manually copy and execute the output of large models; instead, the models can directly instruct tools to perform specific actions (Actions).
|
||||
|
||||
[MCP Server](https://github.com/oceanbase/mcp-oceanbase/tree/main/src/oceanbase_mcp_server) provides the capability for large models to interact with seekdb through the MCP protocol, allowing the execution of SQL statements. With the right client, you can quickly build a project prototype, and it is open-source on GitHub.
|
||||
|
||||
[Continue](https://continue.dev) is an IDE extension that integrates with the MCP Server, supporting Visual Studio Code and IntelliJ IDEA.
|
||||
|
||||
This topic will guide you on how to integrate Continue with the OceanBase MCP Server to quickly build backend applications.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* You have deployed seekdb.
|
||||
|
||||
* You have installed [Python 3.11 or later](https://www.python.org/downloads/) and the corresponding [pip](https://pip.pypa.io/en/stable/installation/). If your machine has a low Python version, you can use Miniconda to create a new Python 3.11 or later environment. For more information, see [Miniconda installation guide](https://docs.anaconda.com/miniconda/install/).
|
||||
|
||||
* Install [Git](https://git-scm.com//downloads) based on your operating system.
|
||||
|
||||
* Install uv, a Python package manager. After the installation, run the `uv --version` command to verify the installation:
|
||||
|
||||
```shell
|
||||
pip install uv
|
||||
uv --version
|
||||
```
|
||||
|
||||
* Install the Continue extension in Visual Studio Code or IntelliJ IDEA. The extension name is `Continue`.
|
||||
|
||||

|
||||
|
||||
* After the installation is complete, click `Add Models` to configure the large model API for Continue. The API configuration is as follows:
|
||||
|
||||

|
||||
|
||||
* The configuration file is as follows:
|
||||
|
||||
```json
|
||||
name: Local Assistant
|
||||
version: 1.0.0
|
||||
schema: v1
|
||||
models:
|
||||
# Model name
|
||||
- name: DeepSeek-R1-671B
|
||||
# Model provider
|
||||
provider: deepseek
|
||||
# Model type
|
||||
model: DeepSeek-R1-671B
|
||||
# URL address for accessing the model
|
||||
apiBase: *********
|
||||
# API key for accessing the model
|
||||
apiKey: ********
|
||||
# Context provider
|
||||
context:
|
||||
- provider: code
|
||||
- provider: docs
|
||||
- provider: diff
|
||||
- provider: terminal
|
||||
- provider: problems
|
||||
- provider: folder
|
||||
- provider: codebase
|
||||
```
|
||||
|
||||
## Step 1: Obtain the database connection information
|
||||
|
||||
Contact your seekdb deployment engineer or administrator to obtain the database connection string. For example:
|
||||
|
||||
```sql
|
||||
obclient -h$host -P$port -u$user_name -p$password -D$database_name
|
||||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
* `$host`: The IP address for connecting to seekdb.
|
||||
* `$port`: The port number for connecting to seekdb. Default is `2881`.
|
||||
* `$database_name`: The name of the database to access.
|
||||
|
||||
:::tip
|
||||
The connected user must have <code>CREATE</code>, <code>INSERT</code>, <code>DROP</code>, and <code>SELECT</code> privileges on the database.
|
||||
:::
|
||||
|
||||
* `$user_name`: The username for connecting to the database.
|
||||
* `$password`: The password for the account.
|
||||
|
||||
## 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 device:
|
||||
|
||||
```shell
|
||||
git clone https://github.com/oceanbase/mcp-oceanbase.git
|
||||
```
|
||||
|
||||
Go to the source code directory:
|
||||
|
||||
```shell
|
||||
cd mcp-oceanbase
|
||||
```
|
||||
|
||||
### Install dependencies
|
||||
|
||||
Run the following command in the `mcp-oceanbase` directory to create a virtual environment and install dependencies:
|
||||
|
||||
```shell
|
||||
uv venv
|
||||
source .venv/bin/activate
|
||||
uv pip install .
|
||||
```
|
||||
|
||||
### Add and configure MCP servers
|
||||
|
||||
1. Click the button in the upper-right corner of the menu bar to open the MCP panel.
|
||||
|
||||

|
||||
|
||||
2. Click Add `MCP Servers`.
|
||||
|
||||
:::tip
|
||||
MCP can be used only in the Continue Agent mode.
|
||||
:::
|
||||
|
||||

|
||||
|
||||
3. Fill in the configuration file and click OK.
|
||||
|
||||
Replace `/path/to/your/mcp-oceanbase/src/oceanbase_mcp_server` with the absolute path of the `oceanbase_mcp_server` folder. Replace `OB_HOST`, `OB_PORT`, `OB_USER`, `OB_PASSWORD`, and `OB_DATABASE` with the corresponding information of your database:
|
||||
|
||||
```json
|
||||
name: SeekDB
|
||||
version: 0.0.1
|
||||
schema: v1
|
||||
mcpServers:
|
||||
- name: SeekDB
|
||||
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: "***"
|
||||
```
|
||||
|
||||
4. If the configuration is successful, the following message is displayed:
|
||||
|
||||

|
||||
@@ -0,0 +1,291 @@
|
||||
---
|
||||
sidebar_label: TRAE
|
||||
slug: /trae
|
||||
---
|
||||
|
||||
# Integrate OceanBase MCP Server with TRAE
|
||||
|
||||
[Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) is an open-source protocol introduced by Anthropic in November 2024. It allows large language models to interact with external tools or data sources. With MCP, you do not need to manually copy and execute the output of large language models. Instead, the large language model can directly command tools to perform specific actions.
|
||||
|
||||
[MCP Server](https://github.com/oceanbase/mcp-oceanbase/tree/main/src/oceanbase_mcp_server) enables large language models to interact with OceanBase Database through the MCP protocol and execute SQL statements. It allows you to quickly build a project prototype with the help of an appropriate client and has been open-sourced on GitHub.
|
||||
|
||||
[TRAE](https://www.trae.ai/) is an IDE that can integrate with MCP Server, can be downloaded from its official website.
|
||||
|
||||
This topic will guide you through the process of integrating TRAE IDE with OceanBase MCP Server to quickly build a backend application.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* You have deployed seekdb.
|
||||
|
||||
* You have installed [Python 3.11 or later](https://www.python.org/downloads/) and the corresponding [pip](https://pip.pypa.io/en/stable/installation/). If your system has a low Python version, you can use Miniconda to create a new Python 3.11 or later environment. For more information, see [Install Miniconda](https://docs.anaconda.com/miniconda/install/).
|
||||
|
||||
* You have installed [Git](https://git-scm.com//downloads) based on your operating system.
|
||||
|
||||
* You have installed uv, a Python package manager. After the installation, run the `uv --version` command to check whether the installation was successful:
|
||||
|
||||
```shell
|
||||
pip install uv
|
||||
uv --version
|
||||
```
|
||||
|
||||
* You have downloaded [TRAE IDE](https://www.trae.ai/download) and installed the version suitable for your operating system.
|
||||
|
||||
## Step 1: Obtain the database connection information
|
||||
|
||||
Contact your seekdb deployment engineer or administrator to obtain the database connection string. For example:
|
||||
|
||||
```sql
|
||||
obclient -h$host -P$port -u$user_name -p$password -D$database_name
|
||||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
* `$host`: The IP address for connecting to seekdb.
|
||||
* `$port`: The port number for connecting to seekdb. Default is `2881`.
|
||||
* `$database_name`: The name of the database to access.
|
||||
|
||||
:::tip
|
||||
The connected user must have <code>CREATE</code>, <code>INSERT</code>, <code>DROP</code>, and <code>SELECT</code> privileges on the database.
|
||||
:::
|
||||
|
||||
* `$user_name`: The username for connecting to the database.
|
||||
* `$password`: The password for the account.
|
||||
|
||||
## 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 device:
|
||||
|
||||
```shell
|
||||
git clone https://github.com/oceanbase/mcp-oceanbase.git
|
||||
```
|
||||
|
||||
Go to the source code directory:
|
||||
|
||||
```shell
|
||||
cd mcp-oceanbase
|
||||
```
|
||||
|
||||
### Install the dependencies
|
||||
|
||||
Run the following commands in the `mcp-oceanbase` directory to create a virtual environment and install the dependencies:
|
||||
|
||||
```shell
|
||||
uv venv
|
||||
source .venv/bin/activate
|
||||
uv pip install .
|
||||
```
|
||||
|
||||
### Create a working directory for the TRAE client
|
||||
|
||||
Manually create a working directory for TRAE and open it. TRAE will generate files in this directory. The example directory name is `trae-generate`.
|
||||
|
||||

|
||||
|
||||
### Configure the OceanBase MCP Server in TRAE
|
||||
|
||||
Press `Ctrl + U` (Windows) or `Command + U` (MacOS) to open the chat box. Click the gear icon in the upper-right corner and select **MCP**.
|
||||
|
||||

|
||||
|
||||
### Add and configure MCP servers
|
||||
|
||||
1. Click **Add MCP Servers** and select **Add Manually**.
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
2. Delete the sample content in the edit box.
|
||||
|
||||

|
||||
|
||||
Then enter the following contents:
|
||||
|
||||
```json
|
||||
{
|
||||
"mcpServers": {
|
||||
"oceanbase": {
|
||||
"command": "uv",
|
||||
"args": [
|
||||
"--directory",
|
||||
// Replace with the absolute path of the oceanbase_mcp_server folder.
|
||||
"/path/to/your/mcp-oceanbase/src/oceanbase_mcp_server",
|
||||
"run",
|
||||
"oceanbase_mcp_server"
|
||||
],
|
||||
"env": {
|
||||
// Replace with your OceanBase Database connection information.
|
||||
"OB_HOST": "***",
|
||||
"OB_PORT": "***",
|
||||
"OB_USER": "***",
|
||||
"OB_PASSWORD": "***",
|
||||
"OB_DATABASE": "***"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3. The configuration is successful.
|
||||
|
||||

|
||||
|
||||
### Test the MCP server
|
||||
|
||||
1. Select the **Builder with MCP** agent.
|
||||
|
||||

|
||||
|
||||
2. In the dialog box, enter `How many tables are there in the test database`. The TRAE client will display the SQL statement to be executed. Confirm the SQL statement and click the `Run` button.
|
||||
|
||||

|
||||
|
||||
3. The TRAE client will display the number of tables in the `test` database. This indicates that you have successfully connected to seekdb.
|
||||
|
||||

|
||||
|
||||
### Create a RESTful API project using FastAPI
|
||||
|
||||
You can use FastAPI to quickly create a RESTful API project. FastAPI is a Python web framework that enables you to build RESTful APIs efficiently.
|
||||
|
||||
1. Create the customer table.
|
||||
|
||||
In the dialog box, enter `Create a "customer" table with "Id" as the primary key, including the fields of "name", "age", "telephone", and "location"`. Confirm the SQL statement and click the `Run` button.
|
||||
|
||||

|
||||
|
||||
2. Insert test data.
|
||||
|
||||
In the dialog box, enter `Insert 10 test data entries`. Confirm the SQL statement and click the `Run` button.
|
||||
|
||||

|
||||
|
||||
The execution result is displayed after the insertion is successful:
|
||||
|
||||

|
||||
|
||||
3. Create a FastAPI project.
|
||||
|
||||
In the dialog box, enter `Create a FastAPI project and generate a RESTful API based on the "customer" table`. Confirm the SQL statement and click the `Run` button.
|
||||
|
||||

|
||||
|
||||
This step will generate three files. We recommend that you select "Accept All" for the first use, because the files generated by AI may contain uncertain contents. You can adjust them based on your actual needs later.
|
||||
|
||||
4. Create a virtual environment and install dependencies
|
||||
|
||||
Execute the following command to use the uv package manager to create a virtual environment and install the required packages in the current directory:
|
||||
|
||||
```shell
|
||||
uv venv
|
||||
source .venv/bin/activate
|
||||
uv pip install -r requirements.txt
|
||||
```
|
||||
|
||||
5. Start the FastAPI project.
|
||||
|
||||
Run the following command to start the FastAPI project:
|
||||
|
||||
```shell
|
||||
uvicorn main:app --reload
|
||||
```
|
||||
|
||||
6. View the data in the table.
|
||||
|
||||
Run the following command in the command line or use other request tools to view the data in the table:
|
||||
|
||||
```shell
|
||||
curl http://127.0.0.1:8000/customers
|
||||
```
|
||||
|
||||
The return result is as follows:
|
||||
|
||||
```json
|
||||
[{"Id":1,"name":"Alice","age":25,"telephone":"123-***-7890","location":"New York"},{"Id":2,"name":"Bob","age":30,"telephone":"234-***-8901","location":"Los Angeles"},{"Id":3,"name":"Charlie","age":35,"telephone":"345-***-9012","location":"Chicago"},{"Id":4,"name":"David","age":40,"telephone":"456-***-0123","location":"Houston"},{"Id":5,"name":"Eve","age":45,"telephone":"567-***-1234","location":"Miami"},{"Id":6,"name":"Frank","age":50,"telephone":"678-***-2345","location":"Seattle"},{"Id":7,"name":"Grace","age":55,"telephone":"789-***-3456","location":"Denver"},{"Id":8,"name":"Heidi","age":60,"telephone":"890-***-4567","location":"Boston"},{"Id":9,"name":"Ivan","age":65,"telephone":"901-***-5678","location":"Philadelphia"},{"Id":10,"name":"Judy","age":70,"telephone":"012-***-6789","location":"San Francisco"}]
|
||||
```
|
||||
|
||||
You can see that the RESTful APIs for CRUD operations has been successfully generated:
|
||||
|
||||
```shell
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
import mysql.connector
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# Database connection configuration
|
||||
config = {
|
||||
'user': '*******',
|
||||
'password': '******',
|
||||
'host': 'xx.xxx.xxx.xx',
|
||||
'database': 'test',
|
||||
'port':xxxx,
|
||||
'raise_on_warnings': True
|
||||
}
|
||||
|
||||
class Customer(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
age: int
|
||||
telephone: str
|
||||
location: str
|
||||
|
||||
@app.get('/customers')
|
||||
async def get_customers():
|
||||
cnx = mysql.connector.connect(**config)
|
||||
cursor = cnx.cursor(dictionary=True)
|
||||
query = 'SELECT * FROM customer'
|
||||
cursor.execute(query)
|
||||
results = cursor.fetchall()
|
||||
cursor.close()
|
||||
cnx.close()
|
||||
return results
|
||||
|
||||
@app.get('/customers/{customer_id}')
|
||||
async def get_customer(customer_id: int):
|
||||
cnx = mysql.connector.connect(**config)
|
||||
cursor = cnx.cursor(dictionary=True)
|
||||
query = 'SELECT * FROM customer WHERE ID = %s'
|
||||
cursor.execute(query, (customer_id,))
|
||||
result = cursor.fetchone()
|
||||
cursor.close()
|
||||
cnx.close()
|
||||
return result
|
||||
|
||||
@app.post('/customers')
|
||||
async def create_customer(customer: Customer):
|
||||
cnx = mysql.connector.connect(**config)
|
||||
cursor = cnx.cursor()
|
||||
query = 'INSERT INTO customer (ID, name, age, telephone, location) VALUES (%s, %s, %s, %s, %s)'
|
||||
data = (customer.id, customer.name, customer.age, customer.telephone, customer.location)
|
||||
cursor.execute(query, data)
|
||||
cnx.commit()
|
||||
cursor.close()
|
||||
cnx.close()
|
||||
return {'message': 'Customer created successfully'}
|
||||
|
||||
@app.put('/customers/{customer_id}')
|
||||
async def update_customer(customer_id: int, customer: Customer):
|
||||
cnx = mysql.connector.connect(**config)
|
||||
cursor = cnx.cursor()
|
||||
query = 'UPDATE customer SET name = %s, age = %s, telephone = %s, location = %s WHERE ID = %s'
|
||||
data = (customer.name, customer.age, customer.telephone, customer.location, customer_id)
|
||||
cursor.execute(query, data)
|
||||
cnx.commit()
|
||||
cursor.close()
|
||||
cnx.close()
|
||||
return {'message': 'Customer updated successfully'}
|
||||
|
||||
@app.delete('/customers/{customer_id}')
|
||||
async def delete_customer(customer_id: int):
|
||||
cnx = mysql.connector.connect(**config)
|
||||
cursor = cnx.cursor()
|
||||
query = 'DELETE FROM customer WHERE ID = %s'
|
||||
cursor.execute(query, (customer_id,))
|
||||
cnx.commit()
|
||||
cursor.close()
|
||||
cnx.close()
|
||||
return {'message': 'Customer deleted successfully'}
|
||||
```
|
||||
Reference in New Issue
Block a user