Skip to main content
Glama
pavanjava

PostgreSQL Explorer MCP Server

by pavanjava

mcp-registry

关于 MLflow MCP Server Registry 的小型端到端演练:使用 FastMCP 构建 MCP 服务器,使用 FastMCP 客户端调用它们,然后将它们注册到 MLflow 中,使其可被发现——包含版本、访问端点和自动发现的工具。

该仓库包含三个部分:

部分

路径

功能

MCP 服务器

src/

两个 FastMCP 服务器——一个玩具问候服务器和一个真实的 PostgreSQL 探索器

MCP 客户端

clients/

轻量级 FastMCP 客户端,通过可流式 HTTP 调用工具

注册工具

mlflow_mcp_registry_util.py

将服务器注册到 MLflow,刷新其工具,列出访问端点


要求

  • Python ≥ 3.13

  • 本地运行的 MLflow 跟踪服务器(v3.15+)——注册 API 是服务器端功能

  • PostgreSQL(如果你想使用 PostgreSQL 服务器)

  • uv(仓库提供了 uv.lock)

依赖项(pyproject.toml):fastmcp>=3.4.7,mlflow>=3.15.1,psycopg2 + psycopg2-binary, python-dotenv。

uv sync

Related MCP server: postgres-mcp-readonly

配置

PostgreSQL 服务器从仓库根目录的 .env 文件中读取连接设置(通过 python-dotenv 加载)。.env 已被 gitignore——需要自行创建:

PGHOST=localhost
PGPORT=5432
PGUSER=root
PGPASSWORD=root
PGSSLMODE=prefer
PGADMINDB=postgres

变量

默认值

用途

PGHOST

localhost

服务器主机

PGPORT

5432

服务器端口

PGUSER

root

登录角色

PGPASSWORD

root

密码

PGSSLMODE

prefer

libpq SSL 模式

PGADMINDB

postgres

用于服务器级查询的数据库(ListDatabases)

src/simple_server.py 无需配置。


1. 启动一个 MLflow 跟踪服务器

mlflow_mcp_registry_util.py 指向 http://127.0.0.1:5000。仓库中已包含一个 mlflow.db(已被 gitignore)来自 SQLite 备份的运行:

uv run mlflow server --backend-store-uri sqlite:///mlflow.db --host 127.0.0.1 --port 5000

如果你的服务器在其他位置,请编辑 mlflow_mcp_registry_util.py 顶部的 mlflow.set_tracking_uri(...) 调用。


2. 运行一个 MCP 服务器

