1
1
# 适用于 Databend 的 MCP 服务器
2
2
3
- [ mcp-databend] ( https://github.com/databendlabs/mcp-databend ) 是一个 MCP(Model Context Protocol,模型上下文协议 )服务器,它让 AI 助手能够使用自然语言直接与你的 Databend 数据库交互。
3
+ [ mcp-databend] ( https://github.com/databendlabs/mcp-databend ) 是一个 MCP(Model Context Protocol)服务器,它让 AI 助手能够使用自然语言直接与你的 Databend 数据库交互。
4
4
5
5
## mcp-databend 能做什么
6
6
7
- - ** execute_sql** - 执行 SQL 查询,并带有超时保护
7
+ - ** execute_sql** - 执行带超时保护的 SQL 查询
8
8
- ** show_databases** - 列出所有可用数据库
9
- - ** show_tables** - 列出数据库中的表(支持可选过滤 )
9
+ - ** show_tables** - 列出数据库中的表(可带可选过滤 )
10
10
- ** describe_table** - 获取详细的表结构信息
11
11
12
12
## 构建 ChatBI 工具
13
13
14
- 本教程将演示如何使用 mcp-databend 和 Agno 框架构建一个对话式商业智能(Business Intelligence)工具。你将创建一个本地代理,能够用自然语言回答数据问题。
14
+ 本教程将演示如何使用 mcp-databend 和 Agno 框架构建对话式商业智能工具。你将创建一个本地代理,用自然语言回答数据问题。
15
+
16
+ ![ Databend MCP ChatBI] ( @site/static/img/connect/databend-mcp-chatbi.png )
15
17
16
18
## 分步教程
17
19
18
- ### 步骤 1 :配置 Databend 连接
20
+ ### 第 1 步 :配置 Databend 连接
19
21
20
22
首先,你需要一个可连接的 Databend 数据库:
21
23
22
24
1 . ** 注册 [ Databend Cloud] ( https://app.databend.cn ) ** (提供免费套餐)
23
- 2 . ** 创建计算集群(Warehouse)和数据库 **
25
+ 2 . ** 创建计算集群和数据库 **
24
26
3 . ** 在控制台获取连接字符串**
25
27
26
28
有关 DSN 格式和示例的详细信息,请参阅[ 连接字符串文档] ( https://docs.databend.cn/developer/drivers/#connection-string-dsn ) 。
30
32
| ** Databend Cloud** | ` databend://user:pwd@host:443/database?warehouse=wh ` |
31
33
| ** 自托管** | ` databend://user:pwd@localhost:8000/database?sslmode=disable ` |
32
34
33
- ### 步骤 2 :安装依赖
35
+ ### 第 2 步 :安装依赖
34
36
35
37
创建虚拟环境并安装所需包:
36
38
37
39
``` bash
38
- # Create virtual environment
40
+ # 创建虚拟环境
39
41
python3 -m venv .venv
40
42
source .venv/bin/activate
41
43
42
- # Install packages
44
+ # 安装包
43
45
pip install packaging openai agno openrouter sqlalchemy fastapi mcp-databend
44
46
```
45
47
46
- ### 步骤 3 :创建 ChatBI 代理
48
+ ### 第 3 步 :创建 ChatBI 代理
47
49
48
- 现在创建 ChatBI 代理,它将使用 mcp-databend 与数据库交互 。
50
+ 现在创建使用 mcp-databend 与数据库交互的 ChatBI 代理 。
49
51
50
52
创建文件 ` agent.py ` :
51
53
54
+ <details >
55
+ <summary >点击查看 agent.py 代码</summary >
56
+
52
57
``` python
53
58
from contextlib import asynccontextmanager
54
59
import os
@@ -66,7 +71,7 @@ logging.basicConfig(level=logging.INFO)
66
71
logger = logging.getLogger(__name__ )
67
72
68
73
def check_env_vars ():
69
- """ Check required environment variables """
74
+ """ 检查必需的环境变量 """
70
75
required = {
71
76
" DATABEND_DSN" : " https://docs.databend.cn/developer/drivers/#connection-string-dsn" ,
72
77
" OPENROUTER_API_KEY" : " https://openrouter.ai/settings/keys"
@@ -75,13 +80,13 @@ def check_env_vars():
75
80
missing = [var for var in required if not os.getenv(var)]
76
81
77
82
if missing:
78
- print (" ❌ Missing environment variables: " )
83
+ print (" ❌ 缺少环境变量: " )
79
84
for var in missing:
80
85
print (f " • { var} : { required[var]} " )
81
- print (" \n Example: export DATABEND_DSN='...' OPENROUTER_API_KEY='...'" )
86
+ print (" \n 示例: export DATABEND_DSN='...' OPENROUTER_API_KEY='...'" )
82
87
sys.exit(1 )
83
88
84
- print (" ✅ Environment variables OK " )
89
+ print (" ✅ 环境变量检查通过 " )
85
90
86
91
check_env_vars()
87
92
@@ -103,10 +108,10 @@ class DatabendTool:
103
108
async def init (self ):
104
109
try :
105
110
await self .mcp.connect()
106
- logger.info(" ✓ Connected to Databend" )
111
+ logger.info(" ✓ 已连接到 Databend" )
107
112
return True
108
113
except Exception as e:
109
- logger.error(f " ✗ Databend connection failed: { e} " )
114
+ logger.error(f " ✗ Databend 连接失败: { e} " )
110
115
return False
111
116
112
117
databend = DatabendTool()
@@ -119,11 +124,11 @@ agent = Agent(
119
124
),
120
125
tools = [],
121
126
instructions = [
122
- " You are ChatBI - a Business Intelligence assistant for Databend. " ,
123
- " Help users explore and analyze their data using natural language. " ,
124
- " Always start by exploring available databases and tables. " ,
125
- " Format query results in clear, readable tables. " ,
126
- " Provide insights and explanations with your analysis. "
127
+ " 你是 ChatBI - Databend 的商业智能助手。 " ,
128
+ " 帮助用户使用自然语言探索和分析数据。 " ,
129
+ " 始终从探索可用数据库和表开始。 " ,
130
+ " 将查询结果格式化为清晰易读的表格。 " ,
131
+ " 在分析中提供见解和解释。 "
127
132
],
128
133
storage = SqliteStorage(table_name = " chatbi" , db_file = " chatbi.db" ),
129
134
add_datetime_to_instructions = True ,
@@ -137,11 +142,11 @@ agent = Agent(
137
142
async def lifespan (app : FastAPI):
138
143
tool = databend.create()
139
144
if not await databend.init():
140
- logger.error(" Failed to initialize Databend" )
141
- raise RuntimeError (" Databend connection failed " )
145
+ logger.error(" 初始化 Databend 失败 " )
146
+ raise RuntimeError (" Databend 连接失败 " )
142
147
143
148
agent.tools.append(tool)
144
- logger.info(" ChatBI initialized successfully " )
149
+ logger.info(" ChatBI 初始化成功 " )
145
150
146
151
yield
147
152
@@ -151,77 +156,79 @@ async def lifespan(app: FastAPI):
151
156
playground = Playground(
152
157
agents = [agent],
153
158
name = " ChatBI with Databend" ,
154
- description = " Business Intelligence Assistant powered by Databend"
159
+ description = " 由 Databend 驱动的商业智能助手 "
155
160
)
156
161
157
162
app = playground.get_app(lifespan = lifespan)
158
163
159
164
if __name__ == " __main__" :
160
- print (" 🤖 Starting MCP Server for Databend " )
161
- print (" Open http://localhost:7777 to start chatting! " )
165
+ print (" 🤖 正在启动 Databend 的 MCP 服务器 " )
166
+ print (" 打开 http://localhost:7777 开始聊天! " )
162
167
playground.serve(app = " agent:app" , host = " 127.0.0.1" , port = 7777 )
163
168
```
164
169
165
- ### 步骤 4:配置环境
170
+ </details >
171
+
172
+ ### 第 4 步:配置环境
166
173
167
174
设置 API 密钥和数据库连接:
168
175
169
176
``` bash
170
- # Set your OpenRouter API key
177
+ # 设置 OpenRouter API 密钥
171
178
export OPENROUTER_API_KEY=" your-openrouter-key"
172
179
173
- # Set your Databend connection string
180
+ # 设置 Databend 连接字符串
174
181
export DATABEND_DSN=" your-databend-connection-string"
175
182
```
176
183
177
- ### 步骤 5 :启动 ChatBI 代理
184
+ ### 第 5 步 :启动 ChatBI 代理
178
185
179
186
运行代理以启动本地服务器:
180
187
181
188
``` bash
182
189
python agent.py
183
190
```
184
191
185
- 你应该会看到 :
192
+ 你将看到 :
186
193
```
187
- ✅ Environment variables OK
188
- 🤖 Starting MCP Server for Databend
189
- Open http://localhost:7777 to start chatting!
190
- INFO Starting playground on http://127.0.0.1:7777
191
- INFO: Started server process [189851]
192
- INFO: Waiting for application startup.
193
- INFO:agent:✓ Connected to Databend
194
- INFO:agent:ChatBI initialized successfully
195
- INFO: Application startup complete.
196
- INFO: Uvicorn running on http://127.0.0.1:7777 (Press CTRL+C to quit)
194
+ ✅ 环境变量检查通过
195
+ 🤖 正在启动 Databend 的 MCP 服务器
196
+ 打开 http://localhost:7777 开始聊天!
197
+ INFO 正在 http://127.0.0.1:7777 启动 playground
198
+ INFO: 已启动服务器进程 [189851]
199
+ INFO: 等待应用程序启动。
200
+ INFO:agent:✓ 已连接到 Databend
201
+ INFO:agent:ChatBI 初始化成功
202
+ INFO: 应用程序启动完成。
203
+ INFO: Uvicorn 正在 http://127.0.0.1:7777 运行(按 CTRL+C 退出)
197
204
```
198
205
199
- ### 步骤 6 :设置 Web 界面
206
+ ### 第 6 步 :设置 Web 界面
200
207
201
- 为获得更佳体验,可配置 Agno 的 Web 界面:
208
+ 为获得更好体验,可设置 Agno 的 Web 界面:
202
209
203
210
``` bash
204
- # Create the Agent UI
211
+ # 创建 Agent UI
205
212
npx create-agent-ui@latest
206
213
207
- # Enter 'y' when prompted, then run:
214
+ # 出现提示时输入 'y',然后运行:
208
215
cd agent-ui && npm run dev
209
216
```
210
217
211
218
** 连接到代理:**
212
219
1 . 打开 [ http://localhost:3000 ] ( http://localhost:3000 )
213
220
2 . 选择 "localhost:7777" 作为端点
214
- 3 . 开始用自然语言提问数据问题 !
221
+ 3 . 开始提问关于数据的问题 !
215
222
216
223
** 试试这些查询:**
217
- - "Show me all databases "
218
- - "What tables do I have? "
219
- - "Describe the structure of my tables "
220
- - "Run a query to show sample data "
224
+ - "显示所有数据库 "
225
+ - "我有哪些表? "
226
+ - "描述我的表结构 "
227
+ - "运行查询显示示例数据 "
221
228
222
229
## 资源
223
230
224
- - ** GitHub 仓库** : [ databendlabs/mcp-databend] ( https://github.com/databendlabs/mcp-databend )
225
- - ** PyPI 包** : [ mcp-databend] ( https://pypi.org/project/mcp-databend )
226
- - ** Agno 框架** : [ Agno MCP] ( https://docs.agno.com/tools/mcp/mcp )
227
- - ** Agent UI** : [ Agent UI] ( https://docs.agno.com/agent-ui/introduction )
231
+ - ** GitHub 仓库** : [ databendlabs/mcp-databend] ( https://github.com/databendlabs/mcp-databend )
232
+ - ** PyPI 包** : [ mcp-databend] ( https://pypi.org/project/mcp-databend )
233
+ - ** Agno 框架** : [ Agno MCP] ( https://docs.agno.com/tools/mcp/mcp )
234
+ - ** Agent UI** : [ Agent UI] ( https://docs.agno.com/agent-ui/introduction )
0 commit comments