holon-sh 0.0.1.dev23__tar.gz → 0.0.1.dev25__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.
Files changed (26) hide show
  1. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/PKG-INFO +1 -1
  2. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/holon/ext/events/_server.py +11 -12
  3. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/holon/serve.py +6 -2
  4. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/pyproject.toml +1 -1
  5. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/tests/test_events.py +26 -23
  6. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/.gitignore +0 -0
  7. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/LICENSE +0 -0
  8. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/README.md +0 -0
  9. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/holon/client.py +0 -0
  10. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/holon/context.py +0 -0
  11. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/holon/ext/__init__.py +0 -0
  12. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/holon/ext/events/__init__.py +0 -0
  13. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/holon/ext/events/_client.py +0 -0
  14. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/holon/ext/events/_wire.py +0 -0
  15. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/holon/ext/files/__init__.py +0 -0
  16. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/holon/ext/files/_client.py +0 -0
  17. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/holon/ext/files/_server.py +0 -0
  18. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/holon/ext/files/_wire.py +0 -0
  19. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/holon/py.typed +0 -0
  20. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/scripts/verify_distribution.py +0 -0
  21. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/tests/fixtures/caller/holon/apps/__init__.py +0 -0
  22. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/tests/test_elicit.py +0 -0
  23. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/tests/test_files.py +0 -0
  24. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/tests/test_package.py +0 -0
  25. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/tests/test_runtime.py +0 -0
  26. {holon_sh-0.0.1.dev23 → holon_sh-0.0.1.dev25}/tests/test_serve.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: holon-sh
3
- Version: 0.0.1.dev23
3
+ Version: 0.0.1.dev25
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
@@ -370,7 +370,7 @@ class Events(Extension):
370
370
  self._epoch = secrets.token_hex(6)
371
371
  self._types: dict[str, EventType[Any, Any]] = {}
372
372
  self._held: Counter[str] = Counter()
373
- self._sending: Counter[str] = Counter()
373
+ self._sending = 0
374
374
  self._sent = asyncio.Event()
375
375
  self._leases: dict[str, _Hold] = {}
376
376
  self._loop: asyncio.AbstractEventLoop | None = None
@@ -595,7 +595,10 @@ class Events(Extension):
595
595
  "ResourceExhausted",
596
596
  {"limit": "subscriptions", "max": _MAX_SUBSCRIPTIONS},
597
597
  )
598
- if self._held[request.caller] >= _MAX_SUBSCRIPTIONS_PER_CALLER:
598
+ if (
599
+ request.caller
600
+ and self._held[request.caller] >= _MAX_SUBSCRIPTIONS_PER_CALLER
601
+ ):
599
602
  raise MCPError(
600
603
  RESOURCE_EXHAUSTED,
601
604
  "ResourceExhausted",
@@ -676,20 +679,16 @@ class Events(Extension):
676
679
  hold.lease.cancel()
677
680
  self._close(hold)
678
681
 
679
- # Waits for room to send `size` bytes. One caller may use a quarter of the budget.
682
+ # Waits for room to send `size` bytes.
680
683
  @asynccontextmanager
681
- async def _send_budget(self, size: int, caller: str) -> AsyncIterator[None]:
682
- while (
683
- (sending := self._sending.total()) and sending + size > _MAX_SENDING_BYTES
684
- ) or (
685
- (mine := self._sending[caller]) and mine + size > _MAX_SENDING_BYTES // 4
686
- ):
684
+ async def _send_budget(self, size: int) -> AsyncIterator[None]:
685
+ while self._sending and self._sending + size > _MAX_SENDING_BYTES:
687
686
  await self._sent.wait()
688
- self._sending[caller] += size
687
+ self._sending += size
689
688
  try:
690
689
  yield
691
690
  finally:
692
- _release(self._sending, caller, size)
691
+ self._sending -= size
693
692
  self._sent.set()
694
693
  self._sent = asyncio.Event()
695
694
 
@@ -708,7 +707,7 @@ class Events(Extension):
708
707
  method=f"notifications/events/{method}", params=body
709
708
  )
710
709
  with anyio.move_on_after(2 * _HEARTBEAT) as waited:
