package app import ( "context" "crypto/rand" "crypto/subtle" "encoding/hex" "encoding/json" "errors" "fmt" "log/slog" "net/http" "strconv" "strings" ) func (s *Service) apiHandler() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { id := newID() w.Header().Set("x-request-id", id) if r.Method == "GET" && r.URL.Path == "/healthz" { writeJSON(w, 200, map[string]any{"ok": true, "jevConfigured": s.jev.Key != "", "summarizerConfigured": s.llm.Key != ""}) return } token := r.Header.Get("x-api-key") if token == "" { parts := strings.Fields(r.Header.Get("Authorization")) if len(parts) == 2 && strings.EqualFold(parts[0], "bearer") { token = parts[1] } } admin := s.cfg.Token != "" && subtle.ConstantTimeCompare([]byte(token), []byte(s.cfg.Token)) == 1 caller := "" if !admin { var err error caller, err = s.auth.caller(token) if err != nil { slog.Error("issued-token store unavailable", "error", err) writeJSON(w, 503, APIError{RequestID: id, Tag: "AUTH_STORE_UNAVAILABLE", Message: "authentication store is unavailable"}) return } } public := token == "" && s.gate != nil if public && r.Method == "GET" && (r.URL.Path == "/" || r.URL.Path == "/docs") { w.Header().Set("Content-Type", "text/html; charset=utf-8") if r.URL.Path == "/docs" { _, _ = w.Write(publicDocs()) } else { _, _ = w.Write([]byte(publicNotice)) } return } if !admin && caller == "" && !public { w.Header().Set("WWW-Authenticate", "Bearer") writeJSON(w, 401, APIError{RequestID: id, Tag: "INVALID_API_KEY", Message: "invalid api key"}) return } if r.Method != "POST" { w.Header().Set("Allow", "POST") writeJSON(w, 405, APIError{RequestID: id, Tag: "METHOD_NOT_ALLOWED", Message: "use post"}) return } ctx, cancel := context.WithTimeout(r.Context(), s.cfg.RequestTimeout) defer cancel() if admin { caller = takeRunes(r.Header.Get("x-caller"), 200) if caller == "" { caller = takeRunes(r.Header.Get("x-agent-name"), 200) } if caller == "" { caller = "admin" } } call := callContext{ID: id, Caller: caller} path := r.URL.Path if public { call = callContext{ID: id, Caller: "public", Public: true, Contribute: contributes(r)} if path == "/mcp/contribute" { path = "/mcp" } } ctx = context.WithValue(ctx, contextKey{}, call) body, err := readBounded(r.Body, 64<<10) if err != nil { s.respond(w, ctx, nil, failure(413, "INVALID_REQUEST_BODY", "request body exceeds 64 kib")) return } if public { if ok, wait := s.gate.admit(clientAddr(r, s.cfg.Public.TrustedProxies), runsTool(path, body)); !ok { w.Header().Set("Retry-After", strconv.Itoa(wait)) s.respond(w, ctx, nil, failure(429, "RATE_LIMITED", "too many requests; see Retry-After")) return } } if path == "/mcp" { if result := s.mcp(ctx, body); result != nil { writeJSON(w, 200, result) } else { w.WriteHeader(http.StatusAccepted) } return } var out any endpoint := strings.TrimPrefix(path, "/") switch path { case "/search": var q SearchRequest if err = decodeRequest(body, &q); err == nil { out, err = s.search(ctx, q) } case "/contents": var q ContentsRequest if err = decodeRequest(body, &q); err == nil { out, err = s.contents(ctx, q) } case "/answer": var q AnswerRequest if err = decodeRequest(body, &q); err == nil { out, err = s.answer(ctx, q) } case "/v1/extract", "/v1/preview", "/v1/summarize": endpoint = "contents" out, err = s.pageAlias(ctx, strings.TrimPrefix(path, "/v1/"), body) default: err = failure(404, "NOT_FOUND", "unknown endpoint") } if err != nil { var syntax *json.SyntaxError var typeErr *json.UnmarshalTypeError if errors.As(err, &syntax) || errors.As(err, &typeErr) || strings.HasPrefix(err.Error(), "json:") || err.Error() == "expected one json value" { err = failure(400, "INVALID_REQUEST_BODY", err.Error()) } } s.recordAPI(ctx, endpoint, body, out, err) s.respond(w, ctx, out, err) }) } func (s *Service) recordAPI(ctx context.Context, endpoint string, body []byte, out any, err error) { detail := map[string]any{} outcome := "ok" var fields map[string]json.RawMessage _ = json.Unmarshal(body, &fields) if q := fields["query"]; q != nil { var v string _ = json.Unmarshal(q, &v) detail["query"] = v } if result, ok := out.(ResultsResponse); ok { urls := []string{} for _, r := range result.Results { urls = append(urls, r.ID) } if endpoint == "search" { detail["result_urls"] = urls } if endpoint == "contents" { detail["urls"] = urls } if len(result.Statuses) > 0 { detail["statuses"] = result.Statuses for _, status := range result.Statuses { if status.Status == "error" { outcome = "partial" } } } } if err != nil { outcome = apiError(ctx, err).Tag } s.log.request(ctx, endpoint, detail, outcome) // Preserve the old exporter’s search→extract follow-through labels for // explicit /contents reads without inventing model completions. if endpoint == "contents" { if result, ok := out.(ResultsResponse); ok { for _, r := range result.Results { s.log.request(ctx, "extract", map[string]any{"url": r.ID}, "ok") } } } } func apiError(ctx context.Context, err error) APIError { var a *APIError if errors.As(err, &a) { copy := *a copy.RequestID = callInfo(ctx).ID return copy } status, tag := 502, "UPSTREAM_ERROR" if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) { status = 504 tag = "REQUEST_TIMEOUT" } return APIError{Status: status, RequestID: callInfo(ctx).ID, Tag: tag, Message: err.Error()} } func (s *Service) respond(w http.ResponseWriter, ctx context.Context, out any, err error) { if err != nil { a := apiError(ctx, err) writeJSON(w, a.Status, a) return } writeJSON(w, 200, out) } func writeJSON(w http.ResponseWriter, status int, v any) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) _ = json.NewEncoder(w).Encode(v) } func newID() string { var b [16]byte if _, err := rand.Read(b[:]); err != nil { panic(err) } return hex.EncodeToString(b[:]) } func decodeRequest(b []byte, v any) error { if err := strictJSON(b, v); err != nil { return failure(400, "INVALID_REQUEST_BODY", err.Error()) } return nil } // MCP transport metadata is not a tool argument. Keep the service's strict // validation for every actual argument, including unknown nested fields. func stripMCPMeta(arguments json.RawMessage) (json.RawMessage, error) { var fields map[string]json.RawMessage if err := json.Unmarshal(arguments, &fields); err != nil { return nil, err } if _, ok := fields["_meta"]; !ok { return arguments, nil } delete(fields, "_meta") return json.Marshal(fields) } var toolsSchema = json.RawMessage(`[ {"name":"web_search","description":"Search the web (SearXNG), rerank with a decision model, and optionally return text, highlights or grounded summaries. Exa-shaped input and output.","inputSchema":{"type":"object","required":["query"],"additionalProperties":false,"properties":{"query":{"type":"string"},"type":{"type":"string","enum":["auto"]},"numResults":{"type":"integer","minimum":1,"maximum":20},"includeDomains":{"type":"array","items":{"type":"string"}},"excludeDomains":{"type":"array","items":{"type":"string"}},"contents":{"type":"object","properties":{"text":{"oneOf":[{"type":"boolean"},{"type":"object","properties":{"maxCharacters":{"type":"integer","minimum":0,"maximum":10000,"description":"Maximum returned text characters (1–10000); 0 or omitted means uncapped."}}}]},"highlights":{"oneOf":[{"type":"boolean"},{"type":"object","properties":{"query":{"type":"string"},"maxCharacters":{"type":"integer","minimum":0,"maximum":10000,"description":"Maximum total highlight characters (1–10000); 0 or omitted uses the configured evidence budget."}}}]},"summary":{"oneOf":[{"type":"boolean"},{"type":"object","properties":{"query":{"type":"string"}}}]},"maxAgeHours":{"type":"integer","minimum":-1,"maximum":720},"livecrawlTimeout":{"type":"integer","minimum":1,"maximum":90000}},"additionalProperties":false}}}}, {"name":"web_contents","description":"Read up to 20 known urls. Full text by default; request verbatim highlights picked by the decision model or a query-focused summary explicitly.","inputSchema":{"type":"object","additionalProperties":false,"properties":{"urls":{"type":"array","minItems":1,"maxItems":20,"items":{"type":"string"}},"ids":{"type":"array","minItems":1,"maxItems":20,"items":{"type":"string"}},"text":{"oneOf":[{"type":"boolean"},{"type":"object","properties":{"maxCharacters":{"type":"integer","minimum":0,"maximum":10000,"description":"Maximum returned text characters (1–10000); 0 or omitted means uncapped."}}}]},"highlights":{"oneOf":[{"type":"boolean"},{"type":"object","properties":{"query":{"type":"string"},"maxCharacters":{"type":"integer","minimum":0,"maximum":10000,"description":"Maximum total highlight characters (1–10000); 0 or omitted uses the configured evidence budget."}}}]},"summary":{"oneOf":[{"type":"boolean"},{"type":"object","properties":{"query":{"type":"string"}}}]},"maxAgeHours":{"type":"integer","minimum":-1,"maximum":720},"livecrawlTimeout":{"type":"integer","minimum":1,"maximum":90000}},"oneOf":[{"required":["urls"]},{"required":["ids"]}]}}, {"name":"web_answer","description":"Search and return one grounded plain-prose answer plus sources, using the original evidence the decision model picked rather than summaries of summaries.","inputSchema":{"type":"object","required":["query"],"additionalProperties":false,"properties":{"query":{"type":"string"},"text":{"type":"boolean"},"stream":{"type":"boolean","enum":[false]}}}}, {"name":"web_extract","description":"Single-url alias for web_contents with text enabled, without model calls. Returns the new requestId/results/statuses shape, not legacy markdown fields.","inputSchema":{"type":"object","required":["url"],"additionalProperties":false,"properties":{"url":{"type":"string"},"max_chars":{"type":"integer","minimum":0,"maximum":10000}}}}, {"name":"web_preview","description":"Single-url alias for web_contents with a query-focused, evidence-grounded summary. Returns the new requestId/results/statuses shape. Inspect per-url statuses.","inputSchema":{"type":"object","required":["url","query"],"additionalProperties":false,"properties":{"url":{"type":"string"},"query":{"type":"string","minLength":1,"maxLength":4000},"include_markdown":{"type":"boolean"},"max_chars":{"type":"integer","minimum":0,"maximum":10000}}}}, {"name":"web_summarize","description":"Single-url alias for web_contents with an evidence-grounded summary. Optional query (or instructions as the fallback query) focuses the summary. Returns the new requestId/results/statuses shape.","inputSchema":{"type":"object","required":["url"],"additionalProperties":false,"properties":{"url":{"type":"string"},"query":{"type":"string","maxLength":4000},"instructions":{"type":"string","maxLength":4000},"include_markdown":{"type":"boolean"},"max_chars":{"type":"integer","minimum":0,"maximum":10000}}}} ]`) func (s *Service) mcp(ctx context.Context, body []byte) any { var req struct { JSONRPC string `json:"jsonrpc"` ID json.RawMessage `json:"id"` Method string `json:"method"` Params json.RawMessage `json:"params"` } if err := json.Unmarshal(body, &req); err != nil || req.JSONRPC != "2.0" { return map[string]any{"jsonrpc": "2.0", "id": nil, "error": map[string]any{"code": -32600, "message": "invalid json-rpc request"}} } if len(req.ID) == 0 { return nil } reply := func(result any) any { return map[string]any{"jsonrpc": "2.0", "id": req.ID, "result": result} } switch req.Method { case "initialize": return reply(map[string]any{"protocolVersion": "2025-03-26", "capabilities": map[string]any{"tools": map[string]bool{"listChanged": false}}, "serverInfo": map[string]string{"name": "web-extract", "version": "2.0.0"}}) case "ping": return reply(map[string]any{}) case "tools/list": return reply(map[string]any{"tools": toolsSchema}) case "tools/call": var p struct { Name string `json:"name"` Arguments json.RawMessage `json:"arguments"` Meta json.RawMessage `json:"_meta"` } err := strictJSON(req.Params, &p) var out any endpoint := "" if err == nil { p.Arguments, err = stripMCPMeta(p.Arguments) } if err == nil { switch p.Name { case "web_search": endpoint = "search" var q SearchRequest if err = strictJSON(p.Arguments, &q); err == nil { out, err = s.search(ctx, q) } case "web_contents": endpoint = "contents" var q ContentsRequest if err = strictJSON(p.Arguments, &q); err == nil { out, err = s.contents(ctx, q) } case "web_answer": endpoint = "answer" var q AnswerRequest if err = strictJSON(p.Arguments, &q); err == nil { out, err = s.answer(ctx, q) } case "web_extract", "web_preview", "web_summarize": endpoint = "contents" out, err = s.pageAlias(ctx, strings.TrimPrefix(p.Name, "web_"), p.Arguments) default: err = fmt.Errorf("unknown tool") } } s.recordAPI(ctx, endpoint, p.Arguments, out, err) if err != nil { a := apiError(ctx, err) return reply(map[string]any{"isError": true, "content": []any{map[string]string{"type": "text", "text": jsonText(a)}}, "structuredContent": a}) } else { return reply(map[string]any{"isError": false, "content": []any{map[string]string{"type": "text", "text": jsonText(out)}}, "structuredContent": out}) } default: return map[string]any{"jsonrpc": "2.0", "id": req.ID, "error": map[string]any{"code": -32601, "message": "method not found"}} } }