|
| 1 | +import { ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 2 | +import type { |
| 3 | + McpServer, |
| 4 | + ReadResourceTemplateCallback, |
| 5 | +} from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 6 | +import { ROLLBAR_API_BASE } from "../config.js"; |
| 7 | +import { makeRollbarRequest } from "../utils/api.js"; |
| 8 | +import { RollbarApiResponse } from "../types/index.js"; |
| 9 | + |
| 10 | +const REPLAY_URI_TEMPLATE = |
| 11 | + "rollbar://replay/{environment}/{sessionId}/{replayId}"; |
| 12 | +const REPLAY_RESOURCE_NAME = "rollbar-session-replay"; |
| 13 | +const REPLAY_RESOURCE_TITLE = "Rollbar Session Replay"; |
| 14 | +const REPLAY_MIME_TYPE = "application/json"; |
| 15 | +const CACHE_TTL_MS = 5 * 60 * 1000; |
| 16 | + |
| 17 | +type ReplayCacheEntry = { |
| 18 | + data: unknown; |
| 19 | + expiresAt: number; |
| 20 | +}; |
| 21 | + |
| 22 | +const replayCache = new Map<string, ReplayCacheEntry>(); |
| 23 | +const registeredServers = new WeakSet<McpServer>(); |
| 24 | + |
| 25 | +function normalizeTemplateVariable( |
| 26 | + value: string | string[] | undefined, |
| 27 | +): string { |
| 28 | + if (Array.isArray(value)) { |
| 29 | + return value[0] ?? ""; |
| 30 | + } |
| 31 | + |
| 32 | + return value ?? ""; |
| 33 | +} |
| 34 | + |
| 35 | +function buildReplayApiUrl( |
| 36 | + environment: string, |
| 37 | + sessionId: string, |
| 38 | + replayId: string, |
| 39 | +): string { |
| 40 | + return `${ROLLBAR_API_BASE}/environment/${encodeURIComponent( |
| 41 | + environment, |
| 42 | + )}/session/${encodeURIComponent(sessionId)}/replay/${encodeURIComponent( |
| 43 | + replayId, |
| 44 | + )}`; |
| 45 | +} |
| 46 | + |
| 47 | +export function buildReplayResourceUri( |
| 48 | + environment: string, |
| 49 | + sessionId: string, |
| 50 | + replayId: string, |
| 51 | +): string { |
| 52 | + return `rollbar://replay/${encodeURIComponent( |
| 53 | + environment, |
| 54 | + )}/${encodeURIComponent(sessionId)}/${encodeURIComponent(replayId)}`; |
| 55 | +} |
| 56 | + |
| 57 | +export function cacheReplayData(uri: string, data: unknown) { |
| 58 | + replayCache.set(uri, { data, expiresAt: Date.now() + CACHE_TTL_MS }); |
| 59 | +} |
| 60 | + |
| 61 | +function getCachedReplayData(uri: string) { |
| 62 | + const cached = replayCache.get(uri); |
| 63 | + if (!cached) { |
| 64 | + return undefined; |
| 65 | + } |
| 66 | + |
| 67 | + if (cached.expiresAt < Date.now()) { |
| 68 | + replayCache.delete(uri); |
| 69 | + return undefined; |
| 70 | + } |
| 71 | + |
| 72 | + return cached.data; |
| 73 | +} |
| 74 | + |
| 75 | +export async function fetchReplayData( |
| 76 | + environment: string, |
| 77 | + sessionId: string, |
| 78 | + replayId: string, |
| 79 | +): Promise<unknown> { |
| 80 | + const replayUrl = buildReplayApiUrl(environment, sessionId, replayId); |
| 81 | + |
| 82 | + const replayResponse = await makeRollbarRequest<RollbarApiResponse<unknown>>( |
| 83 | + replayUrl, |
| 84 | + "get-replay", |
| 85 | + ); |
| 86 | + |
| 87 | + if (replayResponse.err !== 0) { |
| 88 | + const errorMessage = |
| 89 | + replayResponse.message || `Unknown error (code: ${replayResponse.err})`; |
| 90 | + throw new Error(`Rollbar API returned error: ${errorMessage}`); |
| 91 | + } |
| 92 | + |
| 93 | + return replayResponse.result; |
| 94 | +} |
| 95 | + |
| 96 | +const readReplayResource: ReadResourceTemplateCallback = async ( |
| 97 | + uri, |
| 98 | + variables, |
| 99 | +) => { |
| 100 | + const environmentValue = normalizeTemplateVariable(variables.environment); |
| 101 | + const sessionValue = normalizeTemplateVariable(variables.sessionId); |
| 102 | + const replayValue = normalizeTemplateVariable(variables.replayId); |
| 103 | + |
| 104 | + const environment = environmentValue |
| 105 | + ? decodeURIComponent(environmentValue) |
| 106 | + : ""; |
| 107 | + const sessionId = sessionValue ? decodeURIComponent(sessionValue) : ""; |
| 108 | + const replayId = replayValue ? decodeURIComponent(replayValue) : ""; |
| 109 | + |
| 110 | + if (!environment || !sessionId || !replayId) { |
| 111 | + throw new Error("Invalid replay resource URI"); |
| 112 | + } |
| 113 | + |
| 114 | + const resourceUri = buildReplayResourceUri(environment, sessionId, replayId); |
| 115 | + const cached = getCachedReplayData(resourceUri); |
| 116 | + |
| 117 | + const replayData = |
| 118 | + cached !== undefined |
| 119 | + ? cached |
| 120 | + : await fetchReplayData(environment, sessionId, replayId); |
| 121 | + |
| 122 | + if (cached === undefined) { |
| 123 | + cacheReplayData(resourceUri, replayData); |
| 124 | + } |
| 125 | + |
| 126 | + return { |
| 127 | + contents: [ |
| 128 | + { |
| 129 | + uri: uri.toString(), |
| 130 | + mimeType: REPLAY_MIME_TYPE, |
| 131 | + text: JSON.stringify(replayData), |
| 132 | + }, |
| 133 | + ], |
| 134 | + }; |
| 135 | +}; |
| 136 | + |
| 137 | +export function registerReplayResource(server: McpServer) { |
| 138 | + if (registeredServers.has(server)) { |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + const template = new ResourceTemplate(REPLAY_URI_TEMPLATE, { |
| 143 | + list: () => ({ resources: [] }), |
| 144 | + }); |
| 145 | + |
| 146 | + server.resource( |
| 147 | + REPLAY_RESOURCE_NAME, |
| 148 | + template, |
| 149 | + { |
| 150 | + title: REPLAY_RESOURCE_TITLE, |
| 151 | + description: |
| 152 | + "Session replay payloads returned from the Rollbar Replay API.", |
| 153 | + mimeType: REPLAY_MIME_TYPE, |
| 154 | + }, |
| 155 | + readReplayResource, |
| 156 | + ); |
| 157 | + |
| 158 | + registeredServers.add(server); |
| 159 | +} |
0 commit comments