11# 适用于 Databend 的 MCP 服务器
22
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 数据库交互。
44
55## mcp-databend 能做什么
66
7- - ** execute_sql** - 执行 SQL 查询,并带有超时保护
7+ - ** execute_sql** - 执行带超时保护的 SQL 查询
88- ** show_databases** - 列出所有可用数据库
9- - ** show_tables** - 列出数据库中的表(支持可选过滤 )
9+ - ** show_tables** - 列出数据库中的表(可带可选过滤 )
1010- ** describe_table** - 获取详细的表结构信息
1111
1212## 构建 ChatBI 工具
1313
14- 本教程将演示如何使用 mcp-databend 和 Agno 框架构建一个对话式商业智能(Business Intelligence)工具。你将创建一个本地代理,能够用自然语言回答数据问题。
14+ 本教程将演示如何使用 mcp-databend 和 Agno 框架构建对话式商业智能工具。你将创建一个本地代理,用自然语言回答数据问题。
15+
16+ ![ Databend MCP ChatBI] ( @site/static/img/connect/databend-mcp-chatbi.png )
1517
1618## 分步教程
1719
18- ### 步骤 1 :配置 Databend 连接
20+ ### 第 1 步 :配置 Databend 连接
1921
2022首先,你需要一个可连接的 Databend 数据库:
2123
22241 . ** 注册 [ Databend Cloud] ( https://app.databend.cn ) ** (提供免费套餐)
23- 2 . ** 创建计算集群(Warehouse)和数据库 **
25+ 2 . ** 创建计算集群和数据库 **
24263 . ** 在控制台获取连接字符串**
2527
2628有关 DSN 格式和示例的详细信息,请参阅[ 连接字符串文档] ( https://docs.databend.cn/developer/drivers/#connection-string-dsn ) 。
3032| ** Databend Cloud** | ` databend://user:pwd@host:443/database?warehouse=wh ` |
3133| ** 自托管** | ` databend://user:pwd@localhost:8000/database?sslmode=disable ` |
3234
33- ### 步骤 2 :安装依赖
35+ ### 第 2 步 :安装依赖
3436
3537创建虚拟环境并安装所需包:
3638
3739``` bash
38- # Create virtual environment
40+ # 创建虚拟环境
3941python3 -m venv .venv
4042source .venv/bin/activate
4143
42- # Install packages
44+ # 安装包
4345pip install packaging openai agno openrouter sqlalchemy fastapi mcp-databend
4446```
4547
46- ### 步骤 3 :创建 ChatBI 代理
48+ ### 第 3 步 :创建 ChatBI 代理
4749
48- 现在创建 ChatBI 代理,它将使用 mcp-databend 与数据库交互 。
50+ 现在创建使用 mcp-databend 与数据库交互的 ChatBI 代理 。
4951
5052创建文件 ` agent.py ` :
5153
54+ <details >
55+ <summary >点击查看 agent.py 代码</summary >
56+
5257``` python
5358from contextlib import asynccontextmanager
5459import os
@@ -66,7 +71,7 @@ logging.basicConfig(level=logging.INFO)
6671logger = logging.getLogger(__name__ )
6772
6873def check_env_vars ():
69- """ Check required environment variables """
74+ """ 检查必需的环境变量 """
7075 required = {
7176 " DATABEND_DSN" : " https://docs.databend.cn/developer/drivers/#connection-string-dsn" ,
7277 " OPENROUTER_API_KEY" : " https://openrouter.ai/settings/keys"
@@ -75,13 +80,13 @@ def check_env_vars():
7580 missing = [var for var in required if not os.getenv(var)]
7681
7782 if missing:
78- print (" ❌ Missing environment variables: " )
83+ print (" ❌ 缺少环境变量: " )
7984 for var in missing:
8085 print (f " • { var} : { required[var]} " )
81- print (" \n Example: export DATABEND_DSN='...' OPENROUTER_API_KEY='...'" )
86+ print (" \n 示例: export DATABEND_DSN='...' OPENROUTER_API_KEY='...'" )
8287 sys.exit(1 )
8388
84- print (" ✅ Environment variables OK " )
89+ print (" ✅ 环境变量检查通过 " )
8590
8691check_env_vars()
8792
@@ -103,10 +108,10 @@ class DatabendTool:
103108 async def init (self ):
104109 try :
105110 await self .mcp.connect()
106- logger.info(" ✓ Connected to Databend" )
111+ logger.info(" ✓ 已连接到 Databend" )
107112 return True
108113 except Exception as e:
109- logger.error(f " ✗ Databend connection failed: { e} " )
114+ logger.error(f " ✗ Databend 连接失败: { e} " )
110115 return False
111116
112117databend = DatabendTool()
@@ -119,11 +124,11 @@ agent = Agent(
119124 ),
120125 tools = [],
121126 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+ " 在分析中提供见解和解释。 "
127132 ],
128133 storage = SqliteStorage(table_name = " chatbi" , db_file = " chatbi.db" ),
129134 add_datetime_to_instructions = True ,
@@ -137,11 +142,11 @@ agent = Agent(
137142async def lifespan (app : FastAPI):
138143 tool = databend.create()
139144 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 连接失败 " )
142147
143148 agent.tools.append(tool)
144- logger.info(" ChatBI initialized successfully " )
149+ logger.info(" ChatBI 初始化成功 " )
145150
146151 yield
147152
@@ -151,77 +156,79 @@ async def lifespan(app: FastAPI):
151156playground = Playground(
152157 agents = [agent],
153158 name = " ChatBI with Databend" ,
154- description = " Business Intelligence Assistant powered by Databend"
159+ description = " 由 Databend 驱动的商业智能助手 "
155160)
156161
157162app = playground.get_app(lifespan = lifespan)
158163
159164if __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 开始聊天! " )
162167 playground.serve(app = " agent:app" , host = " 127.0.0.1" , port = 7777 )
163168```
164169
165- ### 步骤 4:配置环境
170+ </details >
171+
172+ ### 第 4 步:配置环境
166173
167174设置 API 密钥和数据库连接:
168175
169176``` bash
170- # Set your OpenRouter API key
177+ # 设置 OpenRouter API 密钥
171178export OPENROUTER_API_KEY=" your-openrouter-key"
172179
173- # Set your Databend connection string
180+ # 设置 Databend 连接字符串
174181export DATABEND_DSN=" your-databend-connection-string"
175182```
176183
177- ### 步骤 5 :启动 ChatBI 代理
184+ ### 第 5 步 :启动 ChatBI 代理
178185
179186运行代理以启动本地服务器:
180187
181188``` bash
182189python agent.py
183190```
184191
185- 你应该会看到 :
192+ 你将看到 :
186193```
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 退出)
197204```
198205
199- ### 步骤 6 :设置 Web 界面
206+ ### 第 6 步 :设置 Web 界面
200207
201- 为获得更佳体验,可配置 Agno 的 Web 界面:
208+ 为获得更好体验,可设置 Agno 的 Web 界面:
202209
203210``` bash
204- # Create the Agent UI
211+ # 创建 Agent UI
205212npx create-agent-ui@latest
206213
207- # Enter 'y' when prompted, then run:
214+ # 出现提示时输入 'y',然后运行:
208215cd agent-ui && npm run dev
209216```
210217
211218** 连接到代理:**
2122191 . 打开 [ http://localhost:3000 ] ( http://localhost:3000 )
2132202 . 选择 "localhost:7777" 作为端点
214- 3 . 开始用自然语言提问数据问题 !
221+ 3 . 开始提问关于数据的问题 !
215222
216223** 试试这些查询:**
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+ - "运行查询显示示例数据 "
221228
222229## 资源
223230
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