AP

amornpan/py-mcp-mssql

开发工具

本页由 AI 翻译,技术细节请以原文为准。 查看原文

27 stars 16 forks 质量 56 趋势 0

py-mcp-mssql

概览

一个用 Python 实现的模型上下文协议服务器,提供访问 Microsoft SQL Server 数据库的权限。该服务器使语言模型能够通过标准化接口检查表模式并执行 SQL 查询。 * 使用 Python 的 asyncio 异步操作 * 基于环境的配置 使用 python-dotenv * 全面的日志系统 * 通过 pyodbc 进行连接池和管理 * 错误处理与恢复 * API 端点的快速API集成 * 数据验证的路径模型 * 使用ODBC驱动处理MSSQL连接 * Python 3.x * 必修 Python 包:* pyodbc * pydantic * python-dotenv * mcp-server * SQL Server 的 ODBC 驱动 17 上方截图展示了服务器与 Claude 一起分析和可视化 SQL 数据的过程。 * src/mssql/ - 主源代码目录 * .py - 包初始化 * server.py - 主服务器实现 * tests/ - 测试文件目录 * .py - 测试包初始化 * test_mssql。

概述(AI 翻译)

一个用 Python 实现的模型上下文协议服务器,提供访问 Microsoft SQL Server 数据库的权限。该服务器使语言模型能够通过标准化接口检查表模式并执行 SQL 查询。

特色

核心功能

  • 使用 Python asyncio 的异步操作
  • 基于环境的配置,使用 python-dotenv
  • 综合记录系统
  • 通过pyodbc进行连接池管理
  • 错误处理与恢复
  • API端点的FastAPI集成
  • 用于数据验证的意向模型
  • 使用ODBC驱动处理MSSQL连接

先修条件

  • Python 3.x
  • 必备的 Python 包:
  • PYODDBC
  • 皮丹特
  • python-dotenv
  • MCP-服务器
  • SQL Server 的 ODBC 驱动 17

安装

截图

上方截图展示了服务器与 Claude 一起分析和可视化 SQL 数据的过程。

项目结构

目录结构解释

  • src/mssql/ - 主源代码目录
  • __init__.py - 包初始化
  • server.py - 主服务器实现
  • tests/ - 测试文件目录
  • __init__.py - 测试包初始化
  • 'test_mssql。

本页由 AI 翻译,技术细节请以原文为准。

README

以下为原文 README 节选,完整内容请查看原文。

Python MSSQL MCP Server

A Model Context Protocol server implementation in Python that provides access to Microsoft SQL Server databases. This server enables Language Models to inspect table schemas and execute SQL queries through a standardized interface.

Features

Core Functionality

  • Asynchronous operation using Python’s asyncio
  • Environment-based configuration using python-dotenv
  • Comprehensive logging system
  • Connection pooling and management via pyodbc
  • Error handling and recovery
  • FastAPI integration for API endpoints
  • Pydantic models for data validation
  • MSSQL connection handling with ODBC Driver

Prerequisites

  • Python 3.x
  • Required Python packages:
    • pyodbc
    • pydantic
    • python-dotenv
    • mcp-server
  • ODBC Driver 17 for SQL Server

Installation

git clone https://github.com/amornpan/py-mcp-mssql.git
cd py-mcp-mssql
pip install -r requirements.txt

Screenshots

The screenshot above demonstrates the server being used with Claude to analyze and visualize SQL data.

Project Structure

PY-MCP-MSSQL/
├── src/
│   └── mssql/
│       ├── __init__.py
│       └── server.py
├── tests/
│   ├── __init__.py
│   ├── test_mssql.py
│   └── test_packages.py
├── .env
├── .env.example
├── .gitignore
├── README.md
└── requirements.txt

Directory Structure Explanation

  • src/mssql/ - Main source code directory
    • __init__.py - Package initialization
    • server.py - Main server implementation
  • tests/ - Test files directory
    • __init__.py - Test package initialization
    • test_mssql.py - MSSQL functionality tests
    • test_packages.py - Package dependency tests
  • .env - Environment configuration file (not in git)
  • .env.example - Example environment configuration
  • .gitignore - Git ignore rules
  • README.md - Project documentation
  • requirements.txt - Project dependencies

Configuration

Create a .env file in the project root:

MSSQL_SERVER=your_server
MSSQL_DATABASE=your_database
MSSQL_USER=your_username
MSSQL_PASSWORD=your_password
MSSQL_DRIVER={ODBC Driver 17 for SQL Server}

API Implementation Details

Resource Listing

@app.list_resources()
async def list_resources() -> list[Resource]
  • Lists all available tables in the database
  • Returns table names with URIs in the format mssql:///data
  • Includes table descriptions and MIME types

Resource Reading

@app.read_resource()
在 GitHub 查看完整 README

安装

This server does not publish a one-line install command.

Open the repository installation guide 新手?先看 mcpskill.org 教程 →

配置

{ "mcpServers": { "mssql": { "command": "python", "args": [ "server.py" ], "env": { "MSSQL_SERVER": "your_server", "MSSQL_DATABASE": "your_database", "MSSQL_USER": "your_username", "MSSQL_PASSWORD": "your_password", "MSSQL_DRIVER": "{ODBC Driver 17 for SQL Server}" } } } }