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
+336
-41
@@ -1,9 +1,14 @@
|
||||
//! Minimal MCP Server (stdio JSON-RPC) — tools map to `lib.member` via host `call_lib_path`.
|
||||
//! Minimal MCP Server — stdio JSON-RPC + Streamable HTTP (spec 2025-03-26).
|
||||
|
||||
use serde_json::{json, Map, Value};
|
||||
use std::ffi::CStr;
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::io::{BufRead, BufReader, Read, Write};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::os::raw::{c_char, c_int};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::{host_query_json_args, parse_args, set_err, set_out};
|
||||
|
||||
@@ -98,6 +103,40 @@ fn call_tool(tools: &[Value], name: &str, arguments: &Value) -> Result<Value, St
|
||||
}))
|
||||
}
|
||||
|
||||
/// Handle one JSON-RPC message. Returns `None` for notifications (no response body).
|
||||
fn dispatch_rpc(name: &str, tools: &[Value], msg: &Value) -> Option<Value> {
|
||||
let id = msg.get("id").cloned().unwrap_or(Value::Null);
|
||||
let method = msg.get("method").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let params = msg.get("params").cloned().unwrap_or(json!({}));
|
||||
|
||||
if matches!(id, Value::Null) && method.starts_with("notifications/") {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(match method {
|
||||
"initialize" => rpc_result(
|
||||
&id,
|
||||
json!({
|
||||
"protocolVersion": "2025-03-26",
|
||||
"capabilities": { "tools": {} },
|
||||
"serverInfo": { "name": name, "version": "0.1.0" }
|
||||
}),
|
||||
),
|
||||
"tools/list" | "list_tools" => rpc_result(&id, tools_list(tools)),
|
||||
"tools/call" | "call_tool" => {
|
||||
let tname = params.get("name").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let args = params.get("arguments").cloned().unwrap_or(json!({}));
|
||||
match call_tool(tools, tname, &args) {
|
||||
Ok(r) => rpc_result(&id, r),
|
||||
Err(e) => rpc_error(&id, -32000, &e),
|
||||
}
|
||||
}
|
||||
"ping" => rpc_result(&id, json!({})),
|
||||
"" => rpc_error(&id, -32600, "missing method"),
|
||||
other => rpc_error(&id, -32601, &format!("method not found: {other}")),
|
||||
})
|
||||
}
|
||||
|
||||
/// Blocking stdio MCP loop. `tools` is a JSON array of `{name, fn, description}`.
|
||||
pub fn serve_stdio(name: &str, tools: Vec<Value>) -> Result<Value, String> {
|
||||
let stdin = std::io::stdin();
|
||||
@@ -106,41 +145,274 @@ pub fn serve_stdio(name: &str, tools: Vec<Value>) -> Result<Value, String> {
|
||||
let Some(msg) = read_line_message(&mut reader)? else {
|
||||
break;
|
||||
};
|
||||
let id = msg.get("id").cloned().unwrap_or(Value::Null);
|
||||
let method = msg.get("method").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let params = msg.get("params").cloned().unwrap_or(json!({}));
|
||||
|
||||
if matches!(id, Value::Null) && method.starts_with("notifications/") {
|
||||
continue;
|
||||
if let Some(reply) = dispatch_rpc(name, &tools, &msg) {
|
||||
write_message(&reply)?;
|
||||
}
|
||||
|
||||
let reply_msg = match method {
|
||||
"initialize" => rpc_result(
|
||||
&id,
|
||||
json!({
|
||||
"protocolVersion": "2024-11-05",
|
||||
"capabilities": { "tools": {} },
|
||||
"serverInfo": { "name": name, "version": "0.1.0" }
|
||||
}),
|
||||
),
|
||||
"tools/list" | "list_tools" => rpc_result(&id, tools_list(&tools)),
|
||||
"tools/call" | "call_tool" => {
|
||||
let tname = params.get("name").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let args = params.get("arguments").cloned().unwrap_or(json!({}));
|
||||
match call_tool(&tools, tname, &args) {
|
||||
Ok(r) => rpc_result(&id, r),
|
||||
Err(e) => rpc_error(&id, -32000, &e),
|
||||
}
|
||||
}
|
||||
"ping" => rpc_result(&id, json!({})),
|
||||
"" => rpc_error(&id, -32600, "missing method"),
|
||||
other => rpc_error(&id, -32601, &format!("method not found: {other}")),
|
||||
};
|
||||
write_message(&reply_msg)?;
|
||||
}
|
||||
Ok(json!({ "ok": true, "transport": "stdio" }))
|
||||
}
|
||||
|
||||
fn new_session_id() -> String {
|
||||
static SEQ: AtomicU64 = AtomicU64::new(1);
|
||||
let n = SEQ.fetch_add(1, Ordering::Relaxed);
|
||||
let ms = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|d| d.as_millis())
|
||||
.unwrap_or(0);
|
||||
format!("mcp-{ms}-{n}")
|
||||
}
|
||||
|
||||
fn read_http_request(
|
||||
reader: &mut BufReader<TcpStream>,
|
||||
) -> Result<(String, String, Vec<(String, String)>, Vec<u8>), String> {
|
||||
let mut request_line = String::new();
|
||||
reader
|
||||
.read_line(&mut request_line)
|
||||
.map_err(|e| format!("read request: {e}"))?;
|
||||
let parts: Vec<&str> = request_line.trim().split_whitespace().collect();
|
||||
if parts.len() < 2 {
|
||||
return Err("bad request line".into());
|
||||
}
|
||||
let method = parts[0].to_string();
|
||||
let path = parts[1].to_string();
|
||||
let mut headers = Vec::new();
|
||||
let mut content_length = 0usize;
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
reader
|
||||
.read_line(&mut line)
|
||||
.map_err(|e| format!("read header: {e}"))?;
|
||||
let line = line.trim_end_matches(['\r', '\n']);
|
||||
if line.is_empty() {
|
||||
break;
|
||||
}
|
||||
if let Some((k, v)) = line.split_once(':') {
|
||||
let key = k.trim().to_string();
|
||||
let val = v.trim().to_string();
|
||||
if key.eq_ignore_ascii_case("content-length") {
|
||||
content_length = val.parse().unwrap_or(0);
|
||||
}
|
||||
headers.push((key, val));
|
||||
}
|
||||
}
|
||||
let mut body = vec![0u8; content_length];
|
||||
if content_length > 0 {
|
||||
reader
|
||||
.read_exact(&mut body)
|
||||
.map_err(|e| format!("read body: {e}"))?;
|
||||
}
|
||||
Ok((method, path, headers, body))
|
||||
}
|
||||
|
||||
fn header_get<'a>(headers: &'a [(String, String)], name: &str) -> Option<&'a str> {
|
||||
headers
|
||||
.iter()
|
||||
.find(|(k, _)| k.eq_ignore_ascii_case(name))
|
||||
.map(|(_, v)| v.as_str())
|
||||
}
|
||||
|
||||
fn write_http(
|
||||
stream: &mut TcpStream,
|
||||
status: u16,
|
||||
reason: &str,
|
||||
content_type: &str,
|
||||
body: &[u8],
|
||||
extra: &[(&str, &str)],
|
||||
) -> Result<(), String> {
|
||||
let mut head = format!(
|
||||
"HTTP/1.1 {status} {reason}\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n",
|
||||
body.len()
|
||||
);
|
||||
for (k, v) in extra {
|
||||
head.push_str(k);
|
||||
head.push_str(": ");
|
||||
head.push_str(v);
|
||||
head.push_str("\r\n");
|
||||
}
|
||||
head.push_str("\r\n");
|
||||
stream
|
||||
.write_all(head.as_bytes())
|
||||
.map_err(|e| e.to_string())?;
|
||||
stream.write_all(body).map_err(|e| e.to_string())?;
|
||||
stream.flush().map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn path_matches(req_path: &str, endpoint: &str) -> bool {
|
||||
let base = req_path.split('?').next().unwrap_or(req_path);
|
||||
base == endpoint || base.trim_end_matches('/') == endpoint.trim_end_matches('/')
|
||||
}
|
||||
|
||||
fn handle_http_conn(
|
||||
stream: TcpStream,
|
||||
name: &str,
|
||||
tools: &[Value],
|
||||
endpoint: &str,
|
||||
) -> Result<(), String> {
|
||||
let mut reader = BufReader::new(stream);
|
||||
let (method, path, headers, body) = read_http_request(&mut reader)?;
|
||||
let mut stream = reader.into_inner();
|
||||
if !path_matches(&path, endpoint) {
|
||||
write_http(
|
||||
&mut stream,
|
||||
404,
|
||||
"Not Found",
|
||||
"application/json",
|
||||
br#"{"error":"not_found"}"#,
|
||||
&[],
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if method.eq_ignore_ascii_case("GET") {
|
||||
// Optional SSE stream — empty keepalive then close (clients may 405).
|
||||
let accept = header_get(&headers, "Accept").unwrap_or("");
|
||||
if accept.contains("text/event-stream") {
|
||||
let body = b": mcp streamable-http ready\n\n";
|
||||
write_http(
|
||||
&mut stream,
|
||||
200,
|
||||
"OK",
|
||||
"text/event-stream",
|
||||
body,
|
||||
&[("Cache-Control", "no-cache")],
|
||||
)?;
|
||||
} else {
|
||||
write_http(
|
||||
&mut stream,
|
||||
405,
|
||||
"Method Not Allowed",
|
||||
"application/json",
|
||||
br#"{"error":"use_post_for_jsonrpc"}"#,
|
||||
&[("Allow", "POST, GET")],
|
||||
)?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if method.eq_ignore_ascii_case("DELETE") {
|
||||
write_http(&mut stream, 200, "OK", "application/json", b"{}", &[])?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !method.eq_ignore_ascii_case("POST") {
|
||||
write_http(
|
||||
&mut stream,
|
||||
405,
|
||||
"Method Not Allowed",
|
||||
"application/json",
|
||||
br#"{"error":"method_not_allowed"}"#,
|
||||
&[("Allow", "POST, GET, DELETE")],
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let text = String::from_utf8_lossy(&body);
|
||||
let msg: Value = serde_json::from_str(text.trim()).map_err(|e| format!("json-rpc: {e}"))?;
|
||||
let is_init = msg.get("method").and_then(|v| v.as_str()) == Some("initialize");
|
||||
let existing_sid = header_get(&headers, "Mcp-Session-Id")
|
||||
.or_else(|| header_get(&headers, "MCP-Session-Id"))
|
||||
.map(|s| s.to_string());
|
||||
|
||||
// Notifications → 202 Accepted, empty body.
|
||||
if msg.get("id").is_none() || msg.get("id") == Some(&Value::Null) {
|
||||
if let Some(m) = msg.get("method").and_then(|v| v.as_str()) {
|
||||
if m.starts_with("notifications/") {
|
||||
write_http(
|
||||
&mut stream,
|
||||
202,
|
||||
"Accepted",
|
||||
"application/json",
|
||||
b"",
|
||||
&[],
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let reply = match dispatch_rpc(name, tools, &msg) {
|
||||
Some(r) => r,
|
||||
None => {
|
||||
write_http(
|
||||
&mut stream,
|
||||
202,
|
||||
"Accepted",
|
||||
"application/json",
|
||||
b"",
|
||||
&[],
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let body = serde_json::to_vec(&reply).map_err(|e| e.to_string())?;
|
||||
let mut extras: Vec<(&str, String)> = Vec::new();
|
||||
if is_init {
|
||||
let sid = existing_sid.unwrap_or_else(new_session_id);
|
||||
extras.push(("Mcp-Session-Id", sid));
|
||||
} else if let Some(sid) = existing_sid {
|
||||
extras.push(("Mcp-Session-Id", sid));
|
||||
}
|
||||
let extra_refs: Vec<(&str, &str)> = extras.iter().map(|(k, v)| (*k, v.as_str())).collect();
|
||||
write_http(
|
||||
&mut stream,
|
||||
200,
|
||||
"OK",
|
||||
"application/json",
|
||||
&body,
|
||||
&extra_refs,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Streamable HTTP MCP (POST JSON-RPC + optional GET SSE). Spec 2025-03-26.
|
||||
pub fn serve_http(
|
||||
name: &str,
|
||||
tools: Vec<Value>,
|
||||
host: &str,
|
||||
port: u16,
|
||||
path: &str,
|
||||
) -> Result<Value, String> {
|
||||
let addr = format!("{host}:{port}");
|
||||
let listener = TcpListener::bind(&addr).map_err(|e| format!("bind {addr}: {e}"))?;
|
||||
let name = Arc::new(name.to_string());
|
||||
let tools = Arc::new(tools);
|
||||
let endpoint = Arc::new(if path.is_empty() {
|
||||
"/mcp".to_string()
|
||||
} else if path.starts_with('/') {
|
||||
path.to_string()
|
||||
} else {
|
||||
format!("/{path}")
|
||||
});
|
||||
eprintln!(
|
||||
"mcp streamable-http listening on http://{addr}{} (POST JSON-RPC)",
|
||||
&*endpoint
|
||||
);
|
||||
for conn in listener.incoming() {
|
||||
match conn {
|
||||
Ok(stream) => {
|
||||
let name = Arc::clone(&name);
|
||||
let tools = Arc::clone(&tools);
|
||||
let endpoint = Arc::clone(&endpoint);
|
||||
thread::spawn(move || {
|
||||
if let Err(e) = handle_http_conn(stream, &name, &tools, &endpoint) {
|
||||
eprintln!("mcp http conn: {e}");
|
||||
}
|
||||
});
|
||||
}
|
||||
Err(e) => eprintln!("mcp http accept: {e}"),
|
||||
}
|
||||
}
|
||||
Ok(json!({
|
||||
"ok": true,
|
||||
"transport": "http",
|
||||
"host": host,
|
||||
"port": port,
|
||||
"path": *endpoint
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn tools_from_value(v: &Value) -> Vec<Value> {
|
||||
if let Some(arr) = v.as_array() {
|
||||
return arr.clone();
|
||||
@@ -180,13 +452,6 @@ pub unsafe extern "C" fn agent_mcp_serve(
|
||||
.get("transport")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("stdio");
|
||||
if transport != "stdio" {
|
||||
set_err(
|
||||
err_msg,
|
||||
&format!("mcp serve: transport `{transport}` not implemented yet (use stdio)"),
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
let name = args
|
||||
.get("name")
|
||||
.and_then(|v| v.as_str())
|
||||
@@ -195,7 +460,38 @@ pub unsafe extern "C" fn agent_mcp_serve(
|
||||
.get("tools")
|
||||
.map(tools_from_value)
|
||||
.unwrap_or_default();
|
||||
match serve_stdio(name, tools) {
|
||||
|
||||
let result = match transport {
|
||||
"stdio" => serve_stdio(name, tools),
|
||||
"http" | "streamable-http" | "sse" => {
|
||||
let host = args
|
||||
.get("host")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("127.0.0.1");
|
||||
let port = args
|
||||
.get("port")
|
||||
.and_then(|v| {
|
||||
v.as_u64()
|
||||
.or_else(|| v.as_i64().map(|n| n as u64))
|
||||
.or_else(|| v.as_str().and_then(|s| s.parse().ok()))
|
||||
})
|
||||
.unwrap_or(7434) as u16;
|
||||
let path = args
|
||||
.get("path")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("/mcp");
|
||||
serve_http(name, tools, host, port, path)
|
||||
}
|
||||
other => {
|
||||
set_err(
|
||||
err_msg,
|
||||
&format!("mcp serve: transport `{other}` not supported (use stdio|http)"),
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(v) => {
|
||||
set_out(out_json, &v.to_string());
|
||||
0
|
||||
@@ -207,6 +503,5 @@ pub unsafe extern "C" fn agent_mcp_serve(
|
||||
}
|
||||
}
|
||||
|
||||
// Silence unused import if CStr unused in this module.
|
||||
#[allow(dead_code)]
|
||||
fn _unused(_: &CStr) {}
|
||||
@@ -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