Skip to content

Added command to configuration so an Agent can differentiate between multiple mssql mcp servers. Added Port configuration for non-standard sql ports. #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,27 @@ The package will automatically install required system dependencies (like FreeTD
pip install mssql-mcp-server
```

## Local Builds

```bash
pip install hatch
hatch build
pip install dist/mssql_mcp_server-0.1.0.tar.gz
```

## Configuration

Set the following environment variables:

```bash
# Required
MSSQL_SERVER=localhost
MSSQL_USER=your_username
MSSQL_PASSWORD=your_password
MSSQL_DATABASE=your_database
# Optional
MSSQL_COMMAND=execute_sql
MSSQL_PORT=your_database
```

## Usage
Expand All @@ -52,10 +64,15 @@ Add this to your `claude_desktop_config.json`:
"mssql_mcp_server"
],
"env": {
// Required
"MSSQL_SERVER": "localhost",
"MSSQL_USER": "your_username",
"MSSQL_PASSWORD": "your_password",
"MSSQL_DATABASE": "your_database"
"MSSQL_DATABASE": "your_database",

// Optional
"MSSQL_COMMAND": "execute_sql", // search_staging
"MSSQL_PORT": "1433"
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/mssql_mcp_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@ def get_db_config():
"server": os.getenv("MSSQL_SERVER", "localhost"),
"user": os.getenv("MSSQL_USER"),
"password": os.getenv("MSSQL_PASSWORD"),
"database": os.getenv("MSSQL_DATABASE")
"database": os.getenv("MSSQL_DATABASE"),
"port": os.getenv("MSSQL_PORT", "1433"), # Default MSSQL port
}

if not all([config["user"], config["password"], config["database"]]):
logger.error("Missing required database configuration. Please check environment variables:")
logger.error("MSSQL_USER, MSSQL_PASSWORD, and MSSQL_DATABASE are required")
raise ValueError("Missing required database configuration")

return config

def get_command():
"""Get the command to execute SQL queries."""
return os.getenv("MSSQL_COMMAND", "execute_sql")

# Initialize server
app = Server("mssql_mcp_server")

Expand Down Expand Up @@ -97,10 +102,11 @@ async def read_resource(uri: AnyUrl) -> str:
@app.list_tools()
async def list_tools() -> list[Tool]:
"""List available SQL Server tools."""
command = get_command()
logger.info("Listing tools...")
return [
Tool(
name="execute_sql",
name=command,
description="Execute an SQL query on the SQL Server",
inputSchema={
"type": "object",
Expand All @@ -119,9 +125,10 @@ async def list_tools() -> list[Tool]:
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
"""Execute SQL commands."""
config = get_db_config()
command = get_command()
logger.info(f"Calling tool: {name} with arguments: {arguments}")

if name != "execute_sql":
if name != command:
raise ValueError(f"Unknown tool: {name}")

query = arguments.get("query")
Expand Down Expand Up @@ -169,7 +176,8 @@ async def main():

logger.info("Starting MSSQL MCP server...")
config = get_db_config()
logger.info(f"Database config: {config['server']}/{config['database']} as {config['user']}")
command = get_command()
logger.info(f"Database config: {config['server']}/{config['database']}:{config['port']} as {config['user']} with command {command}")

async with stdio_server() as (read_stream, write_stream):
try:
Expand Down