711
- async with self._send_budget(size, request.caller):
710
+ async with self._send_budget(size):
712
711
  await ctx.session.send_notification(
713
712
  cast(ServerNotification, notification),
714
713
  related_request_id=ctx.request_id,
@@ -34,7 +34,11 @@ from holon.ext.events import Events
34
34
 
35
35
  __all__ = ["App", "load", "serve"]
36
36
 
37
- _MCP = re.compile(r"/mcp(/[^/]+)?")
37
+ # On the path before percent-decoding: a segment that decodes to . or .., holds an encoded slash, or is not valid
38
+ # percent-encoding is not an MCP path.
39
+ _MCP = re.compile(
40
+ rb"/mcp(/(?!(?:\.|%2[Ee]){1,2}\Z)(?:[^/%]|%(?!2[Ff])[0-9A-Fa-f]{2})+)?"
41
+ )
38
42
  _CONTEXT = re.compile(r"[A-Za-z0-9_-]+\.([A-Za-z0-9_-]+)\.[A-Za-z0-9_-]+")
39
43
  _CALLER = re.compile(r"([^/]+)/([^/]+)")
40
44
  _MALFORMED = PlainTextResponse(
@@ -120,7 +124,7 @@ def _asgi(app: App) -> ASGIApp:
120
124
  for name, value in headers
121
125
  if not name.startswith(b"x-holon-") and name != b"authorization"
122
126
  ]
123
- if not _MCP.fullmatch(scope["path"]):
127
+ if not _MCP.fullmatch(scope.get("raw_path") or scope["path"].encode()):
124
128
  frontend = app.frontend if scope["method"] in ("GET", "HEAD") else None
125
129
  await _page(frontend, scope["path"])(scope, receive, send)
126
130
  return
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "holon-sh"
7
- version = "0.0.1.dev23"
7
+ version = "0.0.1.dev25"
8
8
  description = "Typed MCP clients and the caller of an MCP request"
9
9
  readme = "README.md"
10
10
  license = "Apache-2.0"
@@ -21,7 +21,6 @@ from mcp.types import (
21
21
  JSONRPC_VERSION,
22
22
  LATEST_PROTOCOL_VERSION,
23
23
  PROTOCOL_VERSION_META_KEY,
24
- CallToolResult,
25
24
  ErrorData,
26
25
  )
27
26
  from pydantic import BaseModel
@@ -452,6 +451,25 @@ async def test_a_poll_that_gives_up_while_it_is_set_up_stops_its_setup_and_holds
452
451
  assert "result" in await call(http, port, "events/poll", changed_on("b2"))
453
452
 
454
453
 
454
+ @asynchronous
455
+ async def test_a_signed_in_caller_is_held_to_its_limit_and_callers_without_an_identity_only_to_the_limit_in_all(
456
+ monkeypatch,
457
+ ):
458
+ monkeypatch.setattr(_server, "_MAX_SUBSCRIPTIONS_PER_CALLER", 1)
459
+ async with (
460
+ served(declare()[0]) as port,
461
+ httpx2.AsyncClient(headers={"x-holon-context": signed_in("ann")}) as ann,
462
+ httpx2.AsyncClient() as anonymous,
463
+ ):
464
+ assert "result" in await call(ann, port, "events/poll", changed_on("b1"))
465
+ refused = await call(ann, port, "events/poll", changed_on("b2"))
466
+ assert refused["error"]["data"] == {"limit": "subscriptionsPerCaller", "max": 1}
467
+ for board in ("b1", "b2"):
468
+ assert "result" in await call(
469
+ anonymous, port, "events/poll", changed_on(board)
470
+ )
471
+
472
+
455
473
  @asynchronous
456
474
  async def test_a_subscription_can_be_retried_after_setup_times_out(
457
475
  monkeypatch,
@@ -471,18 +489,9 @@ async def test_a_subscription_can_be_retried_after_setup_times_out(
471
489
 
472
490
 
473
491
  @asynchronous
474
- async def test_a_subscription_from_the_cursor_read_with_a_snapshot_hears_every_change_after_it():
475
- (events, changed), said = declare(), []
476
-
477
- def tell(text):
478
- said.append(text)
479
- say(changed, "b1", text)
480
-
481
- async def snapshot(context, params):
482
- state = {"said": list(said), "cursor": changed.cursor()}
483
- return CallToolResult(content=[], structured_content=state)
484
-
485
- server = Server("boards", on_call_tool=snapshot)
492
+ async def test_a_low_level_server_serves_the_events():
493
+ events, changed = declare()
494
+ server = Server("boards")
486
495
  events.serve(server)
487
496
  manager = StreamableHTTPSessionManager(server, stateless=True)
488
497
  port, stop = await serve(
@@ -491,15 +500,9 @@ async def test_a_subscription_from_the_cursor_read_with_a_snapshot_hears_every_c
491
500
  lifespan=lambda app: manager.run(),
492
501
  )
493
502
  )
494
- tell("in the snapshot")
495
- binding = {"endpoint": f"http://127.0.0.1:{port}/mcp/"}
496
- boards = Apps(IDENTITY, {}, {"boards": binding}).boards
497
- state = (await boards.call_tool("snapshot")).structured_content
498
- tell("after the snapshot")
499
- async with client(port).subscribe(
500
- "board.changed", {"board": "b1"}, cursor=state["cursor"]
503
+ async with client(port, path="/mcp/").subscribe(
504
+ "board.changed", {"board": "b1"}
501
505
  ) as changes:
502
- assert changes.delivery == "push"
503
- tell("once subscribed")
504
- assert await take(changes, 2) == ["after the snapshot", "once subscribed"]
506
+ say(changed, "b1", "hello")
507
+ assert await take(changes, 1) == ["hello"]
505
508
  await stop()
File without changes
File without changes