@starhawk/mcp-weather-server 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +3 -0
  2. package/build/index.js +170 -0
  3. package/package.json +31 -0
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # A Simple MCP weather Server written in TypeScript
2
+
3
+ See the [Quickstart](https://modelcontextprotocol.io/quickstart) tutorial for more information.
package/build/index.js ADDED
@@ -0,0 +1,170 @@
1
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3
+ import { z } from "zod";
4
+ const NWS_API_BASE = "https://api.weather.gov";
5
+ const USER_AGENT = "weather-app/1.0";
6
+ // Helper function for making NWS API requests
7
+ async function makeNWSRequest(url) {
8
+ const headers = {
9
+ "User-Agent": USER_AGENT,
10
+ Accept: "application/geo+json",
11
+ };
12
+ try {
13
+ const response = await fetch(url, { headers });
14
+ if (!response.ok) {
15
+ throw new Error(`HTTP error! status: ${response.status}`);
16
+ }
17
+ return (await response.json());
18
+ }
19
+ catch (error) {
20
+ console.error("Error making NWS request:", error);
21
+ return null;
22
+ }
23
+ }
24
+ // Format alert data
25
+ function formatAlert(feature) {
26
+ const props = feature.properties;
27
+ return [
28
+ `Event: ${props.event || "Unknown"}`,
29
+ `Area: ${props.areaDesc || "Unknown"}`,
30
+ `Severity: ${props.severity || "Unknown"}`,
31
+ `Status: ${props.status || "Unknown"}`,
32
+ `Headline: ${props.headline || "No headline"}`,
33
+ "---",
34
+ ].join("\n");
35
+ }
36
+ // Create server instance
37
+ const server = new McpServer({
38
+ name: "weather",
39
+ version: "1.0.0",
40
+ });
41
+ // Register weather tools
42
+ server.registerTool("get-alerts", {
43
+ title: "Get Weather Alerts",
44
+ description: "Get weather alerts for a state",
45
+ inputSchema: {
46
+ state: z.string().length(2).describe("Two-letter state code (e.g. CA, NY)"),
47
+ },
48
+ }, async ({ state }) => {
49
+ const stateCode = state.toUpperCase();
50
+ const alertsUrl = `${NWS_API_BASE}/alerts?area=${stateCode}`;
51
+ const alertsData = await makeNWSRequest(alertsUrl);
52
+ if (!alertsData) {
53
+ return {
54
+ content: [
55
+ {
56
+ type: "text",
57
+ text: "Failed to retrieve alerts data",
58
+ },
59
+ ],
60
+ };
61
+ }
62
+ const features = alertsData.features || [];
63
+ if (features.length === 0) {
64
+ return {
65
+ content: [
66
+ {
67
+ type: "text",
68
+ text: `No active alerts for ${stateCode}`,
69
+ },
70
+ ],
71
+ };
72
+ }
73
+ const formattedAlerts = features.map(formatAlert);
74
+ const alertsText = `Active alerts for ${stateCode}:\n\n${formattedAlerts.join("\n")}`;
75
+ return {
76
+ content: [
77
+ {
78
+ type: "text",
79
+ text: alertsText,
80
+ },
81
+ ],
82
+ };
83
+ });
84
+ server.registerTool("get-forecast", {
85
+ title: "Get Weather Forecast",
86
+ description: "Get weather forecast for a location",
87
+ inputSchema: {
88
+ latitude: z.number().min(-90).max(90).describe("Latitude of the location"),
89
+ longitude: z
90
+ .number()
91
+ .min(-180)
92
+ .max(180)
93
+ .describe("Longitude of the location"),
94
+ },
95
+ }, async ({ latitude, longitude }) => {
96
+ // Get grid point data
97
+ const pointsUrl = `${NWS_API_BASE}/points/${latitude.toFixed(4)},${longitude.toFixed(4)}`;
98
+ const pointsData = await makeNWSRequest(pointsUrl);
99
+ if (!pointsData) {
100
+ return {
101
+ content: [
102
+ {
103
+ type: "text",
104
+ 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).`,
105
+ },
106
+ ],
107
+ };
108
+ }
109
+ const forecastUrl = pointsData.properties?.forecast;
110
+ if (!forecastUrl) {
111
+ return {
112
+ content: [
113
+ {
114
+ type: "text",
115
+ text: "Failed to get forecast URL from grid point data",
116
+ },
117
+ ],
118
+ };
119
+ }
120
+ // Get forecast data
121
+ const forecastData = await makeNWSRequest(forecastUrl);
122
+ if (!forecastData) {
123
+ return {
124
+ content: [
125
+ {
126
+ type: "text",
127
+ text: "Failed to retrieve forecast data",
128
+ },
129
+ ],
130
+ };
131
+ }
132
+ const periods = forecastData.properties?.periods || [];
133
+ if (periods.length === 0) {
134
+ return {
135
+ content: [
136
+ {
137
+ type: "text",
138
+ text: "No forecast periods available",
139
+ },
140
+ ],
141
+ };
142
+ }
143
+ // Format forecast periods
144
+ const formattedForecast = periods.map((period) => [
145
+ `${period.name || "Unknown"}:`,
146
+ `Temperature: ${period.temperature || "Unknown"}°${period.temperatureUnit || "F"}`,
147
+ `Wind: ${period.windSpeed || "Unknown"} ${period.windDirection || ""}`,
148
+ `${period.shortForecast || "No forecast available"}`,
149
+ "---",
150
+ ].join("\n"));
151
+ const forecastText = `Forecast for ${latitude}, ${longitude}:\n\n${formattedForecast.join("\n")}`;
152
+ return {
153
+ content: [
154
+ {
155
+ type: "text",
156
+ text: forecastText,
157
+ },
158
+ ],
159
+ };
160
+ });
161
+ // Start the server
162
+ async function main() {
163
+ const transport = new StdioServerTransport();
164
+ await server.connect(transport);
165
+ console.error("Weather MCP Server running on stdio");
166
+ }
167
+ main().catch((error) => {
168
+ console.error("Fatal error in main():", error);
169
+ process.exit(1);
170
+ });
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@starhawk/mcp-weather-server",
3
+ "version": "1.0.1",
4
+ "mcpName": "io.github.starhawk/weather",
5
+ "description": "MCP weather server for Windows tutorial",
6
+ "main": "index.js",
7
+ "type": "module",
8
+ "bin": {
9
+ "weather": "./build/index.js"
10
+ },
11
+ "scripts": {
12
+ "build": "tsc && node -e \"require('fs').chmodSync('build/index.js', '755')\""
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/starhawk/mcp-weather-server.git"
17
+ },
18
+ "files": [
19
+ "build"
20
+ ],
21
+ "keywords": ["mcp"],
22
+ "author": "Keum Dongjoon",
23
+ "license": "ISC",
24
+ "devDependencies": {
25
+ "@types/node": "^22.19.2",
26
+ "typescript": "^5.9.3"
27
+ },
28
+ "dependencies": {
29
+ "@modelcontextprotocol/sdk": "^1.24.3"
30
+ }
31
+ }