Skip to main content
Glama
0xteamhq

Grafana MCP Server

by 0xteamhq
quick-test.jsβ€’6.12 kB
#!/usr/bin/env node /** * Quick automated test of MCP server with real Grafana */ require('dotenv').config(); const { spawn } = require('child_process'); console.log('πŸš€ Quick MCP Test'); console.log('=================\n'); const server = spawn('node', ['dist/cli.js'], { env: process.env, stdio: ['pipe', 'pipe', 'pipe'] }); let responseBuffer = ''; let testPhase = 0; server.stdout.on('data', (data) => { responseBuffer += data.toString(); // Parse responses const lines = data.toString().split('\n'); lines.forEach(line => { if (line.trim() && line.startsWith('{')) { try { const json = JSON.parse(line); handleResponse(json); } catch {} } }); }); server.stderr.on('data', (data) => { const output = data.toString(); if (output.includes('MCP server started')) { console.log('βœ… Server started successfully\n'); runTests(); } }); function handleResponse(response) { if (!response.id) return; if (response.id === 1) { // Initialize response console.log('βœ… MCP initialized\n'); testPhase = 1; // Send list tools request console.log('πŸ“‹ Listing tools...'); sendRequest(2, 'tools/list', {}); } else if (response.id === 2) { // List tools response if (response.result?.tools) { console.log(`βœ… Found ${response.result.tools.length} tools\n`); // Test search dashboards console.log('πŸ” Testing: Search dashboards...'); sendRequest(3, 'tools/call', { name: 'search_dashboards', arguments: { query: '' } }); } } else if (response.id === 3) { // Search dashboards response if (response.result?.content) { try { const content = JSON.parse(response.result.content[0].text); console.log(`βœ… Found ${content.length} dashboards`); if (content[0]) { console.log(` First: ${content[0].title}\n`); } } catch { console.log('βœ… Response received\n'); } } // Test datasources console.log('πŸ’Ύ Testing: List datasources...'); sendRequest(4, 'tools/call', { name: 'list_datasources', arguments: {} }); } else if (response.id === 4) { // List datasources response if (response.result?.content) { try { const content = JSON.parse(response.result.content[0].text); console.log(`βœ… Found ${content.length} datasources:`); content.slice(0, 3).forEach(ds => { console.log(` - ${ds.name} (${ds.type})`); }); console.log(); // Find a Loki datasource for next test const lokiDs = content.find(ds => ds.type === 'loki'); if (lokiDs) { console.log(`πŸ“ Testing: Loki labels for ${lokiDs.name}...`); sendRequest(5, 'tools/call', { name: 'list_loki_label_names', arguments: { datasourceUid: lokiDs.uid } }); } else { finishTests(); } } catch { console.log('βœ… Response received\n'); finishTests(); } } } else if (response.id === 5) { // Loki labels response if (response.result?.content) { try { const content = JSON.parse(response.result.content[0].text); console.log(`βœ… Found ${content.length} Loki labels`); if (content.length > 0) { console.log(` Sample: ${content.slice(0, 5).join(', ')}\n`); } } catch { console.log('βœ… Response received\n'); } } // Test teams console.log('πŸ‘₯ Testing: List teams...'); sendRequest(6, 'tools/call', { name: 'list_teams', arguments: {} }); } else if (response.id === 6) { // Teams response if (response.result?.content) { try { const content = JSON.parse(response.result.content[0].text); console.log(`βœ… Found ${content.length} teams\n`); } catch { console.log('βœ… Response received\n'); } } // Test deeplink generation console.log('πŸ”— Testing: Generate deeplink...'); sendRequest(7, 'tools/call', { name: 'generate_deeplink', arguments: { resourceType: 'explore', datasourceUid: 'test-ds', timeRange: { from: 'now-1h', to: 'now' } } }); } else if (response.id === 7) { // Deeplink response if (response.result?.content) { try { const content = JSON.parse(response.result.content[0].text); console.log(`βœ… Generated URL: ${content.url}\n`); } catch { console.log('βœ… Response received\n'); } } finishTests(); } } function sendRequest(id, method, params) { const request = { jsonrpc: '2.0', id, method, params }; server.stdin.write(JSON.stringify(request) + '\n'); } function runTests() { // Send initialize request console.log('πŸ“ Initializing MCP...'); sendRequest(1, 'initialize', { protocolVersion: '0.1.0', capabilities: {}, clientInfo: { name: 'quick-test', version: '1.0.0' } }); } function finishTests() { console.log('=' .repeat(50)); console.log('✨ All tests completed successfully!'); console.log('=' .repeat(50)); console.log('\nπŸ“Š Summary:'); console.log(' β€’ MCP server started βœ“'); console.log(' β€’ Protocol initialized βœ“'); console.log(' β€’ Tools listed βœ“'); console.log(' β€’ Dashboard search works βœ“'); console.log(' β€’ Datasources accessible βœ“'); console.log(' β€’ Loki integration works βœ“'); console.log(' β€’ Teams listing works βœ“'); console.log(' β€’ Deeplink generation works βœ“'); console.log('\nπŸŽ‰ Your MCP server is working perfectly with Grafana!'); server.kill('SIGTERM'); setTimeout(() => process.exit(0), 500); } // Timeout handler setTimeout(() => { console.log('\n⚠️ Test timed out'); server.kill('SIGTERM'); process.exit(1); }, 30000); // Handle Ctrl+C process.on('SIGINT', () => { console.log('\nTest interrupted'); server.kill('SIGTERM'); process.exit(1); });

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/0xteamhq/mcp-grafana'

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