import { z } from "zod";
import { makeNWSRequest, formatAlert, AlertsResponse, PointsResponse, ForecastResponse, ForecastPeriod } from "../helpers/nwsRequests.js";
import { runRefactor, runTestGenerator, runDocGenerator, runDebugAssistant, runCodeNavigator } from "../modules/index.js";
// Função para registrar as tools no servidor MCP
export function registerWeatherTools(server: any, NWS_API_BASE: string, USER_AGENT: string) {
server.tool(
"get_alerts",
"Get weather alerts for a state",
{
state: z.string().length(2).describe("Two-letter state code (e.g. CA, NY)"),
},
async ({ state }: { state: string }) => {
const stateCode = state.toUpperCase();
const alertsUrl = `${NWS_API_BASE}/alerts?area=${stateCode}`;
const alertsData = await makeNWSRequest<AlertsResponse>(alertsUrl, USER_AGENT);
if (!alertsData) {
return {
content: [
{
type: "text",
text: "Failed to retrieve alerts data",
},
],
};
}
const features = alertsData.features || [];
if (features.length === 0) {
return {
content: [
{
type: "text",
text: `No active alerts for ${stateCode}`,
},
],
};
}
const formattedAlerts = features.map(formatAlert);
const alertsText = `Active alerts for ${stateCode}:\n\n${formattedAlerts.join("\n")}`;
return {
content: [
{
type: "text",
text: alertsText,
},
],
};
},
);
// Refactor Tool
server.tool(
"refactor_code",
"Refatora um arquivo de código conforme instruções.",
{
target_file: z.string().describe("Caminho do arquivo a ser refatorado"),
instructions: z.string().describe("Instruções de refatoração"),
},
async ({ target_file, instructions }: { target_file: string; instructions: string }) => {
return await runRefactor({ target_file, instructions });
}
);
// Test Generator Tool
server.tool(
"generate_tests",
"Gera testes para um arquivo de código.",
{
target_file: z.string().describe("Caminho do arquivo a ser testado"),
framework: z.string().optional().describe("Framework de testes (opcional, padrão: jest)"),
},
async ({ target_file, framework }: { target_file: string; framework?: string }) => {
return await runTestGenerator({ target_file, framework });
}
);
// Doc Generator Tool
server.tool(
"generate_docs",
"Gera documentação para um arquivo de código.",
{
target_file: z.string().describe("Caminho do arquivo a ser documentado"),
},
async ({ target_file }: { target_file: string }) => {
return await runDocGenerator({ target_file });
}
);
// Debug Assistant Tool
server.tool(
"debug_assistant",
"Auxilia na depuração de um arquivo de código a partir de uma mensagem de erro.",
{
target_file: z.string().describe("Caminho do arquivo a ser analisado"),
error_message: z.string().describe("Mensagem de erro encontrada"),
},
async ({ target_file, error_message }: { target_file: string; error_message: string }) => {
return await runDebugAssistant({ target_file, error_message });
}
);
// Code Navigator Tool
server.tool(
"code_navigator",
"Navega e responde perguntas sobre um arquivo de código.",
{
target_file: z.string().describe("Caminho do arquivo a ser navegado"),
query: z.string().describe("Pergunta sobre o código"),
},
async ({ target_file, query }: { target_file: string; query: string }) => {
return await runCodeNavigator({ target_file, query });
}
);
server.tool(
"get_forecast",
"Get weather forecast for a location",
{
latitude: z.number().min(-90).max(90).describe("Latitude of the location"),
longitude: z
.number()
.min(-180)
.max(180)
.describe("Longitude of the location"),
},
async ({ latitude, longitude }: { latitude: number; longitude: number }) => {
// Get grid point data
const pointsUrl = `${NWS_API_BASE}/points/${latitude.toFixed(4)},${longitude.toFixed(4)}`;
const pointsData = await makeNWSRequest<PointsResponse>(pointsUrl, USER_AGENT);
if (!pointsData) {
return {
content: [
{
type: "text",
text: `Failed to retrieve grid point data for coordinates: ${latitude}, ${longitude}. This location may not be supported by the NWS API (only US locations are supported).`,
},
],
};
}
const forecastUrl = pointsData.properties?.forecast;
if (!forecastUrl) {
return {
content: [
{
type: "text",
text: "Failed to get forecast URL from grid point data",
},
],
};
}
// Get forecast data
const forecastData = await makeNWSRequest<ForecastResponse>(forecastUrl, USER_AGENT);
if (!forecastData) {
return {
content: [
{
type: "text",
text: "Failed to retrieve forecast data",
},
],
};
}
const periods = forecastData.properties?.periods || [];
if (periods.length === 0) {
return {
content: [
{
type: "text",
text: "No forecast periods available",
},
],
};
}
// Format forecast periods
const formattedForecast = periods.map((period: ForecastPeriod) =>
[
`${period.name || "Unknown"}:`,
`Temperature: ${period.temperature || "Unknown"}°${period.temperatureUnit || "F"}`,
`Wind: ${period.windSpeed || "Unknown"} ${period.windDirection || ""}`,
`${period.shortForecast || "No forecast available"}`,
"---",
].join("\n"),
);
const forecastText = `Forecast for ${latitude}, ${longitude}:\n\n${formattedForecast.join("\n")}`;
return {
content: [
{
type: "text",
text: forecastText,
},
],
};
},
);
}