feat(web,agent): hosting H1–H4 — proxy, invoke, MCP stdio, cross-module db

Close GAP-01–05 with ext/web streaming reverse proxy and HTTP→## invoke,
transitive object method lookup, corpus_search plugin ensure, and agent MCP
server stdio so downstream apps can drop Python side relays.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-07 22:14:51 +08:00
1 parent 2e068e66d4
commit fc543f1ca5
31 files changed
+2765 -102

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
---
title: agent mcp server smoke helpers
description: Tool target for MCP server smoke.
---
## ping
+ `payload`=None
`out` =
| ok | via |
|----|-----|
| true | mcp |
**out**
+32
View File
@@ -0,0 +1,32 @@
---
title: agent mcp server smoke
description: Offline assemble mcp_server + one tool (no serve).
import agent:ext/ai/agent.mq.md
import plugin:lib/plugin.mq.md
import sys:lib/sys.mq.md
import demo:agent-mcp-server-helpers.mq.md
---
# main
*p = > plugin.native_path name="agent"*
1. `p`
> plugin.load path=`p`
2. *
> sys.exit code=1
*srv = > agent.mcp_server name="marqdo-test"*
*srv = > `srv`.tool name="ping" fn="demo.ping" description="ping tool"*
*tools = srv[^tools]*
*n = > len value=`tools`*
1. `n` == 1
> print text=mcp-tool-ok
2. *
> print text=mcp-tool-fail
*t0 = tools[^1]*
*fn = t0[^fn]*
1. `fn` == "demo.ping"
> print text=mcp-fn-ok
2. *
> print text=mcp-fn-fail
+26
View File
@@ -0,0 +1,26 @@
---
title: web db cross-module lib
description: Opens sqlite, inits a tiny table, returns the db handle.
import web:ext/web/web.mq.md
---
## open
*store = > web.db url="sqlite::memory:"*
`fields` =
| name | type |
|------|------|
| title | text |
> `store`.init name=items fields=`fields`
`seed` =
| @ | title |
|---|-------|
| 1 | hello |
> `store`.insert table=items rows=`seed`
**store**
+24
View File
@@ -0,0 +1,24 @@
---
title: web db cross-module smoke
description: Return a web.db handle from an imported module and call select in the entry module.
import web:ext/web/web.mq.md
import plugin:lib/plugin.mq.md
import sys:lib/sys.mq.md
import storemod:web-db-cross-module-lib.mq.md
---
# main
*p = > plugin.native_path name="web"*
1. `p`
> plugin.load path=`p`
2. *
> sys.exit code=1
*store = > storemod.open*
*rows = > `store`.select table="items" limit=10*
*n = > len value=`rows`*
1. `n` >= 1
> print text=cross-db-ok
2. *
> print text=cross-db-fail
+16
View File
@@ -0,0 +1,16 @@
---
title: web hosting live helpers
description: Tiny lib used by hosting live-server invoke route.
---
## echo
+ `msg`=""
+ `payload`=None
`out` =
| ok | msg |
|----|-----|
| true | `msg` |
**out**
+22
View File
@@ -0,0 +1,22 @@
---
title: web hosting live server
description: Listen with proxy (→ mock upstream) and invoke routes for gold.rs.
import web:ext/web/web.mq.md
import plugin:lib/plugin.mq.md
import sys:lib/sys.mq.md
import demo:web-hosting-helpers.mq.md
---
# main
*p = > plugin.native_path name="web"*
1. `p`
> plugin.load path=`p`
2. *
> sys.exit code=1
*pg = > web.page title="hosting-live"*
*app = > web.app page=`pg` host=127.0.0.1 port=18141*
*app = > `app`.proxy path="/proxy/sse" upstream="http://127.0.0.1:18142/sse" stream=True methods="POST"*
*app = > `app`.invoke path="/api/echo" fn="demo.echo" method="POST" body="json"*
> `app`.listen
+75
View File
@@ -0,0 +1,75 @@
---
title: web proxy/invoke smoke
description: Offline assemble of app.proxy and app.invoke routes (no listen).
import web:ext/web/web.mq.md
import plugin:lib/plugin.mq.md
import sys:lib/sys.mq.md
---
# main
*p = > plugin.native_path name="web"*
1. `p`
> plugin.load path=`p`
2. *
> sys.exit code=1
`proxy_tbl` =
| path | upstream | stream | strip_prefix | methods | headers_from_env | timeout_ms |
|------|----------|--------|--------------|---------|------------------|------------|
| /proxy/chat | $OPENAI_BASE_URL | True | /proxy | POST | OPENAI_API_KEY=Authorization | 60000 |
`invoke_tbl` =
| path | method | fn | body | return |
|------|--------|-----|------|--------|
| /api/echo | POST | demo.echo | json | json |
*pg = > web.page title="proxy-invoke"*
*app = > web.app page=`pg`*
*app = > `app`.configure proxy=`proxy_tbl` invoke=`invoke_tbl`*
*app = > `app`.proxy path="/v1/stream" upstream="https://example.com/v1/chat" stream=True headers_from_env="TOKEN=X-Token"*
*app = > `app`.invoke path="/api/ping" fn="demo.ping" method="GET" body="query"*
*proutes = app[^proxy_routes]*
*chat = proutes[^proxy/chat]*
*up = chat[^upstream]*
1. `up` == "$OPENAI_BASE_URL"
> print text=proxy-table-ok
2. *
> print text=proxy-table-fail
*stream = chat[^stream]*
1. `stream`
> print text=proxy-stream-ok
2. *
> print text=proxy-stream-fail
*hdr = chat[^headers_from_env]*
1. `hdr` == "OPENAI_API_KEY=Authorization"
> print text=proxy-env-ok
2. *
> print text=proxy-env-fail
*v1 = proutes[^v1/stream]*
*v1u = v1[^upstream]*
1. `v1u` == "https://example.com/v1/chat"
> print text=proxy-method-ok
2. *
> print text=proxy-method-fail
*iroutes = app[^invoke_routes]*
*echo = iroutes[^api/echo]*
*efn = echo[^fn]*
1. `efn` == "demo.echo"
> print text=invoke-table-ok
2. *
> print text=invoke-table-fail
*ping = iroutes[^api/ping]*
*pm = ping[^method]*
1. `pm` == "GET"
> print text=invoke-method-ok
2. *
> print text=invoke-method-fail
+161
View File
@@ -1952,6 +1952,167 @@ fn ext_web_middleware_live() {
let _ = child.wait();
}
#[test]
fn ext_web_proxy_invoke_smoke() {
let status = Command::new("cargo")
.args(["build", "-p", "marqdo_plugin_web"])
.current_dir(env!("CARGO_MANIFEST_DIR"))
.status()
.expect("build web plugin");
assert!(status.success(), "failed to build marqdo_plugin_web");
assert_out(
"tests/ext/web-proxy-invoke-smoke.mq.md",
"proxy-table-ok
proxy-stream-ok
proxy-env-ok
proxy-method-ok
invoke-table-ok
invoke-method-ok",
);
}
#[test]
fn ext_web_hosting_live() {
// Mock upstream SSE on 18142 + Marqdo web proxy/invoke on 18141.
let status = Command::new("cargo")
.args(["build", "-p", "marqdo_plugin_web"])
.current_dir(env!("CARGO_MANIFEST_DIR"))
.status()
.expect("build web plugin");
assert!(status.success(), "failed to build marqdo_plugin_web");
use std::io::{Read, Write};
use std::net::TcpListener;
let upstream = TcpListener::bind("127.0.0.1:18142").expect("bind mock upstream");
upstream.set_nonblocking(true).ok();
let upstream_thread = std::thread::spawn(move || {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);
loop {
if std::time::Instant::now() > deadline {
break;
}
match upstream.accept() {
Ok((mut sock, _)) => {
let mut buf = [0u8; 4096];
let _ = sock.set_read_timeout(Some(std::time::Duration::from_secs(2)));
let _ = sock.read(&mut buf);
let body = "data: {\"id\":1}\n\ndata: {\"id\":2}\n\n";
let resp = format!(
"HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
body.len(),
body
);
let _ = sock.write_all(resp.as_bytes());
let _ = sock.flush();
}
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
std::thread::sleep(std::time::Duration::from_millis(50));
}
Err(_) => break,
}
}
});
let script = "tests/ext/web-hosting-live-server.mq.md";
let bin = env!("CARGO_BIN_EXE_marqdo");
let mut child = Command::new(bin)
.args(["run", script])
.current_dir(env!("CARGO_MANIFEST_DIR"))
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("spawn hosting live server");
let base = "http://127.0.0.1:18141";
let ready = std::time::Instant::now();
loop {
if ready.elapsed().as_secs() > 15 {
let _ = child.kill();
panic!("hosting live server did not start in time");
}
let out = Command::new("curl")
.args(["-s", "-o", "/dev/null", "-w", "%{http_code}", &format!("{base}/")])
.output()
.expect("curl probe");
if String::from_utf8_lossy(&out.stdout) == "200" {
break;
}
std::thread::sleep(std::time::Duration::from_millis(300));
}
// Proxy SSE: expect both data frames.
let out = Command::new("curl")
.args([
"-s",
"-N",
"-X",
"POST",
&format!("{base}/proxy/sse"),
"-H",
"Content-Type: application/json",
"-d",
"{}",
])
.output()
.expect("curl proxy");
let body = String::from_utf8_lossy(&out.stdout);
assert!(
body.contains("data: {\"id\":1}") && body.contains("data: {\"id\":2}"),
"proxy sse body={body}"
);
// Invoke: POST JSON → demo.echo
let out = Command::new("curl")
.args([
"-s",
"-X",
"POST",
&format!("{base}/api/echo"),
"-H",
"Content-Type: application/json",
"-d",
r#"{"msg":"hi"}"#,
])
.output()
.expect("curl invoke");
let body = String::from_utf8_lossy(&out.stdout);
assert!(
body.contains("\"ok\"") && body.contains("\"msg\":\"hi\""),
"invoke body={body}"
);
let _ = child.kill();
let _ = child.wait();
let _ = upstream_thread.join();
}
#[test]
fn ext_web_db_cross_module_smoke() {
let status = Command::new("cargo")
.args(["build", "-p", "marqdo_plugin_web"])
.current_dir(env!("CARGO_MANIFEST_DIR"))
.status()
.expect("build web plugin");
assert!(status.success(), "failed to build marqdo_plugin_web");
assert_out("tests/ext/web-db-cross-module-smoke.mq.md", "cross-db-ok");
}
#[test]
fn ext_agent_mcp_server_smoke() {
let status = Command::new("cargo")
.args(["build", "-p", "marqdo_plugin_agent"])
.current_dir(env!("CARGO_MANIFEST_DIR"))
.status()
.expect("build agent plugin");
assert!(status.success(), "failed to build marqdo_plugin_agent");
assert_out(
"tests/ext/agent-mcp-server-smoke.mq.md",
"mcp-tool-ok
mcp-fn-ok",
);
}
#[test]
fn ext_web_security_smoke() {
let status = Command::new("cargo")