Compare commits

...
2 Commits
Author SHA1 Message Date
cflmyandCursor bfd468d5e3 release: Marqdo 1.4.5
Streamable HTTP MCP, web invoke Bearer/401, openai/agent json.get hardening.
Bump CLI/Ext/wasm and debian changelog.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-16 17:36:07 +08:00
cflmyandCursor 8353ba38fe 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>
2026-10-16 17:33:24 +08:00
16 changed files with 578 additions and 125 deletions

No files matched your search

+2 -2
View File
@@ -126,8 +126,8 @@ jobs:
## Marqdo ${{ github.ref_name }}
### 亮点 / Highlights
- **v1.4.4**:LLM 传输软失败 + `ext/ai` 表优先——`status=error` 不 abort · 去 json.set 胶水
- **v1.4.4**: Soft LLM transport errors + table-first ext/ai — structured status=error, no json.set glue
- **v1.4.5**:Streamable HTTP MCP + web invoke Bearer/401 · openai/agent json.get 硬化
- **v1.4.5**: Streamable HTTP MCP + web invoke Bearer/401; openai/agent json.get hardening
- **v1.4.2**:Agent/LLM 优化 O1–O5——sandbox trial · promote proof · step_route/EFI/cost · lint · quarantine
- **v1.4.2**: Agent/LLM optimize O1–O5 — sandbox trial, promote proof, step_route/EFI/cost, lint, quarantine
- **v1.4.1**:view 标注连线——SVG mid→mid(underline 底中 → chip 顶中)· 帧 chip id 对齐 · L→R 密度
+31
View File
@@ -8,6 +8,37 @@
### Changed
## v1.4.5 — 2026-10-16
### Highlights
**Streamable HTTP MCP + web `/v1` 鉴权硬化**:`agent_mcp_serve` 支持 `transport=http`(MCP Streamable HTTP 2025-03-26);web invoke 注入 `Authorization` / `X-Api-Key`,`ok=false`+`unauthorized` → HTTP 401;`ext/ai` openai/agent 字段访问改 `json.get`,消除 footnote 硬崩。CLI · Ext · wasm **1.4.5**。
```bash
git checkout v1.4.5 && cargo build --release
cargo build --release -p marqdo_plugin_agent
# HTTP MCP:
# QDAGENT_MCP_TRANSPORT=http marqdo run cli/求道-mcp.mq.md
marqdo ext add agent # CDN → 1.4.5
```
**核心表面 Δ(Core surface Δ)**:**新增** MCP Streamable HTTP 传输(`transport=http|streamable-http`);web invoke 契约扩展(header 注入 · 401);**无** MQ 关键词 / 表几何变更。
### Added
- **`agent_mcp_serve` Streamable HTTP**:`transport=http` · `host`/`port`/`path`;POST JSON-RPC + 可选 GET SSE;`Mcp-Session-Id`。facade:`ext/ai/agent` `## serve`。
### Fixed
- **web invoke**:`Authorization` / `X-Api-Key` 注入 payload;`jsonWrapInvokeValue` 对 `unauthorized` 返回 401;`{sse}` 自动 event-stream。
- **`ext/ai/llm/openai` · `agent` · `llm`**:字段访问改 `json.get` / `at`,避免 `[field](x)` 在非 map 上硬崩。
- **explore workbook**:子任务 `stream=False`(网关 SSE 524 更稳)。
### Changed
- CLI / Ext / wasm SemVer **1.4.5**。
- 设计:[doc/design/ext-web-artifact.md](doc/design/ext-web-artifact.md)(sse / bearer)。
## v1.4.4 — 2026-10-16
### Highlights
Generated
+2 -2
View File
@@ -572,7 +572,7 @@ checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad"
[[package]]
name = "marqdo"
version = "1.4.4"
version = "1.4.5"
dependencies = [
"anyhow",
"assert_cmd",
@@ -603,7 +603,7 @@ dependencies = [
[[package]]
name = "marqdo-wasm"
version = "1.4.4"
version = "1.4.5"
dependencies = [
"marqdo",
"serde_json",
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "marqdo"
version = "1.4.4"
version = "1.4.5"
edition = "2021"
license = "Apache-2.0"
description = "Marqdo — Markdown markers as an interpreted programming language"
+7 -6
View File
@@ -149,10 +149,11 @@ marqdo catalog public -o .marqdo
---
## 现状(v1.4.4)
## 现状(v1.4.5)
- **Streamable HTTP MCP + web invoke 鉴权(v1.4.5)**:`agent_mcp_serve transport=http` · Bearer/`X-Api-Key` 注入 · `unauthorized`→401 · openai/agent `json.get` 硬化
- **LLM 传输软失败 + `ext/ai` 表优先(v1.4.4)**:上游不可达 → `status=error`(不 abort);`agent`/`llm` 去 `json.set` 胶水 · [agent-llm-transport-soft-2026-10.md](doc/research/agent-llm-transport-soft-2026-10.md)
- **Ext pack 1.4.3**:修复 `agent.run` cold EFI(`` `self`.preflight `` + miss resolve `llm_free`);已并入 **1.4.4** · [agent-run-efi-resolve-bugs-2026-10.md](doc/research/agent-run-efi-resolve-bugs-2026-10.md)
- **Ext pack 1.4.3**:修复 `agent.run` cold EFI(`` `self`.preflight `` + miss resolve `llm_free`);已并入 **≥1.4.4** · [agent-run-efi-resolve-bugs-2026-10.md](doc/research/agent-run-efi-resolve-bugs-2026-10.md)
- **Agent/LLM 优化 O1–O5(v1.4.2)**:`ext/ai/sandbox` CoW · `agent.trial` / `sandbox=` · promote sandbox proof · `step_route` / EFI / cost · style lint · quarantine · [ext-ai-optimize.md](doc/roadmap/ext-ai-optimize.md)
- **view 标注连线(v1.4.1)**:underline→chip SVG mid→mid(底边中点→轨道→短竖线→顶边中点)· 帧 chip 共用 id · L→R 密度 · [view-prose-annot-gaps.md](doc/design/view-prose-annot-gaps.md)
- **叙述统一 / 公式 meta / 图片自写回(v1.4.0)**:`% marqdo: var=` 块公式绑定 · 臂/循环体叙述与 foreach 中英糖 · `![marqdo-out](…)` 图片自写回 · view 推断形参实值 · [fence-meta-and-control-prose.md](doc/design/fence-meta-and-control-prose.md) · [stdlib-writeback.md](doc/design/stdlib-writeback.md)
@@ -173,7 +174,7 @@ marqdo catalog public -o .marqdo
- **`web`**:**Artifact 模型(ADR [0007](doc/adr/0007-web-document-endpoint.md))** — `type: web|endpoint` Document/Endpoint;facade `page`/`route`/`serve`/`render`/`inspect`/`use`;Resource 在 `ext/data` · `ext/security` · `ext/net`;设计 [ext-web-artifact.md](doc/design/ext-web-artifact.md);原生 **Go `libweb`**([ADR 0004](doc/adr/0004-web-plugin-go.md));示例 [marqdo-blog](examples/marqdo-blog/) · [web-client-site](examples/web-client-site/) · [anlian-mq](examples/anlian-mq/)
- **`quantum`**:Q0–Q7 + Q8a/b 主题 SVG;示例 [quantum-entanglement](examples/quantum-entanglement/)
- **`linalg`**:L0–L6 + 公式文档面(中缀 / `declare` / 展示化简);示例 [linalg-transpose](examples/linalg-transpose/) · [linalg-svd](examples/linalg-svd/) · [linalg-least-squares](examples/linalg-least-squares/)
- **`agent`**:A1–A4 + **MCP Server/Client stdio** + Skill Compilation + Adaptive Routing + resume + **Engineering preflight**(`agent.preflight` / `eng_preflight`);宿主缺口见 [ext-hosting.md](doc/roadmap/ext-hosting.md) · [agent-framework-gaps-after-a4.md](doc/research/agent-framework-gaps-after-a4.md) · [engineering-knowledge.md](doc/design/engineering-knowledge.md)
- **`agent`**:A1–A4 + **MCP Server/Client**(stdio + **Streamable HTTP**)+ Skill Compilation + Adaptive Routing + resume + **Engineering preflight**(`agent.preflight` / `eng_preflight`);宿主缺口见 [ext-hosting.md](doc/roadmap/ext-hosting.md) · [agent-framework-gaps-after-a4.md](doc/research/agent-framework-gaps-after-a4.md) · [engineering-knowledge.md](doc/design/engineering-knowledge.md)
- **`llm`**:语义 `ask`/`stream`/`collect`(OpenAI/Ollama;可选原生 `plugins/llm`)
- 安装:`marqdo ext list` / `add …` / `remove`([ext-cli.md](doc/design/ext-cli.md);默认 `~/.marqdo/ext`)。原生插件先 `cargo build -p marqdo_plugin_*` 再 `ext add`
- **原生插件 ABI**:[`include/marqdo_abi.h`](include/marqdo_abi.h) · [ext-abi.md](doc/design/ext-abi.md);`plugins/{demo,agent,web,quantum,linalg,llm}`(`web` 为 Go;其余为 Rust)
@@ -182,7 +183,7 @@ marqdo catalog public -o .marqdo
- **浏览器 Marqdo(WASM)**:`marqdo wasm build` + 官方 bridge 自启(作者零业务 JS;桥内可含列表/路由/storage/ws/文件/Canvas/音频/Observer/拖放);`lib/browser` + GFM;`web.client_embed`;示例 [browser-hello](examples/browser-hello/) · [browser-app](examples/browser-app/) · [browser-media](examples/browser-media/) · [web-client-site](examples/web-client-site/)([ADR 0002](doc/adr/0002-browser-marqdo-wasm.md) · [D](doc/roadmap/browser-wasm-d.md) · [E](doc/roadmap/browser-wasm-e.md) · [F](doc/roadmap/browser-wasm-f.md))
- 选型:[ADR 0001 — Rust](doc/adr/0001-implementation-language.md) · [ADR 0002 — 浏览器 WASM](doc/adr/0002-browser-marqdo-wasm.md)(C0–C5 完结,见 [roadmap/browser-wasm.md](doc/roadmap/browser-wasm.md))· [ADR 0003 — 异步效应](doc/adr/0003-browser-async-effects.md)
### 如何使用最新 Marqdo(v1.4.4)
### 如何使用最新 Marqdo(v1.4.5)
**安装解释器(任选其一)**
@@ -197,7 +198,7 @@ marqdo catalog public -o .marqdo
```bash
# 1) 源码安装(跟 tag 或 main)
git clone https://github.com/cflmy/marqdo.git && cd marqdo
git checkout v1.4.4 # 或留在 main
git checkout v1.4.5 # 或留在 main
cargo build --release
export PATH="$PWD/target/release:$PATH"
marqdo version
@@ -236,7 +237,7 @@ marqdo wasm build
# → dist/wasm/ … 见 examples/browser-hello/
# 6) Releases:Windows exe/zip/vsix;Linux CLI zip + native `.so` zip
# https://github.com/cflmy/marqdo/releases/tag/v1.4.4
# https://github.com/cflmy/marqdo/releases/tag/v1.4.5
```
开发期也可用 `cargo run -- …` 代替已安装的 `marqdo`:
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "marqdo-wasm"
version = "1.4.4"
version = "1.4.5"
edition = "2021"
license = "Apache-2.0"
description = "Marqdo interpreter as WebAssembly (browser host bridge)"
+8
View File
@@ -1,3 +1,11 @@
marqdo (1.4.5-1ppa1) noble; urgency=medium
* Marqdo 1.4.5: Streamable HTTP MCP (agent_mcp_serve transport=http);
web invoke Bearer/X-Api-Key inject + unauthorized→401; openai/agent
field access via json.get (no footnote hard-fail).
-- cflmy <pingan@cflmy.cn> Fri, 16 Oct 2026 17:40:00 +0800
marqdo (1.4.4-1ppa1) noble; urgency=medium
* Marqdo 1.4.4: Soft LLM transport errors (structured status=error);
+2 -2
View File
@@ -120,8 +120,8 @@ import data:ext/data/db.mq.md
| `path` | `路径` | URL |
| `method` | `方法` | GET/POST/… |
| `request` | `请求` | `json` / `form` / `none` |
| `response` | `响应` | `json` / `text` / `html` |
| `auth` | `鉴权` | 角色名或 `none` |
| `response` | `响应` | `json` / `text` / `html` / `sse`(`text/event-stream`) |
| `auth` | `鉴权` | 角色名或 `none`;`bearer` / `api_key` = 不要求 cookie,把 `Authorization` / `X-Api-Key` 注入 payload 由 `# main` 校验 |
正文在请求时执行;返回值按 `response` 序列化。Result 协议见 §8。
+1 -1
View File
@@ -1 +1 @@
1.4.4
1.4.5
+40 -34
View File
@@ -66,14 +66,14 @@ One LLM turn via the semantic surface (`ask` / `stream`+`collect`). Returns `{te
**err = None**
**calls = 1**
1. not `text`
**e0 = [1](evs)**
**e0 = > at value=`evs` index=0**
**err = > json.get value=`e0` key="message"**
1. `err`
**calls = 0**
2. *
**r = > model.ask prompt=`prompt`**
**text = [text](r)**
**tokens = [tokens](r)**
**text = > json.get value=`r` key="text"**
**tokens = > json.get value=`r` key="tokens"**
**err = > json.get value=`r` key="error"**
**fin = > json.get value=`r` key="finish"**
1. `tokens`
@@ -181,7 +181,7 @@ Allowlist check, then invoke the runbook `##` via `lib/subtask` (`spawn fn=` →
Build a runnable workbook. `skeleton=single` (default): one worker `step`. `skeleton=dual`: research then write agents. On success the parent chooses DONE; runtime `agent_workbook_solidify` freezes the answer.
**q = > json.quote text=`goal`**
**esc = > json.parse text={"h":"---\ntitle: agent workbook\nimport llm:ext/ai/llm.mq.md\nimport agent:ext/ai/agent.mq.md\nimport json:lib/json.mq.md\nimport sys:lib/sys.mq.md\nimport wb:lib/writeback.mq.md\nimport subtask:lib/subtask.mq.md\n---\n\n# Goal\n\nParent goal is embedded as JSON in # main.\n\n## Solidify\n\nWhen the child returns a good value, parent DECISION: DONE — runtime agent_workbook_solidify freezes the answer as **return** (do not invent long FIND/REPLACE).\n\n# main\n\n> llm.load_env\n\n","s1":"\u002a\u0060model\u0060 = > llm.llm \u002a\n","s2":"\u002a\u0060tools\u0060 = > table.empty_list \u002a\n","s3":"\u002a\u0060worker\u0060 = > agent.agent model=\u0060model\u0060 tools=\u0060tools\u0060 standing=You are a workbook worker. tools=[] means never CALL. Finish the task with a direct final answer; do not invent tools. \u002a\n","s4":"\u002a\u0060wrap\u0060 = > json.parse text={\"task\":","s5":"} \u002a\n","s6":"\u002a\u0060task\u0060 = > json.get value=\u0060wrap\u0060 key=task \u002a\n","s7":"\u002a\u0060out\u0060 = > \u0060worker\u0060.step task=\u0060task\u0060 stream=True \u002a\n","s7b":"\u002a\u0060text\u0060 = > json.get value=\u0060out\u0060 key=result \u002a\n","s8":"\u002a\u002a\u0060text\u0060\u002a\u002a\n","d3":"\u002a\u0060research\u0060 = > agent.agent model=\u0060model\u0060 tools=\u0060tools\u0060 standing=You gather facts for the goal. Be concise. Do not invent tools. \u002a\n","d3b":"\u002a\u0060writer\u0060 = > agent.agent model=\u0060model\u0060 tools=\u0060tools\u0060 standing=You write the final answer from research notes. Do not invent tools. \u002a\n","d4":"\u002a\u0060wrap\u0060 = > json.parse text={\"task\":","d5":"} \u002a\n","d6":"\u002a\u0060task\u0060 = > json.get value=\u0060wrap\u0060 key=task \u002a\n","d7":"\u002a\u0060notes\u0060 = > \u0060research\u0060.step task=\u0060task\u0060 stream=True \u002a\n","d8":"\u002a\u0060ns\u0060 = > json.stringify value=\u0060notes\u0060 \u002a\n","d9":"\u002a\u0060wt\u0060 = Write the final answer. Research notes: \u002a\n","d10":"\u002a\u0060wt\u0060 = \u0060wt\u0060 + \u0060ns\u0060 \u002a\n","d11":"\u002a\u0060out\u0060 = > \u0060writer\u0060.step task=\u0060wt\u0060 stream=True \u002a\n","d11b":"\u002a\u0060text\u0060 = > json.get value=\u0060out\u0060 key=result \u002a\n","d12":"\u002a\u002a\u0060text\u0060\u002a\u002a\n"}**
**esc = > json.parse text={"h":"---\ntitle: agent workbook\nimport llm:ext/ai/llm.mq.md\nimport agent:ext/ai/agent.mq.md\nimport json:lib/json.mq.md\nimport sys:lib/sys.mq.md\nimport wb:lib/writeback.mq.md\nimport subtask:lib/subtask.mq.md\n---\n\n# Goal\n\nParent goal is embedded as JSON in # main.\n\n## Solidify\n\nWhen the child returns a good value, parent DECISION: DONE — runtime agent_workbook_solidify freezes the answer as **return** (do not invent long FIND/REPLACE).\n\n# main\n\n> llm.load_env\n\n","s1":"\u002a\u0060model\u0060 = > llm.llm \u002a\n","s2":"\u002a\u0060tools\u0060 = > table.empty_list \u002a\n","s3":"\u002a\u0060worker\u0060 = > agent.agent model=\u0060model\u0060 tools=\u0060tools\u0060 standing=You are a workbook worker. tools=[] means never CALL. Finish the task with a direct final answer; do not invent tools. \u002a\n","s4":"\u002a\u0060wrap\u0060 = > json.parse text={\"task\":","s5":"} \u002a\n","s6":"\u002a\u0060task\u0060 = > json.get value=\u0060wrap\u0060 key=task \u002a\n","s7":"\u002a\u0060out\u0060 = > \u0060worker\u0060.step task=\u0060task\u0060 stream=False \u002a\n","s7b":"\u002a\u0060text\u0060 = > json.get value=\u0060out\u0060 key=result \u002a\n","s8":"\u002a\u002a\u0060text\u0060\u002a\u002a\n","d3":"\u002a\u0060research\u0060 = > agent.agent model=\u0060model\u0060 tools=\u0060tools\u0060 standing=You gather facts for the goal. Be concise. Do not invent tools. \u002a\n","d3b":"\u002a\u0060writer\u0060 = > agent.agent model=\u0060model\u0060 tools=\u0060tools\u0060 standing=You write the final answer from research notes. Do not invent tools. \u002a\n","d4":"\u002a\u0060wrap\u0060 = > json.parse text={\"task\":","d5":"} \u002a\n","d6":"\u002a\u0060task\u0060 = > json.get value=\u0060wrap\u0060 key=task \u002a\n","d7":"\u002a\u0060notes\u0060 = > \u0060research\u0060.step task=\u0060task\u0060 stream=False \u002a\n","d8":"\u002a\u0060ns\u0060 = > json.stringify value=\u0060notes\u0060 \u002a\n","d9":"\u002a\u0060wt\u0060 = Write the final answer. Research notes: \u002a\n","d10":"\u002a\u0060wt\u0060 = \u0060wt\u0060 + \u0060ns\u0060 \u002a\n","d11":"\u002a\u0060out\u0060 = > \u0060writer\u0060.step task=\u0060wt\u0060 stream=False \u002a\n","d11b":"\u002a\u0060text\u0060 = > json.get value=\u0060out\u0060 key=result \u002a\n","d12":"\u002a\u002a\u0060text\u0060\u002a\u002a\n"}**
**h = > json.get value=`esc` key="h"**
**s1 = > json.get value=`esc` key="s1"**
**s2 = > json.get value=`esc` key="s2"**
@@ -281,8 +281,8 @@ Opt-in CoW trial (O1): open → optional upper write → optional run → diff
Host tree unchanged until a successful commit. Return map is a GFM table (link-index readable).
**sb = sandbox.open root=`root` profile=`profile`**
**id = [id](sb)**
**prof = [profile](sb)**
**id = > json.get value=`sb` key="id"**
**prof = > json.get value=`sb` key="profile"**
**ok = True**
**stdout = ""**
**stderr = ""**
@@ -297,14 +297,14 @@ Host tree unchanged until a successful commit. Return map is a GFM table (link-i
1. `source`
**ret = sandbox.run id=`id` source=`source`**
**ok = [ok](ret)**
**stdout = [stdout](ret)**
**stderr = [stderr](ret)**
**ok = > json.get value=`ret` key="ok"**
**stdout = > json.get value=`ret` key="stdout"**
**stderr = > json.get value=`ret` key="stderr"**
2. `path`
**ret = sandbox.run id=`id` path=`path`**
**ok = [ok](ret)**
**stdout = [stdout](ret)**
**stderr = [stderr](ret)**
**ok = > json.get value=`ret` key="ok"**
**stdout = > json.get value=`ret` key="stdout"**
**stderr = > json.get value=`ret` key="stderr"**
3. *
**_ = 1**
@@ -360,9 +360,9 @@ With `sandbox=True`, run via `## trial` first (commit on ok); host unchanged unt
1. `sandbox`
**tr = trial path=`path` root=`sandbox_root` profile=`sandbox_profile` commit=True**
**ok = [ok](tr)**
**stdout = [stdout](tr)**
**stderr = [stderr](tr)**
**ok = > json.get value=`tr` key="ok"**
**stdout = > json.get value=`tr` key="stdout"**
**stderr = > json.get value=`tr` key="stderr"**
1. `ok`
**code = 0**
2. *
@@ -379,10 +379,10 @@ With `sandbox=True`, run via `## trial` first (commit on ok); host unchanged unt
2. *
**id = subtask.spawn path=`path`**
**waited = subtask.wait id=`id`**
**code = [code](waited)**
**value = [value](waited)**
**stdout = [stdout](waited)**
**stderr = [stderr](waited)**
**code = > json.get value=`waited` key="code"**
**value = > json.get value=`waited` key="value"**
**stdout = > json.get value=`waited` key="stdout"**
**stderr = > json.get value=`waited` key="stderr"**
**obs = inspect_workbook path=`path` exit_code=`code` value=`value` stdout=`stdout` stderr=`stderr`**
**out = **
@@ -1595,7 +1595,7 @@ LLM Plan → CALL/READ/DECISION loop. Returns `{decision,reply,observation,event
**evs = > model.stream prompt=`ctx` echo=`echo`**
**last_reply = > llm.collect events=`evs`**
**evs_all = > plan_merge_deltas events=`evs_all` from=`evs` stream=`stream`**
**e0 = [1](evs)**
**e0 = > at value=`evs` index=0**
**terr = > json.get value=`e0` key="message"**
**etype = > json.get value=`e0` key="type"**
1. `etype` == "error"
@@ -1903,7 +1903,7 @@ Optional `step_kind=` (O3) selects backend via module `## step_route` before the
1. `step_kind`
**sr = step_route kind=`step_kind`**
**backend = [backend](sr)**
**backend = > json.get value=`sr` key="backend"**
2. *
**_ = 1**
@@ -2054,9 +2054,9 @@ Primary author entry (v2 + P4): Adaptive `route` → execute (policy/kb skill) o
**path = > json.get value=`routed` key="resource"**
1. `path`
**aw = await_workbook path=`path` sandbox=`sandbox` sandbox_profile=`sandbox_profile`**
**code = [code](aw)**
**child_val = [value](aw)**
**last_obs = [observation](aw)**
**code = > json.get value=`aw` key="code"**
**child_val = > json.get value=`aw` key="value"**
**last_obs = > json.get value=`aw` key="observation"**
1. `code` == 0
> agent_kb_record_hit kb_dir=`kb_dir` goal=`task` improve_every=`improve_every`
**lf = > json.get value=`routed` key="llm_free"**
@@ -2094,9 +2094,9 @@ Primary author entry (v2 + P4): Adaptive `route` → execute (policy/kb skill) o
1. `am`
**path = > json.get value=`applied` key="resource"**
**aw = await_workbook path=`path` sandbox=`sandbox` sandbox_profile=`sandbox_profile`**
**code = [code](aw)**
**child_val = [value](aw)**
**last_obs = [observation](aw)**
**code = > json.get value=`aw` key="code"**
**child_val = > json.get value=`aw` key="value"**
**last_obs = > json.get value=`aw` key="observation"**
1. `code` == 0
**exec_mode = "routed"**
**sk = > json.get value=`applied` key="skill"**
@@ -2153,9 +2153,9 @@ Primary author entry (v2 + P4): Adaptive `route` → execute (policy/kb skill) o
1. `llm_free`
**path = > json.get value=`resolved` key="resource"**
**aw = await_workbook path=`path` sandbox=`sandbox` sandbox_profile=`sandbox_profile`**
**code = [code](aw)**
**child_val = [value](aw)**
**last_obs = [observation](aw)**
**code = > json.get value=`aw` key="code"**
**child_val = > json.get value=`aw` key="value"**
**last_obs = > json.get value=`aw` key="observation"**
1. `code` == 0
> agent_kb_record_hit kb_dir=`kb_dir` goal=`task` improve_every=`improve_every`
**exec_mode = "compiled"**
@@ -2977,7 +2977,7 @@ Deterministic success stop (loop engineering): when the child already returned a
# mcp_server
+ `name`=marqdo
Minimal MCP Server host (stdio JSON-RPC). Tools point at entry-module `lib.member` functions (same bridge as `ext/web` invoke). Evidence/results may carry `authority=workbook`.
Minimal MCP Server host (stdio JSON-RPC / Streamable HTTP). Tools point at entry-module `lib.member` functions (same bridge as `ext/web` invoke). Evidence/results may carry `authority=workbook`.
**p = > plugin.native_path name="agent"**
1. `p`
@@ -2986,11 +2986,12 @@ Minimal MCP Server host (stdio JSON-RPC). Tools point at entry-module `lib.membe
> print text=ext/ai/agent: native agent plugin not found (build marqdo_plugin_agent or marqdo ext add agent)
> sys.exit code=1
**tools0 = > table.empty_list**
`h` =
| _type | tools | name |
|-------|-------|------|
| mcp_server | [] | `name` |
| mcp_server | `tools0` | `name` |
*h*
@@ -3002,6 +3003,8 @@ Minimal MCP Server host (stdio JSON-RPC). Tools point at entry-module `lib.membe
Register one tool. `fn` must be `lib.member` imported on the entry workbook.
**tools = > json.get value=`self` key="tools"**
1. not `tools`
**tools = > table.empty_list**
`row` =
| name | fn | description |
@@ -3014,9 +3017,12 @@ Register one tool. `fn` must be `lib.member` imported on the entry workbook.
## serve
+ `transport`=stdio
+ `host`="127.0.0.1"
+ `port`=7434
+ `path`="/mcp"
Block on stdio MCP (`initialize` / `tools/list` / `tools/call`). HTTP transport is H4b.
Block on MCP: `stdio` (line JSON-RPC) or `http` / `streamable-http` (Streamable HTTP, POST JSON-RPC + optional GET SSE). Spec 2025-03-26.
**name = > json.get value=`self` key="name"**
**tools = > json.get value=`self` key="tools"**
*> agent_mcp_serve name=`name` tools=`tools` transport=`transport`*
*> agent_mcp_serve name=`name` tools=`tools` transport=`transport` host=`host` port=`port` path=`path`*
+14 -11
View File
@@ -26,14 +26,14 @@ Collapse a stream event list to final answer text (`done.result` or `error.messa
**answer = ""**
- [ev](events)
**t = [type](ev)**
**t = > json.get value=`ev` key="type"**
1. `t` == "done"
**answer = [result](ev)**
**answer = > json.get value=`ev` key="result"**
2. `t` == "error"
**answer = [message](ev)**
**answer = > json.get value=`ev` key="message"**
3. `t` == "delta"
4. `t` == "text"
**chunk = [text](ev)**
**chunk = > json.get value=`ev` key="text"**
1. `chunk`
**answer = answer + chunk**
2. *
@@ -168,7 +168,8 @@ Module convenience: create a default handle and return **answer text**. Pass `pa
**m = > create model=`model` base_url=`base_url` api_key=`api_key`**
**r = > m.ask prompt=`p`**
*[text](r)*
**_ret = > json.get value=`r` key="text"**
*_ret*
# llm
+ `model`=None
@@ -306,11 +307,11 @@ Hoist receiver fields before the result table — table cells can shadow `self`
column names (`model`, `usage`), so `[model](self)` / `[total_tokens](usage)`
inside the table is unsafe.
**mdl = [model](self)**
**b = [backend](self)**
**nm = [name](self)**
**mdl = > json.get value=`self` key="model"**
**b = > json.get value=`self` key="backend"**
**nm = > json.get value=`self` key="name"**
1. `usage`
**tt = [total_tokens](usage)**
**tt = > json.get value=`usage` key="total_tokens"**
2. *
**tt = 0**
1. not `tt`
@@ -369,10 +370,12 @@ Internal / compatibility primitive. Prefer `ask` / `stream` in new code.
1. `stream`
**pack = > self.complete_result prompt=`prompt` stream=True echo=`echo`**
*[events](pack)*
**_ret = > json.get value=`pack` key="events"**
*_ret*
2. *
**pack = > self.complete_result prompt=`prompt` stream=False echo=`echo`**
*[text](pack)*
**_ret = > json.get value=`pack` key="text"**
*_ret*
## chat
+ `prompt`
+14 -11
View File
@@ -104,8 +104,9 @@ POST `/chat/completions` (or stream SSE). Prefers `plugins/llm` ABI (`llm_chat_c
**req = > table.put in=`req` at="stream" value=True**
**body = > json.stringify value=`req`**
**resp = > net.http_post_sse url=`url` body=`body` headers=`headers` echo=`echo`**
1. [status](resp) == 200
**evs = [events](resp)**
**st = > json.get value=`resp` key="status"**
1. `st` == 200
**evs = > json.get value=`resp` key="events"**
`pack` =
@@ -116,11 +117,10 @@ POST `/chat/completions` (or stream SSE). Prefers `plugins/llm` ABI (`llm_chat_c
*pack*
2. *
Soft transport / HTTP failure — structured pack, never abort the VM.
**st = [status](resp)**
**msg = "HTTP " + `st`**
**evs = [events](resp)**
**evs = > json.get value=`resp` key="events"**
1. `evs`
**e0 = [1](evs)**
**e0 = > at value=`evs` index=0**
**m2 = > json.get value=`e0` key="message"**
1. `m2`
**msg = `m2`**
@@ -134,16 +134,20 @@ POST `/chat/completions` (or stream SSE). Prefers `plugins/llm` ABI (`llm_chat_c
2. *
**body = > json.stringify value=`req`**
**resp = > net.http_post url=`url` body=`body` headers=`headers`**
1. [status](resp) == 200
**data = > json.parse text=[body](resp)**
**msg = [message]([1]([choices](data)))**
**st = > json.get value=`resp` key="status"**
1. `st` == 200
**raw = > json.get value=`resp` key="body"**
**data = > json.parse text=`raw`**
**choices = > json.get value=`data` key="choices"**
**choice0 = > at value=`choices` index=0**
**msg = > json.get value=`choice0` key="message"**
**text = > json.get value=`msg` key="content"**
1. not `text`
**text = ""**
2. *
**_ = 1**
**usage = > usage_from data=`data`**
**fr = > json.get value=[1]([choices](data)) key="finish_reason"**
**fr = > json.get value=`choice0` key="finish_reason"**
1. not `fr`
**fr = "stop"**
2. *
@@ -159,8 +163,7 @@ POST `/chat/completions` (or stream SSE). Prefers `plugins/llm` ABI (`llm_chat_c
*pack*
2. *
Soft transport / HTTP failure — structured pack, never abort the VM.
**st = [status](resp)**
**raw = [body](resp)**
**raw = > json.get value=`resp` key="body"**
**msg = "HTTP " + `st`**
1. `raw`
**msg = `msg` + ": " + `raw`**
+336 -41
View File
@@ -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) {}
+64 -1
View File
@@ -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 {
+29 -12
View File
@@ -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")