NLP(一百一十八)SQLite+Agent+Docker:一站式打造智能数据库助手

本文将会介绍如何使用Docker来启动MCP Server,并结合大模型和Agent实现Sqlite数据库的本地操作。

前言

在笔者之前的文章NLP(一百一十五)MCP入门与实践中,笔者介绍了MCP入门使用与项目实战。对于MCP Server,常见的启动方式有两种:NPXDocker,笔者之前已经介绍过NPX,因此本文将会介绍如何使用Docker来启动MCP Server。

我们以SQLite MCP Server为例(其网址为:https://github.com/modelcontextprotocol/servers/tree/main/src/sqlite),来演示如何使用Docker来启动MCP Server,操作步骤如下:

  1. 电脑系统中需要安装Docker
  2. 拉取上述项目代码,创建本地Docker镜像:
1
docker build -t mcp/sqlite .
  1. 使用Docker启动MCP Server:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"mcpServers": {
"sqlite": {
"command": "docker",
"args": [
"run",
"--rm",
"-i",
"-v",
"mcp-test:/mcp",
"mcp/sqlite",
"--db-path",
"/mcp/test.db"
]
}
}

上述启动命令为官网示例,具体启动参数需要根据自身情况进行调整。

Cursor中使用SQLite MCP Server

Cursor IDE 支持MCP配置,对于上述的SQLite MCP Server,配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"mcpServers": {
"sqlite": {
"command": "docker",
"args": [
"run",
"--rm",
"-i",
"-v",
"~/mcp-test:/mcp",
"mcp/sqlite",
"--db-path",
"/mcp/test.db"
]
}
}
}

在Cursor MCP Servers中等待服务加载,绿色表示服务加载成功。在Cursor右侧Chat页面选择Agent模式进行回答。两个问题如下:

  • Create a new table for fake products. Each product should have a name and a price. Generate 10 fake products based on video games.
  • 现在产品表里面有哪些数据,给我返回前5条。

Cursor中大模型执行结果如下图:

wmremove-transformed.png

此时后台已运行响应的Docker容器,如下:

1
2
3
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
12696a2c09b6 mcp/sqlite "mcp-server-sqlite -…" 11 minutes ago Up 11 minutes nifty_haibt

查看本地Sqlite db文件(路径为~/mcp-test/test.db),里面内容如下:

mcp_docker_2.png

Openai Agents中使用SQLite MCP Server

在文章NLP(一百一十七)使用Openai Agents SDK构建MCP,笔者介绍了如何使用Openai开源的Agent框架Openai Agents来构建MCP。这里继续介绍如何使用该框架来启动SQLite MCP Server,Python代码如下:

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import base64
import logfire
import shutil
import asyncio
from agents import Agent, Runner
from agents.mcp import MCPServer, MCPServerStdio

from dotenv import load_dotenv

load_dotenv()

# Build Basic Auth header.
LANGFUSE_AUTH = base64.b64encode(
f"{os.environ.get('LANGFUSE_PUBLIC_KEY')}:{os.environ.get('LANGFUSE_SECRET_KEY')}".encode()
).decode()

# Configure OpenTelemetry endpoint & headers
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = os.environ.get("LANGFUSE_HOST") + "/api/public/otel"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {LANGFUSE_AUTH}"

# Configure logfire instrumentation.
logfire.configure(
service_name='my_agent_service',
send_to_logfire=False
)
# This method automatically patches the OpenAI Agents SDK to send logs via OTLP to Langfuse.
logfire.instrument_openai_agents()


async def run(mcp_server: MCPServer):
agent = Agent(
name="SQlite DB Assistant",
model="gpt-4o",
instructions="Use the tools to read, write, and search the sqlite db and answer user's question based on the db.",
mcp_servers=[mcp_server],
)

message = "在这个数据库中,有哪几张表格,它们的名字、列名、列类型、行数分别是多少?"
print(f"Running: {message}")
result = await Runner.run(starting_agent=agent, input=message)
print(result.final_output)


async def main():
async with MCPServerStdio(
name="SQlite Server, via docker",
params={
"command": "docker",
"args": ["run", "--rm", "-i", "-v", "/Users/admin/mcp-test:/mcp", "mcp/sqlite", "--db-path", "/mcp/test.db"],
},
) as server:
await run(server)


if __name__ == "__main__":
# Let's make sure the user has npx installed
if not shutil.which("docker"):
raise RuntimeError("Docker is not installed. Please install it.")

asyncio.run(main())

输出结果如下:

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
27
28
29
30
31
Running: 在这个数据库中,有哪几张表格,它们的名字、列名、列类型、行数分别是多少?
12:59:14.187 OpenAI Agents trace: Agent workflow
12:59:14.190 Agent run: 'SQlite DB Assistant'
12:59:14.191 OpenAI agents: mcp_tools span
12:59:14.214 Responses API with 'gpt-4o'
12:59:18.321 Function: list_tables
12:59:18.340 Responses API with 'gpt-4o'
12:59:19.538 Function: describe_table
12:59:19.558 Responses API with 'gpt-4o'
12:59:20.515 Function: read_query
12:59:20.534 Responses API with 'gpt-4o'
12:59:21.598 Function: describe_table
12:59:21.618 Responses API with 'gpt-4o'
12:59:22.818 Function: read_query
12:59:22.833 Responses API with 'gpt-4o'
数据库中有以下两张表:

1. **products**
- 列信息:
- `id`: INTEGER, 主键
- `name`: TEXT, 非空
- `price`: DECIMAL(10,2), 非空
- 行数:10

2. **sqlite_sequence**
- 列信息:
- `name`: TEXT
- `seq`: INTEGER
- 行数:1

如果需要更多信息,请告诉我。

总结

本文全面介绍了如何通过 Docker 启动并使用 SQLite MCP Server,结合 Cursor IDE 和 OpenAI Agents 实现本地数据库的智能交互。借助大模型的强大理解与推理能力,用户可以自然语言驱动数据库操作,无需编写 SQL 语句,大大提升了操作体验与效率。

通过 Cursor,用户可以快速测试和验证 Agent 的数据库交互能力;而在 OpenAI Agents SDK 的支持下,还能实现灵活可扩展的服务部署,为本地数据智能分析提供了更强大的可能性。这种结合 Docker、本地数据库与智能 Agent 的范式,为开发者提供了一种轻量、高效、实用的智能数据操作方案。

欢迎关注我的公众号NLP奇幻之旅,原创技术文章第一时间推送。

欢迎关注我的知识星球“自然语言处理奇幻之旅”,笔者正在努力构建自己的技术社区。


NLP(一百一十八)SQLite+Agent+Docker:一站式打造智能数据库助手
https://percent4.github.io/NLP(一百一十八)SQLite-Agent-Docker:一站式打造智能数据库助手/
作者
Jclian91
发布于
2025年4月27日
许可协议