@ryancardin/noaa-tides-currents-mcp-server
Version:
MCP Server that interfaces with NOAA Tides and Currents API using FastMCP
104 lines (103 loc) • 3.74 kB
JavaScript
import { GetWaterLevelsSchema, GetTidePredictionsSchema, GetCurrentsSchema, GetCurrentPredictionsSchema, GetMeteorologicalDataSchema, GetStationsSchema, GetStationDetailsSchema } from '../interfaces/noaa.js';
// Class for the MCP server
export class McpServer {
tools;
noaaService;
constructor(noaaService) {
this.noaaService = noaaService;
this.tools = this.initializeTools();
}
// Initialize the tools
initializeTools() {
// Water Levels tool
const getWaterLevels = {
name: "get_water_levels",
description: "Get water level data for a station",
inputSchema: GetWaterLevelsSchema,
handler: async (params) => {
return this.noaaService.getWaterLevels(params);
}
};
// Tide Predictions tool
const getTidePredictions = {
name: "get_tide_predictions",
description: "Get tide prediction data",
inputSchema: GetTidePredictionsSchema,
handler: async (params) => {
return this.noaaService.getTidePredictions(params);
}
};
// Currents tool
const getCurrents = {
name: "get_currents",
description: "Get currents data for a station",
inputSchema: GetCurrentsSchema,
handler: async (params) => {
return this.noaaService.getCurrents(params);
}
};
// Current Predictions tool
const getCurrentPredictions = {
name: "get_current_predictions",
description: "Get current predictions",
inputSchema: GetCurrentPredictionsSchema,
handler: async (params) => {
return this.noaaService.getCurrentPredictions(params);
}
};
// Meteorological Data tool
const getMeteorologicalData = {
name: "get_meteorological_data",
description: "Get meteorological data",
inputSchema: GetMeteorologicalDataSchema,
handler: async (params) => {
return this.noaaService.getMeteorologicalData(params);
}
};
// Stations tool
const getStations = {
name: "get_stations",
description: "Get list of stations",
inputSchema: GetStationsSchema,
handler: async (params) => {
return this.noaaService.getStations(params);
}
};
// Station Details tool
const getStationDetails = {
name: "get_station_details",
description: "Get detailed information about a station",
inputSchema: GetStationDetailsSchema,
handler: async (params) => {
return this.noaaService.getStationDetails(params);
}
};
return [
getWaterLevels,
getTidePredictions,
getCurrents,
getCurrentPredictions,
getMeteorologicalData,
getStations,
getStationDetails
];
}
// Method to get all tools
getTools() {
return this.tools.map(tool => ({
name: tool.name,
description: tool.description
}));
}
// Method to handle tool execution
async executeTool(toolName, params) {
const tool = this.tools.find(t => t.name === toolName);
if (!tool) {
throw new Error(`Tool '${toolName}' not found`);
}
// Validate the parameters against the schema
const validatedParams = tool.inputSchema.parse(params);
// Execute the tool handler
return tool.handler(validatedParams);
}
}