Skip to main content
Glama

Node.js MCP Weather Server

test-client.jsโ€ข4.1 kB
const axios = require('axios'); class MCPTestClient { constructor(baseUrl = 'http://localhost:8000') { this.baseUrl = baseUrl; this.mcpEndpoint = `${baseUrl}/mcp/stream`; } async makeRequest(method, params = {}, id = null) { const request = { jsonrpc: '2.0', method, params, id: id || Math.floor(Math.random() * 1000) }; try { console.log(`\n๐Ÿ”„ Sending request: ${method}`); console.log('Request:', JSON.stringify(request, null, 2)); const response = await axios.post(this.mcpEndpoint, request); console.log('โœ… Response received:'); console.log(JSON.stringify(response.data, null, 2)); return response.data; } catch (error) { console.error('โŒ Error:', error.message); if (error.response) { console.error('Response data:', error.response.data); } throw error; } } async testInitialize() { console.log('\n=== Testing Initialize ==='); return await this.makeRequest('initialize', {}); } async testListTools() { console.log('\n=== Testing List Tools ==='); return await this.makeRequest('tools/list'); } async testGetAlerts(state = 'CA') { console.log('\n=== Testing Weather Alerts ==='); return await this.makeRequest('tools/call', { name: 'get_alerts', arguments: { state } }); } async testGetForecast(latitude = 37.7749, longitude = -122.4194) { console.log('\n=== Testing Weather Forecast ==='); return await this.makeRequest('tools/call', { name: 'get_forecast', arguments: { latitude, longitude } }); } async testListResources() { console.log('\n=== Testing List Resources ==='); return await this.makeRequest('resources/list'); } async testReadResource(uri = 'mcp://server/sample') { console.log('\n=== Testing Read Resource ==='); return await this.makeRequest('resources/read', { uri }); } async checkHealth() { console.log('\n=== Checking Server Health ==='); try { const response = await axios.get(`${this.baseUrl}/health`); console.log('โœ… Health check passed:'); console.log(JSON.stringify(response.data, null, 2)); return response.data; } catch (error) { console.error('โŒ Health check failed:', error.message); throw error; } } async runAllTests() { console.log('๐Ÿš€ Starting MCP Server Tests'); console.log('================================'); try { // Check server health first await this.checkHealth(); // Test MCP protocol methods await this.testInitialize(); await this.testListTools(); await this.testListResources(); await this.testReadResource(); // Test weather tools await this.testGetAlerts('CA'); await this.testGetForecast(37.7749, -122.4194); // San Francisco console.log('\n๐ŸŽ‰ All tests completed successfully!'); } catch (error) { console.error('\n๐Ÿ’ฅ Test suite failed:', error.message); process.exit(1); } } } // Run tests if this file is executed directly if (require.main === module) { const client = new MCPTestClient(); // Parse command line arguments const args = process.argv.slice(2); const serverUrl = args.find(arg => arg.startsWith('--url='))?.split('=')[1] || 'http://localhost:8000'; if (serverUrl !== 'http://localhost:8000') { client.baseUrl = serverUrl; client.mcpEndpoint = `${serverUrl}/mcp/stream`; console.log(`๐ŸŒ Testing server at: ${serverUrl}`); } client.runAllTests(); } module.exports = MCPTestClient;

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/Azure-Samples/remote-mcp-webapp-node'

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