Skip to main content
Glama

MSSQL MCP Server

by knight0zh

query

Execute SQL queries on Microsoft SQL Server databases by providing connection details or a connection string. Manage database interactions and retrieve results directly.

Instructions

Execute a SQL query on a MSSQL database

Input Schema

NameRequiredDescriptionDefault
connectionStringNoFull connection string (alternative to individual parameters)
databaseNoDatabase name (default: master)
encryptNoEnable encryption (default: false)
hostNoDatabase server hostname
passwordNoDatabase password
portNoDatabase server port (default: 1433)
queryYesSQL query to execute
trustServerCertificateNoTrust server certificate (default: true)
usernameNoDatabase username

Input Schema (JSON Schema)

{ "oneOf": [ { "required": [ "connectionString" ] }, { "required": [ "host", "username", "password" ] } ], "properties": { "connectionString": { "description": "Full connection string (alternative to individual parameters)", "type": "string" }, "database": { "description": "Database name (default: master)", "type": "string" }, "encrypt": { "description": "Enable encryption (default: false)", "type": "boolean" }, "host": { "description": "Database server hostname", "type": "string" }, "password": { "description": "Database password", "type": "string" }, "port": { "description": "Database server port (default: 1433)", "type": "number" }, "query": { "description": "SQL query to execute", "type": "string" }, "trustServerCertificate": { "description": "Trust server certificate (default: true)", "type": "boolean" }, "username": { "description": "Database username", "type": "string" } }, "required": [ "query" ], "type": "object" }

Implementation Reference

  • The handler function that executes the SQL query on the MSSQL database using the mssql library. It handles connection configuration, pooling, query execution, and formats the result as JSON text.
    async handleQuery(args: QueryArgs): Promise<{ content: Array<{ type: string; text: string }> }> { try { const config = this.getConnectionConfig(args); const pool = await this.getPool(config); const result = await pool.request().query(args.query); return { content: [ { type: 'text', text: JSON.stringify(result.recordset, null, 2), }, ], }; } catch (error) { const message = error instanceof Error ? error.message : String(error); throw new McpError(ErrorCode.InternalError, `Database error: ${message}`); } }
  • JSON Schema for the input parameters of the 'query' tool, specifying properties like connection details, query string, and validation rules including required fields and oneOf constraints.
    inputSchema: { type: 'object', properties: { connectionString: { type: 'string', description: 'Full connection string (alternative to individual parameters)', }, host: { type: 'string', description: 'Database server hostname', }, port: { type: 'number', description: 'Database server port (default: 1433)', }, database: { type: 'string', description: 'Database name (default: master)', }, username: { type: 'string', description: 'Database username', }, password: { type: 'string', description: 'Database password', }, query: { type: 'string', description: 'SQL query to execute', }, encrypt: { type: 'boolean', description: 'Enable encryption (default: false)', }, trustServerCertificate: { type: 'boolean', description: 'Trust server certificate (default: true)', }, }, required: ['query'], oneOf: [ { required: ['connectionString'] }, { required: ['host', 'username', 'password'] }, ], },
  • src/index.ts:174-223 (registration)
    The tool registration in the ListToolsRequest handler, defining the 'query' tool's name, description, and input schema.
    { name: 'query', description: 'Execute a SQL query on a MSSQL database', inputSchema: { type: 'object', properties: { connectionString: { type: 'string', description: 'Full connection string (alternative to individual parameters)', }, host: { type: 'string', description: 'Database server hostname', }, port: { type: 'number', description: 'Database server port (default: 1433)', }, database: { type: 'string', description: 'Database name (default: master)', }, username: { type: 'string', description: 'Database username', }, password: { type: 'string', description: 'Database password', }, query: { type: 'string', description: 'SQL query to execute', }, encrypt: { type: 'boolean', description: 'Enable encryption (default: false)', }, trustServerCertificate: { type: 'boolean', description: 'Trust server certificate (default: true)', }, }, required: ['query'], oneOf: [ { required: ['connectionString'] }, { required: ['host', 'username', 'password'] }, ], }, },
  • TypeScript interface defining the structure of arguments expected by the query tool handler.
    interface QueryArgs { connectionString?: string; host?: string; port?: number; database?: string; username?: string; password?: string; query: string; encrypt?: boolean; trustServerCertificate?: boolean; }
  • Runtime validation function to ensure query arguments conform to the QueryArgs interface before execution.
    const isValidQueryArgs = (args: unknown): args is QueryArgs => { const candidate = args as Record<string, unknown>; if (typeof candidate !== 'object' || candidate === null) { return false; } // Query is required if (typeof candidate.query !== 'string') { return false; } // Either connectionString OR (host + username + password) must be provided if (candidate.connectionString !== undefined) { if (typeof candidate.connectionString !== 'string') { return false; } } else { if (typeof candidate.host !== 'string') { return false; } if (typeof candidate.username !== 'string') { return false; } if (typeof candidate.password !== 'string') { return false; } } // Optional parameters if (candidate.port !== undefined && typeof candidate.port !== 'number') { return false; } if (candidate.database !== undefined && typeof candidate.database !== 'string') { return false; } if (candidate.encrypt !== undefined && typeof candidate.encrypt !== 'boolean') { return false; } if ( candidate.trustServerCertificate !== undefined && typeof candidate.trustServerCertificate !== 'boolean' ) { return false; } return true; };

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/knight0zh/mssql-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server