Skip to main content
Glama
by michaelyuwh
test.cjsโ€ข8.87 kB
#!/usr/bin/env node /** * Enhanced test script for the MCP MSSQL Server * Tests all new security, monitoring, and bulk operation features */ const { spawn } = require('child_process'); // Test configuration const TEST_CONFIG = { server: 'localhost', port: 1433, user: 'sa', password: 'YourStrongP@ssw0rd', database: 'MCPTest', trustServerCertificate: true, requestTimeout: 30000, connectionTimeout: 30000 }; class EnhancedMCPTester { constructor() { this.mcpProcess = null; this.messageId = 1; } async startMCPServer() { console.log('๐Ÿš€ Starting enhanced MCP server...'); this.mcpProcess = spawn('node', ['dist/index.js'], { stdio: ['pipe', 'pipe', 'pipe'], cwd: process.cwd() }); return new Promise((resolve, reject) => { let initBuffer = ''; const onData = (data) => { initBuffer += data.toString(); const lines = initBuffer.split('\n'); for (const line of lines) { if (line.trim()) { try { const message = JSON.parse(line); if (message.result && message.result.capabilities) { this.mcpProcess.stdout.off('data', onData); console.log('โœ… Enhanced MCP server started successfully'); resolve(); return; } } catch (e) { // Ignore parsing errors during initialization } } } }; this.mcpProcess.stdout.on('data', onData); this.mcpProcess.stderr.on('data', (data) => { const stderr = data.toString(); if (stderr.includes('MSSQL MCP Server running')) { // Server is ready } else { console.error('Server stderr:', stderr); } }); this.mcpProcess.on('error', reject); this.mcpProcess.on('exit', (code) => { if (code !== 0) { reject(new Error(`MCP process exited with code ${code}`)); } }); // Initialize the server this.sendMessage({ jsonrpc: '2.0', id: this.messageId++, method: 'initialize', params: { protocolVersion: '2024-11-05', capabilities: {}, clientInfo: { name: 'enhanced-test-client', version: '2.0.0' } } }); }); } sendMessage(message) { if (this.mcpProcess) { this.mcpProcess.stdin.write(JSON.stringify(message) + '\n'); } } async callTool(name, arguments_) { return new Promise((resolve, reject) => { const id = this.messageId++; const onData = (data) => { const lines = data.toString().split('\n'); for (const line of lines) { if (line.trim()) { try { const response = JSON.parse(line); if (response.id === id) { this.mcpProcess.stdout.off('data', onData); if (response.error) { reject(new Error(response.error.message)); } else { resolve(response.result); } return; } } catch (e) { // Ignore parsing errors } } } }; this.mcpProcess.stdout.on('data', onData); setTimeout(() => { this.mcpProcess.stdout.off('data', onData); reject(new Error('Timeout waiting for response')); }, 15000); this.sendMessage({ jsonrpc: '2.0', id: id, method: 'tools/call', params: { name: name, arguments: arguments_ } }); }); } async runEnhancedTests() { try { await this.startMCPServer(); console.log('\n๐Ÿงช Running Enhanced MCP Tests...\n'); // Test 1: Health Check console.log('๐Ÿฅ Testing health check...'); try { const health = await this.callTool('mssql_health_check', { ...TEST_CONFIG, includeMetrics: true }); const healthData = JSON.parse(health.content[0].text); console.log(' โœ… Server status:', healthData.status); console.log(' ๐Ÿ“Š Response time:', healthData.responseTime, 'ms'); console.log(' ๐Ÿ“ˆ Metrics available:', !!healthData.metrics); } catch (error) { console.log(' โŒ Health check failed:', error.message); } // Test 2: Query Validation console.log('\n๐Ÿ”’ Testing query validation...'); // Test valid query try { const validation1 = await this.callTool('mssql_validate_query', { query: 'SELECT * FROM Customers WHERE Country = \'USA\'', includeOptimizations: true }); const result1 = JSON.parse(validation1.content[0].text); console.log(' โœ… Valid query result:', result1.isValid ? 'PASSED' : 'FAILED'); if (result1.warnings.length > 0) { console.log(' โš ๏ธ Warnings:', result1.warnings.length); } } catch (error) { console.log(' โŒ Valid query test failed:', error.message); } // Test dangerous query try { const validation2 = await this.callTool('mssql_validate_query', { query: 'DROP TABLE Customers; SELECT * FROM Users', includeOptimizations: false }); const result2 = JSON.parse(validation2.content[0].text); console.log(' ๐Ÿ›ก๏ธ Dangerous query blocked:', !result2.isValid ? 'PASSED' : 'FAILED'); console.log(' ๐Ÿ“ Error count:', result2.errors.length); } catch (error) { console.log(' โŒ Dangerous query test failed:', error.message); } // Test 3: Enhanced Query with Security console.log('\n๐Ÿ” Testing enhanced query execution...'); try { const queryResult = await this.callTool('mssql_query', { ...TEST_CONFIG, query: 'SELECT TOP 5 CustomerID, CompanyName, Country FROM Customers ORDER BY CompanyName', limit: 5 }); const result = JSON.parse(queryResult.content[0].text); console.log(' โœ… Query executed successfully'); console.log(' ๐Ÿ“Š Rows returned:', result.rowCount); console.log(' โฑ๏ธ Execution time:', result.executionTime, 'ms'); } catch (error) { console.log(' โŒ Enhanced query failed:', error.message); } // Test 4: Bulk Insert console.log('\n๐Ÿ“ฆ Testing bulk insert operations...'); try { // First, create test data const testData = [ { CustomerID: 'TEST1', CompanyName: 'Test Company 1', Country: 'USA' }, { CustomerID: 'TEST2', CompanyName: 'Test Company 2', Country: 'Canada' }, { CustomerID: 'TEST3', CompanyName: 'Test Company 3', Country: 'UK' } ]; const bulkResult = await this.callTool('mssql_bulk_insert', { ...TEST_CONFIG, table: 'Customers', data: testData }); const result = JSON.parse(bulkResult.content[0].text); console.log(' โœ… Bulk insert completed'); console.log(' ๐Ÿ“ˆ Rows processed:', result.totalRowsProcessed); console.log(' ๐Ÿ’พ Rows inserted:', result.totalRowsInserted); } catch (error) { console.log(' โš ๏ธ Bulk insert test:', error.message); // This might fail if test data conflicts with existing data } // Test 5: Verify all original tools still work console.log('\n๐Ÿ”„ Testing backward compatibility...'); const originalTests = [ { name: 'mssql_list_databases', args: TEST_CONFIG }, { name: 'mssql_list_tables', args: TEST_CONFIG }, { name: 'mssql_describe_table', args: { ...TEST_CONFIG, table: 'Customers' } }, { name: 'mssql_sample_data', args: { ...TEST_CONFIG, table: 'Customers', limit: 2 } }, { name: 'mssql_get_relationships', args: TEST_CONFIG } ]; for (const test of originalTests) { try { const result = await this.callTool(test.name, test.args); console.log(` โœ… ${test.name}: OK`); } catch (error) { console.log(` โŒ ${test.name}: ${error.message}`); } } console.log('\n๐ŸŽ‰ Enhanced MCP Server Testing Complete!'); console.log('โœจ New features successfully implemented and tested'); } catch (error) { console.error('\n๐Ÿ’ฅ Enhanced test failed:', error.message); } finally { this.cleanup(); } } cleanup() { if (this.mcpProcess) { this.mcpProcess.kill(); console.log('๐Ÿงน MCP process cleaned up'); } } } // Run the enhanced tests const tester = new EnhancedMCPTester(); tester.runEnhancedTests().catch(console.error); // Cleanup on exit process.on('SIGINT', () => { tester.cleanup(); process.exit(0); });

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/michaelyuwh/mcp-mssql-connector'

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