Merge feat/mcp-server (9708154) into feat/mcp-auth
The base branch moved twelve commits while this work was in flight --
including the per-session live stream and its own contract bump -- so this
integrates rather than leaving a PR nobody can merge. Six files
conflicted; the interesting resolutions are the two that were not
textual.
**The contract is mcp-v3.1, not v2.1.** This branch bumped v2 -> v2.1 for
an additive change; `feat/mcp-server` bumped v2 -> v3 for the live stream,
also additive. Both point releases, both correct, and they cannot both be
the same document. So v3 stands as written and the HTTP transport is the
point release on top of it, by the same rule each of us applied
independently: v2 is still the only major bump, because it is still the
only one that removed anything.
**The counts are no longer literals.** This branch asserted `21 tools` and
`5 resources` in `checks/vm-mcp-http.nix` and `5` in `test_mcp_http.py`;
the stream added two tools and one fixed resource, so the surface is 23
and 6. Rather than re-hardcode numbers that will go stale again, the
check now counts against `--list-tools` and `--list-resources` -- the
server's own table -- and the unit test against `resources.FIXED`. That
is the lesson 9708154 ("count the unnarrowed resources against the
listing, not a literal") had already learned on the other side of this
merge, arrived at twice. A stale number here would have failed in a way
indistinguishable from the HTTP transport dropping tools, which is the
one thing those subtests exist to catch.
The rest were additive on both sides and both halves are kept: the TODO
table, the checks wiring, the README check rows, the architecture diagram
(their 23/10 counts with this branch's two-transport annotation).
`nix/module.nix` needed reconstructing -- git interleaved the bodies of
`hhstream-live` and `hhstream-mcp` because both units land between the
same surrounding lines -- so the live unit is taken verbatim and the MCP
unit placed after it, with this branch's options, tmpfiles entry,
firewall rule, two assertions and two warnings re-applied on top.
One thing deliberately NOT touched: the v3 change table cites `§6` for the
idle-clock exception, but the rule it means is `§8`. That is the other
branch's prose and its author's call, not something to quietly rewrite in
a merge.
Verified after resolution: 394 unit tests pass, and the module evaluates
with both units, the right ExecStart, one firewall port and both
warnings.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KchcvnbfhhZhUHy83YL3Ya
This commit is contained in:
commit
f83717ba71
37 files changed
+4241
-101
No files matched your search
@@ -29,6 +29,7 @@ import pytest
|
||||
|
||||
from hhstream_mcp import authorizations as auth
|
||||
from hhstream_mcp import http as http_transport
|
||||
from hhstream_mcp import resources as mcp_resources
|
||||
from hhstream_mcp import tools
|
||||
from hhstream_mcp.invoke import Runner
|
||||
|
||||
@@ -298,10 +299,10 @@ def test_the_full_surface_is_offered_to_an_unrestricted_token(store):
|
||||
with TestClient(make_app(store)) as client:
|
||||
extra = session(client, token)
|
||||
listed = call(client, token, "tools/list", headers=extra)["result"]["tools"]
|
||||
resources = call(client, token, "resources/list", headers=extra,
|
||||
request_id=3)["result"]["resources"]
|
||||
listed_resources = call(client, token, "resources/list", headers=extra,
|
||||
request_id=3)["result"]["resources"]
|
||||
assert len(listed) == len(tools.TOOLS)
|
||||
assert len(resources) == 5
|
||||
assert len(listed_resources) == len(mcp_resources.FIXED)
|
||||
|
||||
|
||||
def test_using_a_token_stamps_it(store):
|
||||
|
||||
@@ -42,11 +42,13 @@ EXPECTED_FIXED = {
|
||||
"hhstream://sessions",
|
||||
"hhstream://pointer",
|
||||
"hhstream://containers",
|
||||
"hhstream://streams",
|
||||
"hhstream://screen",
|
||||
}
|
||||
EXPECTED_TEMPLATES = {
|
||||
"hhstream://sessions/{id}{?events}",
|
||||
"hhstream://sessions/{id}/logs{?lines}",
|
||||
"hhstream://sessions/{id}/stream",
|
||||
"hhstream://outputs/{name}",
|
||||
}
|
||||
|
||||
@@ -76,7 +78,8 @@ def test_a_template_is_exactly_an_entry_with_a_capture():
|
||||
def test_the_three_reads_the_activity_rule_names_say_so_themselves():
|
||||
"""The rule has to be where it is read, and these are read, not called."""
|
||||
|
||||
for name in ("sessions", "session", "session-logs", "pointer", "containers"):
|
||||
for name in ("sessions", "session", "session-logs", "pointer", "containers",
|
||||
"streams", "session-stream"):
|
||||
description = resources.BY_NAME[name].description
|
||||
assert "does NOT reset" in description, name
|
||||
assert "`session_touch`" in description, name
|
||||
@@ -109,6 +112,7 @@ def test_each_fixed_resource_runs_the_verb_it_names(read):
|
||||
"hhstream://sessions": ["session", "list", "--json"],
|
||||
"hhstream://pointer": ["session", "pointer", "state", "--json"],
|
||||
"hhstream://containers": ["session", "container", "--json"],
|
||||
"hhstream://streams": ["session", "stream", "list", "--json"],
|
||||
}
|
||||
for uri, argv in cases.items():
|
||||
_, calls = read(uri, stdout=mcp_fake.reply(ok=True))
|
||||
@@ -290,6 +294,48 @@ def test_a_refused_capture_carries_the_cli_s_own_words(read):
|
||||
assert "hh-gone" in str(caught.value)
|
||||
|
||||
|
||||
# ── the stream ─────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_one_session_s_stream_reads_through_its_own_route(read):
|
||||
_, calls = read("hhstream://sessions/foot-abc/stream", stdout=mcp_fake.reply(ok=True))
|
||||
assert calls == [["session", "stream", "status", "--json", "--", "foot-abc"]]
|
||||
|
||||
|
||||
def test_the_stream_routes_do_not_collide_with_the_logs_route():
|
||||
"""Three templates under `sessions/{id}`; each must resolve to its own."""
|
||||
|
||||
assert resources.resolve("hhstream://sessions/x")[0].name == "session"
|
||||
assert resources.resolve("hhstream://sessions/x/logs")[0].name == "session-logs"
|
||||
assert resources.resolve("hhstream://sessions/x/stream")[0].name == "session-stream"
|
||||
|
||||
|
||||
def test_reading_a_stream_never_enables_one(read):
|
||||
"""The read is `status`, not `enable` -- switching one on is a tool."""
|
||||
|
||||
_, calls = read("hhstream://sessions/x/stream", stdout=mcp_fake.reply(ok=True))
|
||||
assert "enable" not in calls[0]
|
||||
_, calls = read("hhstream://streams", stdout=mcp_fake.reply(ok=True))
|
||||
assert "enable" not in calls[0]
|
||||
|
||||
|
||||
def test_both_stream_reads_warn_that_the_url_carries_its_credential():
|
||||
"""The URL is in these bodies, so the warning has to be on them."""
|
||||
|
||||
for name in ("streams", "session-stream"):
|
||||
description = resources.BY_NAME[name].description
|
||||
assert "PHRASE" in description, name
|
||||
assert "only thing protecting" in resources.BY_NAME["streams"].description
|
||||
|
||||
|
||||
def test_the_streams_listing_says_a_viewer_is_the_activity_exception():
|
||||
"""The one exception to `session_touch`, where somebody will read it."""
|
||||
|
||||
description = resources.BY_NAME["streams"].description
|
||||
assert "does NOT reset" in description
|
||||
assert "connected viewer" in description
|
||||
|
||||
|
||||
# ── the one read that must not be a resource ───────────────────────────────
|
||||
|
||||
|
||||
|
||||
@@ -117,7 +117,7 @@ def test_tools_list_returns_every_tool_with_a_title_and_a_description(session):
|
||||
|
||||
listed = run(go).tools
|
||||
assert {tool.name for tool in listed} == set(tools.BY_NAME)
|
||||
assert len(listed) == 21
|
||||
assert len(listed) == 23
|
||||
for tool in listed:
|
||||
assert tool.title, tool.name
|
||||
assert tool.description and len(tool.description) > 40, tool.name
|
||||
@@ -527,6 +527,53 @@ def test_a_tool_outside_the_selection_cannot_be_called_by_name(session):
|
||||
assert fake.calls == []
|
||||
|
||||
|
||||
def test_the_stream_url_round_trips_through_a_resource_read(session):
|
||||
"""The phrase reaching an agent is the point of the whole feature."""
|
||||
|
||||
reply = {
|
||||
"ok": True,
|
||||
"id": "s-1",
|
||||
"stream": {
|
||||
"enabled": True,
|
||||
"attached": True,
|
||||
"output": "hh-s-1",
|
||||
"fps": 5,
|
||||
"viewers": 1,
|
||||
"url": "http://127.0.0.1:9988/amber-otter-lantern-harbor-7k2qp/",
|
||||
},
|
||||
}
|
||||
fake, connected = session(stdout=json.dumps(reply, indent=2))
|
||||
|
||||
async def go():
|
||||
async with connected as client:
|
||||
await client.initialize()
|
||||
return await client.read_resource(AnyUrl("hhstream://sessions/s-1/stream"))
|
||||
|
||||
content = run(go).contents[0]
|
||||
assert json.loads(content.text)["stream"]["url"] == reply["stream"]["url"]
|
||||
assert fake.calls == [["session", "stream", "status", "--json", "--", "s-1"]]
|
||||
|
||||
|
||||
def test_enabling_a_stream_through_a_tool_call_returns_its_url(session):
|
||||
reply = {
|
||||
"ok": True,
|
||||
"id": "s-1",
|
||||
"stream": {"attached": True, "url": "http://127.0.0.1:9988/a-b-c-d-12345/"},
|
||||
}
|
||||
fake, connected = session(stdout=json.dumps(reply, indent=2))
|
||||
|
||||
async def go():
|
||||
async with connected as client:
|
||||
await client.initialize()
|
||||
return await client.call_tool("session_stream_enable", {"id": "s-1"})
|
||||
|
||||
result = run(go)
|
||||
assert not result.isError
|
||||
assert result.content[0].text.startswith(reply["stream"]["url"])
|
||||
assert result.structuredContent == reply
|
||||
assert fake.calls == [["session", "stream", "enable", "--json", "--", "s-1"]]
|
||||
|
||||
|
||||
def test_content_for_a_text_only_result_has_no_image_block():
|
||||
blocks = server.content_for(tools.Result(text="hello"))
|
||||
assert [block.type for block in blocks] == ["text"]
|
||||
|
||||
@@ -51,6 +51,7 @@ EXPECTED = {
|
||||
# lifecycle. The read-only verbs are resources; see MOVED_TO_RESOURCES.
|
||||
"session_start", "session_touch", "session_kill", "session_gc",
|
||||
"session_adopt", "session_container", "session_reconcile",
|
||||
"session_stream_enable", "session_stream_disable",
|
||||
# pointer
|
||||
"pointer_acquire", "pointer_move", "pointer_click", "pointer_scroll",
|
||||
"pointer_down", "pointer_up", "pointer_release",
|
||||
@@ -72,7 +73,7 @@ MOVED_TO_RESOURCES = {
|
||||
|
||||
def test_the_acting_verbs_are_tools_and_nothing_else_is():
|
||||
assert set(tools.BY_NAME) == EXPECTED
|
||||
assert len(tools.TOOLS) == 21
|
||||
assert len(tools.TOOLS) == 23
|
||||
|
||||
|
||||
def test_the_read_only_verbs_are_not_tools():
|
||||
@@ -108,7 +109,12 @@ def test_only_the_verbs_that_read_are_marked_read_only():
|
||||
# `record` writes a file, so it is not read-only either.
|
||||
assert not tools.BY_NAME["record"].read_only
|
||||
assert tools.BY_NAME["session_kill"].destructive
|
||||
assert [t.name for t in tools.TOOLS if t.destructive] == ["session_kill"]
|
||||
# `stream_disable` is destructive too, and for a reason worth naming: it
|
||||
# forgets the phrase, so anybody holding the URL loses it.
|
||||
assert [t.name for t in tools.TOOLS if t.destructive] == [
|
||||
"session_kill",
|
||||
"session_stream_disable",
|
||||
]
|
||||
|
||||
|
||||
# ── the two rules an agent gets wrong ──────────────────────────────────────
|
||||
@@ -448,6 +454,151 @@ def test_a_broker_that_is_down_is_reported_as_the_operator_s_problem(call):
|
||||
assert "frame tools" in result.text
|
||||
|
||||
|
||||
# ── the live stream ────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_enabling_a_stream_puts_the_url_where_a_caller_will_look(call):
|
||||
reply = {
|
||||
"ok": True,
|
||||
"id": "s-1",
|
||||
"stream": {
|
||||
"enabled": True,
|
||||
"output": "hh-s-1",
|
||||
"fps": 5,
|
||||
"attached": True,
|
||||
"url": "http://127.0.0.1:9988/amber-otter-lantern-harbor-7k2qp/",
|
||||
"viewers": 0,
|
||||
},
|
||||
}
|
||||
result, calls = call(
|
||||
"session_stream_enable", {"id": "s-1"}, stdout=json.dumps(reply, indent=2)
|
||||
)
|
||||
|
||||
assert calls == [["session", "stream", "enable", "--json", "--", "s-1"]]
|
||||
# The URL on the first line: it is the whole point of the call.
|
||||
assert result.text.startswith(reply["stream"]["url"])
|
||||
assert result.data == reply
|
||||
# ...and the one thing a caller must know about it.
|
||||
assert "only thing protecting it" in result.text
|
||||
|
||||
|
||||
def test_enabling_a_stream_passes_a_phrase_and_a_rate_through(call):
|
||||
_, calls = call(
|
||||
"session_stream_enable",
|
||||
{"id": "s-1", "phrase": "amber-otter-lantern-harbor-7k2qp", "fps": 3},
|
||||
stdout=mcp_fake.reply(ok=True, id="s-1", stream={"url": "http://x/"}),
|
||||
)
|
||||
assert calls == [
|
||||
[
|
||||
"session", "stream", "enable", "--json",
|
||||
"--phrase=amber-otter-lantern-harbor-7k2qp",
|
||||
"--fps=3",
|
||||
"--", "s-1",
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
def test_an_enable_the_daemon_did_not_confirm_does_not_invent_a_url(call):
|
||||
"""A printed URL that serves nothing is worse than saying there is none."""
|
||||
|
||||
result, _ = call(
|
||||
"session_stream_enable",
|
||||
{"id": "s-1"},
|
||||
stdout=mcp_fake.reply(ok=True, id="s-1", stream={"attached": False}),
|
||||
)
|
||||
assert "did not confirm" in result.text
|
||||
assert "http" not in result.text.split("\n\n")[0]
|
||||
|
||||
|
||||
def test_a_stream_on_a_session_with_no_container_is_exit_ten(call):
|
||||
"""It must never fall back to the main output -- that one carries everyone."""
|
||||
|
||||
result, _ = call(
|
||||
"session_stream_enable",
|
||||
{"id": "s-1"},
|
||||
stderr=mcp_fake.reply(
|
||||
ok=False,
|
||||
error="stream_unavailable",
|
||||
reason="session s-1 has no container output to publish",
|
||||
),
|
||||
exit_code=int(ExitCode.COMPOSITOR),
|
||||
)
|
||||
assert result.is_error
|
||||
assert result.data["exit_code"] == int(ExitCode.COMPOSITOR)
|
||||
assert "not lost" in result.text # the exit-10 remedy: degraded, not dead
|
||||
|
||||
|
||||
def test_a_phrase_that_is_not_this_session_s_is_exit_eight(call):
|
||||
result, _ = call(
|
||||
"session_stream_enable",
|
||||
{"id": "s-1", "phrase": "other-words-go-here-99999"},
|
||||
stderr=mcp_fake.reply(ok=False, error="stream_busy", reason="already has a stream"),
|
||||
exit_code=int(ExitCode.BUSY),
|
||||
)
|
||||
assert result.is_error
|
||||
assert result.text.startswith("exit 8 (BUSY)")
|
||||
|
||||
|
||||
def test_disabling_a_stream_is_marked_destructive_and_forgets_the_phrase(call):
|
||||
_, calls = call(
|
||||
"session_stream_disable", {"id": "s-1"}, stdout=mcp_fake.reply(ok=True, detached=True)
|
||||
)
|
||||
assert calls == [["session", "stream", "disable", "--json", "--", "s-1"]]
|
||||
assert tools.BY_NAME["session_stream_disable"].destructive
|
||||
assert "forget" in tools.BY_NAME["session_stream_disable"].description
|
||||
|
||||
|
||||
def test_start_can_ask_for_a_stream_and_gets_its_url_back(call):
|
||||
"""One call, because a stream needs the container output that start makes."""
|
||||
|
||||
reply = {
|
||||
"ok": True,
|
||||
"id": "s-9",
|
||||
"stream": {"url": "http://127.0.0.1:9988/amber-otter-lantern-harbor-7k2qp/"},
|
||||
}
|
||||
result, calls = call(
|
||||
"session_start",
|
||||
{"app": "chromium", "stream": True, "stream_fps": 4},
|
||||
stdout=json.dumps(reply, indent=2),
|
||||
)
|
||||
assert calls == [
|
||||
["session", "start", "--json", "--stream", "--stream-fps=4", "--", "chromium"]
|
||||
]
|
||||
assert "session id: s-9" in result.text
|
||||
assert reply["stream"]["url"] in result.text
|
||||
|
||||
|
||||
def test_a_start_whose_stream_failed_says_so_although_it_succeeded(call):
|
||||
"""Exit 0 with a running session; a caller must not read that as a stream."""
|
||||
|
||||
result, _ = call(
|
||||
"session_start",
|
||||
{"app": "chromium", "stream": True},
|
||||
stdout=mcp_fake.reply(
|
||||
ok=True, id="s-9", stream_error="session s-9 has no container output to publish"
|
||||
),
|
||||
)
|
||||
assert not result.is_error
|
||||
assert "stream: NOT enabled" in result.text
|
||||
assert "session_stream_enable" in result.text
|
||||
|
||||
|
||||
def test_a_start_that_asked_for_no_stream_says_nothing_about_one(call):
|
||||
result, calls = call(
|
||||
"session_start", {"app": "foot"}, stdout=mcp_fake.reply(ok=True, id="s-1")
|
||||
)
|
||||
assert "--stream" not in calls[0]
|
||||
assert "stream" not in result.text.lower().split("\n\n")[0]
|
||||
|
||||
|
||||
def test_the_stream_note_carries_both_facts_it_exists_for():
|
||||
assert "CREDENTIAL" in tools.STREAM_NOTE
|
||||
assert "only protection" in tools.STREAM_NOTE
|
||||
# The exception to the activity rule has to be where the stream is.
|
||||
assert "A CONNECTED VIEWER DOES" in tools.STREAM_NOTE
|
||||
assert tools.STREAM_NOTE in tools.BY_NAME["session_stream_enable"].description
|
||||
|
||||
|
||||
# ── shot and record ────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user