Skip to main content
Glama
leomc06

mcp-teste-llm2

by leomc06

MCP + Ollama — 对 Postgres 数据库的自然语言查询

只读 MCP(Model Context Protocol)服务器 + 本地代理后端,使用本地 LLM(Ollama)决定调用哪个工具,将葡萄牙语提问转换为安全的 SQL 查询。示例数据是虚构的:服务订单(OS)和客户。

工作原理

Usuário (interface web)
  ↓ pergunta em português
Backend agente (agent/server.js)
  ↓ roteamento determinístico (regex) OU decisão do LLM
Ollama (function calling, modelo qwen2.5:3b)
  ↓ escolhe uma tool + argumentos
Cliente MCP (agent/mcp-client.js)
  ↓ JSON-RPC via STDIO
Servidor MCP (src/server.js)
  ↓ SQL parametrizado, usuário somente-leitura
PostgreSQL

值得强调的安全要点:

  • MCP 服务器只执行参数化的 SELECT 查询;Postgres 使用专用用户(mcp_reader)连接,该用户仅有 GRANT SELECT 权限,在只读事务(default_transaction_read_only)中运行,并设有较短的 statement_timeout

  • 在调用 LLM 之前,后端就会拦截请求写入的问题(agent/write-policy.js)——不依赖模型行为良好。

  • 只要问题匹配已知模式(agent/os-routing.jsagent/client-routing.js),工具和参数就按规则选择,而不是由 LLM 决定——这样更可预测、成本更低。只有当问题存在歧义时,LLM 才会自由判断。

Related MCP server: kond-royalties-agent

要求

  • Node.js 20+

  • npm

  • Docker 和 Docker Compose

  • 本地安装 Ollama,并具备模型 qwen2.5:3b

从零开始运行的步骤

1. 克隆并安装依赖

git clone https://github.com/leomc06/mcp-teste-llm2.git
cd mcp-teste-llm2
npm install

2. 配置环境变量

cp .env.example .env

编辑 .env,将标记为 troque 的值(Postgres 数据库的用户、密码和名称,以及只读用户 mcp_reader 的密码)替换掉。.env 永远不会被纳入版本控制——请确认它没有出现在 git status 中。

3. 启动 PostgreSQL

docker compose up -d

这会创建容器 mcp-teste-llm,并在卷首次挂载时自动运行 db/init.sh 脚本——该脚本创建表、示例数据和 mcp_reader 用户。迁移文件位于 db/migrations/,并通过 init.sh 内的 \ir 按顺序(001002003、...)应用。

如果容器来自之前的运行,并且你添加了新的 migration,init.sh 不会自动再次运行(卷中已有数据)。请手动应用 migration:

docker compose exec -T postgres psql -U <POSTGRES_USER> -d <POSTGRES_DB> -f - < db/migrations/00X_nome.sql

4. 准备 Ollama

sudo systemctl start ollama
ollama pull qwen2.5:3b

5. 运行测试(可选,但推荐)

npm test

对所有文件运行 node --check,并运行 node --test 测试套件(仅涉及路由和格式化逻辑,不需要 Postgres 或 Ollama 处于运行状态)。

6. 启动代理后端

npm run start:agent

后端会自动启动 MCP 服务器(通过 STDIO)、连接 Postgres,并提供 Web 界面。访问:

http://127.0.0.1:3100

可以提出如下问题:

  • “哪些 OS 已逾期?”

  • “列出负责人 Carlos 的 OS。”

  • “客户 Bruno Santos 已完成多少张 OS?”

  • “哪些客户处于非活跃状态?”

7. 结束运行

在后端终端中按 Ctrl+C(这也会结束子 MCP 服务器)。

sudo systemctl stop ollama
docker compose stop   # para o Postgres sem apagar dados/volumes

项目结构

src/server.js            servidor MCP: define as tools e faz as queries SQL
agent/server.js          backend HTTP: recebe a pergunta, orquestra tudo
agent/os-routing.js      roteamento por regex das perguntas sobre OS
agent/client-routing.js  roteamento por regex das perguntas sobre clientes
agent/tool-selector.js   junta as duas rotas e decide quais tools expor ao LLM
agent/agent-loop.js      loop de function calling com o Ollama
agent/mcp-client.js      cliente MCP + allowlist de tools permitidas
agent/write-policy.js    bloqueio de perguntas que pedem escrita
agent/response-formatter.js  formata o resultado das tools em texto
db/init.sh               script de inicialização do Postgres (roles, grants)
db/migrations/           migrations SQL, aplicadas em ordem
web/                      interface web estática
test/                     testes (node --test)
integration-agent.mjs     teste de integração ponta a ponta (precisa da stack de pé)

运行集成测试

在另一个终端中,后端(npm run start:agent)和 Ollama 均已就绪后:

npm run test:integration

可用工具

MCP 服务器公开关于服务订单(OS)的查询工具(按编号查找、列出未关闭/逾期/最近的订单、按状态、优先级、负责人、请求人或客户筛选、历史记录、摘要和平均解决时间),以及关于客户的工具(列出、列出非活跃/最近客户、按 id/电子邮件/名称查找、电子邮件域名、摘要)。开放给代理的完整且最新的工具列表位于 agent/mcp-client.js 开头的 allowedToolNames 中。

F
license - not found
Not graded
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    A read-only MCP server for PostgreSQL that enables safe database introspection and querying via natural language.
    539
    MIT
  • F
    license
    Not graded
    quality
    B
    maintenance
    MCP server for querying artist royalty performance in Brazilian Portuguese, using natural language, charts, and PDF reports from a Postgres database.
  • F
    license
    Not graded
    quality
    C
    maintenance
    Read-only MCP server for querying Brazilian CNES health establishment data in PostgreSQL, enabling AI-assisted database exploration and analysis.
  • F
    license
    Not graded
    quality
    C
    maintenance
    PostgreSQL MCP server that converts natural language to SQL and executes queries, with multi-database support and robust read-only safety checks.

View all related MCP servers

Related MCP Connectors

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/leomc06/mcp-teste-llm2'

If you have feedback or need assistance with the MCP directory API, please join our Discord server