Add Streamable HTTP MCP and harden web /v1 invoke auth.
agent_mcp_serve gains http transport; invoke injects Bearer/X-Api-Key and maps unauthorized to 401; openai/agent field access prefers json.get over footnotes. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
b43a08b2e2
commit
8353ba38fe
8 files changed
+525
-112
No files matched your search
@@ -3,6 +3,7 @@ package httpx
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
@@ -118,12 +119,74 @@ func jsonWrapInvokeValue(value any) (int, map[string]any, error) {
|
||||
}
|
||||
switch t := value.(type) {
|
||||
case map[string]any:
|
||||
return http.StatusOK, t, nil
|
||||
code := http.StatusOK
|
||||
if ok, has := t["ok"]; has {
|
||||
if b, isBool := ok.(bool); isBool && !b {
|
||||
if errMsg, _ := t["error"].(string); strings.EqualFold(errMsg, "unauthorized") ||
|
||||
strings.Contains(strings.ToLower(errMsg), "unauthorized") {
|
||||
code = http.StatusUnauthorized
|
||||
}
|
||||
}
|
||||
}
|
||||
return code, t, nil
|
||||
default:
|
||||
return http.StatusOK, map[string]any{"ok": true, "result": value}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// writeSSE writes OpenAI-compatible (or generic) Server-Sent Events.
|
||||
// value may be a string body, or a map with key "sse" / "body" / "chunks".
|
||||
func writeSSE(w http.ResponseWriter, value any) {
|
||||
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
flusher, _ := w.(http.Flusher)
|
||||
|
||||
writeChunk := func(s string) {
|
||||
if s == "" {
|
||||
return
|
||||
}
|
||||
if !strings.HasSuffix(s, "\n") {
|
||||
s += "\n"
|
||||
}
|
||||
_, _ = io.WriteString(w, s)
|
||||
if flusher != nil {
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
switch t := value.(type) {
|
||||
case string:
|
||||
writeChunk(t)
|
||||
case map[string]any:
|
||||
if s, ok := t["sse"].(string); ok {
|
||||
writeChunk(s)
|
||||
return
|
||||
}
|
||||
if s, ok := t["body"].(string); ok {
|
||||
writeChunk(s)
|
||||
return
|
||||
}
|
||||
if chunks, ok := t["chunks"].([]any); ok {
|
||||
for _, c := range chunks {
|
||||
writeChunk(fmt.Sprint(c))
|
||||
}
|
||||
return
|
||||
}
|
||||
b, err := json.Marshal(t)
|
||||
if err != nil {
|
||||
writeChunk("data: {\"error\":\"sse_encode\"}\n\n")
|
||||
return
|
||||
}
|
||||
writeChunk("data: " + string(b) + "\n\n")
|
||||
writeChunk("data: [DONE]\n\n")
|
||||
default:
|
||||
writeChunk("data: " + fmt.Sprint(value) + "\n\n")
|
||||
writeChunk("data: [DONE]\n\n")
|
||||
}
|
||||
}
|
||||
|
||||
func invokeArgsFromPayload(payload map[string]any) map[string]any {
|
||||
args := map[string]any{}
|
||||
for k, v := range payload {
|
||||
|
||||
@@ -17,8 +17,8 @@ type artifactRoute struct {
|
||||
Path string
|
||||
File string
|
||||
Request string // json | form | none | query
|
||||
Response string // json | text | html
|
||||
Auth string // role name or "" / "none"
|
||||
Response string // json | text | html | sse
|
||||
Auth string // role name or "" / "none" | "bearer"
|
||||
}
|
||||
|
||||
func parseArtifactRoute(v any) artifactRoute {
|
||||
@@ -57,16 +57,21 @@ func (st *state) mountArtifactRoutes(mux *http.ServeMux) {
|
||||
|
||||
func (st *state) handleArtifact(w http.ResponseWriter, r *http.Request, route artifactRoute) {
|
||||
if route.Auth != "" && !strings.EqualFold(route.Auth, "none") {
|
||||
cookie := r.Header.Get("Cookie")
|
||||
user := session.UsernameFromCookie(cookie)
|
||||
if user == "" {
|
||||
jsonErr(w, http.StatusUnauthorized, "auth required")
|
||||
return
|
||||
}
|
||||
role := session.RoleFromCookie(cookie)
|
||||
if role != "" && role != route.Auth && role != "admin" && role != "superadmin" {
|
||||
jsonErr(w, http.StatusForbidden, "forbidden")
|
||||
return
|
||||
if strings.EqualFold(route.Auth, "bearer") || strings.EqualFold(route.Auth, "api_key") {
|
||||
// Bearer / API-key auth is enforced inside the Endpoint `# main`
|
||||
// (payload carries authorization / api_key). Do not require cookies.
|
||||
} else {
|
||||
cookie := r.Header.Get("Cookie")
|
||||
user := session.UsernameFromCookie(cookie)
|
||||
if user == "" {
|
||||
jsonErr(w, http.StatusUnauthorized, "auth required")
|
||||
return
|
||||
}
|
||||
role := session.RoleFromCookie(cookie)
|
||||
if role != "" && role != route.Auth && role != "admin" && role != "superadmin" {
|
||||
jsonErr(w, http.StatusForbidden, "forbidden")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,6 +120,16 @@ func (st *state) handleArtifact(w http.ResponseWriter, r *http.Request, route ar
|
||||
payload[name] = v
|
||||
}
|
||||
}
|
||||
if auth := strings.TrimSpace(r.Header.Get("Authorization")); auth != "" {
|
||||
if _, exists := payload["authorization"]; !exists {
|
||||
payload["authorization"] = auth
|
||||
}
|
||||
}
|
||||
if key := strings.TrimSpace(r.Header.Get("X-Api-Key")); key != "" {
|
||||
if _, exists := payload["api_key"]; !exists {
|
||||
payload["api_key"] = key
|
||||
}
|
||||
}
|
||||
|
||||
value, err := plugin.RunArtifact(route.File, payload)
|
||||
if err != nil {
|
||||
@@ -131,6 +146,8 @@ func (st *state) handleArtifact(w http.ResponseWriter, r *http.Request, route ar
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = io.WriteString(w, fmt.Sprint(value))
|
||||
case "sse", "event-stream", "text/event-stream":
|
||||
writeSSE(w, value)
|
||||
default:
|
||||
code, body, err := jsonWrapInvokeValue(value)
|
||||
if err != nil {
|
||||
|
||||
@@ -102,6 +102,18 @@ func (st *state) handleInvoke(w http.ResponseWriter, r *http.Request, route invo
|
||||
}
|
||||
}
|
||||
|
||||
// Bind common auth / client headers for OpenAI-compatible and API-key surfaces.
|
||||
if auth := strings.TrimSpace(r.Header.Get("Authorization")); auth != "" {
|
||||
if _, exists := payload["authorization"]; !exists {
|
||||
payload["authorization"] = auth
|
||||
}
|
||||
}
|
||||
if key := strings.TrimSpace(r.Header.Get("X-Api-Key")); key != "" {
|
||||
if _, exists := payload["api_key"]; !exists {
|
||||
payload["api_key"] = key
|
||||
}
|
||||
}
|
||||
|
||||
args := invokeArgsFromPayload(payload)
|
||||
value, err := plugin.CallLibPath(route.FnPath, args)
|
||||
if err != nil {
|
||||
@@ -114,10 +126,22 @@ func (st *state) handleInvoke(w http.ResponseWriter, r *http.Request, route invo
|
||||
strings.Contains(msg, "no active host") {
|
||||
code = http.StatusBadRequest
|
||||
}
|
||||
if strings.Contains(msg, "unauthorized") || strings.Contains(msg, "Unauthorized") {
|
||||
code = http.StatusUnauthorized
|
||||
}
|
||||
jsonErr(w, code, msg)
|
||||
return
|
||||
}
|
||||
|
||||
// OpenAI-compatible handlers may return `{sse: "..."}` to force event-stream
|
||||
// even when the route's declared return mode is json.
|
||||
if m, ok := value.(map[string]any); ok {
|
||||
if s, ok := m["sse"].(string); ok && strings.TrimSpace(s) != "" {
|
||||
writeSSE(w, m)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
switch route.ReturnMode {
|
||||
case "text", "html", "text/html":
|
||||
if value == nil {
|
||||
@@ -135,6 +159,8 @@ func (st *state) handleInvoke(w http.ResponseWriter, r *http.Request, route invo
|
||||
w.Header().Set("Content-Type", ct)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = io.WriteString(w, text)
|
||||
case "sse", "event-stream", "text/event-stream":
|
||||
writeSSE(w, value)
|
||||
case "status":
|
||||
if value == nil {
|
||||
jsonErr(w, http.StatusInternalServerError, "null result")
|
||||
|
||||
Reference in new issue
Block a user