Open
Conversation
The discovery package (pkg/server/discovery/) was already implemented and tested but had zero imports in the codebase. This wires it into the serve command so clients (CLI, Studio) can auto-discover a running server without hardcoded ports or environment variables. On startup, thv serve now generates a cryptographic nonce, writes a discovery file to $XDG_STATE_HOME/toolhive/server/server.json with the actual listen URL (supporting port 0 and Unix sockets), and returns the nonce via the X-Toolhive-Nonce health check header. On shutdown the file is removed. The skills client now tries discovery before falling back to the TOOLHIVE_API_URL env var or the default localhost:8080, with loopback and socket-path validation on discovered URLs. Additional fixes: SIGTERM handling in the serve command, a 30-second shutdown timeout (was unbounded), symlink rejection on the discovery file read path, directory permission tightening after MkdirAll, and constant-time nonce comparison. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Large PR Detected
This PR exceeds 1000 lines of changes and requires justification before it can be reviewed.
How to unblock this PR:
Add a section to your PR description with the following format:
## Large PR Justification
[Explain why this PR must be large, such as:]
- Generated code that cannot be split
- Large refactoring that must be atomic
- Multiple related changes that would break if separated
- Migration or data transformationAlternative:
Consider splitting this PR into smaller, focused changes (< 1000 lines each) for easier review and reduced risk.
See our Contributing Guidelines for more details.
This review will be automatically dismissed once you add the justification section.
36bc774 to
7fdf8fa
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4319 +/- ##
==========================================
- Coverage 69.19% 69.15% -0.04%
==========================================
Files 478 481 +3
Lines 48157 48379 +222
==========================================
+ Hits 33320 33455 +135
- Misses 12254 12324 +70
- Partials 2583 2600 +17 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
RFC-0034 proposes a long-running local server architecture where
thv serveadvertises itself via a discovery file so clients (CLI, Studio) can find it without hardcoded ports. The discovery package (pkg/server/discovery/) was already fully implemented and tested but had zero imports anywhere in the codebase. This PR wires it in, giving us a working discovery protocol that immediately benefits Studio and the skills CLI client.$XDG_STATE_HOME/toolhive/server/server.json) on startup with the actual listen URL, PID, and a cryptographic nonce, and removes it on shutdown/healthendpoint returns the nonce viaX-Toolhive-Nonceso clients can verify server instance identity (prevents PID-reuse confusion)TOOLHIVE_API_URLorlocalhost:8080, with loopback and socket-path validation on discovered URLsType of change
Test plan
task test)task lint-fix)Manual testing: run
thv serve, verifyserver.jsonappears with correct URL/PID/nonce,curl -v /healthshowsX-Toolhive-Nonceheader, stop server and verify file is removed, start a second server while first is running and verify it refuses.Changes
pkg/server/discovery/*.gopkg/api/server.goWithNonce()builder,ListenURL(), discovery write/remove in lifecycle, startup guard, shutdown timeoutpkg/api/v1/healthcheck.goX-Toolhive-Nonceheader on 204 responsescmd/thv/app/server.goSIGTERMto signal handlingpkg/skills/client/client.gopkg/api/server_test.gogenerateNonce,ListenURL(TCP + Unix)pkg/api/v1/healtcheck_test.gopkg/server/discovery/*_test.goDoes this introduce a user-facing change?
thv servenow writes a discovery file that allows other tools (Studio, skills CLI) to automatically find the running server without requiringTOOLHIVE_API_URLor knowing the port. This is transparent to users -- existing behavior is preserved as a fallback.Special notes for reviewers
pkg/server/discovery/package was previously implemented and tested in a separate effort but never wired in. This PR includes it as new files since it had no prior commits.thv server start/stop/statussubcommands, CLI auto-start, lock file singleton, SSE events, Studio integration.Generated with Claude Code