Repository navigation
feat: add caller-controlled MCP methods #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HareeshBahuleyan
wants to merge
3
commits into
main
Choose a base branch
from
feat/65-public-mcp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| """Caller-controlled MCP, using generated models but a single-attempt transport. | ||
|
|
||
| The generated urllib3 client inherits connection retries unless overridden. | ||
| Use the client's HTTPX transport instead: zero connection retries, no status | ||
| retries, no redirects, and no inference error mapping (a 409 here is not a | ||
| batch error). The owning Otari client closes this shared transport. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
| from uuid import UUID | ||
|
|
||
| import httpx | ||
|
|
||
| from otari._base import build_request | ||
| from otari.errors import MCPError, MCPOutcomeUnknownError | ||
| from otari.types import CallToolResult, McpErrorBody, McpExecuteRequest, McpToolsResponse | ||
|
|
||
| if TYPE_CHECKING: | ||
| from otari.async_client import AsyncOtariClient | ||
| from otari.client import OtariClient | ||
|
|
||
|
|
||
| def _unknown_response(response: httpx.Response | None = None) -> MCPOutcomeUnknownError: | ||
| # Never include raw response/exception text, which may contain call data. | ||
| return MCPOutcomeUnknownError( | ||
| "MCP execution outcome is unknown; the remote tool may already have run", | ||
| execution_state="outcome_unknown", | ||
| status_code=response.status_code if response is not None else None, | ||
| request_id=response.headers.get("X-Otari-Request-ID") if response is not None else None, | ||
| ) | ||
|
|
||
|
|
||
| def _check_response(response: httpx.Response, *, executing: bool) -> None: | ||
| if response.status_code == 200: | ||
| return | ||
| try: | ||
| body = McpErrorBody.model_validate(response.json()) | ||
| except ValueError: | ||
| if executing: | ||
| raise _unknown_response(response) from None | ||
| raise MCPError( | ||
| "MCP discovery failed without a typed gateway response", | ||
| execution_state="not_started", | ||
| status_code=response.status_code, | ||
| request_id=response.headers.get("X-Otari-Request-ID"), | ||
| ) from None | ||
| error_type = MCPOutcomeUnknownError if body.execution_state == "outcome_unknown" else MCPError | ||
| raise error_type( | ||
| body.detail, | ||
| code=body.code, | ||
| execution_state="outcome_unknown" if body.execution_state == "outcome_unknown" else "not_started", | ||
| request_id=body.request_id, | ||
| status_code=response.status_code, | ||
| retry_after=response.headers.get("Retry-After"), | ||
| ) | ||
|
|
||
|
|
||
| def _execution_body( | ||
| mcp_server_id: str | UUID, | ||
| tool_name: str, | ||
| arguments: dict[str, Any], | ||
| server_revision: str, | ||
| client_execution_id: str | UUID, | ||
| ) -> dict[str, Any]: | ||
| request = build_request(McpExecuteRequest, { | ||
| "mcp_server_id": mcp_server_id, | ||
| "tool_name": tool_name, | ||
| "arguments": arguments, | ||
| "server_revision": server_revision, | ||
| "client_execution_id": client_execution_id, | ||
| }) | ||
| # JSON mode serializes UUIDs; do not exclude None recursively, since null | ||
| # values inside authorized arguments must survive unchanged. | ||
| return request.model_dump(mode="json", by_alias=True, exclude={"additional_properties"}) | ||
|
|
||
|
|
||
| def _execution_result(response: httpx.Response) -> CallToolResult: | ||
| _check_response(response, executing=True) | ||
| try: | ||
| payload = response.json() | ||
| if not isinstance(payload, dict): | ||
| raise TypeError("Expected an MCP result object") # noqa: TRY301 | ||
| return build_request(CallToolResult, payload) | ||
| except (ValueError, TypeError, KeyError): | ||
| # A malformed success cannot establish that execution did not happen. | ||
| raise _unknown_response(response) from None | ||
|
|
||
|
|
||
| class MCP: | ||
| """Stored-server MCP discovery and caller-authorized execution.""" | ||
|
|
||
| def __init__(self, client: OtariClient) -> None: | ||
| self._client = client | ||
|
|
||
| def list_tools(self, mcp_server_id: str | UUID) -> McpToolsResponse: | ||
| """Discover the authorized catalog and the stored-server revision. | ||
|
|
||
| Tool annotations are untrusted metadata, not authorization decisions. | ||
| """ | ||
| server_id = UUID(str(mcp_server_id)) | ||
| try: | ||
| response = self._client._http.get( | ||
| f"{self._client._base_url}/mcp/servers/{server_id}/tools", | ||
| headers=self._client._default_headers, | ||
| follow_redirects=False, | ||
| ) | ||
| except httpx.RequestError: | ||
| raise MCPError("MCP discovery transport failed", execution_state="not_started") from None | ||
| _check_response(response, executing=False) | ||
| return McpToolsResponse.model_validate(response.json()) | ||
|
|
||
| def execute( | ||
| self, | ||
| *, | ||
| mcp_server_id: str | UUID, | ||
| tool_name: str, | ||
| arguments: dict[str, Any], | ||
| server_revision: str, | ||
| client_execution_id: str | UUID, | ||
| ) -> CallToolResult: | ||
| """Execute exactly the caller-authorized call with one HTTP attempt. | ||
|
|
||
| The application owns approval, edited arguments, rejection and | ||
| cancellation. ``client_execution_id`` is correlation, NOT idempotency. | ||
| Persist discovery's ``server_revision`` with the authorized call. | ||
| Network failures and untyped responses are outcome-unknown; never | ||
| retry automatically or fall back to direct MCP execution. | ||
| A native result with ``is_error=True`` is a definitive result. | ||
| """ | ||
| body = _execution_body(mcp_server_id, tool_name, arguments, server_revision, client_execution_id) | ||
| try: | ||
| response = self._client._http.post( | ||
| f"{self._client._base_url}/mcp/execute", | ||
| json=body, | ||
| headers=self._client._default_headers, | ||
| follow_redirects=False, | ||
| ) | ||
| except httpx.RequestError: | ||
| raise _unknown_response() from None | ||
| return _execution_result(response) | ||
|
|
||
|
|
||
| class AsyncMCP: | ||
| """Async counterpart of :class:`MCP`, with the same execution safety contract.""" | ||
|
|
||
| def __init__(self, client: AsyncOtariClient) -> None: | ||
| self._client = client | ||
|
|
||
| async def list_tools(self, mcp_server_id: str | UUID) -> McpToolsResponse: | ||
| """Discover the authorized catalog and stored-server revision.""" | ||
| server_id = UUID(str(mcp_server_id)) | ||
| try: | ||
| response = await self._client._http.get( | ||
| f"{self._client._base_url}/mcp/servers/{server_id}/tools", | ||
| headers=self._client._default_headers, | ||
| follow_redirects=False, | ||
| ) | ||
| except httpx.RequestError: | ||
| raise MCPError("MCP discovery transport failed", execution_state="not_started") from None | ||
| _check_response(response, executing=False) | ||
| return McpToolsResponse.model_validate(response.json()) | ||
|
|
||
| async def execute( | ||
| self, | ||
| *, | ||
| mcp_server_id: str | UUID, | ||
| tool_name: str, | ||
| arguments: dict[str, Any], | ||
| server_revision: str, | ||
| client_execution_id: str | UUID, | ||
| ) -> CallToolResult: | ||
| """Execute one authorized call, without retries; see :meth:`MCP.execute`. | ||
|
|
||
| Task cancellation propagates normally. Cancellation after starting the | ||
| request does not establish whether the remote tool ran. | ||
| """ | ||
| body = _execution_body(mcp_server_id, tool_name, arguments, server_revision, client_execution_id) | ||
| try: | ||
| response = await self._client._http.post( | ||
| f"{self._client._base_url}/mcp/execute", | ||
| json=body, | ||
| headers=self._client._default_headers, | ||
| follow_redirects=False, | ||
| ) | ||
| except httpx.RequestError: | ||
| raise _unknown_response() from None | ||
| return _execution_result(response) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.