Sponsored by Deepsite.site

MCP SQL Server

Created By
dennismartis9 months ago
Content

MCP SQL Server

A FastMCP server that provides SQL database interaction tools via a conversational AI interface.

Overview

This project creates a server that exposes MS SQL Server operations through a conversational AI interface. It uses the FastMCP framework to provide tools for querying and manipulating SQL data, allowing users to interact with databases using natural language.

Features

  • Execute SQL queries and view results
  • List available tables in the database
  • Describe table structure with column information
  • Execute non-query operations (INSERT, UPDATE, DELETE)
  • List available ODBC drivers on the system
  • View database information and server details

Requirements

  • Python 3.7+
  • pyodbc
  • asyncio
  • FastMCP framework
  • Microsoft SQL Server
  • ODBC Driver 17 for SQL Server

Installation

  1. Install Python dependencies:
pip install pyodbc asyncio fastmcp
  1. Ensure you have Microsoft SQL Server installed and the ODBC Driver 17 for SQL Server.

  2. Configure the connection settings in the script:

# Connection parameters
SERVER = "server\\instance"  # Change to your SQL Server instance
DATABASE = "db_name"              # Change to your database name

Usage

Run the server:

python mcp_sql_server.py

The server will initialize and establish a connection to the specified SQL Server database.

Available Tools

query_sql

Execute a SQL query and return the results.

query_sql(query: str = None) -> str
  • If no query is provided, it defaults to SELECT * FROM [dbo].[Table_1]
  • Returns query results as a formatted string

list_tables

List all tables available in the database.

list_tables() -> str
  • Returns a list of table names as a string

describe_table

Get the structure of a specific table.

describe_table(table_name: str) -> str
  • table_name: Name of the table to describe
  • Returns column information including names and data types

execute_nonquery

Execute INSERT, UPDATE, DELETE or other non-query SQL statements.

execute_nonquery(sql: str) -> str
  • sql: The SQL statement to execute
  • Returns operation results, including number of affected rows
  • Automatically handles transactions (commit/rollback)

list_odbc_drivers

List all available ODBC drivers on the system.

list_odbc_drivers() -> str
  • Returns a comma-separated list of installed ODBC drivers

database_info

Get general information about the connected database.

database_info() -> str
  • Returns server name, database name, SQL Server version, current server time, and table count

Architecture

The server uses an asynchronous architecture to avoid blocking operations:

  1. Lifecycle Management: The app_lifespan context manager handles database connection setup and teardown.

  2. Non-blocking Operations: Database operations run in a separate thread using asyncio.get_event_loop().run_in_executor() to prevent blocking the main event loop.

  3. Error Handling: All operations include comprehensive error handling with useful error messages.

Error Handling

The server handles various error conditions:

  • Database connection failures
  • SQL query syntax errors
  • Table not found errors
  • Permission-related issues

All errors are logged and appropriate error messages are returned to the client.

Customization

To add new database tools or modify existing ones, follow the pattern used in the existing tools:

@mcp.tool()
async def your_new_tool(ctx: Context, param1: str) -> str:
    """Documentation for your tool"""
    try:
        conn = ctx.request_context.lifespan_context["conn"]
        
        if conn is None:
            return "Database connection is not available."
            
        def your_db_operation():
            # Your database operations here
            pass
            
        loop = asyncio.get_event_loop()
        result = await loop.run_in_executor(None, your_db_operation)
        
        # Process and return results
        return "Your result"
    except Exception as e:
        return f"Error: {str(e)}"

Security Considerations

  • The server uses Windows Authentication ("Trusted_Connection=yes")
  • Consider implementing input validation for SQL queries to prevent SQL injection
  • Restrict database user permissions based on the principle of least privilege

Troubleshooting

Common issues:

  1. Connection errors: Verify the SQL Server instance name and ensure it's running
  2. ODBC driver errors: Confirm ODBC Driver 17 for SQL Server is installed
  3. Permission errors: Check that the Windows user running the application has appropriate SQL Server permissions

License

[Your License Information]

Contact

[Your Contact Information]

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Serper MCP ServerA Serper MCP Server
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Tavily Mcp
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Playwright McpPlaywright MCP server
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
WindsurfThe new purpose-built IDE to harness magic
ChatWiseThe second fastest AI chatbot™
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
DeepChatYour AI Partner on Desktop
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Amap Maps高德地图官方 MCP Server
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
CursorThe AI Code Editor