Skip to content
Prev Previous commit
Next Next commit
Update README with comprehensive documentation for stored procedure a…
…nd function tools

- Document all 13 MCP tools (7 existing + 6 new)
- Add detailed testing documentation
- Include example usage for procedures and functions
- Update tool count from 7 to 13 tools
- Add troubleshooting section for new features
  • Loading branch information
shubham070 committed Aug 7, 2025
commit b88cbfbe37dbd0ea3d41b8809a010f1407425dc7
134 changes: 119 additions & 15 deletions MssqlMcp/dotnet/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Mssql SQL MCP Server (.NET 8)

This project is a .NET 8 console application implementing a Model Context Protocol (MCP) server for MSSQL Databases using the official [MCP C# SDK](https://github.com/modelcontextprotocol/csharp-sdk).
Expand All @@ -7,15 +6,70 @@ This project is a .NET 8 console application implementing a Model Context Protoc

- Provide connection string via environment variable `CONNECTION_STRING`.
- **MCP Tools Implemented**:
- ListTables: List all tables in the database.
- DescribeTable: Get schema/details for a table.
- CreateTable: Create new tables.
- DropTable: Drop existing tables.
- InsertData: Insert data into tables.
- ReadData: Read/query data from tables.
- UpdateData: Update values in tables.
- **Table Operations**:
- ListTables: List all tables in the database.
- DescribeTable: Get schema/details for a table.
- CreateTable: Create new tables.
- DropTable: Drop existing tables.
- **Data Operations**:
- InsertData: Insert data into tables.
- ReadData: Read/query data from tables.
- UpdateData: Update values in tables.
- **Stored Procedure & Function Operations**:
- CreateProcedure: Create new stored procedures.
- CreateFunction: Create new functions (scalar and table-valued).
- ExecuteStoredProcedure: Execute stored procedures with optional parameters.
- ExecuteFunction: Execute table-valued functions with optional parameters.
- ListProceduresAndFunctions: List all stored procedures and functions in the database.
- DescribeProcedureOrFunction: Get detailed metadata about specific procedures/functions.
- **Logging**: Console logging using Microsoft.Extensions.Logging.
- **Unit Tests**: xUnit-based unit tests for all major components.
- **Comprehensive Testing**:
- **Unit Tests**: Fast, database-independent tests using mocks (22+ tests)
- **Integration Tests**: End-to-end testing with real SQL Server (14+ tests)

## Testing

The project includes two types of tests following the Test Pyramid principle:

### Unit Tests (`ToolsUnitTests.cs`)
**Purpose**: Fast, isolated tests that don't require external dependencies.

- ✅ **No database required** - Run anywhere, anytime
- ✅ **Fast execution** - Complete in seconds
- ✅ **Parameter validation** - Test input validation logic
- ✅ **Business logic** - Test pure functions and data structures
- ✅ **22+ tests** covering all new stored procedure and function tools

**Run unit tests only:**
```bash
dotnet test --filter "FullyQualifiedName~ToolsUnitTests"
```

### Integration Tests (`UnitTests.cs` -> `MssqlMcpTests`)
**Purpose**: End-to-end testing with real SQL Server database.

- 🔌 **Database required** - Tests full SQL Server integration
- 📊 **Real data operations** - Creates tables, stored procedures, functions
- 🧪 **Complete workflows** - Tests actual MCP tool execution
- ⚡ **14+ tests** - Core CRUD and error handling scenarios

**Prerequisites for integration tests:**
1. SQL Server running locally
2. Database named 'test'
3. Set environment variable:
```bash
SET CONNECTION_STRING=Server=.;Database=test;Trusted_Connection=True;TrustServerCertificate=True
```

**Run integration tests only:**
```bash
dotnet test --filter "FullyQualifiedName~MssqlMcpTests"
```

**Run all tests:**
```bash
dotnet test
```

## Getting Started

Expand All @@ -25,7 +79,7 @@ This project is a .NET 8 console application implementing a Model Context Protoc

### Setup

1. **Build ***
1. **Build**

---
```sh
Expand All @@ -34,7 +88,6 @@ This project is a .NET 8 console application implementing a Model Context Protoc
```
---


2. VSCode: **Start VSCode, and add MCP Server config to VSCode Settings**

Load the settings file in VSCode (Ctrl+Shift+P > Preferences: Open Settings (JSON)).
Expand Down Expand Up @@ -120,11 +173,62 @@ Add a new MCP Server with the following settings:
```
---

Save the file, start a new Chat, you'll see the "Tools" icon, it should list 7 MSSQL MCP tools.
Save the file, start a new Chat, you'll see the "Tools" icon, it should list **13 MSSQL MCP tools** (7 original + 6 new stored procedure/function tools).

## Available MCP Tools

### Table Operations (7 tools)
1. **ListTables** - List all tables in the database
2. **DescribeTable** - Get schema/details for a table
3. **CreateTable** - Create new tables
4. **DropTable** - Drop existing tables
5. **InsertData** - Insert data into tables
6. **ReadData** - Read/query data from tables
7. **UpdateData** - Update values in tables

### Stored Procedure & Function Operations (6 tools)
8. **CreateProcedure** - Create new stored procedures with full SQL support
9. **CreateFunction** - Create new functions (scalar and table-valued)
10. **ExecuteStoredProcedure** - Execute stored procedures with optional parameters
11. **ExecuteFunction** - Execute table-valued functions with optional parameters
12. **ListProceduresAndFunctions** - List all stored procedures and functions
13. **DescribeProcedureOrFunction** - Get detailed metadata about procedures/functions

## Example Usage

### Creating and Executing a Stored Procedure
```sql
-- Create a procedure using CreateProcedure tool
CREATE PROCEDURE dbo.GetUsersByRole
@Role NVARCHAR(50)
AS
BEGIN
SELECT * FROM Users WHERE Role = @Role
END

-- Execute the procedure using ExecuteStoredProcedure tool
-- Parameters: {"@Role": "Admin"}
```

### Creating and Executing a Function
```sql
-- Create a table-valued function using CreateFunction tool
CREATE FUNCTION dbo.GetActiveUsers(@MinLoginDate DATE)
RETURNS TABLE
AS
RETURN
(
SELECT * FROM Users
WHERE LastLogin >= @MinLoginDate
AND IsActive = 1
)

-- Execute the function using ExecuteFunction tool
-- Parameters: {"MinLoginDate": "2024-01-01"}
```

# Troubleshooting

1. If you get a "Task canceled" error using "Active Directory Default", try "Active Directory Interactive".



2. For stored procedures with output parameters, include them in the parameters dictionary.
3. Function execution requires the function to be table-valued for proper result return.