UNPKG

@upstash/context7-mcp

Version:
42 lines (41 loc) • 1.63 kB
import { classifyAuthMethod, classifyOAuthClientRegistration } from "./mcp-http-auth.js"; import { mcpRouteFromUrl } from "./mcp-route.js"; import { recordAuthenticationEvent, recordToolCallOutcome } from "./telemetry-runtime.js"; function containsJsonRpcMethod(body, method) { const messages = Array.isArray(body) ? body : [body]; return messages.some((message) => typeof message === "object" && message !== null && "method" in message && message.method === method); } export function observeCredentialedInitialize(response, body, context) { if (context.method === "none" || !containsJsonRpcMethod(body, "initialize")) return; response.once("finish", () => { recordAuthenticationEvent({ ...context, event: response.statusCode >= 200 && response.statusCode < 300 ? "credentialed_initialize_succeeded" : "credentialed_initialize_failed", }); }); } export function recordToolCallTelemetry(ctx, outcome) { recordToolCallOutcome(outcome); if (ctx.transport !== "http" || !ctx.apiKey || !ctx.mcpAuthMode || !ctx.mcpEndpoint || outcome === "error") { return; } const oauthClientRegistration = classifyOAuthClientRegistration(ctx.apiKey); recordAuthenticationEvent({ enforcementMode: ctx.mcpAuthMode, event: "authenticated_tool_call", method: classifyAuthMethod(ctx.apiKey), ...(oauthClientRegistration && { oauthClientRegistration }), outcome: "accepted", route: mcpRouteFromUrl(ctx.mcpEndpoint), }); }