UNPKG

@_odbo_/calmag-mcp-server

Version:

Model Context Protocol server for Webklex CalMag nutrient calculator

77 lines (76 loc) 2.24 kB
/** * API utilities for CalMag MCP Server */ import fetch from 'node-fetch'; /** * Make a CalMag API request */ export async function makeCalmagRequest(serverConfig, payload, expert = false) { const url = `${serverConfig.api_url}${expert ? '?expert=1' : ''}`; const headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'CalMag-MCP-Server/1.0.0' }; try { const response = await fetch(url, { method: 'POST', headers, body: JSON.stringify(payload) }); if (!response.ok) { throw new Error(`CalMag API request failed: ${response.status} ${response.statusText}`); } const data = await response.json(); if (data.error) { throw new Error(`CalMag API error: ${data.error}`); } return data; } catch (error) { console.error('CalMag API request failed:', error); throw error; } } /** * Fetch fertilizers directly from CalMag API endpoint * This uses the new API endpoint: https://www.calmag.eu/?method=fertilizers */ export async function fetchFertilizersFromApi() { const url = 'https://www.calmag.eu/?method=fertilizers'; try { const response = await fetch(url, { headers: { 'Accept': 'application/json', 'User-Agent': 'CalMag-MCP-Server/1.0.0' } }); if (!response.ok) { throw new Error(`Failed to fetch fertilizers: ${response.status} ${response.statusText}`); } const data = await response.json(); return data; } catch (error) { console.error('Failed to fetch fertilizers from API:', error); throw error; } } /** * Test server connectivity */ export async function testServerConnection(serverConfig) { try { const testPayload = { elements: { calcium: 100, magnesium: 30 }, ratio: 3.5, volume: 1.0 }; await makeCalmagRequest(serverConfig, testPayload); return true; } catch (error) { console.error('Server connection test failed:', error); return false; } }