MCP SQL Server
Implements database operations using the pyodbc Python library to provide SQL Server interaction capabilities.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@MCP SQL Serverlist all tables in the database"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
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.
Related MCP server: mssql-mcp
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
Install Python dependencies:
pip install pyodbc asyncio fastmcpEnsure you have Microsoft SQL Server installed and the ODBC Driver 17 for SQL Server.
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 nameUsage
Run the server:
python mcp_sql_server.pyThe 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) -> strIf 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() -> strReturns a list of table names as a string
describe_table
Get the structure of a specific table.
describe_table(table_name: str) -> strtable_name: Name of the table to describeReturns column information including names and data types
execute_nonquery
Execute INSERT, UPDATE, DELETE or other non-query SQL statements.
execute_nonquery(sql: str) -> strsql: The SQL statement to executeReturns 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() -> strReturns a comma-separated list of installed ODBC drivers
database_info
Get general information about the connected database.
database_info() -> strReturns server name, database name, SQL Server version, current server time, and table count
Architecture
The server uses an asynchronous architecture to avoid blocking operations:
Lifecycle Management: The
app_lifespancontext manager handles database connection setup and teardown.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.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:
Connection errors: Verify the SQL Server instance name and ensure it's running
ODBC driver errors: Confirm ODBC Driver 17 for SQL Server is installed
Permission errors: Check that the Windows user running the application has appropriate SQL Server permissions
License
[Your License Information]
Contact
[Your Contact Information]
This server cannot be deployed
Maintenance
Related MCP Connectors
- mcpOAuthcom.gibsonai
GibsonAI MCP server: manage your databases with natural language
Official Microsoft MCP Server to query Microsoft Entra data using natural language
Draxlr's remote MCP server connects AI assistants to your SQL databases and dashboards. Explore schemas, run read-only queries, manage saved queries and dashboards, and export results, all with row-level security so each user sees only their own data.
The Needle MCP server enables semantic search on documents stored in files like PDFs, DOCX, and XLSX by connecting AI applications to external data sources. It provides capabilities to create and manage document collections, perform natural language searches on stored content, and retrieve relevant information without requiring exact keyword matches.
Related MCP Servers
- AlicenseNot gradedqualityCmaintenanceA FastMCP-based server that enables executing Microsoft SQL Server data queries and table structure queries through a standardized MCP protocol interface.7MIT
- AlicenseNot gradedqualityDmaintenanceAn MCP server that enables interaction with Microsoft SQL Server instances using Windows or SQL Server authentication via native ODBC drivers. It allows users to execute SQL queries, list tables, and inspect schemas across multiple configured database environments through natural language.3,162 npmMIT
- AlicenseNot gradedqualityDmaintenanceA comprehensive MCP server for SQL Server database operations, enabling CRUD operations, schema exploration, and stored procedure execution through natural language.7 npmMIT
- AlicenseNot gradedqualityBmaintenanceAn MCP server that enables AI assistants to query and manage Microsoft SQL Server databases using natural language.184 npm5MIT