holon-sh 0.0.1.dev21__tar.gz → 0.0.1.dev23__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/PKG-INFO +1 -1
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/holon/ext/events/_server.py +20 -3
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/holon/serve.py +17 -1
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/pyproject.toml +1 -1
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/tests/test_events.py +7 -26
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/tests/test_serve.py +51 -79
- holon_sh-0.0.1.dev21/tests/test_context.py +0 -71
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/.gitignore +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/LICENSE +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/README.md +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/holon/client.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/holon/context.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/holon/ext/__init__.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/holon/ext/events/__init__.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/holon/ext/events/_client.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/holon/ext/events/_wire.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/holon/ext/files/__init__.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/holon/ext/files/_client.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/holon/ext/files/_server.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/holon/ext/files/_wire.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/holon/py.typed +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/scripts/verify_distribution.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/tests/fixtures/caller/holon/apps/__init__.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/tests/test_elicit.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/tests/test_files.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/tests/test_package.py +0 -0
- {holon_sh-0.0.1.dev21 → holon_sh-0.0.1.dev23}/tests/test_runtime.py +0 -0
|
@@ -374,6 +374,21 @@ class Events(Extension):
|
|
|
374
374
|
self._sent = asyncio.Event()
|
|
375
375
|
self._leases: dict[str, _Hold] = {}
|
|
376
376
|
self._loop: asyncio.AbstractEventLoop | None = None
|
|
377
|
+
self._closed = False
|
|
378
|
+
|
|
379
|
+
def close(self) -> None:
|
|
380
|
+
"""Initiate graceful closure of every open stream, and of each one opened from now on.
|
|
381
|
+
|
|
382
|
+
Each stream then sends its ``StreamEventsResult`` as its final frame, which tells its client that the
|
|
383
|
+
server closed the stream and not the subscription: the client opens it again from its cursor. Call it
|
|
384
|
+
when the server is about to stop, on the event loop that serves the events. It does not wait for the
|
|
385
|
+
streams to finish.
|
|
386
|
+
"""
|
|
387
|
+
self._on_loop()
|
|
388
|
+
self._closed = True
|
|
389
|
+
for declared in self._types.values():
|
|
390
|
+
for hold in declared._holds(None):
|
|
391
|
+
hold.wake.set()
|
|
377
392
|
|
|
378
393
|
def event(
|
|
379
394
|
self,
|
|
@@ -516,8 +531,8 @@ class Events(Extension):
|
|
|
516
531
|
self._loop = self._loop or running
|
|
517
532
|
if running is None or running is not self._loop:
|
|
518
533
|
raise RuntimeError(
|
|
519
|
-
"Events are emitted on the event loop that serves them. From another thread,
|
|
520
|
-
"loop.call_soon_threadsafe
|
|
534
|
+
"Events are emitted and closed on the event loop that serves them. From another thread, "
|
|
535
|
+
"use loop.call_soon_threadsafe."
|
|
521
536
|
)
|
|
522
537
|
|
|
523
538
|
# Checked in the order of https://github.com/modelcontextprotocol/experimental-ext-triggers-events/blob/main/docs/design-sketch-proposal.md#error-codes
|
|
@@ -719,7 +734,7 @@ class Events(Extension):
|
|
|
719
734
|
|
|
720
735
|
await active(*declared._resume(params.cursor, params.max_age_ms, hold.seen))
|
|
721
736
|
beat = time.monotonic() + _HEARTBEAT
|
|
722
|
-
while hold.ended is None:
|
|
737
|
+
while hold.ended is None and not self._closed:
|
|
723
738
|
hold.wake.clear()
|
|
724
739
|
if time.monotonic() >= beat:
|
|
725
740
|
beat = time.monotonic() + _HEARTBEAT
|
|
@@ -740,6 +755,8 @@ class Events(Extension):
|
|
|
740
755
|
else:
|
|
741
756
|
with anyio.move_on_after(beat - time.monotonic()):
|
|
742
757
|
await hold.wake.wait()
|
|
758
|
+
if hold.ended is None:
|
|
759
|
+
return {"_meta": {}}
|
|
743
760
|
while (kept := declared._next(hold.seen)) and kept.sequence <= hold.until:
|
|
744
761
|
await send(kept)
|
|
745
762
|
ended = MCPError.from_error_data(hold.ended)
|
|
@@ -8,6 +8,7 @@ import importlib
|
|
|
8
8
|
import mimetypes
|
|
9
9
|
import os
|
|
10
10
|
import re
|
|
11
|
+
import socket
|
|
11
12
|
import sys
|
|
12
13
|
from dataclasses import dataclass
|
|
13
14
|
from pathlib import Path, PurePath
|
|
@@ -18,6 +19,7 @@ from mcp.server import MCPServer
|
|
|
18
19
|
from mcp.server.auth.middleware.auth_context import AuthContextMiddleware
|
|
19
20
|
from mcp.server.auth.middleware.bearer_auth import AuthenticatedUser
|
|
20
21
|
from mcp.server.auth.provider import AccessToken
|
|
22
|
+
from mcp.server.subscriptions import ListenHandler
|
|
21
23
|
from mcp.server.transport_security import TransportSecuritySettings
|
|
22
24
|
from opentelemetry import context as tracing
|
|
23
25
|
from opentelemetry import propagate
|
|
@@ -28,6 +30,7 @@ from starlette.responses import FileResponse, PlainTextResponse, Response
|
|
|
28
30
|
from starlette.types import ASGIApp, Receive, Scope, Send
|
|
29
31
|
|
|
30
32
|
from holon.context import _Request, _request
|
|
33
|
+
from holon.ext.events import Events
|
|
31
34
|
|
|
32
35
|
__all__ = ["App", "load", "serve"]
|
|
33
36
|
|
|
@@ -80,7 +83,20 @@ async def serve(app: App) -> None:
|
|
|
80
83
|
ws="none",
|
|
81
84
|
timeout_graceful_shutdown=1,
|
|
82
85
|
)
|
|
83
|
-
|
|
86
|
+
|
|
87
|
+
class Server(uvicorn.Server):
|
|
88
|
+
async def shutdown(self, sockets: list[socket.socket] | None = None) -> None:
|
|
89
|
+
listen = app.server.session_manager.app.get_request_handler(
|
|
90
|
+
"subscriptions/listen"
|
|
91
|
+
)
|
|
92
|
+
if listen is not None and isinstance(listen.handler, ListenHandler):
|
|
93
|
+
listen.handler.close()
|
|
94
|
+
for each in app.server.middleware:
|
|
95
|
+
if isinstance(each, Events):
|
|
96
|
+
each.close()
|
|
97
|
+
await super().shutdown(sockets)
|
|
98
|
+
|
|
99
|
+
await Server(config).serve()
|
|
84
100
|
|
|
85
101
|
|
|
86
102
|
def _asgi(app: App) -> ASGIApp:
|
|
@@ -197,9 +197,11 @@ async def test_targeted_events_follow_shared_identity_but_isolate_anonymous_read
|
|
|
197
197
|
)
|
|
198
198
|
async with served(events) as port:
|
|
199
199
|
reader = client(port, user=user)
|
|
200
|
-
async with reader.subscribe("board.changed", {"board": "b1"}) as first:
|
|
200
|
+
async with reader.subscribe("board.changed", {"board": "b1", "n": 1}) as first:
|
|
201
201
|
subscription_id = await subscribed.get()
|
|
202
|
-
async with reader.subscribe(
|
|
202
|
+
async with reader.subscribe(
|
|
203
|
+
"board.changed", {"n": 1, "board": "b1"}
|
|
204
|
+
) as second:
|
|
203
205
|
say(changed, "b1", "private", to=subscription_id)
|
|
204
206
|
say(changed, "b1", "public")
|
|
205
207
|
assert await take(first, 2) == ["private", "public"]
|
|
@@ -207,24 +209,9 @@ async def test_targeted_events_follow_shared_identity_but_isolate_anonymous_read
|
|
|
207
209
|
assert (await anext(second)).event_id == expected
|
|
208
210
|
|
|
209
211
|
|
|
210
|
-
@
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
events, _ = declare(on_subscribe=lambda context, args, id: subscribed.append(args))
|
|
214
|
-
ann = {"x-holon-context": signed_in("ann")}
|
|
215
|
-
async with served(events) as port, httpx2.AsyncClient(headers=ann) as http:
|
|
216
|
-
for arguments in [
|
|
217
|
-
{"board": "b"},
|
|
218
|
-
{"n": 1, "board": "b"},
|
|
219
|
-
{"board": "b", "n": 1},
|
|
220
|
-
]:
|
|
221
|
-
params = {"name": "board.changed", "arguments": arguments}
|
|
222
|
-
assert "result" in await call(http, port, "events/poll", params)
|
|
223
|
-
assert subscribed == [{"board": "b"}, {"n": 1, "board": "b"}]
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
@pytest.mark.parametrize("targeted", [False, True])
|
|
227
|
-
@pytest.mark.parametrize("delivery", ["push", "poll"])
|
|
212
|
+
@pytest.mark.parametrize(
|
|
213
|
+
("delivery", "targeted"), [("push", False), ("poll", False), ("push", True)]
|
|
214
|
+
)
|
|
228
215
|
@asynchronous
|
|
229
216
|
async def test_a_subscription_behind_what_is_kept_is_told_of_the_gap_and_carries_on_but_a_stream_is_sent_all_emitted_to_its_subscription(
|
|
230
217
|
monkeypatch, delivery, targeted
|
|
@@ -370,11 +357,6 @@ async def test_the_messages_on_the_wire_are_the_sketchs():
|
|
|
370
357
|
("events/poll", {"name": "board.moved"}, -32023),
|
|
371
358
|
("events/poll", {"name": "board.changed", "arguments": {}}, INVALID_PARAMS),
|
|
372
359
|
("events/poll", changed_on("b1", 1), INVALID_PARAMS),
|
|
373
|
-
(
|
|
374
|
-
"events/stream",
|
|
375
|
-
{"name": "board.locked", "arguments": {"board": "b1"}},
|
|
376
|
-
-32026,
|
|
377
|
-
),
|
|
378
360
|
(
|
|
379
361
|
"events/poll",
|
|
380
362
|
{"name": "board.locked", "arguments": {"board": "secret"}},
|
|
@@ -519,6 +501,5 @@ async def test_a_subscription_from_the_cursor_read_with_a_snapshot_hears_every_c
|
|
|
519
501
|
) as changes:
|
|
520
502
|
assert changes.delivery == "push"
|
|
521
503
|
tell("once subscribed")
|
|
522
|
-
assert state["said"] == ["in the snapshot"]
|
|
523
504
|
assert await take(changes, 2) == ["after the snapshot", "once subscribed"]
|
|
524
505
|
await stop()
|
|
@@ -12,13 +12,18 @@ from functools import wraps
|
|
|
12
12
|
import httpx2
|
|
13
13
|
import pytest
|
|
14
14
|
import uvicorn
|
|
15
|
+
from mcp import Client
|
|
15
16
|
from mcp.server import MCPServer
|
|
16
17
|
from mcp.server.auth.middleware.auth_context import get_access_token
|
|
17
18
|
from mcp.server.mcpserver import Context
|
|
18
19
|
from mcp.shared.exceptions import MCPError
|
|
20
|
+
from mcp.types import (
|
|
21
|
+
LATEST_PROTOCOL_VERSION,
|
|
22
|
+
)
|
|
19
23
|
from opentelemetry import propagate
|
|
20
24
|
from starlette.datastructures import Headers
|
|
21
25
|
from starlette.responses import RedirectResponse, Response
|
|
26
|
+
from test_events import request
|
|
22
27
|
|
|
23
28
|
from holon.client import Apps
|
|
24
29
|
from holon.context import Caller, Principal, RequestContext, _request, get_context
|
|
@@ -116,8 +121,14 @@ async def test_inside_holon_a_tool_sees_whom_the_waypoint_admitted_and_not_the_h
|
|
|
116
121
|
("/mcp", {"x-holon-context": signed_in}, RequestContext(ann, None)),
|
|
117
122
|
(
|
|
118
123
|
"/mcp/inspector",
|
|
119
|
-
{
|
|
120
|
-
|
|
124
|
+
{
|
|
125
|
+
"x-holon-context": context(principal=PERSON),
|
|
126
|
+
"x-holon-caller": REPORTER,
|
|
127
|
+
},
|
|
128
|
+
RequestContext(
|
|
129
|
+
Principal("https://id.example", "ann", "web", [], {}),
|
|
130
|
+
Caller("reporter", "session"),
|
|
131
|
+
),
|
|
121
132
|
),
|
|
122
133
|
(
|
|
123
134
|
"/mcp",
|
|
@@ -131,39 +142,21 @@ async def test_inside_holon_a_tool_sees_whom_the_waypoint_admitted_and_not_the_h
|
|
|
131
142
|
await call(origin + path, "inspect", headers)
|
|
132
143
|
admitted, token, names, traceparent = seen.pop()
|
|
133
144
|
assert admitted == expected
|
|
145
|
+
assert (token is None) == (expected == RequestContext(None, None))
|
|
134
146
|
assert not {name for name in names if name.startswith("x-holon-")}
|
|
135
147
|
assert "authorization" not in names
|
|
136
148
|
assert traceparent == TRACEPARENT
|
|
137
|
-
if expected == RequestContext(None, None):
|
|
138
|
-
assert token is None
|
|
139
|
-
continue
|
|
140
|
-
assert token.token == headers.get("x-holon-context", "")
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
def token(payload):
|
|
144
|
-
encoded = base64.urlsafe_b64encode(payload.encode()).rstrip(b"=")
|
|
145
|
-
return f"e30.{encoded.decode()}.signature"
|
|
146
149
|
|
|
147
150
|
|
|
148
151
|
@asynchronous
|
|
149
152
|
async def test_a_malformed_context_is_a_bad_request(monkeypatch):
|
|
150
153
|
monkeypatch.setenv("HOLON_PLATFORM", "1")
|
|
151
|
-
|
|
152
|
-
seen = []
|
|
153
|
-
async with served(_asgi(App(inspector(seen)))) as origin:
|
|
154
|
+
async with served(_asgi(App(MCPServer("strict")))) as origin:
|
|
154
155
|
for headers in [
|
|
155
156
|
[("x-holon-context", context()), ("x-holon-context", context())],
|
|
156
157
|
[("x-holon-caller", REPORTER), ("x-holon-caller", REPORTER)],
|
|
157
158
|
[("x-holon-caller", "reporter")],
|
|
158
|
-
[("x-holon-context", "header.claims")],
|
|
159
|
-
[("x-holon-context", "e30.e30=.signature")],
|
|
160
|
-
[("x-holon-context", token("person"))],
|
|
161
|
-
[("x-holon-context", token("[]"))],
|
|
162
|
-
[("x-holon-context", token("null"))],
|
|
163
159
|
[("x-holon-context", context(principal="user"))],
|
|
164
|
-
[("x-holon-context", context(principal={"iss": 1}))],
|
|
165
|
-
[("x-holon-context", context(principal={"claims": []}))],
|
|
166
|
-
[("x-holon-context", context(principal={**person, "act": ["agent"]}))],
|
|
167
160
|
]:
|
|
168
161
|
async with httpx2.AsyncClient() as client:
|
|
169
162
|
response = await client.post(
|
|
@@ -173,7 +166,6 @@ async def test_a_malformed_context_is_a_bad_request(monkeypatch):
|
|
|
173
166
|
400,
|
|
174
167
|
"Malformed Holon context\n",
|
|
175
168
|
), headers
|
|
176
|
-
assert seen == []
|
|
177
169
|
|
|
178
170
|
|
|
179
171
|
@asynchronous
|
|
@@ -187,9 +179,8 @@ async def test_outside_holon_the_same_headers_make_an_anonymous_request(monkeypa
|
|
|
187
179
|
"authorization": "Bearer forged",
|
|
188
180
|
}
|
|
189
181
|
await call(origin + "/mcp", "inspect", headers)
|
|
190
|
-
[(admitted,
|
|
191
|
-
assert
|
|
192
|
-
assert not names & {"x-holon-context", "x-holon-caller", "authorization"}
|
|
182
|
+
[(admitted, _, _, _)] = seen
|
|
183
|
+
assert admitted == RequestContext(None, None)
|
|
193
184
|
|
|
194
185
|
|
|
195
186
|
@asynchronous
|
|
@@ -270,53 +261,31 @@ async def test_the_frontend_serves_its_files_and_nothing_hidden_or_outside_it(
|
|
|
270
261
|
tmp_path,
|
|
271
262
|
):
|
|
272
263
|
web = tmp_path / "web"
|
|
273
|
-
|
|
264
|
+
web.mkdir()
|
|
274
265
|
(web / "index.html").write_text("<p>Home</p>")
|
|
275
|
-
(web / "nested/index.html").write_text("<p>Nested</p>")
|
|
276
266
|
(web / "style.css").write_text("p {}")
|
|
277
|
-
(web / "data.json").write_text("{}")
|
|
278
267
|
(web / ".private").write_text("Hidden")
|
|
279
268
|
(tmp_path / "secret").write_text("Outside")
|
|
280
269
|
(web / "escape").symlink_to(tmp_path / "secret")
|
|
281
|
-
(web / "linked.css").symlink_to(web / "style.css")
|
|
282
|
-
os.mkfifo(web / "pipe")
|
|
283
270
|
app = _asgi(App(MCPServer("site"), web))
|
|
284
271
|
async with httpx2.AsyncClient(
|
|
285
272
|
transport=httpx2.ASGITransport(app=app), base_url="http://app"
|
|
286
273
|
) as http:
|
|
287
274
|
for path, body, kind in [
|
|
288
|
-
("/", "<p>Home</p>", "text/html
|
|
289
|
-
("/
|
|
290
|
-
("/nested", "<p>Nested</p>", "text/html; charset=utf-8"),
|
|
291
|
-
("/nested/", "<p>Nested</p>", "text/html; charset=utf-8"),
|
|
292
|
-
("/style.css", "p {}", "text/css; charset=utf-8"),
|
|
293
|
-
("/linked.css", "p {}", "text/css; charset=utf-8"),
|
|
294
|
-
("/data.json", "{}", "application/json"),
|
|
275
|
+
("/", "<p>Home</p>", "text/html"),
|
|
276
|
+
("/style.css", "p {}", "text/css"),
|
|
295
277
|
]:
|
|
296
278
|
response = await http.get(path)
|
|
297
279
|
assert (response.status_code, response.text) == (200, body), path
|
|
298
|
-
assert response.headers["content-type"]
|
|
280
|
+
assert response.headers["content-type"].startswith(kind)
|
|
299
281
|
assert response.headers["x-content-type-options"] == "nosniff"
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
for path in [
|
|
303
|
-
"/.private",
|
|
304
|
-
"/%2eprivate",
|
|
305
|
-
"/%2E%2E/secret",
|
|
306
|
-
"/escape",
|
|
307
|
-
"/pipe",
|
|
308
|
-
"/missing",
|
|
309
|
-
]:
|
|
282
|
+
assert (await http.head("/style.css")).status_code == 200
|
|
283
|
+
for path in ["/.private", "/escape", "/missing"]:
|
|
310
284
|
assert (await http.get(path)).status_code == 404, path
|
|
311
285
|
assert (await http.post("/")).status_code == 404
|
|
312
|
-
async with httpx2.AsyncClient(
|
|
313
|
-
transport=httpx2.ASGITransport(app=_asgi(App(MCPServer("bare")))),
|
|
314
|
-
base_url="http://app",
|
|
315
|
-
) as http:
|
|
316
|
-
assert (await http.get("/")).status_code == 404
|
|
317
286
|
|
|
318
287
|
|
|
319
|
-
def
|
|
288
|
+
def test_python_m_holon_serve_runs_an_entry_with_a_16_kib_context_and_on_sigterm_closes_its_streams_and_stops(
|
|
320
289
|
tmp_path,
|
|
321
290
|
):
|
|
322
291
|
(tmp_path / "src").mkdir()
|
|
@@ -324,30 +293,21 @@ def test_python_m_holon_serve_runs_an_entry_with_a_16_kib_context_and_stops_on_s
|
|
|
324
293
|
(tmp_path / "web/index.html").write_text("<p>Home</p>")
|
|
325
294
|
(tmp_path / "src/server.py").write_text(
|
|
326
295
|
"""from mcp.server import MCPServer
|
|
327
|
-
from holon.
|
|
296
|
+
from holon.ext.events import Events
|
|
328
297
|
|
|
329
|
-
|
|
298
|
+
events = Events()
|
|
299
|
+
events.event("ticked", input_schema={"type": "object"}, payload_schema={"type": "object"})
|
|
300
|
+
server = MCPServer("large", extensions=[events], middleware=[events])
|
|
330
301
|
frontend = "web"
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
@server.tool()
|
|
334
|
-
def subject() -> str:
|
|
335
|
-
return get_context().principal.subject
|
|
336
302
|
"""
|
|
337
303
|
)
|
|
338
|
-
subject = "a" * 12200
|
|
339
|
-
token = context(principal={**PERSON, "sub": subject})
|
|
340
|
-
assert len(token) >= 16 * 1024
|
|
341
304
|
with socket.socket() as reserved:
|
|
342
305
|
reserved.bind(("127.0.0.1", 0))
|
|
343
306
|
port = reserved.getsockname()[1]
|
|
344
|
-
environment = {**os.environ, "HOLON_PLATFORM": "1", "PORT": str(port)}
|
|
345
307
|
process = subprocess.Popen(
|
|
346
308
|
[sys.executable, "-m", "holon.serve", "src/server.py"],
|
|
347
309
|
cwd=tmp_path,
|
|
348
|
-
env=
|
|
349
|
-
stderr=subprocess.PIPE,
|
|
350
|
-
text=True,
|
|
310
|
+
env={**os.environ, "PORT": str(port)},
|
|
351
311
|
)
|
|
352
312
|
|
|
353
313
|
async def exercise():
|
|
@@ -361,19 +321,31 @@ def subject() -> str:
|
|
|
361
321
|
await asyncio.sleep(0.05)
|
|
362
322
|
assert home.text == "<p>Home</p>"
|
|
363
323
|
reader, writer = await asyncio.open_connection("127.0.0.1", port)
|
|
364
|
-
head = f"GET / HTTP/1.1\r\nhost: app\r\nx-holon-context: {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
324
|
+
head = f"GET / HTTP/1.1\r\nhost: app\r\nx-holon-context: {'a' * 16384}\r\n"
|
|
325
|
+
writer.write(head.encode())
|
|
326
|
+
await writer.drain()
|
|
327
|
+
await asyncio.sleep(0.1)
|
|
328
|
+
writer.write(b"\r\n")
|
|
369
329
|
assert (await reader.readline()).startswith(b"HTTP/1.1 200")
|
|
370
330
|
writer.close()
|
|
371
|
-
|
|
372
|
-
|
|
331
|
+
ticked = request("events/stream", {"name": "ticked", "arguments": {}}, id=7)
|
|
332
|
+
async with (
|
|
333
|
+
Client(origin + "/mcp", mode=LATEST_PROTOCOL_VERSION) as client,
|
|
334
|
+
client.listen(tools_list_changed=True) as subscription,
|
|
335
|
+
httpx2.AsyncClient() as http,
|
|
336
|
+
http.stream("POST", origin + "/mcp", **ticked) as sent,
|
|
337
|
+
):
|
|
338
|
+
frames = sent.aiter_lines()
|
|
339
|
+
while not (await anext(frames)).startswith("data: "):
|
|
340
|
+
pass
|
|
341
|
+
process.send_signal(signal.SIGTERM)
|
|
342
|
+
last = [frame async for frame in frames if frame.startswith("data: ")][-1]
|
|
343
|
+
async for _ in subscription:
|
|
344
|
+
pass
|
|
345
|
+
assert "result" in json.loads(last.removeprefix("data: "))
|
|
373
346
|
|
|
374
347
|
try:
|
|
375
348
|
asyncio.run(asyncio.wait_for(exercise(), timeout=20))
|
|
349
|
+
process.wait(timeout=10)
|
|
376
350
|
finally:
|
|
377
|
-
process.
|
|
378
|
-
_, errors = process.communicate(timeout=10)
|
|
379
|
-
assert "Application shutdown complete" in errors
|
|
351
|
+
process.kill()
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
from mcp.server.auth.middleware.auth_context import auth_context_var
|
|
2
|
-
from mcp.server.auth.middleware.bearer_auth import AuthenticatedUser
|
|
3
|
-
from mcp.server.auth.provider import AccessToken
|
|
4
|
-
|
|
5
|
-
from holon.context import Caller, Principal, RequestContext, get_context
|
|
6
|
-
|
|
7
|
-
PROFILE = {"email": "user@example.com", "email_verified": True}
|
|
8
|
-
REPORTER = {"app": "reporter", "session": "session"}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def test_get_context_reads_the_caller_holon_admitted():
|
|
12
|
-
assert get_context() == RequestContext(principal=None, caller=None)
|
|
13
|
-
for client_id, scopes, extra, expected in [
|
|
14
|
-
(
|
|
15
|
-
"web",
|
|
16
|
-
["read", "write"],
|
|
17
|
-
{
|
|
18
|
-
"iss": "https://id.example",
|
|
19
|
-
"sub": "user",
|
|
20
|
-
"claims": PROFILE,
|
|
21
|
-
"act": {"sub": "agent"},
|
|
22
|
-
},
|
|
23
|
-
RequestContext(
|
|
24
|
-
Principal(
|
|
25
|
-
issuer="https://id.example",
|
|
26
|
-
subject="user",
|
|
27
|
-
client_id="web",
|
|
28
|
-
scopes=["read", "write"],
|
|
29
|
-
claims=PROFILE,
|
|
30
|
-
actor={"sub": "agent"},
|
|
31
|
-
),
|
|
32
|
-
caller=None,
|
|
33
|
-
),
|
|
34
|
-
),
|
|
35
|
-
# An app acting for a person, then an app or its lifecycle hook's job with nobody signed in.
|
|
36
|
-
(
|
|
37
|
-
"web",
|
|
38
|
-
[],
|
|
39
|
-
{"iss": "https://id.example", "sub": "user", "caller": REPORTER},
|
|
40
|
-
RequestContext(
|
|
41
|
-
Principal("https://id.example", "user", "web", [], {}),
|
|
42
|
-
Caller("reporter", "session"),
|
|
43
|
-
),
|
|
44
|
-
),
|
|
45
|
-
(
|
|
46
|
-
"",
|
|
47
|
-
[],
|
|
48
|
-
{"caller": REPORTER},
|
|
49
|
-
RequestContext(None, Caller("reporter", "session")),
|
|
50
|
-
),
|
|
51
|
-
# An older runtime passed the raw mesh identity, and a person needs an issuer.
|
|
52
|
-
(
|
|
53
|
-
"",
|
|
54
|
-
[],
|
|
55
|
-
{"caller": "spiffe://cluster.local/ns/session/sa/reporter"},
|
|
56
|
-
RequestContext(None, None),
|
|
57
|
-
),
|
|
58
|
-
("web", [], {"sub": "user"}, RequestContext(None, None)),
|
|
59
|
-
]:
|
|
60
|
-
token = AccessToken(
|
|
61
|
-
token="t",
|
|
62
|
-
client_id=client_id,
|
|
63
|
-
scopes=scopes,
|
|
64
|
-
subject=extra.get("sub"),
|
|
65
|
-
claims=extra,
|
|
66
|
-
)
|
|
67
|
-
admitted = auth_context_var.set(AuthenticatedUser(token))
|
|
68
|
-
try:
|
|
69
|
-
assert get_context() == expected
|
|
70
|
-
finally:
|
|
71
|
-
auth_context_var.reset(admitted)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|