(config: InspectorConfig)
| 85 | * to avoid CORS restrictions on OAuth discovery and token endpoints. |
| 86 | */ |
| 87 | export function createProxyFetch(config: InspectorConfig): typeof fetch { |
| 88 | const proxyAddress = getMCPProxyAddress(config); |
| 89 | const { token, header } = getMCPProxyAuthToken(config); |
| 90 | |
| 91 | return async ( |
| 92 | input: RequestInfo | URL, |
| 93 | init?: RequestInit, |
| 94 | ): Promise<Response> => { |
| 95 | const url = |
| 96 | typeof input === "string" |
| 97 | ? input |
| 98 | : input instanceof Request |
| 99 | ? input.url |
| 100 | : input.toString(); |
| 101 | |
| 102 | // Serialize body for JSON transport. URLSearchParams and similar don't |
| 103 | // JSON-serialize (they become {}), so we must convert to string first. |
| 104 | let serializedBody: string | undefined; |
| 105 | if (init?.body != null) { |
| 106 | if (typeof init.body === "string") { |
| 107 | serializedBody = init.body; |
| 108 | } else if (init.body instanceof URLSearchParams) { |
| 109 | serializedBody = init.body.toString(); |
| 110 | } else { |
| 111 | serializedBody = String(init.body); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | const proxyResponse = await fetch(`${proxyAddress}/fetch`, { |
| 116 | method: "POST", |
| 117 | headers: { |
| 118 | "Content-Type": "application/json", |
| 119 | [header]: `Bearer ${token}`, |
| 120 | }, |
| 121 | body: JSON.stringify({ |
| 122 | url, |
| 123 | init: { |
| 124 | method: init?.method, |
| 125 | headers: init?.headers |
| 126 | ? Object.fromEntries(new Headers(init.headers)) |
| 127 | : undefined, |
| 128 | body: serializedBody, |
| 129 | }, |
| 130 | }), |
| 131 | }); |
| 132 | |
| 133 | let data: unknown; |
| 134 | try { |
| 135 | data = await proxyResponse.json(); |
| 136 | } catch { |
| 137 | throw new Error( |
| 138 | `Proxy fetch failed: ${proxyResponse.status} ${proxyResponse.statusText}`, |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | const infraMessage = messageFromProxyInfrastructureError(data); |
| 143 | if (infraMessage !== null) { |
| 144 | throw new Error(infraMessage); |
no test coverage detected
searching dependent graphs…