File size: 9,342 Bytes
3464008
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { BillingDenialError, throwIfBillingDenial } from './billing-denial';
import { emitTelemetry } from './telemetry';
import type {
  McpAuthContext,
  McpToolExecutionContext,
} from './types';

export const MCP_CANONICAL_API_ORIGIN = 'https://api.worldmonitor.app';

const VARIANT_HOSTS: ReadonlySet<string> = new Set([
  'tech.worldmonitor.app',
  'finance.worldmonitor.app',
  'commodity.worldmonitor.app',
  'happy.worldmonitor.app',
  'energy.worldmonitor.app',
]);

const SAFE_GATEWAY_ERROR_CODES: ReadonlySet<string> = new Set([
  'invalid_internal_mcp_signature',
  'internal_mcp_replay_cache_unavailable',
  'insufficient_entitlement',
  'entitlement_verification_unavailable',
  'subscription_lapsed',
  'renewal_verification_pending',
  'renewal_verification_failed',
  'payload_too_large',
  'rate_limited',
]);

const SAFE_GATEWAY_ERROR_MESSAGES: ReadonlyMap<string, string> = new Map([
  ['invalid api key', 'invalid_api_key'],
  ['invalid or expired session', 'invalid_session'],
  ['api access requires an active subscription', 'api_subscription_required'],
  ['pro subscription required', 'pro_subscription_required'],
  ['unable to verify api access', 'entitlement_verification_unavailable'],
  ['method not allowed', 'method_not_allowed'],
  ['configuration', 'configuration'],
]);

type ToolFetchResponse = {
  ok: boolean;
  status: number;
  headers?: { get(name: string): string | null };
  body?: ReadableStream<Uint8Array> | null;
  text?: () => Promise<string>;
};

type DownstreamResponseMarker =
  | 'json'
  | 'html'
  | 'other'
  | 'json_error'
  | 'html_error'
  | 'empty_error'
  | 'method_not_allowed'
  | 'billing_verification';

export class ToolFetchError extends Error {
  readonly operation: string;
  readonly status: number;
  readonly safeCode: string;
  readonly responseMarker: DownstreamResponseMarker;

  constructor(
    operation: string,
    status: number,
    safeCode: string,
    responseMarker: DownstreamResponseMarker,
  ) {
    super(`${operation} HTTP ${status}: ${safeCode}`);
    this.name = 'ToolFetchError';
    this.operation = operation;
    this.status = status;
    this.safeCode = safeCode;
    this.responseMarker = responseMarker;
  }
}

type DownstreamObservation = {
  operation: string;
  tool: string;
  auth: McpAuthContext;
  execution?: McpToolExecutionContext;
};

