0%

vscode 串 dab 與 sql server 用自然語言查詢

 

看到一個滿有趣的專案 dab 可以用類似 ords 的方式把資料表暴露成 api 使用
並且支援 mcp, 缺點是他並不是 100% mcp, 會輸出非 json 所以其他 mcp 讀取會出現 error, 只能用 vscode XD

參考
https://learn.microsoft.com/zh-tw/azure/data-api-builder/mcp/quickstart-visual-studio-code?tabs=stdio

install

1
2
dotnet tool install --global Microsoft.DataApiBuilder
dotnet tool list --global

建立 db

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
CREATE DATABASE ProductsDb;
GO

USE ProductsDb;
GO

CREATE TABLE dbo.Products (
Id INT PRIMARY KEY,
Name NVARCHAR(100) NOT NULL,
Inventory INT NOT NULL,
Price DECIMAL(10,2) NOT NULL,
Cost DECIMAL(10,2) NOT NULL
);

INSERT INTO dbo.Products (Id, Name, Inventory, Price, Cost)
VALUES
(1, 'Action Figure', 40, 14.99, 5.00),
(2, 'Building Blocks', 25, 29.99, 10.00),
(3, 'Puzzle 500 pcs', 30, 12.49, 4.00),
(4, 'Toy Car', 50, 7.99, 2.50),
(5, 'Board Game', 20, 34.99, 12.50),
(6, 'Doll House', 10, 79.99, 30.00),
(7, 'Stuffed Bear', 45, 15.99, 6.00),
(8, 'Water Blaster', 35, 19.99, 7.00),
(9, 'Art Kit', 28, 24.99, 8.00),
(10,'RC Helicopter', 12, 59.99, 22.00);

準備自己的連線字串, 我用 localdb

1
Server=(localdb)\\MSSQLLocalDB;Database=ProductsDb;Trusted_Connection=True

初始化

1
2
3
dab init --database-type mssql --connection-string "Server=(localdb)\\MSSQLLocalDB;Database=ProductsDb;Trusted_Connection=True" --host-mode Development --config dab-config.json

dab add Products --source dbo.Products --permissions "anonymous:read" --description "Toy store products with inventory, price, and cost."

加入其他描述

1
2
3
4
5
dab update Products --fields.name Id --fields.primary-key true --fields.description "Product Id"
dab update Products --fields.name Name --fields.description "Product name"
dab update Products --fields.name Inventory --fields.description "Units in stock"
dab update Products --fields.name Price --fields.description "Retail price"
dab update Products --fields.name Cost --fields.description "Store cost"

設定 vscode 的 C:\Users\yourname\AppData\Roaming\Code\User\mcp.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"servers": {
"sql-mcp-server": {
"type": "stdio",
"command": "dab",
"args": [
"start",
"--mcp-stdio",
"role:anonymous",
"--config",
"C:\\Users\\yourname\\Desktop\\dab-test\\dab-config.json"
]
}
},
"inputs": []
}

他這邊 localdb 會噴一個雷, 要手動調整下 dab-config.json 他會給 \\\\ 四個斜線, 有三條還不夠要四條 ROFL ~

1
"connection-string": "Server=(localdb)\\MSSQLLocalDB;Database=ProductsDb;Trusted_Connection=True",
關閉