Skip to main content
Glama
dhinojosac

TypeScript MCP Server Template

by dhinojosac

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
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • 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}`, }, ], }; }
  • 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}`, }, ], }; } );
  • Zod schema for validating getWeatherAlerts tool arguments: requires a 'state' field matching StateSchema (US state code).
    export const WeatherAlertsSchema = z.object({ state: StateSchema, });
  • 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'); }
  • 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; } }

Other Tools

Related Tools

Latest Blog Posts

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/dhinojosac/calendar-mcp'

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