Skip to content

gazisdomain/sql-server-mcp-server

Repository files navigation

SQL Server MCP Server

A Model Context Protocol (MCP) server that provides AI assistants like Claude with secure, structured access to SQL Server databases through natural language queries.

🎯 What is MCP?

Model Context Protocol (MCP) is an open protocol that standardizes how AI applications connect to data sources. Instead of building custom integrations for each AI assistant, you build one MCP server that works with any MCP-compatible client.

Key Benefits:

  • Natural Language Interface: Query databases using plain English instead of writing SQL
  • AI Orchestration: AI can call multiple tools and synthesize results intelligently
  • Standardized Protocol: One server works with Claude, future AI tools, and custom applications
  • Controlled Access: Expose only specific, pre-defined operations rather than raw database access

🏗️ Architecture

┌─────────────────┐
│  Claude Desktop │ (MCP Client)
└────────┬────────┘
         │ JSON-RPC over stdio
         │
┌────────▼────────┐
│   MCP Server    │ (This Project)
│   (C# .NET 9)   │
└────────┬────────┘
         │ Entity Framework Core
         │
┌────────▼────────────┐
│   SQL Server        │
│ (AdventureWorks2025)│
└─────────────────────┘

🛠️ Technologies

  • .NET 9: Modern C# runtime
  • Entity Framework Core: Database ORM with code-first approach
  • Model Context Protocol: JSON-RPC 2.0 over stdio transport
  • SQL Server Express: Local database engine
  • Dependency Injection: Clean architecture with interface-based services

✨ Features

Available Tools

  1. get_customers: Retrieve top N customers from the database
  2. search_products: Search products by name, price range, or category
  3. get_top_selling_products: Get best-selling products by quantity sold

Example Queries

User: "What are our top 5 best-selling products?"
Claude: [Calls get_top_selling_products tool]
Result: Detailed product list with sales metrics

User: "Show me bikes under $1000"
Claude: [Calls search_products with filters]
Result: Filtered product list

User: "What's the average price of our top 10 sellers?"
Claude: [Calls get_top_selling_products, then analyzes]
Result: Calculated insights with context

🚀 Setup Instructions

Prerequisites

  • .NET 9 SDK
  • SQL Server Express (or any SQL Server instance)
  • Claude Desktop app
  • AdventureWorks2025 sample database

1. Install AdventureWorks Database

Download the backup file from Microsoft SQL Server Samples and restore it:

RESTORE DATABASE AdventureWorks2025
FROM DISK = 'path\to\AdventureWorksLT2025.bak'

2. Clone and Configure

git clone <your-repo-url>
cd SqlServerMcpServer

Copy the example configuration and update with your connection string:

copy appsettings.example.json appsettings.json

Edit appsettings.json:

{
  "ConnectionStrings": {
    "AdventureWorks": "Server=YOUR_SERVER\\SQLEXPRESS;Database=AdventureWorks2025;Trusted_Connection=True;TrustServerCertificate=True;"
  }
}

3. Build and Publish

dotnet restore
dotnet build
dotnet publish -c Release -o publish

4. Configure Claude Desktop

Edit Claude Desktop's configuration file:

  • Windows: %APPDATA%\Claude\claude_desktop_config.json

Add your MCP server:

{
  "mcpServers": {
    "sql-server": {
      "command": "dotnet",
      "args": ["<full-path-to-project>\\publish\\SqlServerMcpServer.dll"],
      "cwd": "<full-path-to-project>\\publish"
    }
  }
}

5. Restart Claude Desktop

Close and reopen Claude Desktop. You should see a hammer icon (🔨) in the input box indicating MCP tools are available.

🔒 Security Considerations

⚠️ Important: This is a demonstration project using public sample data.

Current Implementation

  • Database queries execute with full user credentials
  • Query results are sent to Anthropic's cloud for AI processing
  • NOT suitable for production use with sensitive data

Enterprise Deployment Recommendations

For production use with real data, implement:

  1. Read-Only Database Access

    • Create SQL user with SELECT-only permissions
    • Restrict access to specific tables/views
    • Use row-level security if available
  2. Data Masking/Anonymization

    • Filter PII before returning results
    • Hash sensitive identifiers
    • Implement field-level encryption for sensitive data
  3. Self-Hosted AI Models

    • Deploy open-source LLMs (Llama, Mistral) on-premise
    • Build custom MCP client that doesn't send data to external APIs
    • Keep all data processing within your network
  4. Audit Logging

    • Log all MCP tool invocations
    • Track which users accessed what data
    • Implement compliance reporting
  5. Rate Limiting & Throttling

    • Prevent excessive database queries
    • Implement query complexity limits
    • Add authentication/authorization layers

Data Privacy

When using Claude Desktop with this MCP server:

  • Your queries and database results are sent to Anthropic's API for processing
  • Anthropic's data usage policies apply
  • Only use with non-sensitive, non-PII data
  • For sensitive data, consider enterprise contracts with DPA agreements

🧪 Testing

Manual Testing with MCP Inspector

npm install -g @modelcontextprotocol/inspector
cd SqlServerMcpServer\publish
mcp-inspector dotnet SqlServerMcpServer.dll

This opens a web UI where you can test individual tools before connecting to Claude.

Command Line Testing

Run the server directly and send JSON-RPC requests:

dotnet run

Then input:

{"jsonrpc":"2.0","method":"tools/list","params":{},"id":1}

📚 Learning Resources

🤝 Contributing

This is a learning project demonstrating MCP concepts. Feel free to:

  • Fork and experiment
  • Add new tools (sales analytics, customer insights, etc.)
  • Improve error handling
  • Add unit tests

📝 License

MIT License - feel free to use this as a learning resource or starting point for your own MCP servers.

👤 Author

Built as a learning project to explore AI integration patterns and the Model Context Protocol.


Note: This project demonstrates MCP concepts using Microsoft's AdventureWorks sample database. Always review security implications before adapting for production use with real data.

About

Model Context Protocol (MCP) server for SQL Server database integration with AI assistants

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages