Improve SSE status and event auth handling

This commit is contained in:
Brandon Zhang
2026-03-27 17:55:28 +08:00
parent f437f6939c
commit 65b29bcf03
6 changed files with 77 additions and 22 deletions

View File

@@ -3,6 +3,8 @@ package api
import (
"net/http"
"time"
"github.com/local-mcp/local-mcp-go/internal/models"
)
func handleHealth() http.HandlerFunc {
@@ -16,28 +18,58 @@ func handleHealth() http.HandlerFunc {
func handleStatus(stores Stores) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
counts, _ := stores.Instructions.Counts()
latest, _ := stores.Agents.Latest()
cfg, _ := stores.Settings.Get()
counts, err := stores.Instructions.Counts()
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
resp := map[string]any{
"uptime_seconds": int(time.Since(serverStartTime).Seconds()),
"queue_pending": counts.PendingCount,
"queue_consumed": counts.ConsumedCount,
"agent_stale_after_seconds": cfg.AgentStaleAfterSeconds,
cfg, err := stores.Settings.Get()
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
latest, err := stores.Agents.Latest()
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
agent := map[string]any{
"connected": false,
"last_seen_at": nil,
"last_fetch_at": nil,
"agent_id": nil,
}
if latest != nil {
isStale := time.Since(latest.LastSeenAt).Seconds() > float64(cfg.AgentStaleAfterSeconds)
resp["agent"] = map[string]any{
"agent_id": latest.AgentID,
"last_fetch_at": latest.LastFetchAt.Format(time.RFC3339Nano),
"last_result_type": latest.LastResultType,
"is_stale": isStale,
connected := time.Since(latest.LastSeenAt).Seconds() <= float64(cfg.AgentStaleAfterSeconds)
agent = map[string]any{
"connected": connected,
"last_seen_at": latest.LastSeenAt.Format(time.RFC3339Nano),
"last_fetch_at": latest.LastFetchAt.Format(time.RFC3339Nano),
"agent_id": latest.AgentID,
}
}
resp := map[string]any{
"server": map[string]any{
"status": "up",
"started_at": serverStartTime.Format(time.RFC3339Nano),
},
"agent": agent,
"queue": map[string]any{
"pending_count": counts.PendingCount,
"consumed_count": counts.ConsumedCount,
},
"settings": models.Settings{
DefaultWaitSeconds: cfg.DefaultWaitSeconds,
DefaultEmptyResponse: cfg.DefaultEmptyResponse,
AgentStaleAfterSeconds: cfg.AgentStaleAfterSeconds,
},
}
writeJSON(w, http.StatusOK, resp)
}
}