holon-sh 0.0.1.dev18__tar.gz → 0.0.1.dev20__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.dev18 → holon_sh-0.0.1.dev20}/PKG-INFO +1 -1
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/holon/ext/events/_server.py +26 -33
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/holon/ext/files/__init__.py +2 -1
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/holon/ext/files/_server.py +46 -25
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/pyproject.toml +1 -1
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/tests/test_events.py +38 -43
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/tests/test_files.py +44 -11
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/tests/test_package.py +1 -1
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/.gitignore +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/LICENSE +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/README.md +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/holon/client.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/holon/context.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/holon/ext/__init__.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/holon/ext/events/__init__.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/holon/ext/events/_client.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/holon/ext/events/_wire.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/holon/ext/files/_client.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/holon/ext/files/_wire.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/holon/py.typed +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/holon/serve.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/scripts/verify_distribution.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/tests/fixtures/caller/holon/apps/__init__.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/tests/test_context.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/tests/test_elicit.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/tests/test_runtime.py +0 -0
- {holon_sh-0.0.1.dev18 → holon_sh-0.0.1.dev20}/tests/test_serve.py +0 -0
|
@@ -12,13 +12,15 @@ import secrets
|
|
|
12
12
|
import time
|
|
13
13
|
import uuid
|
|
14
14
|
from collections import Counter
|
|
15
|
-
from collections.abc import AsyncIterator, Awaitable, Callable, Sequence
|
|
15
|
+
from collections.abc import AsyncIterator, Awaitable, Callable, Mapping, Sequence
|
|
16
16
|
from contextlib import asynccontextmanager
|
|
17
17
|
from dataclasses import dataclass, field
|
|
18
18
|
from datetime import UTC, datetime
|
|
19
19
|
from typing import Any, Generic, TypeAlias, TypeVar, cast
|
|
20
20
|
|
|
21
21
|
import anyio
|
|
22
|
+
import jsonschema
|
|
23
|
+
from jsonschema.protocols import Validator
|
|
22
24
|
from mcp.server.context import CallNext, HandlerResult, ServerRequestContext
|
|
23
25
|
from mcp.server.extension import Extension, MethodBinding
|
|
24
26
|
from mcp.server.lowlevel.server import Server
|
|
@@ -33,8 +35,9 @@ from mcp.types import (
|
|
|
33
35
|
RequestParams,
|
|
34
36
|
ServerNotification,
|
|
35
37
|
)
|
|
36
|
-
from pydantic import Field, StrictInt, StrictStr
|
|
38
|
+
from pydantic import Field, StrictInt, StrictStr
|
|
37
39
|
from pydantic_core import to_json, to_jsonable_python
|
|
40
|
+
from referencing import Registry
|
|
38
41
|
|
|
39
42
|
from holon.context import get_context
|
|
40
43
|
from holon.ext.events._wire import (
|
|
@@ -64,7 +67,7 @@ _MAX_SUBSCRIPTIONS_PER_CALLER = 100
|
|
|
64
67
|
_LIST_PAGE_SIZE = 100
|
|
65
68
|
_SUBSCRIBE_TIMEOUT = 30.0
|
|
66
69
|
|
|
67
|
-
A = TypeVar("A")
|
|
70
|
+
A = TypeVar("A", bound=Mapping[str, Any])
|
|
68
71
|
T = TypeVar("T")
|
|
69
72
|
|
|
70
73
|
|
|
@@ -159,7 +162,7 @@ class EventType(Generic[A, D]):
|
|
|
159
162
|
|
|
160
163
|
name: str
|
|
161
164
|
_events: Events
|
|
162
|
-
_input:
|
|
165
|
+
_input: Validator
|
|
163
166
|
_descriptor: dict[str, Any]
|
|
164
167
|
_match: Match[D, A] | None
|
|
165
168
|
_transform: Transform[D, A] | None
|
|
@@ -376,7 +379,7 @@ class Events(Extension):
|
|
|
376
379
|
self,
|
|
377
380
|
name: str,
|
|
378
381
|
*,
|
|
379
|
-
input_schema:
|
|
382
|
+
input_schema: dict[str, Any],
|
|
380
383
|
payload_schema: dict[str, Any],
|
|
381
384
|
description: str | None = None,
|
|
382
385
|
match: Match[D, A] | None = None,
|
|
@@ -387,12 +390,14 @@ class Events(Extension):
|
|
|
387
390
|
) -> EventType[A, D]:
|
|
388
391
|
"""Declare an event type.
|
|
389
392
|
|
|
390
|
-
Annotate the result as ``EventType[Arguments, Data]`` to type ``emit``.
|
|
391
|
-
|
|
392
|
-
|
|
393
|
+
Annotate the result as ``EventType[Arguments, Data]`` to type the hooks and ``emit``. Hooks receive the
|
|
394
|
+
arguments as the subscription sent them, so ``Arguments`` is a ``TypedDict`` or ``dict[str, Any]``. Each
|
|
395
|
+
hook's ``context`` is the server's context of the request it runs in. A subscription outlives the first
|
|
396
|
+
request to name it, where ``on_subscribe`` runs, and other requests may share it: keep what the hook reads
|
|
397
|
+
from ``context``, not ``context``.
|
|
393
398
|
|
|
394
399
|
Args:
|
|
395
|
-
input_schema:
|
|
400
|
+
input_schema: JSON Schema of a subscription's arguments. Arguments that do not match are refused.
|
|
396
401
|
payload_schema: JSON Schema of each occurrence's data.
|
|
397
402
|
match: ``(context, event, arguments)``: whether a subscription receives an event. Defaults to all.
|
|
398
403
|
transform: ``(context, event, arguments)``: the data a subscription receives.
|
|
@@ -405,21 +410,18 @@ class Events(Extension):
|
|
|
405
410
|
raise ValueError(f"Event {name} is already declared.")
|
|
406
411
|
if not delivery:
|
|
407
412
|
raise ValueError(f"Event {name} must be delivered by poll, push or both.")
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
if isinstance(input_schema, TypeAdapter)
|
|
411
|
-
else TypeAdapter(input_schema)
|
|
412
|
-
)
|
|
413
|
+
checker = jsonschema.validators.validator_for(input_schema)
|
|
414
|
+
checker.check_schema(input_schema)
|
|
413
415
|
descriptor = {
|
|
414
416
|
"name": name,
|
|
415
417
|
**({"description": description} if description else {}),
|
|
416
|
-
"inputSchema":
|
|
418
|
+
"inputSchema": input_schema,
|
|
417
419
|
"payloadSchema": payload_schema,
|
|
418
420
|
}
|
|
419
421
|
declared = EventType(
|
|
420
422
|
name,
|
|
421
423
|
self,
|
|
422
|
-
|
|
424
|
+
checker(input_schema, registry=Registry()),
|
|
423
425
|
descriptor,
|
|
424
426
|
match,
|
|
425
427
|
transform,
|
|
@@ -532,16 +534,14 @@ class Events(Extension):
|
|
|
532
534
|
"Unsupported",
|
|
533
535
|
{"feature": "deliveryMode", "value": delivery},
|
|
534
536
|
)
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
f"{'.'.join(map(str, each['loc'])) or 'arguments'}: {each['msg']}"
|
|
540
|
-
for each in error.errors()
|
|
541
|
-
)
|
|
537
|
+
arguments = params.arguments or {}
|
|
538
|
+
if error := jsonschema.exceptions.best_match(
|
|
539
|
+
declared._input.iter_errors(arguments)
|
|
540
|
+
):
|
|
542
541
|
raise MCPError(
|
|
543
|
-
INVALID_PARAMS,
|
|
544
|
-
|
|
542
|
+
INVALID_PARAMS,
|
|
543
|
+
f"Invalid arguments for {params.name}: {error.json_path}: {error.message}",
|
|
544
|
+
)
|
|
545
545
|
principal = get_context().principal
|
|
546
546
|
caller = ""
|
|
547
547
|
if principal is not None:
|
|
@@ -556,14 +556,7 @@ class Events(Extension):
|
|
|
556
556
|
if principal is None and delivery == "push":
|
|
557
557
|
id = f"sub_{uuid.uuid4().hex}"
|
|
558
558
|
else:
|
|
559
|
-
canonical = json.dumps(
|
|
560
|
-
[
|
|
561
|
-
caller,
|
|
562
|
-
declared.name,
|
|
563
|
-
declared._input.dump_python(arguments, mode="json"),
|
|
564
|
-
],
|
|
565
|
-
sort_keys=True,
|
|
566
|
-
)
|
|
559
|
+
canonical = json.dumps([caller, declared.name, arguments], sort_keys=True)
|
|
567
560
|
id = f"sub_{hashlib.sha256(canonical.encode()).hexdigest()[:32]}"
|
|
568
561
|
request = _Request(
|
|
569
562
|
declared,
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"""Files in MCP tool calls.
|
|
2
2
|
|
|
3
3
|
Clients pass a :class:`File` and read returned files with :func:`downloads`. Servers accept files with
|
|
4
|
-
:class:`FileServer` and a :class:`Storage` such as :class:`LocalStorage
|
|
4
|
+
:class:`FileServer` and a :class:`Storage` such as :class:`LocalStorage`; a tool declares each file argument a
|
|
5
|
+
:class:`ReceivedFile`.
|
|
5
6
|
|
|
6
7
|
https://github.com/modelcontextprotocol/experimental-ext-file-uploads/blob/main/specification/draft/file-uploads.md
|
|
7
8
|
"""
|
|
@@ -13,9 +13,9 @@ import shutil
|
|
|
13
13
|
import tempfile
|
|
14
14
|
import time
|
|
15
15
|
from collections.abc import Callable, Mapping
|
|
16
|
-
from dataclasses import
|
|
16
|
+
from dataclasses import replace
|
|
17
17
|
from pathlib import Path
|
|
18
|
-
from typing import Annotated, Any, Literal, Protocol
|
|
18
|
+
from typing import Annotated, Any, Literal, Protocol, final
|
|
19
19
|
from urllib.parse import quote
|
|
20
20
|
|
|
21
21
|
import anyio
|
|
@@ -38,6 +38,7 @@ from mcp.types import (
|
|
|
38
38
|
TextContent,
|
|
39
39
|
)
|
|
40
40
|
from pydantic import BaseModel, ConfigDict, Field, ValidationError
|
|
41
|
+
from pydantic_core import CoreSchema, core_schema
|
|
41
42
|
from starlette import status
|
|
42
43
|
from starlette.requests import ClientDisconnect, Request
|
|
43
44
|
from starlette.responses import FileResponse, Response
|
|
@@ -50,6 +51,7 @@ from holon.ext.files._wire import (
|
|
|
50
51
|
b64url,
|
|
51
52
|
declared,
|
|
52
53
|
digest_required,
|
|
54
|
+
file_input_schema,
|
|
53
55
|
media_type,
|
|
54
56
|
sha256,
|
|
55
57
|
walk_files,
|
|
@@ -61,23 +63,50 @@ _STATE = "files."
|
|
|
61
63
|
_TTL = 900.0
|
|
62
64
|
|
|
63
65
|
|
|
64
|
-
@
|
|
66
|
+
@final
|
|
65
67
|
class ReceivedFile:
|
|
66
|
-
"""A file argument, its bytes checked against its size and any digest.
|
|
68
|
+
"""A file argument of the current call, its bytes checked against its size and any digest.
|
|
69
|
+
|
|
70
|
+
A tool takes one by declaring an argument ``ReceivedFile``. ``ReceivedFile(value)`` decodes
|
|
71
|
+
an argument's value where Pydantic does not, such as in a msgspec ``dec_hook``.
|
|
67
72
|
|
|
68
73
|
Attributes:
|
|
69
74
|
name: Client-given name. Untrusted.
|
|
70
75
|
mime_type: Client-claimed media type. Untrusted.
|
|
71
76
|
size: Size in bytes.
|
|
72
77
|
digest: Base64url SHA-256, when verified.
|
|
78
|
+
|
|
79
|
+
Raises:
|
|
80
|
+
ValueError: ``value`` is not a file argument of the current call.
|
|
73
81
|
"""
|
|
74
82
|
|
|
83
|
+
__slots__ = ("_source", "digest", "mime_type", "name", "size")
|
|
75
84
|
name: str | None
|
|
76
85
|
mime_type: str
|
|
77
86
|
size: int
|
|
78
87
|
digest: str | None
|
|
79
88
|
_source: Path | bytes
|
|
80
89
|
|
|
90
|
+
def __new__(cls, value: object) -> ReceivedFile:
|
|
91
|
+
file_id = value.get("id") if isinstance(value, dict) else None
|
|
92
|
+
found = (
|
|
93
|
+
(_received.get() or {}).get(file_id) if isinstance(file_id, str) else None
|
|
94
|
+
)
|
|
95
|
+
if found is None:
|
|
96
|
+
raise ValueError("not a file argument of this call")
|
|
97
|
+
return found
|
|
98
|
+
|
|
99
|
+
def __setattr__(self, name: str, value: object) -> None:
|
|
100
|
+
raise AttributeError(f"ReceivedFile.{name} is read-only")
|
|
101
|
+
|
|
102
|
+
@classmethod
|
|
103
|
+
def __get_pydantic_core_schema__(cls, *_: object) -> CoreSchema:
|
|
104
|
+
return core_schema.no_info_plain_validator_function(cls)
|
|
105
|
+
|
|
106
|
+
@classmethod
|
|
107
|
+
def __get_pydantic_json_schema__(cls, *_: object) -> dict[str, Any]:
|
|
108
|
+
return file_input_schema()
|
|
109
|
+
|
|
81
110
|
async def read(self) -> bytes:
|
|
82
111
|
if isinstance(self._source, bytes):
|
|
83
112
|
return self._source
|
|
@@ -153,6 +182,16 @@ class _FileInput(BaseModel):
|
|
|
153
182
|
class _Refused(Exception): ...
|
|
154
183
|
|
|
155
184
|
|
|
185
|
+
def _verified(
|
|
186
|
+
file: _FileInput, digest: str | None, source: Path | bytes
|
|
187
|
+
) -> ReceivedFile:
|
|
188
|
+
made = object.__new__(ReceivedFile)
|
|
189
|
+
values = (source, digest, file.mimeType, file.name, file.size)
|
|
190
|
+
for slot, value in zip(ReceivedFile.__slots__, values, strict=True):
|
|
191
|
+
object.__setattr__(made, slot, value)
|
|
192
|
+
return made
|
|
193
|
+
|
|
194
|
+
|
|
156
195
|
_received: contextvars.ContextVar[dict[str, ReceivedFile] | None] = (
|
|
157
196
|
contextvars.ContextVar("holon_files_received", default=None)
|
|
158
197
|
)
|
|
@@ -161,7 +200,7 @@ _received: contextvars.ContextVar[dict[str, ReceivedFile] | None] = (
|
|
|
161
200
|
class FileServer:
|
|
162
201
|
"""Low-level server middleware that accepts file arguments in ``tools/call``.
|
|
163
202
|
|
|
164
|
-
Insert it first::
|
|
203
|
+
Tools declare each file argument as a ``ReceivedFile``. Insert it first::
|
|
165
204
|
|
|
166
205
|
server.extensions[EXTENSION] = files.settings()
|
|
167
206
|
server.middleware.insert(0, files)
|
|
@@ -192,20 +231,6 @@ class FileServer:
|
|
|
192
231
|
return {"upload": {}, "maxInlineBytes": self._max_inline_bytes}
|
|
193
232
|
return {"upload": {}}
|
|
194
233
|
|
|
195
|
-
def received(self, file_input: Mapping[str, Any]) -> ReceivedFile:
|
|
196
|
-
"""The checked bytes of a file argument of the current call.
|
|
197
|
-
|
|
198
|
-
Raises:
|
|
199
|
-
LookupError: The argument is not a file of this call.
|
|
200
|
-
"""
|
|
201
|
-
file_id = file_input.get("id")
|
|
202
|
-
found = (
|
|
203
|
-
(_received.get() or {}).get(file_id) if isinstance(file_id, str) else None
|
|
204
|
-
)
|
|
205
|
-
if found is None:
|
|
206
|
-
raise LookupError("not a file argument of this call")
|
|
207
|
-
return found
|
|
208
|
-
|
|
209
234
|
async def reply(
|
|
210
235
|
self,
|
|
211
236
|
ctx: ServerRequestContext[Any, Any],
|
|
@@ -331,9 +356,7 @@ class FileServer:
|
|
|
331
356
|
return _error(
|
|
332
357
|
f"The bytes of file {file.name or file.id!r} did not arrive intact."
|
|
333
358
|
)
|
|
334
|
-
received[file.id] =
|
|
335
|
-
file.name, file.mimeType, file.size, digest, path
|
|
336
|
-
)
|
|
359
|
+
received[file.id] = _verified(file, digest, path)
|
|
337
360
|
annotations = definition.get("annotations") or {}
|
|
338
361
|
if annotations.get("idempotentHint") is not True and not (
|
|
339
362
|
await self._storage.consume(record["n"], record["e"])
|
|
@@ -441,9 +464,7 @@ def _inspect(
|
|
|
441
464
|
if digest is not None and sha256(raw) != digest:
|
|
442
465
|
raise _Refused(f"File {label} does not match its digest.")
|
|
443
466
|
inline += file.size
|
|
444
|
-
received[file.id] =
|
|
445
|
-
file.name, file.mimeType, file.size, digest, raw
|
|
446
|
-
)
|
|
467
|
+
received[file.id] = _verified(file, digest, raw)
|
|
447
468
|
if inline > max_inline_bytes:
|
|
448
469
|
raise _Refused("The inline file data is larger than this server accepts.")
|
|
449
470
|
return received, owed
|
|
@@ -52,11 +52,11 @@ def asynchronous(test):
|
|
|
52
52
|
return run
|
|
53
53
|
|
|
54
54
|
|
|
55
|
-
def declare(events=None, **hooks) -> tuple[Events, EventType[
|
|
55
|
+
def declare(events=None, **hooks) -> tuple[Events, EventType[dict[str, str], Change]]:
|
|
56
56
|
events = events or Events()
|
|
57
57
|
return events, events.event(
|
|
58
58
|
"board.changed",
|
|
59
|
-
input_schema=Board,
|
|
59
|
+
input_schema=Board.model_json_schema(),
|
|
60
60
|
payload_schema=Change.model_json_schema(),
|
|
61
61
|
**hooks,
|
|
62
62
|
)
|
|
@@ -166,7 +166,7 @@ async def test_a_subscription_hears_what_its_arguments_match_as_shaped_across_a_
|
|
|
166
166
|
delivery,
|
|
167
167
|
):
|
|
168
168
|
events, changed = declare(
|
|
169
|
-
match=lambda context, event, arguments: event.data.board == arguments
|
|
169
|
+
match=lambda context, event, arguments: event.data.board == arguments["board"],
|
|
170
170
|
transform=lambda context, event, arguments: {
|
|
171
171
|
"board": event.data.board,
|
|
172
172
|
"text": event.data.text.upper(),
|
|
@@ -207,6 +207,22 @@ async def test_targeted_events_follow_shared_identity_but_isolate_anonymous_read
|
|
|
207
207
|
assert (await anext(second)).event_id == expected
|
|
208
208
|
|
|
209
209
|
|
|
210
|
+
@asynchronous
|
|
211
|
+
async def test_a_subscription_is_named_by_its_arguments_as_sent():
|
|
212
|
+
subscribed = []
|
|
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
|
+
|
|
210
226
|
@pytest.mark.parametrize("targeted", [False, True])
|
|
211
227
|
@pytest.mark.parametrize("delivery", ["push", "poll"])
|
|
212
228
|
@asynchronous
|
|
@@ -236,28 +252,21 @@ async def test_a_subscription_behind_what_is_kept_is_told_of_the_gap_and_carries
|
|
|
236
252
|
|
|
237
253
|
|
|
238
254
|
@asynchronous
|
|
239
|
-
async def
|
|
240
|
-
events, changed = declare(
|
|
241
|
-
|
|
242
|
-
changed, arguments.board, "welcome", to=id
|
|
243
|
-
)
|
|
244
|
-
)
|
|
245
|
-
restarted = declare()[1].cursor()
|
|
246
|
-
gaps = []
|
|
255
|
+
async def test_a_cursor_from_before_a_restart_is_a_gap():
|
|
256
|
+
events, changed = declare()
|
|
257
|
+
restarted, gaps = declare()[1].cursor(), []
|
|
247
258
|
async with served(events) as port:
|
|
248
|
-
for
|
|
249
|
-
|
|
250
|
-
board
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
)
|
|
258
|
-
|
|
259
|
-
assert await take(changes, 2) == ["welcome", "hello"]
|
|
260
|
-
assert gaps == ["push resumed", "poll resumed"]
|
|
259
|
+
for delivery in ["push", "poll"]:
|
|
260
|
+
async with client(port, user="ann").subscribe(
|
|
261
|
+
"board.changed",
|
|
262
|
+
{"board": delivery},
|
|
263
|
+
delivery=delivery,
|
|
264
|
+
cursor=restarted,
|
|
265
|
+
on_gap=partial(gaps.append, delivery),
|
|
266
|
+
) as changes:
|
|
267
|
+
say(changed, delivery, "hello")
|
|
268
|
+
assert await take(changes, 1) == ["hello"]
|
|
269
|
+
assert gaps == ["push", "poll"]
|
|
261
270
|
|
|
262
271
|
|
|
263
272
|
@asynchronous
|
|
@@ -265,7 +274,7 @@ async def test_a_subscription_the_app_ends_is_sent_what_was_emitted_to_it_unless
|
|
|
265
274
|
ids = {}
|
|
266
275
|
|
|
267
276
|
def on_subscribe(context, arguments, id):
|
|
268
|
-
ids[arguments
|
|
277
|
+
ids[arguments["board"]] = id
|
|
269
278
|
|
|
270
279
|
events, changed = declare(on_subscribe=on_subscribe)
|
|
271
280
|
async with served(events) as port:
|
|
@@ -297,16 +306,6 @@ async def test_a_subscription_the_app_ends_is_sent_what_was_emitted_to_it_unless
|
|
|
297
306
|
|
|
298
307
|
@asynchronous
|
|
299
308
|
async def test_the_messages_on_the_wire_are_the_sketchs():
|
|
300
|
-
recursive = {
|
|
301
|
-
"$ref": "#/$defs/Node",
|
|
302
|
-
"$defs": {
|
|
303
|
-
"Node": {
|
|
304
|
-
"type": "object",
|
|
305
|
-
"properties": {"child": {"$ref": "#/$defs/Node"}},
|
|
306
|
-
}
|
|
307
|
-
},
|
|
308
|
-
}
|
|
309
|
-
|
|
310
309
|
def shaped(context, event, arguments):
|
|
311
310
|
if event.data.text == "bad":
|
|
312
311
|
raise ValueError("cannot be shaped")
|
|
@@ -319,10 +318,10 @@ async def test_the_messages_on_the_wire_are_the_sketchs():
|
|
|
319
318
|
)
|
|
320
319
|
events.event(
|
|
321
320
|
"board.locked",
|
|
322
|
-
input_schema=Board,
|
|
323
|
-
payload_schema=
|
|
321
|
+
input_schema=Board.model_json_schema(),
|
|
322
|
+
payload_schema=Change.model_json_schema(),
|
|
324
323
|
delivery=["poll"],
|
|
325
|
-
authorize=lambda context, arguments: arguments
|
|
324
|
+
authorize=lambda context, arguments: arguments["board"] != "secret",
|
|
326
325
|
)
|
|
327
326
|
async with served(events) as port, httpx2.AsyncClient(timeout=10) as http:
|
|
328
327
|
discovered = await call(http, port, "server/discover", {})
|
|
@@ -331,7 +330,6 @@ async def test_the_messages_on_the_wire_are_the_sketchs():
|
|
|
331
330
|
event["name"]: event
|
|
332
331
|
for event in (await call(http, port, "events/list", {}))["result"]["events"]
|
|
333
332
|
}
|
|
334
|
-
assert listed["board.locked"]["payloadSchema"] == recursive
|
|
335
333
|
assert set(listed["board.changed"]["delivery"]) == {"poll", "push"}
|
|
336
334
|
expected = {
|
|
337
335
|
"description": "A board changed",
|
|
@@ -486,10 +484,7 @@ async def test_a_subscription_can_be_retried_after_setup_times_out(
|
|
|
486
484
|
|
|
487
485
|
@asynchronous
|
|
488
486
|
async def test_a_subscription_from_the_cursor_read_with_a_snapshot_hears_every_change_after_it():
|
|
489
|
-
events, said =
|
|
490
|
-
changed: EventType[Board, Change] = events.event(
|
|
491
|
-
"board.changed", input_schema=Board, payload_schema=Change.model_json_schema()
|
|
492
|
-
)
|
|
487
|
+
(events, changed), said = declare(), []
|
|
493
488
|
|
|
494
489
|
def tell(text):
|
|
495
490
|
said.append(text)
|
|
@@ -13,7 +13,9 @@ from pathlib import Path
|
|
|
13
13
|
import httpx2
|
|
14
14
|
import pytest
|
|
15
15
|
import uvicorn
|
|
16
|
+
from mcp.server import MCPServer
|
|
16
17
|
from mcp.server.lowlevel import Server
|
|
18
|
+
from mcp.server.mcpserver import Context
|
|
17
19
|
from mcp.server.streamable_http import CONTENT_TYPE_JSON, CONTENT_TYPE_SSE
|
|
18
20
|
from mcp.shared.inbound import (
|
|
19
21
|
MCP_METHOD_HEADER,
|
|
@@ -48,6 +50,7 @@ from holon.ext.files import (
|
|
|
48
50
|
FileTransferError,
|
|
49
51
|
LocalStorage,
|
|
50
52
|
OutcomeUnknownError,
|
|
53
|
+
ReceivedFile,
|
|
51
54
|
downloads,
|
|
52
55
|
file_input_schema,
|
|
53
56
|
)
|
|
@@ -84,6 +87,16 @@ TOOLS = [
|
|
|
84
87
|
]
|
|
85
88
|
|
|
86
89
|
|
|
90
|
+
def asking():
|
|
91
|
+
question = ElicitRequestFormParams(
|
|
92
|
+
message="Why?", requested_schema={"type": "object", "properties": {}}
|
|
93
|
+
)
|
|
94
|
+
return InputRequiredResult(
|
|
95
|
+
input_requests={"why": ElicitRequest(params=question)},
|
|
96
|
+
request_state="asked",
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
|
|
87
100
|
def serve(tmp_path):
|
|
88
101
|
"""A server whose tools save, check and export files; returns (app, ran)."""
|
|
89
102
|
storage = LocalStorage(tmp_path / "storage")
|
|
@@ -102,15 +115,9 @@ def serve(tmp_path):
|
|
|
102
115
|
if params.name == "export":
|
|
103
116
|
return await files.reply(ctx, CSV, name="report.csv", text="Exported.")
|
|
104
117
|
if arguments.get("note") == "ask" and not params.input_responses:
|
|
105
|
-
|
|
106
|
-
message="Why?", requested_schema={"type": "object", "properties": {}}
|
|
107
|
-
)
|
|
108
|
-
return InputRequiredResult(
|
|
109
|
-
input_requests={"why": ElicitRequest(params=question)},
|
|
110
|
-
request_state="asked",
|
|
111
|
-
)
|
|
118
|
+
return asking()
|
|
112
119
|
for value in arguments.get("files") or [arguments["file"]]:
|
|
113
|
-
received =
|
|
120
|
+
received = ReceivedFile(value)
|
|
114
121
|
if params.name == "save":
|
|
115
122
|
await received.save(tmp_path / "saved" / Path(received.name).name)
|
|
116
123
|
return CallToolResult(content=[TextContent(type="text", text="ok")])
|
|
@@ -176,15 +183,16 @@ def run(app, body):
|
|
|
176
183
|
asyncio.run(asyncio.wait_for(main(), timeout=20))
|
|
177
184
|
|
|
178
185
|
|
|
186
|
+
async def answer(params):
|
|
187
|
+
return ElicitResult(action="accept", content={})
|
|
188
|
+
|
|
189
|
+
|
|
179
190
|
def test_files_reach_the_tool_inline_and_out_of_band(tmp_path):
|
|
180
191
|
(tmp_path / "notes.txt").write_bytes(b"x" * 100)
|
|
181
192
|
app, ran = serve(tmp_path)
|
|
182
193
|
seen = []
|
|
183
194
|
files = client(app, seen)
|
|
184
195
|
|
|
185
|
-
async def answer(params):
|
|
186
|
-
return ElicitResult(action="accept", content={})
|
|
187
|
-
|
|
188
196
|
async def body():
|
|
189
197
|
await files.tools.save(file=File(b"tiny", name="a.txt"), cover=None)
|
|
190
198
|
[inline] = tool_calls(seen)
|
|
@@ -220,6 +228,31 @@ def test_files_reach_the_tool_inline_and_out_of_band(tmp_path):
|
|
|
220
228
|
assert ran == ["save"] * 3
|
|
221
229
|
|
|
222
230
|
|
|
231
|
+
def test_a_declared_file_reaches_its_tool_across_an_input_request(tmp_path):
|
|
232
|
+
storage, server, read = LocalStorage(tmp_path), MCPServer("files"), []
|
|
233
|
+
|
|
234
|
+
async def save(file: ReceivedFile, ctx: Context) -> str:
|
|
235
|
+
if not ctx.input_responses:
|
|
236
|
+
return asking()
|
|
237
|
+
read.append(await file.read())
|
|
238
|
+
return "ok"
|
|
239
|
+
|
|
240
|
+
server.add_tool(save)
|
|
241
|
+
[tool] = asyncio.run(server.list_tools())
|
|
242
|
+
declared = tool.model_dump(by_alias=True, exclude_none=True)
|
|
243
|
+
files = FileServer(storage, tools=lambda name: declared, max_inline_bytes=16)
|
|
244
|
+
server._lowlevel_server.extensions[EXTENSION] = files.settings()
|
|
245
|
+
server.middleware.insert(0, files)
|
|
246
|
+
app = server.streamable_http_app(stateless_http=True, json_response=True)
|
|
247
|
+
app.mount("/mcp/files", storage.app)
|
|
248
|
+
|
|
249
|
+
async def body():
|
|
250
|
+
await client(app, []).tools.save({"elicit": answer}, file=File(b"x" * 100))
|
|
251
|
+
|
|
252
|
+
run(app, body)
|
|
253
|
+
assert read == [b"x" * 100]
|
|
254
|
+
|
|
255
|
+
|
|
223
256
|
def sink(attempts, served=None):
|
|
224
257
|
"""Where transfers land: records each (method, authorization), serves
|
|
225
258
|
``served`` by name, and answers 503 for ``busy``."""
|
|
@@ -20,7 +20,7 @@ from holon.ext.events import Events, EventType
|
|
|
20
20
|
|
|
21
21
|
def emit() -> None:
|
|
22
22
|
changed: EventType[dict[str, str], dict[str, str]] = Events().event(
|
|
23
|
-
"board.changed", input_schema=
|
|
23
|
+
"board.changed", input_schema={"type": "object"},
|
|
24
24
|
payload_schema={"type": "object", "additionalProperties": {"type": "string"}},
|
|
25
25
|
)
|
|
26
26
|
changed.emit({"text": "hello"})
|
|
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
|