(mcpUrl: string, runDir: string)
| 49 | }; |
| 50 | |
| 51 | const installTraceparentFetch = (mcpUrl: string, runDir: string): void => { |
| 52 | traceSink = { mcpUrl, runDir }; |
| 53 | if (traceFetchInstalled) return; |
| 54 | traceFetchInstalled = true; |
| 55 | const original = globalThis.fetch; |
| 56 | globalThis.fetch = async (input, init) => { |
| 57 | const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url; |
| 58 | const method = ( |
| 59 | init?.method ?? (input instanceof Request ? input.method : "GET") |
| 60 | ).toUpperCase(); |
| 61 | const sink = traceSink; |
| 62 | if (!sink || method !== "POST" || !url.startsWith(sink.mcpUrl)) { |
| 63 | return original(input, init); |
| 64 | } |
| 65 | const traceId = randomBytes(16).toString("hex"); |
| 66 | const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : {})); |
| 67 | headers.set("traceparent", `00-${traceId}-${randomBytes(8).toString("hex")}-01`); |
| 68 | const at = Date.now(); |
| 69 | const finish = (status?: number) => |
| 70 | appendTraces(sink.runDir, [ |
| 71 | { |
| 72 | id: traceId, |
| 73 | at, |
| 74 | url, |
| 75 | ms: Date.now() - at, |
| 76 | ...(status === undefined ? {} : { status }), |
| 77 | source: "terminal" as const, |
| 78 | label: rpcLabel(init?.body), |
| 79 | }, |
| 80 | ]); |
| 81 | try { |
| 82 | const response = await original(input, { ...init, headers }); |
| 83 | finish(response.status); |
| 84 | return response; |
| 85 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- fetch adapter boundary |
| 86 | } catch (error) { |
| 87 | finish(); |
| 88 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- fetch adapter boundary |
| 89 | throw error; |
| 90 | } |
| 91 | }; |
| 92 | }; |
| 93 | |
| 94 | export interface McpCallResult { |
| 95 | readonly raw: unknown; |
no test coverage detected