|
| 1 | +from typing import Optional, Union |
| 2 | +from urllib.parse import quote |
| 3 | + |
| 4 | +from hyperbrowser.client._request import coerce_request, dump_request |
| 5 | +from hyperbrowser.models.webmcp import ( |
| 6 | + WebMCPInvocation, |
| 7 | + WebMCPInvokeParams, |
| 8 | + WebMCPInvokeResult, |
| 9 | + WebMCPResultParams, |
| 10 | + WebMCPStartParams, |
| 11 | + WebMCPToolsResponse, |
| 12 | +) |
| 13 | +from hyperbrowser.types.webmcp import ( |
| 14 | + WebMCPInvokeParams as WebMCPInvokeParamsDict, |
| 15 | + WebMCPResultParams as WebMCPResultParamsDict, |
| 16 | + WebMCPStartParams as WebMCPStartParamsDict, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class WebMCPManager: |
| 21 | + """Discover and invoke page tools in sessions created with enable_web_mcp=True.""" |
| 22 | + |
| 23 | + def __init__(self, client): |
| 24 | + self._client = client |
| 25 | + |
| 26 | + def _url(self, session_id: str, suffix: str) -> str: |
| 27 | + return self._client._build_url( |
| 28 | + f"/session/{quote(session_id, safe='')}/webmcp{suffix}" |
| 29 | + ) |
| 30 | + |
| 31 | + async def list_tools(self, session_id: str) -> WebMCPToolsResponse: |
| 32 | + """Discover document-bound tools across the session's tabs and frames.""" |
| 33 | + response = await self._client.transport.get( |
| 34 | + self._url(session_id, "/tools"), timeout=max(self._client.timeout, 40) |
| 35 | + ) |
| 36 | + return WebMCPToolsResponse(**response.data) |
| 37 | + |
| 38 | + async def invoke( |
| 39 | + self, |
| 40 | + session_id: str, |
| 41 | + params: Union[WebMCPInvokeParamsDict, WebMCPInvokeParams], |
| 42 | + ) -> WebMCPInvokeResult: |
| 43 | + """Invoke once and wait. A form can return awaiting_submission with a handle. |
| 44 | +
|
| 45 | + This POST is never automatically retried: losing its response can leave the |
| 46 | + outcome unknown, and repeating it could duplicate a side effect. |
| 47 | + """ |
| 48 | + request = coerce_request(params, WebMCPInvokeParams) |
| 49 | + response = await self._client.transport.post( |
| 50 | + self._url(session_id, "/invoke"), |
| 51 | + data=dump_request(request, WebMCPInvokeParams), |
| 52 | + timeout=max(self._client.timeout, request.timeout_seconds + 35), |
| 53 | + ) |
| 54 | + return WebMCPInvokeResult(**response.data) |
| 55 | + |
| 56 | + async def start( |
| 57 | + self, |
| 58 | + session_id: str, |
| 59 | + params: Union[WebMCPStartParamsDict, WebMCPStartParams], |
| 60 | + ) -> WebMCPInvocation: |
| 61 | + """Start once and return a handle; use get_result to observe its outcome.""" |
| 62 | + response = await self._client.transport.post( |
| 63 | + self._url(session_id, "/invocations"), |
| 64 | + data=dump_request(params, WebMCPStartParams), |
| 65 | + timeout=max(self._client.timeout, 40), |
| 66 | + ) |
| 67 | + return WebMCPInvocation(**response.data) |
| 68 | + |
| 69 | + async def get_result( |
| 70 | + self, |
| 71 | + session_id: str, |
| 72 | + invocation_id: str, |
| 73 | + params: Optional[Union[WebMCPResultParamsDict, WebMCPResultParams]] = None, |
| 74 | + ) -> WebMCPInvocation: |
| 75 | + """Read or long-poll once. Running/awaiting_submission can remain pending. |
| 76 | +
|
| 77 | + Handles are in memory. Terminal results expire after 10 minutes and may |
| 78 | + be evicted sooner; browser shutdown or receiver restart also loses them. |
| 79 | + """ |
| 80 | + request = coerce_request( |
| 81 | + params if params is not None else {}, WebMCPResultParams |
| 82 | + ) |
| 83 | + response = await self._client.transport.get( |
| 84 | + self._url(session_id, f"/invocations/{quote(invocation_id, safe='')}"), |
| 85 | + params=dump_request(request, WebMCPResultParams), |
| 86 | + timeout=max(self._client.timeout, request.wait_seconds + 20), |
| 87 | + ) |
| 88 | + return WebMCPInvocation(**response.data) |
| 89 | + |
| 90 | + async def cancel(self, session_id: str, invocation_id: str) -> WebMCPInvocation: |
| 91 | + """Request best-effort cancellation. Completion can win; side effects persist.""" |
| 92 | + response = await self._client.transport.post( |
| 93 | + self._url( |
| 94 | + session_id, f"/invocations/{quote(invocation_id, safe='')}/cancel" |
| 95 | + ), |
| 96 | + timeout=max(self._client.timeout, 20), |
| 97 | + ) |
| 98 | + return WebMCPInvocation(**response.data) |
0 commit comments