test/tools: catalog, grants, and async tool helpers

This commit is contained in:
2026-07-24 14:57:03 +08:00
parent 2f6133f685
commit 12c8adda3a
12 changed files with 285 additions and 35 deletions
+14 -1
View File
@@ -1,5 +1,6 @@
from __future__ import annotations
import asyncio
import inspect
from typing import TYPE_CHECKING, Any, cast
@@ -8,8 +9,20 @@ if TYPE_CHECKING:
def call_sync(definition: ToolDefinition, *args: object, **kwargs: object) -> str:
"""Invoke a tool handler from a **sync** test.
Async handlers are run with ``asyncio.run``. Inside an already-running
event loop (async tests), use :func:`call_async` instead.
"""
result: Any = definition.handler(*args, **kwargs)
assert not inspect.isawaitable(result)
if inspect.isawaitable(result):
try:
asyncio.get_running_loop()
except RuntimeError:
result = asyncio.run(result)
else:
msg = "call_sync cannot await inside a running event loop; use call_async"
raise RuntimeError(msg)
return cast("str", result)