holon-sh 0.0.1.dev12__tar.gz → 0.0.1.dev14__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.dev12 → holon_sh-0.0.1.dev14}/PKG-INFO +5 -1
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/holon/client.py +5 -1
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/holon/context.py +12 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/holon/ext/events/_server.py +26 -31
- holon_sh-0.0.1.dev14/holon/serve.py +220 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/pyproject.toml +10 -2
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/tests/test_events.py +33 -37
- holon_sh-0.0.1.dev14/tests/test_serve.py +379 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/.gitignore +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/LICENSE +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/README.md +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/holon/ext/__init__.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/holon/ext/events/__init__.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/holon/ext/events/_client.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/holon/ext/events/_wire.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/holon/ext/files/__init__.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/holon/ext/files/_client.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/holon/ext/files/_server.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/holon/ext/files/_wire.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/holon/py.typed +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/scripts/verify_distribution.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/tests/fixtures/caller/holon/apps/__init__.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/tests/test_context.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/tests/test_elicit.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/tests/test_files.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/tests/test_package.py +0 -0
- {holon_sh-0.0.1.dev12 → holon_sh-0.0.1.dev14}/tests/test_runtime.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: holon-sh
|
|
3
|
-
Version: 0.0.1.
|
|
3
|
+
Version: 0.0.1.dev14
|
|
4
4
|
Summary: Typed MCP clients and the caller of an MCP request
|
|
5
5
|
Project-URL: Repository, https://github.com/holon-sh/sdk-py
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -9,6 +9,10 @@ Requires-Python: >=3.11
|
|
|
9
9
|
Requires-Dist: httpx2<3,>=2.10
|
|
10
10
|
Requires-Dist: jsonschema<5,>=4.26
|
|
11
11
|
Requires-Dist: mcp<3,>=2.3
|
|
12
|
+
Requires-Dist: opentelemetry-api<2,>=1.28
|
|
13
|
+
Requires-Dist: pydantic<3,>=2.12
|
|
14
|
+
Requires-Dist: starlette<2,>=0.48
|
|
15
|
+
Requires-Dist: uvicorn<1,>=0.31.1
|
|
12
16
|
Description-Content-Type: text/markdown
|
|
13
17
|
|
|
14
18
|
# Holon Python SDK
|
|
@@ -40,6 +40,7 @@ from mcp.types import (
|
|
|
40
40
|
)
|
|
41
41
|
from referencing import Registry
|
|
42
42
|
|
|
43
|
+
from holon.context import _request
|
|
43
44
|
from holon.ext.events import AppEvents
|
|
44
45
|
from holon.ext.files._client import (
|
|
45
46
|
FILES,
|
|
@@ -283,7 +284,10 @@ class _App:
|
|
|
283
284
|
f"Dependency {self._name} is unavailable ({prefix}URL). Set the endpoint or supply a binding."
|
|
284
285
|
)
|
|
285
286
|
token = os.environ.get(prefix + "TOKEN")
|
|
286
|
-
headers = {"Authorization": f"Bearer {token}"} if token else
|
|
287
|
+
headers = {"Authorization": f"Bearer {token}"} if token else {}
|
|
288
|
+
request = _request.get()
|
|
289
|
+
if request is not None and not request.ended and request.token:
|
|
290
|
+
headers["X-Holon-Context"] = request.token
|
|
287
291
|
return endpoint, partial(http_client, headers=headers)
|
|
288
292
|
|
|
289
293
|
# Raises an error from the block as itself, not in the session's ExceptionGroup.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
from contextvars import ContextVar
|
|
3
4
|
from dataclasses import dataclass
|
|
4
5
|
from typing import Any, TypedDict
|
|
5
6
|
|
|
@@ -90,3 +91,14 @@ def get_context() -> RequestContext:
|
|
|
90
91
|
),
|
|
91
92
|
caller,
|
|
92
93
|
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@dataclass
|
|
97
|
+
class _Request:
|
|
98
|
+
"""The MCP request being served. Tasks it spawns share it, and see when it ends."""
|
|
99
|
+
|
|
100
|
+
token: str
|
|
101
|
+
ended: bool = False
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
_request: ContextVar[_Request | None] = ContextVar("holon_request", default=None)
|
|
@@ -155,6 +155,7 @@ class _Hold:
|
|
|
155
155
|
ended: ErrorData | None = None
|
|
156
156
|
until: int = 0
|
|
157
157
|
lease: asyncio.TimerHandle | None = None
|
|
158
|
+
seen: int = 0
|
|
158
159
|
|
|
159
160
|
|
|
160
161
|
@dataclass(eq=False, repr=False)
|
|
@@ -191,7 +192,7 @@ class EventType(Generic[A, D]):
|
|
|
191
192
|
Args:
|
|
192
193
|
event_id: The source's own id, so a client drops duplicates.
|
|
193
194
|
timestamp: When it happened. Defaults to now.
|
|
194
|
-
subscription_id: Send it to this subscription alone.
|
|
195
|
+
subscription_id: Send it to this subscription alone, and keep it until the subscription's open streams have sent it.
|
|
195
196
|
|
|
196
197
|
Call it on the event loop that serves the events; from another thread, use ``loop.call_soon_threadsafe``.
|
|
197
198
|
"""
|
|
@@ -256,8 +257,14 @@ class EventType(Generic[A, D]):
|
|
|
256
257
|
or self._bytes > _KEEP_BYTES
|
|
257
258
|
or self._kept[dropped].at < kept.at - _KEEP_AGE
|
|
258
259
|
):
|
|
259
|
-
|
|
260
|
-
|
|
260
|
+
oldest = self._kept[dropped]
|
|
261
|
+
if oldest.target is not None and any(
|
|
262
|
+
hold.lease is None and hold.seen < oldest.sequence
|
|
263
|
+
for hold in self._holds(oldest.target)
|
|
264
|
+
):
|
|
265
|
+
break
|
|
266
|
+
self._dropped = oldest.sequence
|
|
267
|
+
self._bytes -= oldest.size
|
|
261
268
|
dropped += 1
|
|
262
269
|
del self._kept[:dropped]
|
|
263
270
|
|
|
@@ -753,7 +760,7 @@ class Events(Extension):
|
|
|
753
760
|
declared = request.type
|
|
754
761
|
await declared._settled(request.id)
|
|
755
762
|
hold = self._open(request)
|
|
756
|
-
seen = declared._sequence
|
|
763
|
+
hold.seen = declared._sequence
|
|
757
764
|
try:
|
|
758
765
|
await self._ready(hold)
|
|
759
766
|
|
|
@@ -771,20 +778,23 @@ class Events(Extension):
|
|
|
771
778
|
if waited.cancelled_caught:
|
|
772
779
|
raise _Stalled
|
|
773
780
|
|
|
781
|
+
async def active(seen: int, truncated: bool) -> None:
|
|
782
|
+
hold.seen = seen
|
|
783
|
+
cursor = declared._position(seen)
|
|
784
|
+
await say("active", {"cursor": cursor, "truncated": truncated})
|
|
785
|
+
|
|
774
786
|
async def send(kept: _Kept) -> None:
|
|
787
|
+
hold.seen = kept.sequence
|
|
775
788
|
try:
|
|
776
789
|
shaped = declared._shape(kept, request)
|
|
777
790
|
except Exception as error: # noqa: BLE001
|
|
778
791
|
await say("error", {"error": _described(error)})
|
|
779
792
|
return
|
|
780
793
|
if shaped is not None:
|
|
781
|
-
cursor = declared._position(
|
|
794
|
+
cursor = declared._position(kept.sequence)
|
|
782
795
|
await say("event", {**shaped, "cursor": cursor}, kept.size)
|
|
783
796
|
|
|
784
|
-
|
|
785
|
-
await say(
|
|
786
|
-
"active", {"cursor": declared._position(seen), "truncated": truncated}
|
|
787
|
-
)
|
|
797
|
+
await active(*declared._resume(params.cursor, params.max_age_ms, hold.seen))
|
|
788
798
|
beat = time.monotonic() + _HEARTBEAT
|
|
789
799
|
while hold.ended is None:
|
|
790
800
|
hold.wake.clear()
|
|
@@ -800,34 +810,19 @@ class Events(Extension):
|
|
|
800
810
|
if not allowed:
|
|
801
811
|
hold.ended = _FORBIDDEN
|
|
802
812
|
break
|
|
803
|
-
await say("heartbeat", {"cursor": declared._position(seen)})
|
|
804
|
-
elif seen < declared._dropped:
|
|
813
|
+
await say("heartbeat", {"cursor": declared._position(hold.seen)})
|
|
814
|
+
elif hold.seen < declared._dropped:
|
|
805
815
|
# Evicted before it was sent: a gap.
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
"active",
|
|
809
|
-
{"cursor": declared._position(seen), "truncated": True},
|
|
810
|
-
)
|
|
811
|
-
elif (kept := declared._next(seen)) is not None:
|
|
812
|
-
seen = kept.sequence
|
|
816
|
+
await active(declared._dropped, True)
|
|
817
|
+
elif (kept := declared._next(hold.seen)) is not None:
|
|
813
818
|
await send(kept)
|
|
814
819
|
else:
|
|
815
820
|
with anyio.move_on_after(beat - time.monotonic()):
|
|
816
821
|
await hold.wake.wait()
|
|
817
|
-
while (
|
|
818
|
-
kept := declared._next(seen)
|
|
819
|
-
) is not None and kept.sequence <= hold.until:
|
|
820
|
-
seen = kept.sequence
|
|
822
|
+
while (kept := declared._next(hold.seen)) and kept.sequence <= hold.until:
|
|
821
823
|
await send(kept)
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
"terminated",
|
|
825
|
-
{
|
|
826
|
-
"error": hold.ended.model_dump(
|
|
827
|
-
by_alias=True, exclude_none=True, mode="json"
|
|
828
|
-
)
|
|
829
|
-
},
|
|
830
|
-
)
|
|
824
|
+
ended = MCPError.from_error_data(hold.ended)
|
|
825
|
+
await say("terminated", {"error": _described(ended)})
|
|
831
826
|
except _Stalled:
|
|
832
827
|
logger.info(
|
|
833
828
|
"Closed a stream of %s whose reader took nothing for two heartbeats",
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"""Serves an app at the paths Holon gives it: ``python -m holon.serve server.py``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import base64
|
|
7
|
+
import importlib
|
|
8
|
+
import mimetypes
|
|
9
|
+
import os
|
|
10
|
+
import re
|
|
11
|
+
import sys
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from pathlib import Path, PurePath
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
import uvicorn
|
|
17
|
+
from mcp.server import MCPServer
|
|
18
|
+
from mcp.server.auth.middleware.auth_context import AuthContextMiddleware
|
|
19
|
+
from mcp.server.auth.middleware.bearer_auth import AuthenticatedUser
|
|
20
|
+
from mcp.server.auth.provider import AccessToken
|
|
21
|
+
from mcp.server.transport_security import TransportSecuritySettings
|
|
22
|
+
from opentelemetry import context as tracing
|
|
23
|
+
from opentelemetry import propagate
|
|
24
|
+
from pydantic import BaseModel
|
|
25
|
+
from starlette.authentication import AuthCredentials, UnauthenticatedUser
|
|
26
|
+
from starlette.datastructures import Headers
|
|
27
|
+
from starlette.responses import FileResponse, PlainTextResponse, Response
|
|
28
|
+
from starlette.types import ASGIApp, Receive, Scope, Send
|
|
29
|
+
|
|
30
|
+
from holon.context import _Request, _request
|
|
31
|
+
from holon.ext.events import Events
|
|
32
|
+
|
|
33
|
+
__all__ = ["App", "load", "serve"]
|
|
34
|
+
|
|
35
|
+
_MCP = re.compile(r"/mcp(/[^/]+)?")
|
|
36
|
+
_CONTEXT = re.compile(r"[A-Za-z0-9_-]+\.([A-Za-z0-9_-]+)\.[A-Za-z0-9_-]+")
|
|
37
|
+
_CALLER = re.compile(r"([^/]+)/([^/]+)")
|
|
38
|
+
_MALFORMED = PlainTextResponse(
|
|
39
|
+
"Malformed Holon context\n",
|
|
40
|
+
status_code=400,
|
|
41
|
+
headers={"x-content-type-options": "nosniff"},
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass(frozen=True)
|
|
46
|
+
class App:
|
|
47
|
+
"""An app's module: its MCP server, and the directory of its page."""
|
|
48
|
+
|
|
49
|
+
server: MCPServer[Any]
|
|
50
|
+
frontend: Path | None = None
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def load(entry: str) -> App:
|
|
54
|
+
"""Import an app's module, such as ``server.py`` or ``src/server.py``, from the current directory.
|
|
55
|
+
|
|
56
|
+
The module defines ``server``, an ``MCPServer``, and may define ``frontend``, the directory of its page.
|
|
57
|
+
"""
|
|
58
|
+
sys.path.insert(0, os.getcwd())
|
|
59
|
+
name = ".".join(PurePath(entry.removesuffix(".py")).parts)
|
|
60
|
+
module = importlib.import_module(name)
|
|
61
|
+
server = getattr(module, "server", None)
|
|
62
|
+
if not isinstance(server, MCPServer):
|
|
63
|
+
raise TypeError(f"{entry} must define server, an MCPServer")
|
|
64
|
+
frontend = os.fspath(getattr(module, "frontend", ""))
|
|
65
|
+
return App(server, Path(frontend).absolute() if frontend else None)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
async def serve(app: App) -> None:
|
|
69
|
+
"""Serve ``app`` on ``HOST`` and ``PORT``, or else on 127.0.0.1:3000.
|
|
70
|
+
|
|
71
|
+
The MCP endpoint is at ``/mcp`` and ``/mcp/<app>``, and the frontend's files are everywhere else.
|
|
72
|
+
"""
|
|
73
|
+
config = uvicorn.Config(
|
|
74
|
+
_asgi(app),
|
|
75
|
+
host=os.environ.get("HOST", "127.0.0.1"),
|
|
76
|
+
port=int(os.environ.get("PORT", "3000")),
|
|
77
|
+
lifespan="on",
|
|
78
|
+
h11_max_incomplete_event_size=65536,
|
|
79
|
+
access_log=False,
|
|
80
|
+
proxy_headers=False,
|
|
81
|
+
ws="none",
|
|
82
|
+
timeout_graceful_shutdown=1,
|
|
83
|
+
)
|
|
84
|
+
await uvicorn.Server(config).serve()
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _asgi(app: App) -> ASGIApp:
|
|
88
|
+
server = app.server
|
|
89
|
+
served = server.streamable_http_app(
|
|
90
|
+
stateless_http=True,
|
|
91
|
+
transport_security=TransportSecuritySettings(
|
|
92
|
+
enable_dns_rebinding_protection=False
|
|
93
|
+
),
|
|
94
|
+
)
|
|
95
|
+
manager = server.session_manager
|
|
96
|
+
events = next(
|
|
97
|
+
(each for each in server.middleware if isinstance(each, Events)), None
|
|
98
|
+
)
|
|
99
|
+
mcp = AuthContextMiddleware(
|
|
100
|
+
manager.handle_request if events is None else events.asgi(manager)
|
|
101
|
+
)
|
|
102
|
+
platform = os.environ.get("HOLON_PLATFORM") == "1"
|
|
103
|
+
|
|
104
|
+
async def dispatch(scope: Scope, receive: Receive, send: Send) -> None:
|
|
105
|
+
if scope["type"] == "lifespan":
|
|
106
|
+
await served(scope, receive, send)
|
|
107
|
+
return
|
|
108
|
+
headers = scope["headers"]
|
|
109
|
+
scope["headers"] = [
|
|
110
|
+
(name, value)
|
|
111
|
+
for name, value in headers
|
|
112
|
+
if not name.startswith(b"x-holon-") and name != b"authorization"
|
|
113
|
+
]
|
|
114
|
+
if not _MCP.fullmatch(scope["path"]):
|
|
115
|
+
frontend = app.frontend if scope["method"] in ("GET", "HEAD") else None
|
|
116
|
+
await _page(frontend, scope["path"])(scope, receive, send)
|
|
117
|
+
return
|
|
118
|
+
read = _admit(headers) if platform else ("", None)
|
|
119
|
+
if read is None:
|
|
120
|
+
# A context the waypoint could not have sent is a malformed request (RFC 9110 §15.5.1).
|
|
121
|
+
await _MALFORMED(scope, receive, send)
|
|
122
|
+
return
|
|
123
|
+
token, admitted = read
|
|
124
|
+
scope["user"] = UnauthenticatedUser()
|
|
125
|
+
scope["auth"] = AuthCredentials()
|
|
126
|
+
if admitted is not None:
|
|
127
|
+
scope["user"] = AuthenticatedUser(admitted)
|
|
128
|
+
scope["auth"] = AuthCredentials(admitted.scopes)
|
|
129
|
+
request = _Request(token)
|
|
130
|
+
current = _request.set(request)
|
|
131
|
+
# The SDK's server span parents on params._meta, else on this context: the caller's trace headers.
|
|
132
|
+
trace = tracing.attach(propagate.extract(Headers(scope=scope)))
|
|
133
|
+
try:
|
|
134
|
+
await mcp(scope, receive, send)
|
|
135
|
+
finally:
|
|
136
|
+
request.ended = True
|
|
137
|
+
tracing.detach(trace)
|
|
138
|
+
_request.reset(current)
|
|
139
|
+
|
|
140
|
+
return dispatch
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
class _Principal(BaseModel, strict=True):
|
|
144
|
+
iss: str | None = None
|
|
145
|
+
sub: str | None = None
|
|
146
|
+
client_id: str | None = None
|
|
147
|
+
scope: str | None = None
|
|
148
|
+
claims: dict[str, Any] | None = None
|
|
149
|
+
act: dict[str, Any] | None = None
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
class _Claims(BaseModel, strict=True):
|
|
153
|
+
principal: _Principal | None = None
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _admit(
|
|
157
|
+
headers: list[tuple[bytes, bytes]],
|
|
158
|
+
) -> tuple[str, AccessToken | None] | None:
|
|
159
|
+
"""The request's context and who it comes from, or None when the waypoint's headers are malformed.
|
|
160
|
+
|
|
161
|
+
The waypoint names the calling app of the session, a lifecycle hook's job as its app.
|
|
162
|
+
"""
|
|
163
|
+
contexts = [value for name, value in headers if name == b"x-holon-context"]
|
|
164
|
+
callers = [value for name, value in headers if name == b"x-holon-caller"]
|
|
165
|
+
if len(contexts) > 1 or len(callers) > 1:
|
|
166
|
+
return None
|
|
167
|
+
token, principal, caller = "", None, None
|
|
168
|
+
if contexts:
|
|
169
|
+
token = contexts[0].decode("latin-1")
|
|
170
|
+
if (form := _CONTEXT.fullmatch(token)) is None:
|
|
171
|
+
return None
|
|
172
|
+
payload = form[1] + "=" * (-len(form[1]) % 4)
|
|
173
|
+
try:
|
|
174
|
+
claims = _Claims.model_validate_json(base64.urlsafe_b64decode(payload))
|
|
175
|
+
except ValueError:
|
|
176
|
+
return None
|
|
177
|
+
principal = claims.principal
|
|
178
|
+
if callers and (caller := _CALLER.fullmatch(callers[0].decode("latin-1"))) is None:
|
|
179
|
+
return None
|
|
180
|
+
if principal is None and caller is None:
|
|
181
|
+
return token, None
|
|
182
|
+
extra: dict[str, Any] = {}
|
|
183
|
+
if caller is not None:
|
|
184
|
+
extra["caller"] = {"app": caller[2], "session": caller[1]}
|
|
185
|
+
if principal is not None:
|
|
186
|
+
extra |= {"iss": principal.iss or "", "sub": principal.sub or ""}
|
|
187
|
+
if principal.claims is not None:
|
|
188
|
+
extra["claims"] = principal.claims
|
|
189
|
+
if principal.act is not None:
|
|
190
|
+
extra["act"] = principal.act
|
|
191
|
+
scope = (principal.scope if principal else None) or ""
|
|
192
|
+
return token, AccessToken(
|
|
193
|
+
token=token,
|
|
194
|
+
client_id=(principal.client_id if principal else None) or "",
|
|
195
|
+
scopes=[each for each in scope.split(" ") if each],
|
|
196
|
+
subject=extra.get("sub"),
|
|
197
|
+
claims=extra,
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _page(directory: Path | None, path: str) -> Response:
|
|
202
|
+
if directory is None or "/." in path or "\\" in path:
|
|
203
|
+
return Response(status_code=404)
|
|
204
|
+
try:
|
|
205
|
+
root = directory.resolve()
|
|
206
|
+
name = (root / path.lstrip("/")).resolve()
|
|
207
|
+
if name.is_dir():
|
|
208
|
+
name = (name / "index.html").resolve()
|
|
209
|
+
if name.is_relative_to(root) and name.is_file():
|
|
210
|
+
kind = mimetypes.guess_type(name)[0] or "application/octet-stream"
|
|
211
|
+
return FileResponse(
|
|
212
|
+
name, media_type=kind, headers={"x-content-type-options": "nosniff"}
|
|
213
|
+
)
|
|
214
|
+
except (OSError, ValueError):
|
|
215
|
+
pass
|
|
216
|
+
return Response(status_code=404)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
if __name__ == "__main__":
|
|
220
|
+
asyncio.run(serve(load(sys.argv[1])))
|
|
@@ -4,13 +4,21 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "holon-sh"
|
|
7
|
-
version = "0.0.1.
|
|
7
|
+
version = "0.0.1.dev14"
|
|
8
8
|
description = "Typed MCP clients and the caller of an MCP request"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "Apache-2.0"
|
|
11
11
|
license-files = ["LICENSE"]
|
|
12
12
|
requires-python = ">=3.11"
|
|
13
|
-
dependencies = [
|
|
13
|
+
dependencies = [
|
|
14
|
+
"mcp>=2.3,<3",
|
|
15
|
+
"jsonschema>=4.26,<5",
|
|
16
|
+
"httpx2>=2.10,<3",
|
|
17
|
+
"opentelemetry-api>=1.28,<2",
|
|
18
|
+
"pydantic>=2.12,<3",
|
|
19
|
+
"starlette>=0.48,<2",
|
|
20
|
+
"uvicorn>=0.31.1,<1",
|
|
21
|
+
]
|
|
14
22
|
|
|
15
23
|
[project.urls]
|
|
16
24
|
Repository = "https://github.com/holon-sh/sdk-py"
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import asyncio
|
|
2
|
+
import base64
|
|
2
3
|
import json
|
|
3
4
|
import socket
|
|
4
5
|
import sys
|
|
@@ -9,9 +10,6 @@ import httpx2
|
|
|
9
10
|
import pytest
|
|
10
11
|
import uvicorn
|
|
11
12
|
from mcp.server import MCPServer, Server
|
|
12
|
-
from mcp.server.auth.middleware.auth_context import AuthContextMiddleware
|
|
13
|
-
from mcp.server.auth.middleware.bearer_auth import AuthenticatedUser
|
|
14
|
-
from mcp.server.auth.provider import AccessToken
|
|
15
13
|
from mcp.server.streamable_http import CONTENT_TYPE_JSON, CONTENT_TYPE_SSE
|
|
16
14
|
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
|
|
17
15
|
from mcp.shared.exceptions import MCPError
|
|
@@ -33,6 +31,7 @@ from starlette.routing import Mount
|
|
|
33
31
|
|
|
34
32
|
from holon.client import Apps
|
|
35
33
|
from holon.ext.events import Events, NoCompatibleDeliveryMode, _server
|
|
34
|
+
from holon.serve import App, _asgi
|
|
36
35
|
|
|
37
36
|
IDENTITY = {"name": "watcher", "version": "1", "requires": {"boards": {}}}
|
|
38
37
|
|
|
@@ -90,32 +89,24 @@ async def serve(app, port=0):
|
|
|
90
89
|
return sock.getsockname()[1], stop
|
|
91
90
|
|
|
92
91
|
|
|
93
|
-
|
|
92
|
+
@pytest.fixture(autouse=True)
|
|
93
|
+
def inside_holon(monkeypatch):
|
|
94
|
+
monkeypatch.setenv("HOLON_PLATFORM", "1")
|
|
95
|
+
|
|
96
|
+
|
|
94
97
|
def mounted(events):
|
|
95
|
-
|
|
96
|
-
server.streamable_http_app(stateless_http=True)
|
|
97
|
-
manager = server.session_manager
|
|
98
|
-
app = AuthContextMiddleware(
|
|
99
|
-
Starlette(
|
|
100
|
-
routes=[Mount("/mcp", app=events.asgi(manager))],
|
|
101
|
-
lifespan=lambda app: manager.run(),
|
|
102
|
-
)
|
|
103
|
-
)
|
|
98
|
+
return _asgi(App(MCPServer("boards", extensions=[events], middleware=[events])))
|
|
104
99
|
|
|
105
|
-
async def admitted(scope, receive, send):
|
|
106
|
-
if user := dict(scope.get("headers", [])).get(b"x-user"):
|
|
107
|
-
claims = {"iss": "https://id.example", "sub": user.decode()}
|
|
108
|
-
token = AccessToken(
|
|
109
|
-
token="t",
|
|
110
|
-
client_id="c",
|
|
111
|
-
scopes=[],
|
|
112
|
-
subject=user.decode(),
|
|
113
|
-
claims=claims,
|
|
114
|
-
)
|
|
115
|
-
scope = {**scope, "user": AuthenticatedUser(token)}
|
|
116
|
-
await app(scope, receive, send)
|
|
117
100
|
|
|
118
|
-
|
|
101
|
+
def signed_in(user):
|
|
102
|
+
principal = {"iss": "https://id.example", "sub": user, "client_id": "c"}
|
|
103
|
+
claims = {
|
|
104
|
+
"exp": 1900000000,
|
|
105
|
+
"aud": "https://boards.example",
|
|
106
|
+
"principal": principal,
|
|
107
|
+
}
|
|
108
|
+
payload = base64.urlsafe_b64encode(json.dumps(claims).encode()).rstrip(b"=")
|
|
109
|
+
return f"e30.{payload.decode()}.signature"
|
|
119
110
|
|
|
120
111
|
|
|
121
112
|
@asynccontextmanager
|
|
@@ -127,10 +118,11 @@ async def served(events):
|
|
|
127
118
|
await stop()
|
|
128
119
|
|
|
129
120
|
|
|
130
|
-
def client(port, user=None, path="/mcp
|
|
121
|
+
def client(port, user=None, path="/mcp"):
|
|
131
122
|
binding = {"endpoint": f"http://127.0.0.1:{port}{path}"}
|
|
132
123
|
if user:
|
|
133
|
-
|
|
124
|
+
headers = {"x-holon-context": signed_in(user)}
|
|
125
|
+
binding["http_client"] = lambda: httpx2.AsyncClient(headers=headers)
|
|
134
126
|
return Apps(IDENTITY, {}, {"boards": binding}).boards.ext.events
|
|
135
127
|
|
|
136
128
|
|
|
@@ -157,7 +149,7 @@ def request(method, params, id=1):
|
|
|
157
149
|
|
|
158
150
|
|
|
159
151
|
async def call(http, port, method, params):
|
|
160
|
-
url = f"http://127.0.0.1:{port}/mcp
|
|
152
|
+
url = f"http://127.0.0.1:{port}/mcp"
|
|
161
153
|
return (await http.post(url, **request(method, params))).json()
|
|
162
154
|
|
|
163
155
|
|
|
@@ -213,17 +205,21 @@ async def test_a_signed_in_caller_has_one_subscription_per_arguments_and_a_calle
|
|
|
213
205
|
assert began[3] == began[0]
|
|
214
206
|
|
|
215
207
|
|
|
208
|
+
@pytest.mark.parametrize("targeted", [False, True])
|
|
216
209
|
@pytest.mark.parametrize("delivery", ["push", "poll"])
|
|
217
210
|
@asynchronous
|
|
218
|
-
async def
|
|
219
|
-
monkeypatch, delivery
|
|
211
|
+
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(
|
|
212
|
+
monkeypatch, delivery, targeted
|
|
220
213
|
):
|
|
221
214
|
monkeypatch.setattr(_server, "_KEEP_EVENTS", 2)
|
|
222
|
-
|
|
223
|
-
|
|
215
|
+
ids, heard = [], []
|
|
216
|
+
events, changed = declare(
|
|
217
|
+
on_subscribe=lambda context, arguments, id: ids.append(id)
|
|
218
|
+
)
|
|
219
|
+
owed = delivery == "push" and targeted
|
|
224
220
|
async with (
|
|
225
221
|
served(events) as port,
|
|
226
|
-
client(port).subscribe(
|
|
222
|
+
client(port, user="ann").subscribe(
|
|
227
223
|
"board.changed",
|
|
228
224
|
{"board": "b1"},
|
|
229
225
|
delivery=delivery,
|
|
@@ -231,9 +227,9 @@ async def test_a_subscription_behind_what_is_kept_is_told_of_the_gap_and_carries
|
|
|
231
227
|
) as changes,
|
|
232
228
|
):
|
|
233
229
|
for text in ["a", "b", "c", "d"]:
|
|
234
|
-
say(changed, "b1", text)
|
|
230
|
+
say(changed, "b1", text, to=ids[0] if targeted else None)
|
|
235
231
|
heard.extend(await take(changes, 2))
|
|
236
|
-
assert heard == ["gap", "c", "d"]
|
|
232
|
+
assert heard == (["a", "b"] if owed else ["gap", "c", "d"])
|
|
237
233
|
|
|
238
234
|
|
|
239
235
|
@asynchronous
|
|
@@ -372,7 +368,7 @@ async def test_the_messages_on_the_wire_are_the_sketchs():
|
|
|
372
368
|
]:
|
|
373
369
|
assert (await call(http, port, method, params))["error"]["code"] == code
|
|
374
370
|
|
|
375
|
-
url, opened = f"http://127.0.0.1:{port}/mcp
|
|
371
|
+
url, opened = f"http://127.0.0.1:{port}/mcp", changed.cursor()
|
|
376
372
|
async with http.stream(
|
|
377
373
|
"POST", url, **request("events/stream", changed_on("b1"), id=7)
|
|
378
374
|
) as stream:
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import base64
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import signal
|
|
6
|
+
import socket
|
|
7
|
+
import subprocess
|
|
8
|
+
import sys
|
|
9
|
+
from contextlib import asynccontextmanager
|
|
10
|
+
from functools import wraps
|
|
11
|
+
|
|
12
|
+
import httpx2
|
|
13
|
+
import pytest
|
|
14
|
+
import uvicorn
|
|
15
|
+
from mcp.server import MCPServer
|
|
16
|
+
from mcp.server.auth.middleware.auth_context import get_access_token
|
|
17
|
+
from mcp.server.mcpserver import Context
|
|
18
|
+
from mcp.shared.exceptions import MCPError
|
|
19
|
+
from opentelemetry import propagate
|
|
20
|
+
from starlette.datastructures import Headers
|
|
21
|
+
from starlette.responses import RedirectResponse, Response
|
|
22
|
+
|
|
23
|
+
from holon.client import Apps
|
|
24
|
+
from holon.context import Caller, Principal, RequestContext, _request, get_context
|
|
25
|
+
from holon.serve import App, _asgi
|
|
26
|
+
|
|
27
|
+
PERSON = {"iss": "https://id.example", "sub": "ann", "client_id": "web"}
|
|
28
|
+
PROFILE = {"email": "ann@example.com", "email_verified": True}
|
|
29
|
+
REPORTER = "session/reporter"
|
|
30
|
+
TRACEPARENT = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
|
|
31
|
+
IDENTITY = {"name": "caller", "version": "1", "requires": {"peer": {}}}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def asynchronous(test):
|
|
35
|
+
@wraps(test)
|
|
36
|
+
def run(*args, **kwargs):
|
|
37
|
+
asyncio.run(asyncio.wait_for(test(*args, **kwargs), timeout=20))
|
|
38
|
+
|
|
39
|
+
return run
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def context(**payload):
|
|
43
|
+
claims = {"exp": 1900000060.25, "aud": ["https://holon.example"], **payload}
|
|
44
|
+
encoded = base64.urlsafe_b64encode(json.dumps(claims).encode()).rstrip(b"=")
|
|
45
|
+
return f"e30.{encoded.decode()}.signature"
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
async def until(condition):
|
|
49
|
+
while not condition():
|
|
50
|
+
await asyncio.sleep(0.01)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@asynccontextmanager
|
|
54
|
+
async def served(app):
|
|
55
|
+
sock = socket.create_server(("127.0.0.1", 0))
|
|
56
|
+
server = uvicorn.Server(uvicorn.Config(app, log_level="warning"))
|
|
57
|
+
task = asyncio.create_task(server.serve(sockets=[sock]))
|
|
58
|
+
await until(lambda: server.started)
|
|
59
|
+
try:
|
|
60
|
+
yield f"http://127.0.0.1:{sock.getsockname()[1]}"
|
|
61
|
+
finally:
|
|
62
|
+
server.should_exit = True
|
|
63
|
+
await task
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
async def call(url, tool, headers):
|
|
67
|
+
binding = {
|
|
68
|
+
"endpoint": url,
|
|
69
|
+
"http_client": lambda: httpx2.AsyncClient(headers=headers),
|
|
70
|
+
}
|
|
71
|
+
result = await Apps(IDENTITY, {}, {"peer": binding}).peer.call_tool(tool)
|
|
72
|
+
assert not result.is_error, result
|
|
73
|
+
return result
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def inspector(seen):
|
|
77
|
+
server = MCPServer("inspector")
|
|
78
|
+
|
|
79
|
+
@server.tool()
|
|
80
|
+
async def inspect(ctx: Context) -> str:
|
|
81
|
+
trace = {}
|
|
82
|
+
propagate.inject(trace)
|
|
83
|
+
seen.append(
|
|
84
|
+
(
|
|
85
|
+
get_context(),
|
|
86
|
+
get_access_token(),
|
|
87
|
+
set(ctx.headers),
|
|
88
|
+
trace.get("traceparent"),
|
|
89
|
+
)
|
|
90
|
+
)
|
|
91
|
+
return "seen"
|
|
92
|
+
|
|
93
|
+
return server
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@asynchronous
|
|
97
|
+
async def test_inside_holon_a_tool_sees_whom_the_waypoint_admitted_and_not_the_headers_that_said_so(
|
|
98
|
+
monkeypatch,
|
|
99
|
+
):
|
|
100
|
+
monkeypatch.setenv("HOLON_PLATFORM", "1")
|
|
101
|
+
signed_in = context(
|
|
102
|
+
principal={
|
|
103
|
+
**PERSON,
|
|
104
|
+
"scope": " read write ",
|
|
105
|
+
"claims": PROFILE,
|
|
106
|
+
"act": {"sub": "agent"},
|
|
107
|
+
},
|
|
108
|
+
flowId="flow",
|
|
109
|
+
)
|
|
110
|
+
ann = Principal(
|
|
111
|
+
"https://id.example", "ann", "web", ["read", "write"], PROFILE, {"sub": "agent"}
|
|
112
|
+
)
|
|
113
|
+
seen = []
|
|
114
|
+
async with served(_asgi(App(inspector(seen)))) as origin:
|
|
115
|
+
for path, headers, expected in [
|
|
116
|
+
("/mcp", {"x-holon-context": signed_in}, RequestContext(ann, None)),
|
|
117
|
+
(
|
|
118
|
+
"/mcp/inspector",
|
|
119
|
+
{"x-holon-context": signed_in, "x-holon-caller": REPORTER},
|
|
120
|
+
RequestContext(ann, Caller("reporter", "session")),
|
|
121
|
+
),
|
|
122
|
+
(
|
|
123
|
+
"/mcp",
|
|
124
|
+
{"x-holon-caller": REPORTER},
|
|
125
|
+
RequestContext(None, Caller("reporter", "session")),
|
|
126
|
+
),
|
|
127
|
+
("/mcp", {"x-holon-context": context()}, RequestContext(None, None)),
|
|
128
|
+
]:
|
|
129
|
+
forged = {"x-holon-other": "forged", "authorization": "Bearer forged"}
|
|
130
|
+
headers = {**headers, **forged, "traceparent": TRACEPARENT}
|
|
131
|
+
await call(origin + path, "inspect", headers)
|
|
132
|
+
admitted, token, names, traceparent = seen.pop()
|
|
133
|
+
assert admitted == expected
|
|
134
|
+
assert not {name for name in names if name.startswith("x-holon-")}
|
|
135
|
+
assert "authorization" not in names
|
|
136
|
+
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
|
+
|
|
147
|
+
|
|
148
|
+
@asynchronous
|
|
149
|
+
async def test_a_malformed_context_is_a_bad_request(monkeypatch):
|
|
150
|
+
monkeypatch.setenv("HOLON_PLATFORM", "1")
|
|
151
|
+
person = {**PERSON, "act": {"sub": "agent"}}
|
|
152
|
+
seen = []
|
|
153
|
+
async with served(_asgi(App(inspector(seen)))) as origin:
|
|
154
|
+
for headers in [
|
|
155
|
+
[("x-holon-context", context()), ("x-holon-context", context())],
|
|
156
|
+
[("x-holon-caller", REPORTER), ("x-holon-caller", REPORTER)],
|
|
157
|
+
[("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
|
+
[("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
|
+
]:
|
|
168
|
+
async with httpx2.AsyncClient() as client:
|
|
169
|
+
response = await client.post(
|
|
170
|
+
origin + "/mcp", headers=headers, content="{}"
|
|
171
|
+
)
|
|
172
|
+
assert (response.status_code, response.text) == (
|
|
173
|
+
400,
|
|
174
|
+
"Malformed Holon context\n",
|
|
175
|
+
), headers
|
|
176
|
+
assert seen == []
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
@asynchronous
|
|
180
|
+
async def test_outside_holon_the_same_headers_make_an_anonymous_request(monkeypatch):
|
|
181
|
+
monkeypatch.delenv("HOLON_PLATFORM", raising=False)
|
|
182
|
+
seen = []
|
|
183
|
+
async with served(_asgi(App(inspector(seen)))) as origin:
|
|
184
|
+
headers = {
|
|
185
|
+
"x-holon-context": context(principal=PERSON),
|
|
186
|
+
"x-holon-caller": REPORTER,
|
|
187
|
+
"authorization": "Bearer forged",
|
|
188
|
+
}
|
|
189
|
+
await call(origin + "/mcp", "inspect", headers)
|
|
190
|
+
[(admitted, token, names, _)] = seen
|
|
191
|
+
assert (admitted, token) == (RequestContext(None, None), None)
|
|
192
|
+
assert not names & {"x-holon-context", "x-holon-caller", "authorization"}
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
@asynchronous
|
|
196
|
+
async def test_a_peer_call_carries_the_context_until_its_request_ends_and_the_apps_token_always(
|
|
197
|
+
monkeypatch,
|
|
198
|
+
):
|
|
199
|
+
monkeypatch.setenv("HOLON_PLATFORM", "1")
|
|
200
|
+
monkeypatch.setenv("PEER_MCP_TOKEN", "secret")
|
|
201
|
+
peer, received = MCPServer("peer"), []
|
|
202
|
+
|
|
203
|
+
@peer.tool()
|
|
204
|
+
def ping() -> str:
|
|
205
|
+
return "pong"
|
|
206
|
+
|
|
207
|
+
answer = peer.streamable_http_app(stateless_http=True)
|
|
208
|
+
|
|
209
|
+
async def recording(scope, receive, send):
|
|
210
|
+
if scope["type"] == "http":
|
|
211
|
+
headers = Headers(scope=scope)
|
|
212
|
+
received.append(
|
|
213
|
+
(headers.get("x-holon-context"), headers.get("authorization"))
|
|
214
|
+
)
|
|
215
|
+
await answer(scope, receive, send)
|
|
216
|
+
|
|
217
|
+
server, apps, phases, later = MCPServer("caller"), Apps(IDENTITY, {}), [], []
|
|
218
|
+
|
|
219
|
+
@server.tool()
|
|
220
|
+
async def relay() -> str:
|
|
221
|
+
await apps.peer.call_tool("ping")
|
|
222
|
+
phases.append(len(received))
|
|
223
|
+
bound = {"endpoint": os.environ["PEER_MCP_URL"]}
|
|
224
|
+
await Apps(IDENTITY, {}, {"peer": bound}).peer.call_tool("ping")
|
|
225
|
+
phases.append(len(received))
|
|
226
|
+
request = _request.get()
|
|
227
|
+
|
|
228
|
+
async def afterwards():
|
|
229
|
+
await until(lambda: request.ended)
|
|
230
|
+
await apps.peer.call_tool("ping")
|
|
231
|
+
|
|
232
|
+
later.append(asyncio.create_task(afterwards()))
|
|
233
|
+
return "relayed"
|
|
234
|
+
|
|
235
|
+
token = context(principal=PERSON)
|
|
236
|
+
async with served(recording) as peer_origin, served(_asgi(App(server))) as origin:
|
|
237
|
+
monkeypatch.setenv("PEER_MCP_URL", peer_origin + "/mcp")
|
|
238
|
+
await call(origin + "/mcp", "relay", {"x-holon-context": token})
|
|
239
|
+
await later[0]
|
|
240
|
+
during, bound = received[: phases[0]], received[phases[0] : phases[1]]
|
|
241
|
+
assert set(during) == {(token, "Bearer secret")}
|
|
242
|
+
assert set(bound) == {(None, None)}
|
|
243
|
+
assert set(received[phases[1] :]) == {(None, "Bearer secret")}
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
@asynchronous
|
|
247
|
+
async def test_a_peer_that_redirects_is_not_followed(monkeypatch):
|
|
248
|
+
reached = []
|
|
249
|
+
|
|
250
|
+
async def elsewhere(scope, receive, send):
|
|
251
|
+
if scope["type"] == "http":
|
|
252
|
+
reached.append(scope["path"])
|
|
253
|
+
await Response("followed")(scope, receive, send)
|
|
254
|
+
|
|
255
|
+
async with served(elsewhere) as target:
|
|
256
|
+
|
|
257
|
+
async def moved(scope, receive, send):
|
|
258
|
+
if scope["type"] == "http":
|
|
259
|
+
await RedirectResponse(target + "/mcp", 307)(scope, receive, send)
|
|
260
|
+
|
|
261
|
+
async with served(moved) as origin:
|
|
262
|
+
monkeypatch.setenv("PEER_MCP_URL", origin + "/mcp")
|
|
263
|
+
with pytest.raises(MCPError):
|
|
264
|
+
await Apps(IDENTITY, {}).peer.call_tool("ping")
|
|
265
|
+
assert reached == []
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
@asynchronous
|
|
269
|
+
async def test_the_frontend_serves_its_files_and_nothing_hidden_or_outside_it(
|
|
270
|
+
tmp_path,
|
|
271
|
+
):
|
|
272
|
+
web = tmp_path / "web"
|
|
273
|
+
(web / "nested").mkdir(parents=True)
|
|
274
|
+
(web / "index.html").write_text("<p>Home</p>")
|
|
275
|
+
(web / "nested/index.html").write_text("<p>Nested</p>")
|
|
276
|
+
(web / "style.css").write_text("p {}")
|
|
277
|
+
(web / "data.json").write_text("{}")
|
|
278
|
+
(web / ".private").write_text("Hidden")
|
|
279
|
+
(tmp_path / "secret").write_text("Outside")
|
|
280
|
+
(web / "escape").symlink_to(tmp_path / "secret")
|
|
281
|
+
(web / "linked.css").symlink_to(web / "style.css")
|
|
282
|
+
os.mkfifo(web / "pipe")
|
|
283
|
+
app = _asgi(App(MCPServer("site"), web))
|
|
284
|
+
async with httpx2.AsyncClient(
|
|
285
|
+
transport=httpx2.ASGITransport(app=app), base_url="http://app"
|
|
286
|
+
) as http:
|
|
287
|
+
for path, body, kind in [
|
|
288
|
+
("/", "<p>Home</p>", "text/html; charset=utf-8"),
|
|
289
|
+
("/index.html", "<p>Home</p>", "text/html; charset=utf-8"),
|
|
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"),
|
|
295
|
+
]:
|
|
296
|
+
response = await http.get(path)
|
|
297
|
+
assert (response.status_code, response.text) == (200, body), path
|
|
298
|
+
assert response.headers["content-type"] == kind
|
|
299
|
+
assert response.headers["x-content-type-options"] == "nosniff"
|
|
300
|
+
head = await http.head("/style.css")
|
|
301
|
+
assert (head.status_code, head.content) == (200, b"")
|
|
302
|
+
for path in [
|
|
303
|
+
"/.private",
|
|
304
|
+
"/%2eprivate",
|
|
305
|
+
"/%2E%2E/secret",
|
|
306
|
+
"/escape",
|
|
307
|
+
"/pipe",
|
|
308
|
+
"/missing",
|
|
309
|
+
]:
|
|
310
|
+
assert (await http.get(path)).status_code == 404, path
|
|
311
|
+
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
|
+
|
|
318
|
+
|
|
319
|
+
def test_python_m_holon_serve_runs_an_entry_with_a_16_kib_context_and_stops_on_sigterm(
|
|
320
|
+
tmp_path,
|
|
321
|
+
):
|
|
322
|
+
(tmp_path / "src").mkdir()
|
|
323
|
+
(tmp_path / "web").mkdir()
|
|
324
|
+
(tmp_path / "web/index.html").write_text("<p>Home</p>")
|
|
325
|
+
(tmp_path / "src/server.py").write_text(
|
|
326
|
+
"""from mcp.server import MCPServer
|
|
327
|
+
from holon.context import get_context
|
|
328
|
+
|
|
329
|
+
server = MCPServer("large")
|
|
330
|
+
frontend = "web"
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
@server.tool()
|
|
334
|
+
def subject() -> str:
|
|
335
|
+
return get_context().principal.subject
|
|
336
|
+
"""
|
|
337
|
+
)
|
|
338
|
+
subject = "a" * 12200
|
|
339
|
+
token = context(principal={**PERSON, "sub": subject})
|
|
340
|
+
assert len(token) >= 16 * 1024
|
|
341
|
+
with socket.socket() as reserved:
|
|
342
|
+
reserved.bind(("127.0.0.1", 0))
|
|
343
|
+
port = reserved.getsockname()[1]
|
|
344
|
+
environment = {**os.environ, "HOLON_PLATFORM": "1", "PORT": str(port)}
|
|
345
|
+
process = subprocess.Popen(
|
|
346
|
+
[sys.executable, "-m", "holon.serve", "src/server.py"],
|
|
347
|
+
cwd=tmp_path,
|
|
348
|
+
env=environment,
|
|
349
|
+
stderr=subprocess.PIPE,
|
|
350
|
+
text=True,
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
async def exercise():
|
|
354
|
+
origin = f"http://127.0.0.1:{port}"
|
|
355
|
+
async with httpx2.AsyncClient() as http:
|
|
356
|
+
while True:
|
|
357
|
+
try:
|
|
358
|
+
home = await http.get(origin)
|
|
359
|
+
break
|
|
360
|
+
except httpx2.ConnectError:
|
|
361
|
+
await asyncio.sleep(0.05)
|
|
362
|
+
assert home.text == "<p>Home</p>"
|
|
363
|
+
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: {token}\r\n\r\n"
|
|
365
|
+
for piece in [head[:-2], head[-2:]]:
|
|
366
|
+
writer.write(piece.encode())
|
|
367
|
+
await writer.drain()
|
|
368
|
+
await asyncio.sleep(0.1)
|
|
369
|
+
assert (await reader.readline()).startswith(b"HTTP/1.1 200")
|
|
370
|
+
writer.close()
|
|
371
|
+
result = await call(origin + "/mcp", "subject", {"x-holon-context": token})
|
|
372
|
+
assert result.structured_content == {"result": subject}
|
|
373
|
+
|
|
374
|
+
try:
|
|
375
|
+
asyncio.run(asyncio.wait_for(exercise(), timeout=20))
|
|
376
|
+
finally:
|
|
377
|
+
process.send_signal(signal.SIGTERM)
|
|
378
|
+
_, errors = process.communicate(timeout=10)
|
|
379
|
+
assert "Application shutdown complete" in errors
|
|
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
|