Commit 0736543
Fix transparent proxy for remote MCP servers behind redirects
Three issues prevented MCPRemoteProxy from connecting to third-party
upstream MCP servers that use HTTP redirects:
1. X-Forwarded-Host leaked the proxy's hostname to the upstream. The
upstream used it to construct 307 redirect URLs pointing back to
the proxy, creating a redirect loop. Fix: skip SetXForwarded() for
remote upstreams (isRemote == true).
2. Go's http.Transport.RoundTrip does not follow redirects, but
httputil.ReverseProxy uses Transport directly. Upstream 307/308
redirects (e.g. HTTPS→HTTP scheme changes, path canonicalization)
were returned to the MCP client which cannot follow them through
the proxy. Fix: add forwardFollowingRedirects that transparently
follows up to 10 redirects, preserving method and body for
307/308 (RFC 7538).
3. When disableUpstreamTokenInjection is true, the client's ToolHive
JWT was still forwarded to the upstream in the Authorization
header. Fix: add strip-auth middleware that removes the
Authorization header before forwarding.
Also adds debug logging for outbound request headers and upstream
response status codes to aid diagnosis of remote proxy issues.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 90b3a2a commit 0736543
3 files changed
Lines changed: 101 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| 49 | + | |
48 | 50 | | |
49 | 51 | | |
50 | 52 | | |
| |||
342 | 344 | | |
343 | 345 | | |
344 | 346 | | |
345 | | - | |
| 347 | + | |
| 348 | + | |
346 | 349 | | |
347 | | - | |
| 350 | + | |
348 | 351 | | |
349 | 352 | | |
350 | 353 | | |
| |||
402 | 405 | | |
403 | 406 | | |
404 | 407 | | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
405 | 447 | | |
406 | 448 | | |
407 | 449 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
271 | 271 | | |
272 | 272 | | |
273 | 273 | | |
| 274 | + | |
274 | 275 | | |
275 | 276 | | |
276 | 277 | | |
| |||
284 | 285 | | |
285 | 286 | | |
286 | 287 | | |
| 288 | + | |
287 | 289 | | |
288 | 290 | | |
289 | | - | |
| 291 | + | |
290 | 292 | | |
291 | 293 | | |
292 | 294 | | |
293 | 295 | | |
294 | 296 | | |
295 | | - | |
| 297 | + | |
| 298 | + | |
296 | 299 | | |
297 | 300 | | |
298 | 301 | | |
| |||
303 | 306 | | |
304 | 307 | | |
305 | 308 | | |
| 309 | + | |
306 | 310 | | |
307 | 311 | | |
308 | 312 | | |
| |||
314 | 318 | | |
315 | 319 | | |
316 | 320 | | |
| 321 | + | |
317 | 322 | | |
318 | 323 | | |
319 | 324 | | |
| |||
333 | 338 | | |
334 | 339 | | |
335 | 340 | | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
| 341 | + | |
341 | 342 | | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
350 | 355 | | |
351 | 356 | | |
352 | 357 | | |
| |||
359 | 364 | | |
360 | 365 | | |
361 | 366 | | |
| 367 | + | |
362 | 368 | | |
363 | 369 | | |
364 | 370 | | |
| |||
372 | 378 | | |
373 | 379 | | |
374 | 380 | | |
375 | | - | |
| 381 | + | |
376 | 382 | | |
377 | 383 | | |
378 | 384 | | |
379 | 385 | | |
380 | 386 | | |
381 | 387 | | |
| 388 | + | |
382 | 389 | | |
383 | 390 | | |
384 | 391 | | |
| |||
400 | 407 | | |
401 | 408 | | |
402 | 409 | | |
403 | | - | |
| 410 | + | |
| 411 | + | |
404 | 412 | | |
405 | 413 | | |
406 | 414 | | |
407 | | - | |
| 415 | + | |
408 | 416 | | |
409 | | - | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
410 | 420 | | |
411 | 421 | | |
412 | | - | |
| 422 | + | |
413 | 423 | | |
| 424 | + | |
| 425 | + | |
414 | 426 | | |
415 | 427 | | |
416 | | - | |
| 428 | + | |
417 | 429 | | |
418 | 430 | | |
419 | 431 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
522 | 522 | | |
523 | 523 | | |
524 | 524 | | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
525 | 533 | | |
526 | 534 | | |
527 | 535 | | |
| |||
611 | 619 | | |
612 | 620 | | |
613 | 621 | | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
614 | 629 | | |
615 | 630 | | |
616 | 631 | | |
| |||
1006 | 1021 | | |
1007 | 1022 | | |
1008 | 1023 | | |
1009 | | - | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
1010 | 1033 | | |
1011 | 1034 | | |
1012 | 1035 | | |
| |||
0 commit comments