Skip to main content

MCP server for order center database access

Project description

OPC-MCP

订单中心数据库 MCP 服务,通过 MCP 协议暴露数据库查询能力,供 AI 助手动态查询和排查订单问题。

功能

  • 配置驱动工具 — 在 config.yaml 中定义 SQL 模板,自动生成 MCP 工具
  • run_query — 动态执行任意 SELECT 查询,支持命名参数、LIKE 自动检测、分页
  • query_table_desc — 查询表结构信息(列名、数据类型、注释),表名大小写不敏感
  • 多连接池 — 支持多个数据库(opc、smc 等),连接池名称大小写不敏感
  • 安全防护 — 仅允许 SELECT 语句,拒绝多语句注入,参数化查询

安装

pip install -e .

配置

编辑 config.yaml

server:
  host: "0.0.0.0"
  port: 8000

pools:
  opc:
    host: 10.0.0.1
    port: 1521
    user: readonly
    password: xxx
    database: opc_db
    pool_size: 5
  smc:
    host: 10.0.0.2
    port: 1521
    user: readonly
    password: xxx
    database: smc_db
    pool_size: 3

tools:
  query_sub_order:
    description: "查询子订单信息"
    pool: opc
    sql: "SELECT * FROM or_sub_order WHERE 1=1 AND order_id = :order_id AND status = :status"
    params:
      order_id:
        type: str
        description: "订单号"
      status:
        type: str
        description: "订单状态"
    limit:
      default: 50
      max: 100
    offset:
      default: 0

启动

# Streamable HTTP 模式(默认)
uv run opc-mcp --config config.yaml

# SSE 模式
uv run opc-mcp --config config.yaml --mode sse

# stdio 模式(本地进程通信)
uv run opc-mcp --config config.yaml --mode stdio

# 指定日志级别
uv run opc-mcp --config config.yaml --log-level DEBUG

连接地址

模式 连接方式
Streamable HTTP(默认) http://localhost:8000/mcp
SSE http://localhost:8000/sse
stdio 通过 stdin/stdout 通信

MCP 客户端配置

Streamable HTTP 模式:

{
  "mcpServers": {
    "opc-mcp": {
      "url": "http://localhost:8000/mcp"
    }
  }
}

SSE 模式:

{
  "mcpServers": {
    "opc-mcp": {
      "url": "http://localhost:8000/sse"
    }
  }
}

stdio 模式(通过 cwd 指定项目目录):

  • uv启动方式
{
  "mcpServers": {
    "opc-mcp-stdio": {
      "command": "uv",
      "args": [
        "run",
        "opc-mcp",
        "--config",
        "config.yaml",
        "--mode",
        "stdio"
      ],
      "cwd": "D:/code/python/opc-mcp"
    }
  }
}
  • uvx启动方式
{
	"mcpServers": {
	  "opc-mcp-stdio": {
		"command": "uvx",
		"args": [
		  "--from", "D:/code/python/opc-mcp",
		  "opc-mcp",
		  "--config", "D:/code/python/opc-mcp/config.yaml",
		  "--mode", "stdio"
		]
	  }
	}
}

MCP 工具

run_query

动态执行 SELECT 查询,支持命名参数和分页。

参数:

参数 类型 必填 说明
sql string SELECT SQL,使用 :param_name 占位符
params object 命名参数,key 大小写不敏感
pool string 连接池名称,默认 opc
limit integer 返回行数限制,默认 50,最大 200
offset integer 分页偏移量,默认 0

示例:

{
  "sql": "SELECT * FROM or_dict_def WHERE dict_type = :dict_type AND dict_class = :dict_class",
  "params": {"dict_type": 18, "dict_class": 1},
  "pool": "opc"
}

支持 JOIN、GROUP BY、子查询、UNION、聚合等复杂查询。LIKE 条件自动检测并包裹 %

query_table_desc

查询表的列结构信息。

参数:

参数 类型 必填 说明
table_name string 表名,大小写不敏感
pool string 连接池名称

示例:

{
  "table_name": "or_dict_def",
  "pool": "opc"
}

返回:

{
  "table_name": "OR_DICT_DEF",
  "count": 5,
  "columns": [
    {"table_name": "OR_DICT_DEF", "column_name": "DICT_TYPE", "data_type": "NUMBER", "comments": "字典类型"},
    {"table_name": "OR_DICT_DEF", "column_name": "DICT_NAME", "data_type": "VARCHAR2", "comments": "字典名称"}
  ]
}

测试

pytest tests/ -v

项目结构

src/opc_mcp/
  main.py           # 入口,服务创建与启动
  config.py          # 配置模型(AppConfig, ToolConfig, PoolConfig)
  pool_manager.py    # 连接池管理
  handlers.py        # 工具执行逻辑
  register.py        # MCP 工具注册
  tools.py           # SQL 校验、参数绑定等工具函数

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opc_mcp-0.1.1.tar.gz (70.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opc_mcp-0.1.1-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file opc_mcp-0.1.1.tar.gz.

File metadata

  • Download URL: opc_mcp-0.1.1.tar.gz
  • Upload date:
  • Size: 70.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for opc_mcp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ece756c044d8ab62492b2b740e3f8fa702f1e2bee2b41347f02f3b2848ffd91a
MD5 962b4b9885a168ec111e82c2f9026853
BLAKE2b-256 f87fe1860472dfdbf182441ea7d8001aec6f458d6c6b00ebc10ba16c6cd93c1a

See more details on using hashes here.

File details

Details for the file opc_mcp-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: opc_mcp-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for opc_mcp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c1d0f1267960d2bd69c10ae4d37dc1ac241902ee0795c19b96d4130948b482e8
MD5 0fe30bbe5fae4053c485385660915f7c
BLAKE2b-256 0dc4ebada34245ccc7b26b60c6522e81a8fa587d81c2643aff27af18ee6cc9f9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page