rui-weather-service-api
Version:
Weather API service with OpenWeatherMap integration - includes both standalone API and MCP server
141 lines • 5.34 kB
JavaScript
import { Command } from 'commander';
import { startServer, resetServer } from '../server/mcp-server.js';
import { weatherApi } from '../api/weather-api.js';
import { formatCurrentWeather, formatForecast } from '../formatters/weather-formatter.js';
import { updateConfig } from '../config/index.js';
/**
* Sets up the CLI program with all commands
*/
export function setupCli() {
const program = new Command();
// Set up program metadata
program
.name('weather-service')
.description('Weather Service API CLI - Access weather data via OpenWeatherMap API')
.version('1.0.0');
// Create server command group
const serverCommand = program
.command('server')
.description('Commands for controlling the Weather MCP Server');
// Add start subcommand to server command
serverCommand
.command('start')
.description('Start the Weather MCP Server')
.option('-p, --port <port>', 'Port to run the server on (if supporting HTTP)')
.option('-k, --api-key <key>', 'OpenWeatherMap API Key (overrides environment variable)')
.action(async (options) => {
try {
// Update configuration with CLI options
const config = {};
if (options.apiKey)
config.apiKey = options.apiKey;
if (options.port)
config.port = options.port;
updateConfig(config);
console.log('Starting Weather MCP Server...');
await startServer();
}
catch (error) {
console.error('Failed to start server:', error.message);
process.exit(1);
}
});
// Add reset-cache subcommand to server command
serverCommand
.command('reset-cache')
.description('Reset the MCP server cache to fix tool registration issues')
.action(() => {
try {
console.log('Resetting MCP server cache...');
resetServer();
console.log('MCP server cache has been reset. Use "server start" to restart the server.');
}
catch (error) {
console.error('Failed to reset cache:', error.message);
process.exit(1);
}
});
// Weather command for direct weather queries
program
.command('weather <city>')
.description('Get current weather for a city')
.option('-k, --api-key <key>', 'OpenWeatherMap API Key')
.option('-u, --units <units>', 'Units (metric or imperial)', 'metric')
.option('-l, --language <lang>', 'Language code for weather descriptions', 'en')
.action(async (city, options) => {
try {
// Update configuration with CLI options
const config = {};
if (options.apiKey)
config.apiKey = options.apiKey;
if (options.units)
config.units = options.units;
if (options.language)
config.language = options.language;
updateConfig(config);
// Get and display weather
const weatherData = await weatherApi.getCurrentWeather(city);
const formattedWeather = formatCurrentWeather(weatherData);
console.log(formattedWeather);
}
catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
});
// Forecast command
program
.command('forecast <city>')
.description('Get weather forecast for a city')
.option('-d, --days <days>', 'Number of days (1-5)', '3')
.option('-k, --api-key <key>', 'OpenWeatherMap API Key')
.option('-u, --units <units>', 'Units (metric or imperial)', 'metric')
.option('-l, --language <lang>', 'Language code for weather descriptions', 'en')
.action(async (city, options) => {
try {
// Update configuration with CLI options
const config = {};
if (options.apiKey)
config.apiKey = options.apiKey;
if (options.units)
config.units = options.units;
if (options.language)
config.language = options.language;
updateConfig(config);
// Parse days parameter
const days = parseInt(options.days);
if (isNaN(days) || days < 1 || days > 5) {
console.error('Error: Days must be between 1 and 5');
process.exit(1);
}
// Get and display forecast
const forecastData = await weatherApi.getForecast(city, days);
const formattedForecast = formatForecast(forecastData);
console.log(formattedForecast);
}
catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
});
return program;
}
/**
* Run the CLI program
*/
export function runCli() {
const program = setupCli();
// Parse command-line arguments
program.parse(process.argv);
// Show help if no arguments provided
if (program.args.length === 0) {
program.help();
}
}
// Execute CLI when run directly
if (typeof import.meta.url === 'string' &&
import.meta.url.startsWith('file:')) {
runCli();
}
//# sourceMappingURL=index.js.map