MCPcopy
hub / github.com/claude-code-best/claude-code / configureGlobalAgents

Function configureGlobalAgents

src/utils/proxy.ts:327–388  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

325let proxyInterceptorId: number | undefined
326
327export function configureGlobalAgents(): void {
328 const proxyUrl = getProxyUrl()
329 const mtlsAgent = getMTLSAgent()
330
331 // Eject previous interceptor to avoid stacking on repeated calls
332 if (proxyInterceptorId !== undefined) {
333 axios.interceptors.request.eject(proxyInterceptorId)
334 proxyInterceptorId = undefined
335 }
336
337 // Reset proxy-related defaults so reconfiguration is clean
338 axios.defaults.proxy = undefined
339 axios.defaults.httpAgent = undefined
340 axios.defaults.httpsAgent = undefined
341
342 if (proxyUrl) {
343 // workaround for https://github.com/axios/axios/issues/4531
344 axios.defaults.proxy = false
345
346 // Create proxy agent with mTLS options if available
347 const proxyAgent = createHttpsProxyAgent(proxyUrl)
348
349 // Add axios request interceptor to handle NO_PROXY
350 proxyInterceptorId = axios.interceptors.request.use(config => {
351 // Check if URL should bypass proxy based on NO_PROXY
352 if (config.url && shouldBypassProxy(config.url)) {
353 // Bypass proxy - use mTLS agent if configured, otherwise undefined
354 if (mtlsAgent) {
355 config.httpsAgent = mtlsAgent
356 config.httpAgent = mtlsAgent
357 } else {
358 // Remove any proxy agents to use direct connection
359 delete config.httpsAgent
360 delete config.httpAgent
361 }
362 } else {
363 // Use proxy agent
364 config.httpsAgent = proxyAgent
365 config.httpAgent = proxyAgent
366 }
367 return config
368 })
369
370 // Set global dispatcher that now respects NO_PROXY via EnvHttpProxyAgent
371 // eslint-disable-next-line @typescript-eslint/no-require-imports
372 ;(require('undici') as typeof undici).setGlobalDispatcher(
373 getProxyAgent(proxyUrl),
374 )
375 } else if (mtlsAgent) {
376 // No proxy but mTLS is configured
377 axios.defaults.httpsAgent = mtlsAgent
378
379 // Set undici global dispatcher with mTLS
380 const mtlsOptions = getTLSFetchOptions()
381 if (mtlsOptions.dispatcher) {
382 // eslint-disable-next-line @typescript-eslint/no-require-imports
383 ;(require('undici') as typeof undici).setGlobalDispatcher(
384 mtlsOptions.dispatcher,

Callers 2

init.tsFile · 0.85

Calls 4

getProxyUrlFunction · 0.85
createHttpsProxyAgentFunction · 0.85
shouldBypassProxyFunction · 0.85
getTLSFetchOptionsFunction · 0.85

Tested by

no test coverage detected