两个服务器都绑定 端口 8000 通过可流式 HTTP(http://localhost:8000/mcp),因此请一次只运行一个——或者更改 mcp.run(...) 调用中的端口。

简单问候服务器——两个工具:Greeting 和 Sendoff:

uv run python src/simple_server.py

PostgreSQL 探索器——对运行中的 PostgreSQL 服务器进行只读内省:

uv run python src/postgresql_mcp.py

或通过 FastMCP CLI 运行:

uv run fastmcp run src/postgresql_mcp.py --transport http --port 8000

PostgreSQL 工具

工具

参数

返回

ListDatabases

—

非模板数据库及其拥有者和格式化的大小

ListSchemas

database

用户定义的 schema(排除系统 schema 和临时 schema)

ListTables

database, schema

schema 中的表和视图,带有 table_type

ListTableColumns

database, schema, table

列名、类型、可空性、默认值——按序数顺序

ListTableRelations

database, schema, table

外键分为 outgoing(此表 → 其他表)和 incoming(其他表 → 此表)

ListSchemaRelations

database, schema

整个 schema 的扁平边列表 from_table.from_column → to_table.to_column——列表形式的 ERD

每个工具都通过 _query() 执行,它会打开一个到指定数据库的新连接,通过 RealDictCursor 运行一个参数化的 SELECT 查询,然后关闭连接。不会进行任何写入操作。


3. 从客户端调用服务器

在端口 8000 上运行服务器的情况下:

uv run python clients/postgres_server_client.py   # calls ListDatabases, prints each database name
uv run python clients/simple_server_client.py     # calls the greeting tool with "Ford"

clients/postgres_server_client.py 解包 FastMCP 结果:result.content 中的每个项都包含一个 JSON text 负载,它会解析该负载并读取 database 字段。

注意: clients/simple_server_client.py 调用 client.call_tool("greet", ...),但 src/simple_server.py 中的工具注册名称为 Greeting(greet 只是 Python 函数名)。调用时请使用 "Greeting" 以正确解析。


4. 将服务器注册到 MLflow

mlflow_mcp_registry_util.py 包含四个协程,每个演示一个注册 API:

register_postgresql_mcp_server()

注册一个远程服务器——即已经运行并可通过 HTTP 访问的服务器:

mlflow.genai.register_mcp_server(
    server_json={
        "name": "io.github.pavanjava/postgresql-server",
        "version": "0.1.0",
        "description": "PostgreSQL FastMCP server exposing DB tools",
        "remotes": [{"url": "http://localhost:8000/mcp", "type": "streamable-http"}],
    },
    status="active",
    source="local dev server via fastmcp",
    create_access_endpoints_from_remotes=True,
)

create_access_endpoints_from_remotes=True 会将 remotes 中的每个条目转换为 MLflow 访问端点,这样消费者可以从注册表中解析连接 URL,而无需硬编码。

register_qdrant_mcp_server()

注册一个打包服务器——无需运行中的进程。该条目描述了如何启动它(uvx mcp-server-qdrant 通过 stdio),并声明了其环境变量,包括哪些是必需的以及哪些是秘密的(QDRANT_API_KEY)。

discover_tools()

调用 refresh_mcp_server_version_tools(...)。MLflow 连接到已注册的服务器版本,枚举其工具,并将它们持久化到该版本上——之后 server_version.tools 会列出发现的工具名称。这要求服务器确实可达。

list_endpoints()

调用 search_mcp_access_endpoints(server_name=...) 并打印每个端点的 URL、传输类型以及它解析到的服务器版本。

运行它

__main__ 块一次运行一个协程;其余的被注释掉了。取消注释你想要的协程:

if __name__ == "__main__":
    # asyncio.run(register_postgresql_mcp_server())
    asyncio.run(register_qdrant_mcp_server())
    # asyncio.run(discover_tools())
    # asyncio.run(list_endpoints())
uv run python mlflow_mcp_registry_util.py

之后,已注册的服务器会出现在 MLflow 用户界面的 MCP 服务器 部分,位于 http://127.0.0.1:5000。


建议的端到端路径

  1. 在端口 5000 上启动 MLflow。

  2. 在端口 8000 上启动 PostgreSQL MCP 服务器。

  3. 验证它是否响应:uv run python clients/postgres_server_client.py。

  4. 注册它:取消注释 register_postgresql_mcp_server() 并运行该工具。

  5. 发现其工具:切换到 discover_tools() 并再次运行——应打印出上述六个工具。

  6. 检查其访问端点:切换到 list_endpoints()。

  7. 在 MLflow 用户界面中浏览结果。


仓库布局

.
├── src/
│   ├── simple_server.py        # FastMCP "My MCP Server" — Greeting + Sendoff tools
│   └── postgresql_mcp.py       # FastMCP "PostgreSQL Explorer" — 6 read-only introspection tools
├── clients/
│   ├── simple_server_client.py     # calls a tool on the greeting server
│   └── postgres_server_client.py   # calls ListDatabases and prints database names
├── mlflow_mcp_registry_util.py # MLflow MCP registry: register / refresh tools / list endpoints
├── pyproject.toml
└── uv.lock

mlflow.db、.env、.venv 和 .idea 已被 gitignore。

Related MCP Connectors

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    An open-source MCP server for PostgreSQL schema introspection and guarded read-only queries. It enables MCP clients to discover schemas, tables, columns, indexes, relationships, and safe queryable data from a configured PostgreSQL database.
    21 npm
    MIT
  • A
    license
    Not graded
    quality
    C
    maintenance
    A Python MCP server that enables schema discovery, read-only SQL queries, table previews, and index/relationship analysis on PostgreSQL databases.
    1
    MIT
  • A
    license
    A
    quality
    B
    maintenance
    MCP server for PostgreSQL that enables safe read-only database queries, table schema inspection, and query execution planning.
    6
    27 npm
    BSD 3-Clause