@cyanheads/noaa-spaceweather-mcp-server 0.4.0 → 0.6.0
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.
- package/AGENTS.md +9 -8
- package/CLAUDE.md +9 -8
- package/README.md +13 -12
- package/changelog/0.5.x/0.5.0.md +28 -0
- package/changelog/0.6.x/0.6.0.md +41 -0
- package/changelog/template.md +20 -11
- package/dist/mcp-server/tools/definitions/get-alerts.tool.d.ts +3 -2
- package/dist/mcp-server/tools/definitions/get-alerts.tool.d.ts.map +1 -1
- package/dist/mcp-server/tools/definitions/get-alerts.tool.js +69 -36
- package/dist/mcp-server/tools/definitions/get-alerts.tool.js.map +1 -1
- package/dist/mcp-server/tools/definitions/get-aurora-forecast.tool.d.ts +2 -2
- package/dist/mcp-server/tools/definitions/get-aurora-forecast.tool.d.ts.map +1 -1
- package/dist/mcp-server/tools/definitions/get-aurora-forecast.tool.js +19 -22
- package/dist/mcp-server/tools/definitions/get-aurora-forecast.tool.js.map +1 -1
- package/dist/mcp-server/tools/definitions/get-conditions.tool.d.ts +28 -26
- package/dist/mcp-server/tools/definitions/get-conditions.tool.d.ts.map +1 -1
- package/dist/mcp-server/tools/definitions/get-conditions.tool.js +108 -115
- package/dist/mcp-server/tools/definitions/get-conditions.tool.js.map +1 -1
- package/dist/mcp-server/tools/definitions/get-kp-index.tool.d.ts +8 -5
- package/dist/mcp-server/tools/definitions/get-kp-index.tool.d.ts.map +1 -1
- package/dist/mcp-server/tools/definitions/get-kp-index.tool.js +64 -42
- package/dist/mcp-server/tools/definitions/get-kp-index.tool.js.map +1 -1
- package/dist/mcp-server/tools/definitions/get-solar-activity.tool.d.ts +23 -16
- package/dist/mcp-server/tools/definitions/get-solar-activity.tool.d.ts.map +1 -1
- package/dist/mcp-server/tools/definitions/get-solar-activity.tool.js +151 -64
- package/dist/mcp-server/tools/definitions/get-solar-activity.tool.js.map +1 -1
- package/dist/mcp-server/tools/definitions/get-solar-wind.tool.d.ts +2 -2
- package/dist/mcp-server/tools/definitions/get-solar-wind.tool.d.ts.map +1 -1
- package/dist/mcp-server/tools/definitions/get-solar-wind.tool.js +5 -28
- package/dist/mcp-server/tools/definitions/get-solar-wind.tool.js.map +1 -1
- package/dist/mcp-server/tools/definitions/index.d.ts +117 -104
- package/dist/mcp-server/tools/definitions/index.d.ts.map +1 -1
- package/dist/services/space-weather/kp-headline.d.ts +30 -0
- package/dist/services/space-weather/kp-headline.d.ts.map +1 -0
- package/dist/services/space-weather/kp-headline.js +39 -0
- package/dist/services/space-weather/kp-headline.js.map +1 -0
- package/dist/services/space-weather/space-weather-service.d.ts +37 -8
- package/dist/services/space-weather/space-weather-service.d.ts.map +1 -1
- package/dist/services/space-weather/space-weather-service.js +277 -112
- package/dist/services/space-weather/space-weather-service.js.map +1 -1
- package/dist/services/space-weather/types.d.ts +97 -46
- package/dist/services/space-weather/types.d.ts.map +1 -1
- package/package.json +9 -9
- package/server.json +3 -3
- package/skills/noaa-spaceweather-guide/SKILL.md +8 -8
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* records, and exposes per-feed methods used by all tools.
|
|
6
6
|
* @module services/space-weather/space-weather-service
|
|
7
7
|
*/
|
|
8
|
+
import { z } from '@cyanheads/mcp-ts-core';
|
|
8
9
|
import { JsonRpcErrorCode, McpError, serviceUnavailable } from '@cyanheads/mcp-ts-core/errors';
|
|
9
10
|
import { fetchWithTimeout, withRetry } from '@cyanheads/mcp-ts-core/utils';
|
|
10
11
|
// ── Constants ──────────────────────────────────────────────────────────────
|
|
@@ -58,16 +59,19 @@ export function kpToGScale(kp) {
|
|
|
58
59
|
return 0;
|
|
59
60
|
}
|
|
60
61
|
/**
|
|
61
|
-
*
|
|
62
|
+
* Typical aurora reach by geomagnetic latitude, keyed on NOAA G level, ordered
|
|
62
63
|
* poleward-first. The single table for the whole server: aurora-latitude guidance
|
|
63
64
|
* on the Kp tools and the Kp threshold the aurora tool reports for a location both
|
|
64
65
|
* read it, so the two can no longer state different thresholds for one place.
|
|
65
66
|
*
|
|
66
|
-
* Latitudes for G2–G5 are the NOAA scales page figures
|
|
67
|
+
* Latitudes for G2–G5 are the NOAA scales page figures, which give where each
|
|
68
|
+
* level's aurora is typically seen, not hard limits. The 60° G1 row is this
|
|
67
69
|
* server's interpolation between that page's G2 row and the oval edge — the page
|
|
68
70
|
* states no G1 latitude. The 65° row is not a storm level at all: it is the
|
|
69
71
|
* quiet-time equatorward edge of the auroral oval, where aurora needs no elevated
|
|
70
|
-
* Kp. Below the 40° G5 row no level
|
|
72
|
+
* Kp. Below the 40° G5 row the table assigns no level: that lies beyond the typical
|
|
73
|
+
* G5 band, though an exceptional storm has carried aurora well past it (Puerto Rico,
|
|
74
|
+
* 27° magnetic latitude, May 2024).
|
|
71
75
|
*
|
|
72
76
|
* Each Kp floor is its level's "minus" third — the value at which SWPC starts the
|
|
73
77
|
* level, per {@link kpToGScale} — except G5, which begins at a whole Kp 9.
|
|
@@ -82,7 +86,7 @@ export const AURORA_BANDS = [
|
|
|
82
86
|
];
|
|
83
87
|
/**
|
|
84
88
|
* Resolve the aurora band a geomagnetic latitude sits in, north or south. Returns
|
|
85
|
-
* null equatorward of the 40° G5 edge,
|
|
89
|
+
* null equatorward of the 40° G5 edge, beyond the typical G5 band.
|
|
86
90
|
*/
|
|
87
91
|
export function auroraBandForGeomagneticLatitude(geomagneticLatitude) {
|
|
88
92
|
const abs = Math.abs(geomagneticLatitude);
|
|
@@ -126,24 +130,30 @@ function tryParseJson(text) {
|
|
|
126
130
|
* calling tool's contract, `path` names the feed on every class (an HTTP-origin error
|
|
127
131
|
* carries a status but no path of its own), and `data` from the underlying rejection is
|
|
128
132
|
* preserved so `status`, `statusText`, `retryAfter`, `retryAttempts`, and `available`
|
|
129
|
-
* survive.
|
|
133
|
+
* survive. Its `recovery` is the exception: the framework's own hints (a redirect loop's,
|
|
134
|
+
* an `invalid_url`'s) tell the caller to change a request URL these tools never take, and
|
|
135
|
+
* a hint on the error outranks the contract's, so it would replace the one the caller can
|
|
136
|
+
* act on.
|
|
130
137
|
*
|
|
131
|
-
* Both reasons map to `ServiceUnavailable`: the failure is upstream, and no 4xx
|
|
132
|
-
* these keyless feeds can be caused by caller input. `feed_moved` also carries
|
|
138
|
+
* Both reasons map to `ServiceUnavailable`: the failure is upstream, and no 3xx or 4xx
|
|
139
|
+
* from these keyless feeds can be caused by caller input. `feed_moved` also carries
|
|
133
140
|
* `retryable: false`, so a caller — or any outer retry — can see that re-attempting a
|
|
134
141
|
* path SWPC no longer serves cannot succeed.
|
|
135
142
|
*/
|
|
136
143
|
function feedFailure(reason, message, path, data, cause) {
|
|
144
|
+
const { recovery: _upstreamRecovery, ...upstream } = data ?? {};
|
|
137
145
|
return new McpError(JsonRpcErrorCode.ServiceUnavailable, message, {
|
|
138
|
-
...
|
|
146
|
+
...upstream,
|
|
139
147
|
...(reason === 'feed_moved' ? { retryable: false } : {}),
|
|
140
148
|
path,
|
|
141
149
|
reason,
|
|
142
150
|
}, cause !== undefined ? { cause } : undefined);
|
|
143
151
|
}
|
|
144
152
|
/**
|
|
145
|
-
* The codes the framework's retry predicate treats as transient
|
|
146
|
-
*
|
|
153
|
+
* The codes the framework's retry predicate treats as transient — the ones `withRetry`
|
|
154
|
+
* re-attempts unless the rejection carries `data.retryable: false`. A rejection carrying
|
|
155
|
+
* any other code, or that flag, failed on its first attempt, and re-sending the request
|
|
156
|
+
* gets the same answer.
|
|
147
157
|
*/
|
|
148
158
|
const RETRIED_CODES = new Set([
|
|
149
159
|
JsonRpcErrorCode.ServiceUnavailable,
|
|
@@ -154,26 +164,33 @@ const RETRIED_CODES = new Set([
|
|
|
154
164
|
* Which reason an upstream rejection belongs to, or null when it is not a feed failure
|
|
155
165
|
* at all and must pass through untouched.
|
|
156
166
|
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
*
|
|
167
|
+
* An `McpError` the framework does not retry — a code outside the transient set, or
|
|
168
|
+
* `data.retryable: false` — failed on its first attempt and will fail the same way on
|
|
169
|
+
* the next, so it is `feed_moved`. That covers a 4xx the framework treats as permanent
|
|
170
|
+
* — the feed path itself no longer resolves, and these tools take no upstream
|
|
171
|
+
* identifier, so nothing a caller sent can produce one (#21 was SWPC deleting a feed
|
|
172
|
+
* outright) — a redirect the client cannot follow: a 300 or 304, or a redirect status
|
|
173
|
+
* with no `Location` (each `InvalidRequest`), and a `Location` onto a non-HTTP target or
|
|
174
|
+
* a chain past `fetchWithTimeout`'s hop limit (each `ValidationError`) — and a 501,
|
|
175
|
+
* which keeps `ServiceUnavailable` but is the one status the framework flags
|
|
176
|
+
* `retryable: false`, since the upstream declares the method absent. Keying on the
|
|
177
|
+
* retry verdict rather than a status range is what keeps `feed_moved` synonymous with
|
|
178
|
+
* "failed on the first attempt": a 408 or 425 is a 4xx the framework classifies as
|
|
179
|
+
* `Timeout` and retries, so it belongs with the transient set. Everything else — any
|
|
180
|
+
* other 5xx, 429, network error, client deadline, the
|
|
181
|
+
* ladder's {@link RETRY_DEADLINE_MS} expiring (a `Timeout` carrying
|
|
182
|
+
* `retry_deadline_exceeded` and no status), unparseable or HTML body, and a throw that
|
|
183
|
+
* is no `McpError`, which the framework also retries — clears on its own and stays
|
|
184
|
+
* retryable.
|
|
166
185
|
*/
|
|
167
186
|
function feedFailureReason(error) {
|
|
168
|
-
if (error instanceof McpError)
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
return 'feed_unavailable';
|
|
187
|
+
if (!(error instanceof McpError))
|
|
188
|
+
return 'feed_unavailable';
|
|
189
|
+
if (error.code === JsonRpcErrorCode.RequestCancelled)
|
|
190
|
+
return null;
|
|
191
|
+
return RETRIED_CODES.has(error.code) && error.data?.retryable !== false
|
|
192
|
+
? 'feed_unavailable'
|
|
193
|
+
: 'feed_moved';
|
|
177
194
|
}
|
|
178
195
|
/** Matches a body whose first markup is an HTML document rather than feed content. */
|
|
179
196
|
const HTML_BODY_RE = /^\s*<(!DOCTYPE\s+html|html[\s>])/i;
|
|
@@ -272,16 +289,7 @@ function fetchText(path, ctx, userAgent) {
|
|
|
272
289
|
});
|
|
273
290
|
}
|
|
274
291
|
// ── Normalization helpers ───────────────────────────────────────────────────
|
|
275
|
-
/**
|
|
276
|
-
* Parse numeric string, returning null if the value is the fill value or NaN.
|
|
277
|
-
*
|
|
278
|
-
* The single numeric parse for the scales feed, levels and probabilities alike. SWPC
|
|
279
|
-
* sends `null` for a period it issues no level for — every forecast period's R and S,
|
|
280
|
-
* where the forecast is a probability instead — and that null is upstream saying "no
|
|
281
|
-
* level here", a different claim from level 0. Returning null rather than 0 is what
|
|
282
|
-
* keeps the two apart (#23); an unparseable value is not a level or a percentage
|
|
283
|
-
* either and reads the same way.
|
|
284
|
-
*/
|
|
292
|
+
/** Parse an optional measurement or probability, preserving missing values as null. */
|
|
285
293
|
function parseNum(s) {
|
|
286
294
|
if (s == null)
|
|
287
295
|
return null;
|
|
@@ -290,6 +298,18 @@ function parseNum(s) {
|
|
|
290
298
|
return null;
|
|
291
299
|
return n;
|
|
292
300
|
}
|
|
301
|
+
/** A complete decimal value, including the signed and scientific spellings accepted by parseFloat. */
|
|
302
|
+
const DECIMAL_SCALE_RE = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/;
|
|
303
|
+
/**
|
|
304
|
+
* A NOAA scale level: a whole number 0–5, as a JSON number or a complete decimal
|
|
305
|
+
* string. Null for any other `Scale` value, null and absent included.
|
|
306
|
+
*/
|
|
307
|
+
function scaleLevel(value) {
|
|
308
|
+
const level = typeof value === 'string' && DECIMAL_SCALE_RE.test(value.trim()) ? Number(value) : value;
|
|
309
|
+
return typeof level === 'number' && Number.isInteger(level) && level >= 0 && level <= 5
|
|
310
|
+
? level
|
|
311
|
+
: null;
|
|
312
|
+
}
|
|
293
313
|
/**
|
|
294
314
|
* Normalize a SWPC time tag string to explicit ISO 8601 UTC (always ends in 'Z').
|
|
295
315
|
* SWPC emits two Z-less shapes: solar-wind and alert bodies use space-separated
|
|
@@ -496,6 +516,23 @@ const CANCEL_RE = /^CANCEL\s+(?:WARNING|WATCH|ALERT):/im;
|
|
|
496
516
|
const SERIAL_RE = /^Serial\s+Number:\s*(\S+)/im;
|
|
497
517
|
/** Matches the serial a cancellation names as its target. */
|
|
498
518
|
const CANCEL_SERIAL_RE = /^Cancel\s+Serial\s+Number:\s*(\S+)/im;
|
|
519
|
+
/**
|
|
520
|
+
* A record names the predecessor it replaces only through a headline and that headline's
|
|
521
|
+
* own serial line: an Alert continuation, or a Warning extension. Either line under any
|
|
522
|
+
* other headline, the other link's headline included, establishes nothing.
|
|
523
|
+
*/
|
|
524
|
+
const CONTINUED_ALERT_RE = /^CONTINUED[ \t]+ALERT:/im;
|
|
525
|
+
const CONTINUED_SERIAL_RE = /^Continuation[ \t]+of[ \t]+Serial[ \t]+Number:[ \t]*(\S+)/im;
|
|
526
|
+
const EXTENDED_WARNING_RE = /^EXTENDED[ \t]+WARNING:/im;
|
|
527
|
+
const EXTENDED_SERIAL_RE = /^Extension[ \t]+to[ \t]+Serial[ \t]+Number:[ \t]*(\S+)/im;
|
|
528
|
+
/** The serial of the predecessor a continuation or extension names; null for any other record. */
|
|
529
|
+
function parseContinuesSerial(message) {
|
|
530
|
+
if (CONTINUED_ALERT_RE.test(message))
|
|
531
|
+
return message.match(CONTINUED_SERIAL_RE)?.[1] ?? null;
|
|
532
|
+
if (EXTENDED_WARNING_RE.test(message))
|
|
533
|
+
return message.match(EXTENDED_SERIAL_RE)?.[1] ?? null;
|
|
534
|
+
return null;
|
|
535
|
+
}
|
|
499
536
|
/**
|
|
500
537
|
* Matches the issue time a cancellation restates for its target. Only a cancellation
|
|
501
538
|
* carries this line, so it never collides with the record's own "Issue Time:".
|
|
@@ -647,6 +684,37 @@ function parsePredictedDayEnd(message, issueDatetime) {
|
|
|
647
684
|
// Match the label-parsed values, which carry no milliseconds.
|
|
648
685
|
return end.toISOString().replace(/\.000Z$/, 'Z');
|
|
649
686
|
}
|
|
687
|
+
/**
|
|
688
|
+
* An edited-events `begin_datetime` down to its minute: a date, then a T- or
|
|
689
|
+
* space-separated `HH:MM`. Anchored, one line, fixed-width — the shape of
|
|
690
|
+
* {@link DECIMAL_SCALE_RE} — and run on one short feed field, never on caller text.
|
|
691
|
+
*/
|
|
692
|
+
const EDITED_BEGIN_MINUTE_RE = /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}/;
|
|
693
|
+
/**
|
|
694
|
+
* The edited-events feed (`/json/edited_events.json`): one array interleaving several
|
|
695
|
+
* event types (`XRA`, `FLA`, `RSP`, `RBR`, `DSF`), 30 keys each. Only `type` is read
|
|
696
|
+
* off every record; the GOES X-ray (`XRA`) records are parsed again by
|
|
697
|
+
* {@link EditedXraSchema}.
|
|
698
|
+
*/
|
|
699
|
+
const EditedEventsSchema = z.array(z.looseObject({ type: z.string() }));
|
|
700
|
+
/**
|
|
701
|
+
* The fields of one edited `XRA` record that attribution reads: `observatory` is the GOES
|
|
702
|
+
* satellite (`G18`, `G19`), `begin_datetime` Z-less UTC, and `region` the NOAA region
|
|
703
|
+
* SWPC credited, or null when it credited none.
|
|
704
|
+
*/
|
|
705
|
+
const EditedXraSchema = z.object({
|
|
706
|
+
observatory: z.string(),
|
|
707
|
+
begin_datetime: z.string().regex(EDITED_BEGIN_MINUTE_RE),
|
|
708
|
+
region: z.number().int().nullable(),
|
|
709
|
+
});
|
|
710
|
+
/**
|
|
711
|
+
* The key a flare and its edited `XRA` record share: the GOES satellite and the UTC onset
|
|
712
|
+
* minute. Both times arrive explicit-UTC ISO 8601, so the minute is the first 16
|
|
713
|
+
* characters — no `Date` parse, which would read SWPC's Z-less tag as local time.
|
|
714
|
+
*/
|
|
715
|
+
function flareRegionKey(observatory, beginIso) {
|
|
716
|
+
return `${observatory}|${beginIso.slice(0, 16)}`;
|
|
717
|
+
}
|
|
650
718
|
// ── SpaceWeatherService ─────────────────────────────────────────────────────
|
|
651
719
|
/** NOAA SWPC public feeds client. Initialized once; accessed via accessor. */
|
|
652
720
|
export class SpaceWeatherService {
|
|
@@ -661,60 +729,75 @@ export class SpaceWeatherService {
|
|
|
661
729
|
this.userAgent = buildUserAgent(config.mcpServerVersion);
|
|
662
730
|
}
|
|
663
731
|
// ── NOAA Scales ────────────────────────────────────────────────────────
|
|
664
|
-
/** Fetch
|
|
732
|
+
/** Fetch latest observed levels, rolling 24-hour observed maxima, and the 3-day forecast. */
|
|
665
733
|
async getNoaaScales(ctx) {
|
|
666
734
|
const path = '/products/noaa-scales.json';
|
|
667
735
|
const raw = await fetchFeed(path, ctx, this.userAgent);
|
|
668
|
-
//
|
|
669
|
-
//
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
736
|
+
// A required level slot carries a whole number 0–5 or an explicit null; anything
|
|
737
|
+
// else is a shape break, not an unissued level.
|
|
738
|
+
const requiredScale = (value, key, category) => {
|
|
739
|
+
if (value === null)
|
|
740
|
+
return null;
|
|
741
|
+
const level = scaleLevel(value);
|
|
742
|
+
if (level !== null)
|
|
743
|
+
return level;
|
|
744
|
+
const problem = value === undefined ? 'a missing' : 'an invalid';
|
|
745
|
+
throw feedFailure('feed_moved', `SWPC scales feed has ${problem} ${category} Scale field in key "${key}".`, path);
|
|
746
|
+
};
|
|
747
|
+
/** A descriptor belongs to an issued level; an unissued one carries none. */
|
|
748
|
+
const descriptor = (scale, entry) => scale === null ? null : (entry?.Text ?? null);
|
|
749
|
+
const normalizePeriod = (r, key) => {
|
|
750
|
+
// Key "0" and forecast G levels are required. Key "-1" and forecast R/S levels are
|
|
751
|
+
// supplementary: one that fails the level check is unissued and never fails the call.
|
|
752
|
+
const g = key === undefined ? scaleLevel(r.G?.Scale) : requiredScale(r.G?.Scale, key, 'G');
|
|
753
|
+
const rLevel = key === '0' ? requiredScale(r.R?.Scale, key, 'R') : scaleLevel(r.R?.Scale);
|
|
754
|
+
const s = key === '0' ? requiredScale(r.S?.Scale, key, 'S') : scaleLevel(r.S?.Scale);
|
|
755
|
+
return {
|
|
756
|
+
date: r.DateStamp ?? '',
|
|
757
|
+
time: r.TimeStamp ?? '',
|
|
758
|
+
// The feed splits the period stamp across two Z-less fields; joined they are the
|
|
759
|
+
// space-separated shape normalizeSwpcTime() exists for, and every other
|
|
760
|
+
// timestamp this service emits is explicit ISO 8601 UTC.
|
|
761
|
+
observedAt: r.DateStamp && r.TimeStamp ? normalizeSwpcTime(`${r.DateStamp} ${r.TimeStamp}`) : '',
|
|
762
|
+
G: {
|
|
763
|
+
category: 'G',
|
|
764
|
+
scale: g,
|
|
765
|
+
text: descriptor(g, r.G),
|
|
766
|
+
minorProb: parseNum(r.G?.Prob),
|
|
767
|
+
majorProb: null,
|
|
768
|
+
},
|
|
769
|
+
R: {
|
|
770
|
+
category: 'R',
|
|
771
|
+
scale: rLevel,
|
|
772
|
+
text: descriptor(rLevel, r.R),
|
|
773
|
+
minorProb: parseNum(r.R?.MinorProb),
|
|
774
|
+
majorProb: parseNum(r.R?.MajorProb),
|
|
775
|
+
},
|
|
776
|
+
S: {
|
|
777
|
+
category: 'S',
|
|
778
|
+
scale: s,
|
|
779
|
+
text: descriptor(s, r.S),
|
|
780
|
+
minorProb: parseNum(r.S?.Prob),
|
|
781
|
+
majorProb: null,
|
|
782
|
+
},
|
|
783
|
+
};
|
|
784
|
+
};
|
|
701
785
|
const today = raw['0'];
|
|
702
786
|
// The feed answered, but not with the shape it is documented to have — the same
|
|
703
787
|
// class of break as a path that no longer resolves, and equally unfixable by a retry.
|
|
704
788
|
if (!today)
|
|
705
|
-
throw feedFailure('feed_moved', 'SWPC scales feed missing key "0" (
|
|
789
|
+
throw feedFailure('feed_moved', 'SWPC scales feed missing key "0" (latest observed).', path, {
|
|
706
790
|
available: Object.keys(raw),
|
|
707
791
|
});
|
|
708
|
-
// Key "-1" is
|
|
709
|
-
// day takes nothing away from today or the forecast.
|
|
792
|
+
// Key "-1" is supplemental and may be absent.
|
|
710
793
|
const yesterday = raw['-1'];
|
|
711
794
|
return {
|
|
712
795
|
yesterday: yesterday ? normalizePeriod(yesterday) : null,
|
|
713
|
-
today: normalizePeriod(today),
|
|
714
|
-
forecast: ['1', '2', '3']
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
796
|
+
today: normalizePeriod(today, '0'),
|
|
797
|
+
forecast: ['1', '2', '3'].flatMap((key) => {
|
|
798
|
+
const period = raw[key];
|
|
799
|
+
return period == null ? [] : [normalizePeriod(period, key)];
|
|
800
|
+
}),
|
|
718
801
|
};
|
|
719
802
|
}
|
|
720
803
|
// ── Forecast Discussion ─────────────────────────────────────────────────
|
|
@@ -740,16 +823,18 @@ export class SpaceWeatherService {
|
|
|
740
823
|
async getKpObserved(ctx) {
|
|
741
824
|
const raw = await fetchFeed('/products/noaa-planetary-k-index.json', ctx, this.userAgent);
|
|
742
825
|
return raw.map((r) => {
|
|
743
|
-
const
|
|
744
|
-
const gScale = kpToGScale(kp);
|
|
745
|
-
return {
|
|
826
|
+
const bin = {
|
|
746
827
|
timeTag: normalizeSwpcTime(r.time_tag),
|
|
747
|
-
kp,
|
|
748
|
-
gScale,
|
|
749
|
-
auroraLatitude: gScaleToAuroraLatitude(gScale),
|
|
750
828
|
aRunning: parseNum(r.a_running),
|
|
751
829
|
stationCount: parseNum(r.station_count),
|
|
752
830
|
};
|
|
831
|
+
// A bin without a value stays a bin without a value: reading it as Kp 0 would
|
|
832
|
+
// report a measured quiet G0 for an interval nothing was measured in.
|
|
833
|
+
const kp = parseNum(r.Kp);
|
|
834
|
+
if (kp === null)
|
|
835
|
+
return { ...bin, kp: null, gScale: null, auroraLatitude: null };
|
|
836
|
+
const gScale = kpToGScale(kp);
|
|
837
|
+
return { ...bin, kp, gScale, auroraLatitude: gScaleToAuroraLatitude(gScale) };
|
|
753
838
|
});
|
|
754
839
|
}
|
|
755
840
|
/** Fetch Kp 3-day forecast. */
|
|
@@ -757,7 +842,7 @@ export class SpaceWeatherService {
|
|
|
757
842
|
const raw = await fetchFeed('/products/noaa-planetary-k-index-forecast.json', ctx, this.userAgent);
|
|
758
843
|
return raw.map((r) => ({
|
|
759
844
|
timeTag: normalizeSwpcTime(r.time_tag),
|
|
760
|
-
kp: parseNum(r.kp)
|
|
845
|
+
kp: parseNum(r.kp),
|
|
761
846
|
observed: r.observed,
|
|
762
847
|
noaaScale: r.noaa_scale ?? null,
|
|
763
848
|
}));
|
|
@@ -830,17 +915,19 @@ export class SpaceWeatherService {
|
|
|
830
915
|
* feed — same seven keys, same two energy channels, same 1-minute cadence, same
|
|
831
916
|
* newest time tag — so the past-hour slice its only caller takes is the same 61
|
|
832
917
|
* records either way, for ~4.36 MB less per call.
|
|
918
|
+
*
|
|
919
|
+
* A reading without a finite flux (null, a repaired `NaN`, or the fill value) is
|
|
920
|
+
* dropped rather than passed on: it has no class to report, and the series' last
|
|
921
|
+
* element is read as the latest reading. Zero and negative readings are real and stay.
|
|
833
922
|
*/
|
|
834
923
|
async getXrayFlux(ctx) {
|
|
835
924
|
const raw = await fetchFeed('/json/goes/primary/xrays-6-hour.json', ctx, this.userAgent);
|
|
836
|
-
return raw
|
|
837
|
-
|
|
838
|
-
.
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
energy: r.energy,
|
|
843
|
-
}));
|
|
925
|
+
return raw.flatMap((r) => {
|
|
926
|
+
const fluxWm2 = parseNum(r.flux);
|
|
927
|
+
return r.energy === '0.1-0.8nm' && fluxWm2 !== null
|
|
928
|
+
? [{ timeTag: r.time_tag, satellite: r.satellite, fluxWm2, energy: r.energy }]
|
|
929
|
+
: [];
|
|
930
|
+
});
|
|
844
931
|
}
|
|
845
932
|
/**
|
|
846
933
|
* Fetch discrete GOES X-ray flare events (rolling 7 days), oldest-first.
|
|
@@ -865,6 +952,76 @@ export class SpaceWeatherService {
|
|
|
865
952
|
}))
|
|
866
953
|
.sort(byIsoAscending('beginTime'));
|
|
867
954
|
}
|
|
955
|
+
/**
|
|
956
|
+
* Fetch SWPC's edited events and index their GOES X-ray (`XRA`) records for flare
|
|
957
|
+
* source-region attribution, or report why attribution is unavailable.
|
|
958
|
+
*
|
|
959
|
+
* A flare matches the record from its own satellite (`G<satellite>`) beginning in the
|
|
960
|
+
* same UTC minute, never another satellite's: GOES-19 records an onset a minute or two
|
|
961
|
+
* off GOES-18 on about a third of flares, so a minute-only key would pair the wrong
|
|
962
|
+
* record. Records sharing a key that name different regions read as unassigned.
|
|
963
|
+
*
|
|
964
|
+
* This is an opt-in side lookup, so it rejects only on caller cancellation. Every other
|
|
965
|
+
* failure resolves as `unavailable` with a logged warning, so the caller keeps its
|
|
966
|
+
* primary answer: `feed_unavailable` or `feed_moved` from the shared funnel, and
|
|
967
|
+
* `feed_moved` for a body not in the documented shape. The shape check runs out here,
|
|
968
|
+
* after the funnel, so a break no retry can fix costs one attempt.
|
|
969
|
+
*/
|
|
970
|
+
async getFlareSourceRegions(ctx) {
|
|
971
|
+
const path = '/json/edited_events.json';
|
|
972
|
+
let body;
|
|
973
|
+
try {
|
|
974
|
+
body = await fetchFeed(path, ctx, this.userAgent);
|
|
975
|
+
}
|
|
976
|
+
catch (error) {
|
|
977
|
+
// The caller withdrew the request: a cancellation, never a degraded answer.
|
|
978
|
+
if (ctx.signal.aborted)
|
|
979
|
+
throw error;
|
|
980
|
+
const reason = error instanceof McpError && error.data?.reason === 'feed_moved'
|
|
981
|
+
? 'feed_moved'
|
|
982
|
+
: 'feed_unavailable';
|
|
983
|
+
ctx.log.warning('SWPC edited events unavailable; flare source regions omitted', {
|
|
984
|
+
path,
|
|
985
|
+
reason,
|
|
986
|
+
error: error instanceof Error ? error.message : String(error),
|
|
987
|
+
});
|
|
988
|
+
return { status: 'unavailable', reason };
|
|
989
|
+
}
|
|
990
|
+
const unexpectedShape = (error) => {
|
|
991
|
+
const [issue] = error.issues;
|
|
992
|
+
ctx.log.warning('SWPC edited events not in the documented shape; flare source regions omitted', {
|
|
993
|
+
path,
|
|
994
|
+
reason: 'feed_moved',
|
|
995
|
+
issue: issue?.message,
|
|
996
|
+
at: issue?.path,
|
|
997
|
+
});
|
|
998
|
+
return { status: 'unavailable', reason: 'feed_moved' };
|
|
999
|
+
};
|
|
1000
|
+
const records = EditedEventsSchema.safeParse(body);
|
|
1001
|
+
if (!records.success)
|
|
1002
|
+
return unexpectedShape(records.error);
|
|
1003
|
+
const regionsByKey = new Map();
|
|
1004
|
+
for (const record of records.data) {
|
|
1005
|
+
if (record.type !== 'XRA')
|
|
1006
|
+
continue;
|
|
1007
|
+
const xra = EditedXraSchema.safeParse(record);
|
|
1008
|
+
if (!xra.success)
|
|
1009
|
+
return unexpectedShape(xra.error);
|
|
1010
|
+
const { observatory, begin_datetime, region } = xra.data;
|
|
1011
|
+
const key = flareRegionKey(observatory, normalizeSwpcTime(begin_datetime));
|
|
1012
|
+
regionsByKey.set(key, regionsByKey.has(key) && regionsByKey.get(key) !== region ? null : region);
|
|
1013
|
+
}
|
|
1014
|
+
return {
|
|
1015
|
+
status: 'available',
|
|
1016
|
+
regionOf: ({ satellite, beginTime }) => {
|
|
1017
|
+
const key = flareRegionKey(`G${satellite}`, beginTime);
|
|
1018
|
+
if (!regionsByKey.has(key))
|
|
1019
|
+
return { status: 'not_yet_edited' };
|
|
1020
|
+
const region = regionsByKey.get(key);
|
|
1021
|
+
return region == null ? { status: 'unassigned' } : { status: 'assigned', region };
|
|
1022
|
+
},
|
|
1023
|
+
};
|
|
1024
|
+
}
|
|
868
1025
|
/**
|
|
869
1026
|
* Fetch the latest daily F10.7 solar radio flux report, or null when the feed
|
|
870
1027
|
* carries no Noon record.
|
|
@@ -921,10 +1078,11 @@ export class SpaceWeatherService {
|
|
|
921
1078
|
mFlareCount: r.m_xray_events,
|
|
922
1079
|
xFlareCount: r.x_xray_events,
|
|
923
1080
|
firstObserved: normalizeSwpcTime(r.first_date),
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
1081
|
+
// A probability SWPC left unpublished stays null: a 0 would read as a forecast.
|
|
1082
|
+
cFlareProbability: parseNum(r.c_flare_probability),
|
|
1083
|
+
mFlareProbability: parseNum(r.m_flare_probability),
|
|
1084
|
+
xFlareProbability: parseNum(r.x_flare_probability),
|
|
1085
|
+
protonProbability: parseNum(r.proton_probability),
|
|
928
1086
|
};
|
|
929
1087
|
});
|
|
930
1088
|
}
|
|
@@ -951,10 +1109,12 @@ export class SpaceWeatherService {
|
|
|
951
1109
|
const suffix = dayOffset === 0 ? '1_day' : dayOffset === 1 ? '2_day' : '3_day';
|
|
952
1110
|
// Parse each probability once, then expose it under both the legacy
|
|
953
1111
|
// date-specific name and the date-neutral alias (#16) so the two never drift.
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
const
|
|
957
|
-
const
|
|
1112
|
+
// A value SWPC did not publish — null, or a day column the entry omits — stays
|
|
1113
|
+
// null under both names: a 0 would read as a forecaster's assessment.
|
|
1114
|
+
const cClass = parseNum(latest[`c_class_${suffix}`]);
|
|
1115
|
+
const mClass = parseNum(latest[`m_class_${suffix}`]);
|
|
1116
|
+
const xClass = parseNum(latest[`x_class_${suffix}`]);
|
|
1117
|
+
const protons = parseNum(latest[`10mev_protons_${suffix}`]);
|
|
958
1118
|
return {
|
|
959
1119
|
date: date.toISOString(),
|
|
960
1120
|
cClass1Day: cClass,
|
|
@@ -968,17 +1128,21 @@ export class SpaceWeatherService {
|
|
|
968
1128
|
};
|
|
969
1129
|
});
|
|
970
1130
|
}
|
|
971
|
-
/**
|
|
1131
|
+
/**
|
|
1132
|
+
* Fetch GOES integral proton flux (3-day, ≥10 MeV channel).
|
|
1133
|
+
*
|
|
1134
|
+
* A record without a finite flux (null, a repaired `NaN`, or the fill value) is
|
|
1135
|
+
* dropped, so the series' last element — the reading the S level is classified
|
|
1136
|
+
* from — is the newest one SWPC actually measured, however old.
|
|
1137
|
+
*/
|
|
972
1138
|
async getProtonFlux(ctx) {
|
|
973
1139
|
const raw = await fetchFeed('/json/goes/primary/integral-protons-plot-3-day.json', ctx, this.userAgent);
|
|
974
|
-
return raw
|
|
975
|
-
|
|
976
|
-
.
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
energy: r.energy,
|
|
981
|
-
}));
|
|
1140
|
+
return raw.flatMap((r) => {
|
|
1141
|
+
const fluxPfu = parseNum(r.flux);
|
|
1142
|
+
return r.energy === '>=10 MeV' && fluxPfu !== null
|
|
1143
|
+
? [{ timeTag: r.time_tag, satellite: r.satellite, fluxPfu, energy: r.energy }]
|
|
1144
|
+
: [];
|
|
1145
|
+
});
|
|
982
1146
|
}
|
|
983
1147
|
// ── Alerts ──────────────────────────────────────────────────────────────
|
|
984
1148
|
/** Fetch SWPC alerts, watches, and warnings. */
|
|
@@ -1014,6 +1178,7 @@ export class SpaceWeatherService {
|
|
|
1014
1178
|
serialNumber: message.match(SERIAL_RE)?.[1] ?? null,
|
|
1015
1179
|
cancelsSerialNumber: message.match(CANCEL_SERIAL_RE)?.[1] ?? null,
|
|
1016
1180
|
cancelsOriginalIssueDatetime: parseValidity(message, ORIGINAL_ISSUE_TIME_RE),
|
|
1181
|
+
continuesSerialNumber: parseContinuesSerial(message),
|
|
1017
1182
|
supersedes: SUPERSEDES_RE.test(message),
|
|
1018
1183
|
issueDatetime,
|
|
1019
1184
|
message,
|