Repository navigation
Add localhost reverse proxy for LLM gateway - #5035
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #5035 +/- ##
==========================================
- Coverage 69.81% 69.75% -0.07%
==========================================
Files 560 561 +1
Lines 56488 56606 +118
==========================================
+ Hits 39439 39486 +47
- Misses 14030 14083 +53
- Partials 3019 3037 +18 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
52dcb1f to
44162cb
Compare
There was a problem hiding this comment.
Pull request overview
Adds an LLM localhost reverse proxy and daemon lifecycle to bridge static-key AI tools (e.g., Cursor) to an OIDC-protected LLM gateway.
Changes:
- Introduces
pkg/llm/proxyreverse proxy that strips incomingAuthorization, injects fresh OIDC tokens, and forwards to the gateway (including SSE passthrough). - Adds daemon management (PID file, start detached, stop) with Unix/Windows process handling and log redirection.
- Wires new CLI commands:
thv llm proxy start [--foreground]andthv llm proxy stop.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/llm/proxy/proxy.go | Reverse proxy implementation + loopback guard + graceful shutdown. |
| pkg/llm/proxy/proxy_test.go | Unit tests for loopback guard, auth injection/stripping, forwarding behavior, SSE/error passthrough. |
| pkg/llm/proxy/daemon.go | PID/log file paths, running detection, detached start, stop behavior. |
| pkg/llm/proxy/daemon_unix.go | Unix process liveness + SysProcAttr for detaching. |
| pkg/llm/proxy/daemon_windows.go | Windows process liveness + SysProcAttr for detaching. |
| pkg/llm/proxy/daemon_test.go | Unit tests for PID-file lifecycle and guardrails. |
| cmd/thv/app/llm.go | CLI wiring for starting/stopping proxy (foreground/background). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
f38837e to
d778f95
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
JAORMX
left a comment
There was a problem hiding this comment.
Solid work on the scope trim since the last push. Dropping the daemon lifecycle and running the proxy in the foreground only was the right call. Requesting changes on two axes.
Reuse. The whole reason we're building this inside ToolHive and not as a standalone binary is that we already have the pieces. RFC-0070 §Alternatives Considered is explicit about this. So when we ship a second copy of something that already exists, the RFC's own argument for why this lives here starts to wobble. Two spots in the current diff:
checkLoopbackinproxy.go:78reinvents a narrow version of helpers already inpkg/networking/utilities.go(IsPrivateIP,AddressReferencesPrivateIp,IsLocalhost). RFC-0070 §Validation explicitly namedValidateLoopbackAddressas the shared helper. It never got added.- TokenSource construction is built twice in
cmd/thv/app/llm.go:runLLMToken(lines 211-243) andrunLLMProxyForeground(lines 335-360) have identicalGetSystemSecretsProvider → NewScopedProvider(ScopeLLM) → updater → NewTokenSourcepipelines, with a copy-pastedupdaterclosure.thv llm setupwill be the third caller when it lands.
Easier to consolidate now. Two callers today, three inevitable.
RFC compliance. Two gaps that matter:
- Token-fetch failure now distinguishes
ErrTokenRequired(good), but still returns502and writeserr.Error()into the response body. RFC §Proxy is explicit:401+ OpenAI-compatible JSON schema. Without this, the daemon silently stops working days later (rotated refresh token) and Cursor users see "Bad Gateway" with zero path to recovery. Writingerr.Error()into the body also leaks internal error detail to whatever is on the other end. - No
Host-header allowlist. DNS rebinding turns the proxy into an auth oracle for browser JS. Four-line fix, directly in RFC §Threat Model. - Minor:
/healthzendpoint from RFC §Proxy isn't implemented. RFC also specifiedpkg/llm/internal/proxy.go; PR uses non-internalpkg/llm/proxy/. Either fix the layout or update the RFC.
Housekeeping. A few meta items:
- The PR is behind main (
mergeStateStatus: BEHIND, merge-base is854e0ab7, main is at68f4c2f1). Please rebase onto current main before merge so the diff view stops showing phantom deletions ofpkg/llm/manage.go,pkg/oidc/*, andtest/e2e/cli_llm_config_test.gofrom #5049. - Please squash the two "changes from review" fixups into the original commit.
- Per
.claude/rules/pr-creation.md, optional PR sections should be filled in or removed. The empty<details>block, the emptyChangestable row, and the unusedDoes this introduce a user-facing change?section all want deletion.
Happy to re-review once the blockers land.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ec3b77e to
3862722
Compare
Implements a background daemon that bridges AI tools using static API keys (e.g. Cursor) to an OIDC-protected LLM gateway. New pkg/llm/proxy package: - proxy.go: httputil.ReverseProxy wrapper that strips incoming Authorization headers, injects fresh OIDC tokens, and forwards to the configured gateway. Binds listener in New() so Addr() is valid before Start(). Loopback guard rejects non-localhost listen addrs. SSE streaming works without buffering (FlushInterval: -1). Graceful 5-second shutdown on context cancellation. - daemon.go: PID-file lifecycle (IsRunning, Stop, StartDetached). StartDetached forks the current binary with TOOLHIVE_DETACHED=1, redirecting stdout/stderr to the XDG log file. - daemon_unix.go / daemon_windows.go: platform-specific processAlive and SysProcAttr helpers via build tags. CLI wiring (cmd/thv/app/llm.go): - thv llm proxy start [--foreground]: background by default, foreground with --foreground or when running as detached child. - thv llm proxy stop: sends SIGTERM via process.KillProcess and removes PID file. Closes: #5029
3862722 to
f408829
Compare
f408829 to
f7d71bd
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
f7d71bd to
54a617c
Compare
Summary
Implements a localhost reverse proxy that bridges AI tools using static API keys (e.g. Cursor) to an OIDC-protected LLM gateway.
New `pkg/llm/proxy` package:
CLI wiring (`cmd/thv/app/llm.go`):
No daemon/PID-file machinery — the proxy lifecycle follows the same foreground pattern as `thv proxy stdio` and `thv proxy tunnel`.
Fixes #5029
Type of change
Test plan
API Compatibility
Changes
Does this introduce a user-facing change?
Yes. `thv llm proxy start` starts a localhost reverse proxy on `127.0.0.1:14000` (configurable) that transparently injects OIDC tokens for AI tools configured to use a static API key endpoint.
Special notes for reviewers
The proxy intentionally has no `stop` subcommand or PID-file daemon — this matches how all other ToolHive proxy commands work (`thv proxy stdio`, `thv proxy tunnel`). Users signal termination with Ctrl+C or their process manager.