feat: mcp over http on the official sdk, tls/token transport, docker, sqlc (#8)
MCP moves from mark3labs/mcp-go v0.17 (stdio only, 5 releases behind) to the official modelcontextprotocol/go-sdk with typed tool inputs, and gains a Streamable HTTP transport so a remote node can serve agents. Transport security works without a public DNS name: KNOX_AUTH_TOKEN guards every endpoint, KNOX_TLS_CERT/KEY serve TLS from a self-signed cert, and `knox tls init` mints one whose SANs are whatever addresses clients dial. Daemon hygiene for an always-on host: SIGTERM shutdown of the gossip and metrics servers, --version reporting the VCS revision, KNOX_LOCAL_SOURCES=none for a replicate-only node (no ingest tickers, no Obsidian vault adoption), and default watch dirs that skip paths which do not exist. Packaging: multi-stage Dockerfile (static, non-root, /data volume). sqlc pilot: schema moved to internal/db/schema.sql (embedded) and the read paths behind the MCP tools are now generated. The pilot found real drift — entries.created_at existed only as a runtime ALTER — so the column is declared in schema.sql and a parity test fails if generated code disagrees with db.Open. Conformance: scripts/conformance.sh runs the official suite against a throwaway node; conformance-baseline.yml records the capabilities knox deliberately does not implement (resources, prompts, completions, fixture tools). Refs #8
This commit is contained in:
+85
-14
@@ -1,6 +1,7 @@
|
||||
package watch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/david/knox/internal/db"
|
||||
"github.com/david/knox/internal/httpauth"
|
||||
"github.com/david/knox/internal/ingest"
|
||||
"github.com/david/knox/internal/metrics"
|
||||
"github.com/david/knox/internal/skillsrepo"
|
||||
@@ -45,6 +47,10 @@ type Watcher struct {
|
||||
fileIngesters []ingest.Ingester
|
||||
metrics *metrics.Metrics
|
||||
lastIngest map[string]fileState
|
||||
gossipSrv *http.Server
|
||||
metricsSrv *http.Server
|
||||
quit chan struct{}
|
||||
quitOnce sync.Once
|
||||
}
|
||||
|
||||
// fileDebouncer schedules one ingest per path a settle-window after the last
|
||||
@@ -86,8 +92,12 @@ func (d *fileDebouncer) cancel(path string) {
|
||||
}
|
||||
|
||||
func New(kdb *db.KnoxDB, dirs []string) *Watcher {
|
||||
// Detect Obsidian vault
|
||||
vault, _ := ingest.DetectObsidianVault()
|
||||
// Detect Obsidian vault. A gossip-only node has no local sources to read,
|
||||
// so it must not adopt whatever vault happens to exist on its filesystem.
|
||||
var vault string
|
||||
if LocalSourcesEnabled() {
|
||||
vault, _ = ingest.DetectObsidianVault()
|
||||
}
|
||||
|
||||
return &Watcher{
|
||||
knoxDB: kdb,
|
||||
@@ -100,6 +110,25 @@ func New(kdb *db.KnoxDB, dirs []string) *Watcher {
|
||||
},
|
||||
metrics: metrics.New(kdb, "knox"),
|
||||
lastIngest: make(map[string]fileState),
|
||||
quit: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
// Shutdown asks Start to return after closing the gossip and metrics servers.
|
||||
// It is safe to call more than once.
|
||||
func (w *Watcher) Shutdown() {
|
||||
w.quitOnce.Do(func() { close(w.quit) })
|
||||
}
|
||||
|
||||
// LocalSourcesEnabled reports whether this node ingests local sources
|
||||
// (opencode sessions, browser history, git, Gitea, Karakeep, Obsidian).
|
||||
// Set KNOX_LOCAL_SOURCES=none on a node that only replicates peers.
|
||||
func LocalSourcesEnabled() bool {
|
||||
switch strings.ToLower(strings.TrimSpace(os.Getenv("KNOX_LOCAL_SOURCES"))) {
|
||||
case "none", "0", "false", "off":
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,19 +158,37 @@ func (w *Watcher) Start() error {
|
||||
// seed is still ingesting. Vault/dirs are logged after the seed below.
|
||||
gossipAddr := ListenAddr()
|
||||
node := &Node{Kdb: w.knoxDB, Name: "knox", Addr: gossipAddr, Metrics: w.metrics}
|
||||
srv := &http.Server{Addr: gossipAddr, Handler: node.Handler(), ReadHeaderTimeout: 10 * time.Second, IdleTimeout: 60 * time.Second}
|
||||
srv := &http.Server{
|
||||
Addr: gossipAddr,
|
||||
Handler: httpauth.Middleware(node.Handler()),
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
w.gossipSrv = srv
|
||||
go func() {
|
||||
log.Printf("[knox] gossip listening on %s", gossipAddr)
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Printf("[knox] gossip listening on %s (tls=%t auth=%t)", gossipAddr, httpauth.TLSAvailable(), httpauth.Enabled())
|
||||
if !httpauth.Enabled() && !isLoopbackAddr(gossipAddr) {
|
||||
log.Printf("[knox] WARNING: %s is reachable beyond this host with no KNOX_AUTH_TOKEN", gossipAddr)
|
||||
}
|
||||
if err := httpauth.Serve(srv); err != nil && err != http.ErrServerClosed {
|
||||
log.Printf("[knox] gossip server: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Prometheus scraping on a dedicated port (KNOX_METRICS_ADDR).
|
||||
metricsAddr := MetricsAddr()
|
||||
metricsSrv := &http.Server{Addr: metricsAddr, Handler: node.Metrics.Handler(), ReadHeaderTimeout: 10 * time.Second, IdleTimeout: 60 * time.Second}
|
||||
metricsSrv := &http.Server{
|
||||
Addr: metricsAddr,
|
||||
Handler: node.Metrics.Handler(),
|
||||
ReadHeaderTimeout: 10 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
w.metricsSrv = metricsSrv
|
||||
go func() {
|
||||
log.Printf("[knox] metrics listening on %s", metricsAddr)
|
||||
if !isLoopbackAddr(metricsAddr) {
|
||||
log.Printf("[knox] WARNING: %s exposes metrics unauthenticated; scrape it over a private network", metricsAddr)
|
||||
}
|
||||
if err := metricsSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Printf("[knox] metrics server: %v", err)
|
||||
}
|
||||
@@ -168,6 +215,18 @@ func (w *Watcher) Start() error {
|
||||
threadTicker := time.NewTicker(10 * time.Minute)
|
||||
snippetTicker := time.NewTicker(snippetInterval)
|
||||
gossipTicker := time.NewTicker(gossipInterval)
|
||||
|
||||
// A node that only replicates peers (a server with none of the local
|
||||
// sources) skips the ingest/derive tickers entirely. Nil channels block
|
||||
// forever, so the select simply never fires for them.
|
||||
var browserC, giteaC, gitC, opencodeC, karakeepC, healthC, threadC, snippetC <-chan time.Time
|
||||
if LocalSourcesEnabled() {
|
||||
browserC, giteaC, gitC = browserTicker.C, giteaTicker.C, gitTicker.C
|
||||
opencodeC, karakeepC, healthC = opencodeTicker.C, karakeepTicker.C, healthTicker.C
|
||||
threadC, snippetC = threadTicker.C, snippetTicker.C
|
||||
} else {
|
||||
log.Printf("[knox] KNOX_LOCAL_SOURCES=none: gossip-only mode, local ingest disabled")
|
||||
}
|
||||
var browserRunning, giteaRunning, gitRunning, threadRunning, gossipRunning atomic.Bool
|
||||
var opencodeRunning, karakeepRunning, healthRunning, snippetRunning atomic.Bool
|
||||
|
||||
@@ -215,7 +274,7 @@ func (w *Watcher) Start() error {
|
||||
w.ingestFile(path, trigger)
|
||||
})
|
||||
|
||||
case <-browserTicker.C:
|
||||
case <-browserC:
|
||||
if !browserRunning.CompareAndSwap(false, true) {
|
||||
continue
|
||||
}
|
||||
@@ -223,7 +282,7 @@ func (w *Watcher) Start() error {
|
||||
defer browserRunning.Store(false)
|
||||
w.ingestBrowserHistory()
|
||||
}()
|
||||
case <-giteaTicker.C:
|
||||
case <-giteaC:
|
||||
if !giteaRunning.CompareAndSwap(false, true) {
|
||||
continue
|
||||
}
|
||||
@@ -231,7 +290,7 @@ func (w *Watcher) Start() error {
|
||||
defer giteaRunning.Store(false)
|
||||
w.ingestGitea()
|
||||
}()
|
||||
case <-gitTicker.C:
|
||||
case <-gitC:
|
||||
if !gitRunning.CompareAndSwap(false, true) {
|
||||
continue
|
||||
}
|
||||
@@ -239,7 +298,7 @@ func (w *Watcher) Start() error {
|
||||
defer gitRunning.Store(false)
|
||||
w.ingestGit()
|
||||
}()
|
||||
case <-opencodeTicker.C:
|
||||
case <-opencodeC:
|
||||
if !opencodeRunning.CompareAndSwap(false, true) {
|
||||
continue
|
||||
}
|
||||
@@ -247,7 +306,7 @@ func (w *Watcher) Start() error {
|
||||
defer opencodeRunning.Store(false)
|
||||
w.ingestOpenCodeDB()
|
||||
}()
|
||||
case <-karakeepTicker.C:
|
||||
case <-karakeepC:
|
||||
if !karakeepRunning.CompareAndSwap(false, true) {
|
||||
continue
|
||||
}
|
||||
@@ -255,7 +314,7 @@ func (w *Watcher) Start() error {
|
||||
defer karakeepRunning.Store(false)
|
||||
w.ingestKarakeep()
|
||||
}()
|
||||
case <-healthTicker.C:
|
||||
case <-healthC:
|
||||
if !healthRunning.CompareAndSwap(false, true) {
|
||||
continue
|
||||
}
|
||||
@@ -263,7 +322,7 @@ func (w *Watcher) Start() error {
|
||||
defer healthRunning.Store(false)
|
||||
w.checkSkillsHealth()
|
||||
}()
|
||||
case <-threadTicker.C:
|
||||
case <-threadC:
|
||||
if !threadRunning.CompareAndSwap(false, true) {
|
||||
continue
|
||||
}
|
||||
@@ -271,7 +330,7 @@ func (w *Watcher) Start() error {
|
||||
defer threadRunning.Store(false)
|
||||
w.autoThread()
|
||||
}()
|
||||
case <-snippetTicker.C:
|
||||
case <-snippetC:
|
||||
if !snippetRunning.CompareAndSwap(false, true) {
|
||||
continue
|
||||
}
|
||||
@@ -288,6 +347,18 @@ func (w *Watcher) Start() error {
|
||||
w.syncGossip()
|
||||
}()
|
||||
|
||||
case <-w.quit:
|
||||
log.Printf("[knox] shutting down")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if w.gossipSrv != nil {
|
||||
_ = w.gossipSrv.Shutdown(ctx)
|
||||
}
|
||||
if w.metricsSrv != nil {
|
||||
_ = w.metricsSrv.Shutdown(ctx)
|
||||
}
|
||||
return nil
|
||||
|
||||
case err, ok := <-watcher.Errors:
|
||||
if !ok {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user