function classifyMcpInboundHost(hostname: string): McpToolExecutionContext['inboundHostClass'] {
  hostname = hostname.toLowerCase();
  if (hostname === 'api.worldmonitor.app') return 'canonical_api';
  if (hostname === 'worldmonitor.app') return 'apex';
  if (hostname === 'www.worldmonitor.app') return 'www';
  if (VARIANT_HOSTS.has(hostname)) return 'variant';
  if (hostname.endsWith('.worldmonitor.app')) return 'worldmonitor_subdomain';
  if (hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1') return 'local';
  if (hostname.endsWith('.vercel.app')) return 'vercel_preview';
  return 'other';
}

export function createMcpToolExecutionContext(requestUrl: string): McpToolExecutionContext {
  const inbound = new URL(requestUrl);
  const inboundHostClass = classifyMcpInboundHost(inbound.hostname);
  const isProductionWorldMonitorHost = (
    inbound.hostname === 'worldmonitor.app'
    || inbound.hostname.endsWith('.worldmonitor.app')
  );
  const downstreamOrigin = isProductionWorldMonitorHost
    ? MCP_CANONICAL_API_ORIGIN
    : inbound.origin;
  return {
    inboundHostClass,
    downstreamOrigin,
    // Only the canonical public origin is recorded verbatim. Non-production
    // origins collapse to their bounded host class so preview names, local
    // ports, and self-hosted domains never enter telemetry.
    downstreamOriginTag: downstreamOrigin === MCP_CANONICAL_API_ORIGIN
      ? MCP_CANONICAL_API_ORIGIN
      : inboundHostClass,
  };
}

function contentType(response: ToolFetchResponse): string {
  return (response.headers?.get('Content-Type') ?? '').toLowerCase();
}

function successMarker(response: ToolFetchResponse): DownstreamResponseMarker {
  const type = contentType(response);
  if (type.includes('json')) return 'json';
  if (type.includes('html')) return 'html';
  return 'other';
}

function defaultSafeErrorCode(status: number): string {
  if (status === 401) return 'auth_rejected';
  if (status === 403) return 'forbidden';
  if (status === 405) return 'method_not_allowed';
  if (status === 429) return 'rate_limited';
  return 'upstream_http_error';
}

function safeGatewayErrorCode(value: unknown, status: number): string {
  if (typeof value !== 'string') return defaultSafeErrorCode(status);
  const normalized = value.trim().toLowerCase();
  if (SAFE_GATEWAY_ERROR_CODES.has(normalized)) return normalized;
  return SAFE_GATEWAY_ERROR_MESSAGES.get(normalized) ?? defaultSafeErrorCode(status);
}

async function readBoundedResponseText(
  response: ToolFetchResponse,
  maxBytes = 4096,
): Promise<string> {
  const reader = response.body?.getReader();
  if (!reader) {
    const text = typeof response.text === 'function'
      ? await response.text().catch(() => '')
      : '';
    return text.slice(0, maxBytes);
  }

  const decoder = new TextDecoder();
  let bytesRead = 0;
  let text = '';
  try {
    while (bytesRead < maxBytes) {
      const { done, value } = await reader.read();
      if (done || !value) break;
      const remaining = maxBytes - bytesRead;
      const chunk = value.byteLength > remaining ? value.subarray(0, remaining) : value;
      text += decoder.decode(chunk, { stream: bytesRead + chunk.byteLength < maxBytes });
      bytesRead += chunk.byteLength;
      if (chunk.byteLength < value.byteLength) break;
    }
    text += decoder.decode();
    return text;
  } catch {
    return '';
  } finally {
    await reader.cancel().catch(() => {});
  }
}

async function classifyFailure(
  response: ToolFetchResponse,
): Promise<{ errorCode: string; marker: DownstreamResponseMarker }> {
  if (response.status === 405) {
    return { errorCode: 'method_not_allowed', marker: 'method_not_allowed' };
  }

  const type = contentType(response);
  const detail = await readBoundedResponseText(response);
  if (!detail) {
    return {
      errorCode: defaultSafeErrorCode(response.status),
      marker: 'empty_error',
    };
  }

  if (type.includes('json')) {
    try {
      const parsed = JSON.parse(detail) as { code?: unknown; error?: unknown };
      return {
        errorCode: safeGatewayErrorCode(parsed.code ?? parsed.error, response.status),
        marker: 'json_error',
      };
    } catch {
      return {
        errorCode: defaultSafeErrorCode(response.status),
        marker: 'json_error',
      };
    }
  }

  return {
    errorCode: defaultSafeErrorCode(response.status),
    marker: type.includes('html') ? 'html_error' : 'other',
  };
}

function emitDownstreamTelemetry(
  tool: string,
  operation: string,
  auth: McpAuthContext,
  execution: McpToolExecutionContext | undefined,
  response: ToolFetchResponse,
  errorCode: string | null,
  responseMarker: DownstreamResponseMarker,
): void {
  if (!execution) return;
  emitTelemetry('mcp.downstream', {
    tool,
    auth_kind: auth.kind,
    inbound_host_class: execution.inboundHostClass,
    downstream_origin: execution.downstreamOriginTag,
    downstream_operation: operation,
    status: response.status,
    ok: response.ok,
    error_code: errorCode,
    response_marker: responseMarker,
  });
}

/**
 * Validate one MCP sibling fetch while recording only bounded routing/auth
 * diagnostics. Error response bodies are consumed solely to map a closed set
 * of gateway codes; raw text, unknown values, headers, URLs, and credentials
 * never leave this module.
 */
export async function assertMcpToolFetchOk(
  response: ToolFetchResponse,
  observation: DownstreamObservation,
): Promise<void> {
  const { operation, tool, auth, execution } = observation;
  if (response.ok) {
    emitDownstreamTelemetry(
      tool,
      operation,
      auth,
      execution,
      response,
      null,
      successMarker(response),
    );
    return;
  }

  try {
    throwIfBillingDenial(response, operation);
  } catch (error) {
    if (error instanceof BillingDenialError) {
      emitDownstreamTelemetry(
        tool,
        operation,
        auth,
        execution,
        response,
        error.billingCode,
        'billing_verification',
      );
    }
    throw error;
  }

  const failure = await classifyFailure(response);
  emitDownstreamTelemetry(
    tool,
    operation,
    auth,
    execution,
    response,
    failure.errorCode,
    failure.marker,
  );
  throw new ToolFetchError(
    operation,
    response.status,
    failure.errorCode,
    failure.marker,
  );
}

export function downstreamErrorTags(
  error: unknown,
): Record<string, string> {
  if (error instanceof BillingDenialError) {
    return {
      downstream_operation: error.operation,
      downstream_status: String(error.status),
      downstream_error_code: error.billingCode,
      downstream_response_marker: 'billing_verification',
    };
  }
  if (error instanceof ToolFetchError) {
    return {
      downstream_operation: error.operation,
      downstream_status: String(error.status),
      downstream_error_code: error.safeCode,
      downstream_response_marker: error.responseMarker,
    };
  }
  return {};
}