Skip to content

Commit 84f520a

Browse files
Merge pull request #1622 from QueryaHub/issue/1487-headless-mcp
feat(dx): headless L2 MCP end to end (#1487, part 4)
2 parents aa4e715 + 7c1d3eb commit 84f520a

6 files changed

Lines changed: 379 additions & 25 deletions

File tree

‎.github/workflows/headless-smoke.yml‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ name: Headless smoke (L2)
22

33
# Builds the Linux bundle and runs the real app under Xvfb in portable mode with
44
# scripts/dev/headless_app.sh: it has to start, show a window and stay up. The
5-
# screenshot is kept as an artifact. Not part of ci.yml: a Linux build takes
5+
# screenshot is kept as an artifact. Then the MCP path end to end
6+
# (scripts/dev/headless_mcp_check.sh): the app with its MCP server on, a shared
7+
# fixture connection, and a client reading it through the real querya-mcp. Not part of ci.yml: a Linux build takes
68
# minutes, so this runs when the script or this file changes, weekly, and on
79
# demand.
810
on:
911
pull_request:
1012
paths:
1113
- scripts/dev/headless_app.sh
14+
- scripts/dev/headless_mcp_check.sh
15+
- scripts/dev/mcp_stdio_check.py
16+
- lib/core/mcp/**
17+
- packages/querya_mcp_bridge/**
1218
- .github/workflows/headless-smoke.yml
1319
schedule:
1420
- cron: "0 4 * * 2"
@@ -33,7 +39,7 @@ jobs:
3339
run: |
3440
sudo apt-get update
3541
sudo apt-get install -y clang cmake ninja-build pkg-config libgtk-3-dev \
36-
libsecret-1-dev xvfb xdotool imagemagick
42+
libsecret-1-dev xvfb xdotool imagemagick sqlite3
3743
3844
- name: Setup Flutter
3945
uses: subosito/flutter-action@v2
@@ -57,6 +63,20 @@ jobs:
5763
--log build/headless/app.log \
5864
--wait 90 --keep 5
5965
66+
- name: Build querya-mcp
67+
run: |
68+
cd packages/querya_mcp_bridge
69+
dart pub get
70+
dart compile exe bin/querya_mcp.dart -o "$RUNNER_TEMP/querya-mcp"
71+
72+
- name: MCP end to end under Xvfb
73+
run: |
74+
mkdir -p build/headless
75+
scripts/dev/headless_mcp_check.sh \
76+
--mcp "$RUNNER_TEMP/querya-mcp" \
77+
--bundle build/linux/x64/release/bundle \
78+
--log build/headless/mcp-app.log
79+
6080
- name: Keep the screenshot and the log
6181
if: always()
6282
uses: actions/upload-artifact@v4

‎docs/dev-containers.md‎

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ command and each runnable in a container or on a CI runner
130130
| L0 | unit, widget, e2e and guard tests | `flutter test --exclude-tags=golden` |
131131
| L1 | an extension against a real database, no GUI | `querya-ext-tester ./ext --connect '{...}' --sandbox` (see *Testing an extension*) |
132132
| L2 | the real app under a virtual display, in portable mode | `scripts/dev/headless_app.sh` |
133+
| L2 + MCP | the same, with the MCP server on and a client reading a fixture through `querya-mcp` | `scripts/dev/headless_mcp_check.sh --mcp PATH` |
134+
135+
What each level needs from the database stack (start only that, by service name):
136+
137+
| Level | Containers |
138+
|-------|------------|
139+
| L0 | none; the live MySQL test needs `docker compose --profile live-tests up -d --wait mysql-live` |
140+
| L1 | the database the extension talks to, e.g. `docker compose up -d --wait postgres` (or `clickhouse`, `redis`, `mongo`) |
141+
| L2, L2 + MCP | none: the fixture is a throw-away SQLite file |
133142

134143
L1 needs a tester built from 0.4.19 or newer: older ones exited 0 whatever the
135144
result.
@@ -157,9 +166,36 @@ The [Headless smoke (L2)](../.github/workflows/headless-smoke.yml) workflow buil
157166
the Linux bundle and runs the script (on changes to it, weekly, and on demand),
158167
keeping the screenshot and the log as an artifact.
159168

160-
Whether L2 should grow a real "headless Querya" (core services without a window,
161-
so that `querya-mcp` does not need a running Desktop) is left open until this
162-
exists.
169+
Two more options let a script drive the running app:
170+
171+
- `--seed-sql FILE` applies SQL to the profile database (`querya.db`). The app
172+
starts once to create and migrate it, is stopped, `sqlite3` applies the file,
173+
and the app starts again. Use the same `app_settings` keys and tables the UI
174+
writes, so a test needs no code in the app.
175+
- `--run COMMAND` runs a command once the window is up and keeps the app alive
176+
meanwhile; its exit code becomes the script's. The command gets
177+
`QUERYA_MCP_ENDPOINT` (this run's own endpoint file, so no other instance can
178+
answer), `QUERYA_HEADLESS_DB` and `QUERYA_HEADLESS_DATA`.
179+
180+
### L2 + MCP: `scripts/dev/headless_mcp_check.sh`
181+
182+
```bash
183+
dart compile exe packages/querya_mcp_bridge/bin/querya_mcp.dart -o build/querya-mcp
184+
scripts/dev/headless_mcp_check.sh --mcp build/querya-mcp --bundle build/linux/x64/release/bundle
185+
```
186+
187+
It creates a fixture SQLite database with two connections, turns the MCP server
188+
on and shares only one of them (the same settings keys the Preferences page
189+
writes), starts the app under Xvfb and runs `scripts/dev/mcp_stdio_check.py`: a
190+
small MCP client that talks to the real `querya-mcp` and checks `initialize`,
191+
`tools/list`, that only the shared connection is listed, the table, its columns
192+
and data, that a `DELETE` is refused and changes nothing, and that the unshared
193+
connection is refused. It needs `sqlite3` and `python3` besides what L2 needs.
194+
The same step runs in the Headless smoke workflow.
195+
196+
A window under Xvfb is all the MCP server needs, so there is no separate
197+
"headless Querya" mode: `querya-mcp` still needs a running Desktop, and the
198+
virtual display provides it.
163199

164200
## Run the app in the container
165201

@@ -245,11 +281,27 @@ See [extension-testing.md](extension-testing.md) for the options, reports and
245281
the GitHub Action. The exit code is 0 on success, 1 when a check failed and 2 on
246282
a usage error.
247283

284+
## For extension authors
285+
286+
You do not need this repository to write a database-driver extension. Start from
287+
[QueryaHub/extension-template](https://github.com/QueryaHub/extension-template)
288+
(a Rust driver with a dev container, a test database and CI). The pieces it uses:
289+
290+
- the **Dev Container Feature** `ghcr.io/queryahub/features/querya-ext-tester`
291+
installs `querya-ext-tester` from a release, checked against `SHA256SUMS.txt`
292+
(see [extension-testing.md](extension-testing.md#dev-container-feature));
293+
- **test database images** `ghcr.io/queryahub/querya-testdb-{postgres,mysql,mongo,clickhouse,redis}:<version>`
294+
are the `docker/<db>` seeds baked into the official images, no bind mounts.
295+
Built from `docker/<db>/Dockerfile.testdb`; a new `Dockerfile.testdb` adds a
296+
database to the build by itself.
297+
248298
## Known gaps
249299

250-
- **The extension-author container** (Dev Container Feature, test database
251-
images, template repository, registry CI) is not done yet; it is tracked in
252-
[#1487](https://github.com/QueryaHub/Querya-Desktop/issues/1487).
300+
- **The extension registry** (registry CI and `StaticRegistryRepository`) and the
301+
release hooks that publish the Feature and the images are not done yet; they
302+
are tracked in [#1487](https://github.com/QueryaHub/Querya-Desktop/issues/1487).
303+
Until then the images are published by hand with the *Test database images*
304+
workflow.
253305

254306
## Troubleshooting
255307

10.8 KB
Binary file not shown.

‎scripts/dev/headless_app.sh‎

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,19 @@
1212
# (default 3); catches crashes right after start
1313
# --log FILE copy the app's stdout / stderr there
1414
# --keep-data do not delete the temporary data directory
15+
# --seed-sql FILE SQL applied to the profile database (querya.db) before
16+
# the app under test starts: the app runs once to create
17+
# and migrate the database, is stopped, FILE is applied
18+
# with sqlite3, and the app starts again
19+
# --run COMMAND shell command to run once the window is up (the app
20+
# stays up meanwhile); the exit code is its exit code.
21+
# Gets QUERYA_MCP_ENDPOINT (this run's own endpoint file),
22+
# QUERYA_HEADLESS_DB and QUERYA_HEADLESS_DATA
1523
# -h, --help this text
1624
#
17-
# Exit codes: 0 the app started, showed a window and stayed up; 1 it died, or no
18-
# window appeared in time; 2 usage error, missing bundle or missing tool.
25+
# Exit codes: 0 the app started, showed a window and stayed up (and --run
26+
# succeeded); 1 it died, no window appeared in time, or --run failed; 2 usage
27+
# error, missing bundle or missing tool.
1928
#
2029
# Needs: Xvfb; xdotool or xwininfo to see the window (without them the check is
2130
# "the process is still alive after --wait"); ImageMagick `import` for
@@ -28,6 +37,8 @@ wait_s=60
2837
keep_s=3
2938
log_out=""
3039
keep_data=0
40+
seed_sql=""
41+
run_cmd=""
3142

3243
usage() { sed -n '2,/^set -u/p' "$0" | sed '$d' | sed 's/^# \{0,1\}//'; }
3344
die() { printf 'headless_app: %s\n' "$*" >&2; exit 2; }
@@ -41,6 +52,8 @@ while [ $# -gt 0 ]; do
4152
--keep) keep_s="${2:-}"; shift 2 ;;
4253
--log) log_out="${2:-}"; shift 2 ;;
4354
--keep-data) keep_data=1; shift ;;
55+
--seed-sql) seed_sql="${2:-}"; shift 2 ;;
56+
--run) run_cmd="${2:-}"; shift 2 ;;
4457
-h | --help) usage; exit 0 ;;
4558
*) die "unknown option: $1 (see --help)" ;;
4659
esac
@@ -54,6 +67,10 @@ fi
5467
[ -n "$bundle" ] || die "no bundle found; build one (flutter build linux) or pass --bundle DIR"
5568
[ -x "$bundle/querya_desktop" ] || die "$bundle/querya_desktop is missing or not executable"
5669
command -v Xvfb >/dev/null 2>&1 || die "Xvfb is not installed (apt-get install xvfb)"
70+
if [ -n "$seed_sql" ]; then
71+
[ -f "$seed_sql" ] || die "--seed-sql: $seed_sql is not a file"
72+
command -v sqlite3 >/dev/null 2>&1 || die "--seed-sql needs the sqlite3 command (apt-get install sqlite3)"
73+
fi
5774
if [ -n "$screenshot" ] && ! command -v import >/dev/null 2>&1; then
5875
die "--screenshot needs ImageMagick's 'import' (apt-get install imagemagick)"
5976
fi
@@ -125,13 +142,32 @@ cp -a "$bundle" "$work/bundle" || die "could not copy $bundle"
125142
export QUERYA_PORTABLE=1
126143
export NO_AT_BRIDGE=1
127144

145+
# Own endpoint file, so that a second app on the machine (or a leftover one)
146+
# can never be the one querya-mcp talks to.
147+
export QUERYA_MCP_ENDPOINT="$work/mcp-endpoint.json"
148+
128149
runner=()
129150
if command -v dbus-run-session >/dev/null 2>&1; then
130151
runner=(dbus-run-session --)
131152
fi
132-
${runner[@]+"${runner[@]}"} "$work/bundle/querya_desktop" >"$work/app.log" 2>&1 &
133-
app_pid=$!
134-
say "started querya_desktop (pid $app_pid)"
153+
154+
start_app() {
155+
${runner[@]+"${runner[@]}"} "$work/bundle/querya_desktop" >>"$work/app.log" 2>&1 &
156+
app_pid=$!
157+
say "started querya_desktop (pid $app_pid)"
158+
}
159+
160+
stop_app() {
161+
[ -n "$app_pid" ] || return 0
162+
kill -TERM "$app_pid" 2>/dev/null
163+
for _ in $(seq 1 20); do
164+
kill -0 "$app_pid" 2>/dev/null || break
165+
sleep 0.5
166+
done
167+
kill -KILL "$app_pid" 2>/dev/null
168+
wait "$app_pid" 2>/dev/null
169+
app_pid=""
170+
}
135171

136172
have_window_tool=0
137173
if command -v xdotool >/dev/null 2>&1 || command -v xwininfo >/dev/null 2>&1; then
@@ -147,21 +183,37 @@ window_up() {
147183
fi
148184
}
149185

150-
deadline=$((SECONDS + wait_s))
151-
shown=0
152-
while [ "$SECONDS" -lt "$deadline" ]; do
153-
kill -0 "$app_pid" 2>/dev/null || fail_with_log "the app exited before showing a window"
186+
wait_for_window() {
187+
local deadline=$((SECONDS + wait_s)) shown=0
188+
while [ "$SECONDS" -lt "$deadline" ]; do
189+
kill -0 "$app_pid" 2>/dev/null || fail_with_log "the app exited before showing a window"
190+
if [ "$have_window_tool" -eq 1 ]; then
191+
if window_up; then shown=1; break; fi
192+
fi
193+
sleep 1
194+
done
154195
if [ "$have_window_tool" -eq 1 ]; then
155-
if window_up; then shown=1; break; fi
196+
[ "$shown" -eq 1 ] || fail_with_log "no window after ${wait_s}s"
197+
say "window is up"
198+
else
199+
say "no xdotool / xwininfo: only checking that the process stays alive"
156200
fi
157-
sleep 1
158-
done
201+
}
159202

160-
if [ "$have_window_tool" -eq 1 ]; then
161-
[ "$shown" -eq 1 ] || fail_with_log "no window after ${wait_s}s"
162-
say "window is up"
163-
else
164-
say "no xdotool / xwininfo: only checking that the process stays alive"
203+
start_app
204+
wait_for_window
205+
206+
if [ -n "$seed_sql" ]; then
207+
# The first start created and migrated the profile database. Let it settle,
208+
# stop the app (it holds the file open), seed, and start again.
209+
sleep 2
210+
stop_app
211+
db="$(find "$work/bundle" -name querya.db -print -quit 2>/dev/null)"
212+
[ -n "$db" ] || fail_with_log "the profile database querya.db was not created"
213+
sqlite3 "$db" <"$seed_sql" || fail_with_log "could not apply $seed_sql to $db"
214+
say "seeded $(basename "$seed_sql") into $db"
215+
start_app
216+
wait_for_window
165217
fi
166218

167219
end=$((SECONDS + keep_s))
@@ -180,5 +232,16 @@ if [ -n "$screenshot" ]; then
180232
fi
181233
fi
182234

235+
if [ -n "$run_cmd" ]; then
236+
export QUERYA_HEADLESS_DB
237+
QUERYA_HEADLESS_DB="$(find "$work/bundle" -name querya.db -print -quit 2>/dev/null)"
238+
export QUERYA_HEADLESS_DATA="$work/bundle/QueryaData"
239+
say "running: $run_cmd"
240+
if ! bash -c "$run_cmd"; then
241+
fail_with_log "--run command failed"
242+
fi
243+
kill -0 "$app_pid" 2>/dev/null || fail_with_log "the app died while the command ran"
244+
fi
245+
183246
say "ok"
184247
exit 0

‎scripts/dev/headless_mcp_check.sh‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env bash
2+
# L2 headless MCP end to end: the real app under Xvfb with its MCP server on,
3+
# one fixture SQLite connection shared, and a client talking MCP to it through
4+
# the real `querya-mcp` bridge.
5+
#
6+
# scripts/dev/headless_mcp_check.sh --mcp PATH [--bundle DIR] [--log FILE]
7+
#
8+
# --mcp PATH the querya-mcp executable (dart compile exe
9+
# packages/querya_mcp_bridge/bin/querya_mcp.dart -o querya-mcp)
10+
# --bundle DIR built bundle, as for headless_app.sh
11+
# --log FILE the app's output, as for headless_app.sh
12+
#
13+
# Needs what headless_app.sh needs, plus sqlite3 and python3. Exit codes are
14+
# headless_app.sh's: 0 all checks passed, 1 something failed, 2 usage / tools.
15+
set -euo pipefail
16+
17+
here="$(cd "$(dirname "$0")" && pwd)"
18+
mcp=""
19+
pass=()
20+
while [ $# -gt 0 ]; do
21+
case "$1" in
22+
--mcp) mcp="${2:-}"; shift 2 ;;
23+
--bundle | --log) pass+=("$1" "${2:-}"); shift 2 ;;
24+
-h | --help) sed -n '2,/^set -euo/p' "$0" | sed '$d' | sed 's/^# \{0,1\}//'; exit 0 ;;
25+
*) echo "headless_mcp_check: unknown option: $1" >&2; exit 2 ;;
26+
esac
27+
done
28+
[ -x "$mcp" ] || { echo "headless_mcp_check: --mcp must point at the querya-mcp executable" >&2; exit 2; }
29+
command -v python3 >/dev/null 2>&1 || { echo "headless_mcp_check: python3 is missing" >&2; exit 2; }
30+
command -v sqlite3 >/dev/null 2>&1 || { echo "headless_mcp_check: sqlite3 is missing" >&2; exit 2; }
31+
32+
tmp="$(mktemp -d "${TMPDIR:-/tmp}/querya-mcp-check.XXXXXX")"
33+
trap 'rm -rf "$tmp"' EXIT
34+
35+
# The database the model will query.
36+
sqlite3 "$tmp/fixture.db" <<'SQL'
37+
CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price REAL NOT NULL);
38+
INSERT INTO items (name, price) VALUES ('alpha', 10), ('beta', 20), ('gamma', 30);
39+
SQL
40+
41+
# What the settings page would store: the connection, the server on, the
42+
# connection shared. A second connection stays unshared, so the check can see
43+
# that sharing is opt-in.
44+
cat >"$tmp/seed.sql" <<SQL
45+
INSERT INTO connections (id, type, name, host, created_at)
46+
VALUES (101, 'sqlite', 'mcp-fixture', '$tmp/fixture.db', datetime('now')),
47+
(102, 'sqlite', 'not-shared', '$tmp/fixture.db', datetime('now'));
48+
INSERT OR REPLACE INTO app_settings (key, value) VALUES ('mcp_server_enabled', 'true');
49+
INSERT OR REPLACE INTO app_settings (key, value) VALUES ('mcp_read_connection_ids', '[101]');
50+
SQL
51+
52+
status=0
53+
"$here/headless_app.sh" ${pass[@]+"${pass[@]}"} \
54+
--seed-sql "$tmp/seed.sql" \
55+
--keep 3 \
56+
--run "python3 '$here/mcp_stdio_check.py' --mcp '$mcp' --connection mcp-fixture --unshared-id 102" \
57+
|| status=$?
58+
exit "$status"

0 commit comments

Comments
 (0)