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
| Name | Required | Description | Default |
|---|---|---|---|
| connectionString | No | Full connection string (alternative to individual parameters) | |
| database | No | Database name (default: master) | |
| encrypt | No | Enable encryption (default: false) | |
| host | No | Database server hostname | |
| password | No | Database password | |
| port | No | Database server port (default: 1433) | |
| query | Yes | SQL query to execute | |
| trustServerCertificate | No | Trust server certificate (default: true) | |
| username | No | Database 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
- src/index.ts:150-168 (handler)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}`); } }
- src/index.ts:177-222 (schema)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'] }, ], }, },
- src/index.ts:13-23 (schema)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; }
- src/index.ts:25-72 (helper)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; };