getWeatherAlerts
Retrieve active weather alerts for any US state using this TypeScript-based MCP server tool, designed for real-time weather data integration and server development.
Instructions
Get active weather alerts for a US state
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/plugins/weatherPlugin.ts:51-68 (handler)Handler function that validates the state argument using WeatherAlertsSchema, simulates fetching weather alerts, and returns formatted text content.async (args: { [x: string]: any }) => { const { state }: WeatherAlertsArgs = validateToolArgs( WeatherAlertsSchema, args ); // Simulate alerts API call (replace with actual API) const alerts = await simulateAlertsAPI(); return { content: [ { type: 'text', text: `Weather alerts for ${state}:\n${alerts}`, }, ], }; }
- src/plugins/weatherPlugin.ts:45-69 (registration)Registration of the getWeatherAlerts MCP tool, including title, description, input validation schema, and execution handler.server.registerTool( 'getWeatherAlerts', { title: 'Get Weather Alerts', description: 'Get active weather alerts for a US state', }, async (args: { [x: string]: any }) => { const { state }: WeatherAlertsArgs = validateToolArgs( WeatherAlertsSchema, args ); // Simulate alerts API call (replace with actual API) const alerts = await simulateAlertsAPI(); return { content: [ { type: 'text', text: `Weather alerts for ${state}:\n${alerts}`, }, ], }; } );
- src/schemas/toolSchemas.ts:52-54 (schema)Zod schema for validating getWeatherAlerts tool arguments: requires a 'state' field matching StateSchema (US state code).export const WeatherAlertsSchema = z.object({ state: StateSchema, });
- src/plugins/weatherPlugin.ts:102-127 (helper)Helper function simulating a weather alerts API call, generating random mock alerts or no alerts message.async function simulateAlertsAPI(): Promise<string> { // Simulate API delay await new Promise(resolve => setTimeout(resolve, 100)); // Generate mock alerts based on state const alerts = [ 'Severe Thunderstorm Warning', 'Flash Flood Watch', 'Heat Advisory', 'Winter Weather Advisory', ]; const hasAlerts = Math.random() > 0.5; if (!hasAlerts) { return '✅ No active weather alerts for this state.'; } const activeAlerts = alerts.filter(() => Math.random() > 0.7).slice(0, 2); if (activeAlerts.length === 0) { return '✅ No active weather alerts for this state.'; } return activeAlerts.map(alert => `⚠️ ${alert}`).join('\n'); }
- src/schemas/toolSchemas.ts:65-77 (helper)Utility helper for validating tool arguments against Zod schemas with custom error formatting.export function validateToolArgs<T>(schema: z.ZodSchema<T>, args: unknown): T { try { return schema.parse(args); } catch (error) { if (error instanceof z.ZodError) { const errorMessages = error.errors .map(err => `${err.path.join('.')}: ${err.message}`) .join(', '); throw new Error(`Validation failed: ${errorMessages}`); } throw error; } }