"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const body_parser_1 = __importDefault(require("body-parser"));
const node_fetch_1 = __importDefault(require("node-fetch"));
const app = (0, express_1.default)();
app.use(body_parser_1.default.json());
app.post('/json-rpc', async (req, res) => {
var _a, _b, _c;
const body = req.body;
const { method, params, id } = body;
if (method === 'getWeather') {
const city = params === null || params === void 0 ? void 0 : params.city;
if (!city || typeof city !== 'string' || city.length > 100) {
return res.json({ jsonrpc: '2.0', error: { code: -32602, message: 'Invalid params' }, id });
}
try {
const apiKey = process.env.OWM_KEY;
if (!apiKey)
throw new Error('OWM_KEY not set');
const r = await (0, node_fetch_1.default)(`https://api.openweathermap.org/data/2.5/weather?q=${encodeURIComponent(city)}&appid=${apiKey}&units=metric`);
if (!r.ok)
throw new Error('External API error');
const data = await r.json();
const result = {
city: data.name,
temp_c: (_a = data.main) === null || _a === void 0 ? void 0 : _a.temp,
description: (_c = (_b = data.weather) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.description
};
return res.json({ jsonrpc: '2.0', result, id });
}
catch (err) {
return res.json({ jsonrpc: '2.0', error: { code: -32000, message: String(err.message) }, id });
}
}
else {
res.json({ jsonrpc: '2.0', error: { code: -32601, message: 'Method not found' }, id });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`MCP Weather server listening on ${PORT}`));