diff --git a/proofline/_mcp.py b/proofline/_mcp.py index f80c5e00..10bb81cc 100644 --- a/proofline/_mcp.py +++ b/proofline/_mcp.py @@ -4,6 +4,7 @@ import json from typing import Any, NotRequired, TypedDict, cast +from uuid import UUID import httpx2 @@ -27,13 +28,19 @@ def _refused() -> ProoflineError: return ProoflineError(0, "urn:proofline:sdk:mcp", "Invalid or refused MCP response.") +def _json_default(obj: Any) -> Any: + if isinstance(obj, UUID): + return str(obj) + raise TypeError(f"Object of type {type(obj).__name__} is not JSON serializable") + + def _body(method: str, params: dict[str, Any] | None, identity: int | None) -> bytes: value: dict[str, Any] = {"jsonrpc": "2.0", "method": method} if identity is not None: value["id"] = identity if params is not None: value["params"] = params - data = json.dumps(value).encode("utf-8") + data = json.dumps(value, default=_json_default).encode("utf-8") if len(data) > 8192: raise ValueError("MCP request exceeds 8192 bytes.") return data @@ -96,10 +103,10 @@ def __init__(self, http: httpx2.Client) -> None: self._http = http def _call( - self, name: str, args: dict[str, Any], *, organization_id: str | None = None + self, name: str, args: dict[str, Any], *, organization_id: UUID | str | None = None ) -> KnowledgeResult: if organization_id is not None: - args = {**args, "organization_id": organization_id} + args = {**args, "organization_id": str(organization_id)} call = _body("tools/call", {"name": name, "arguments": args}, 2) frames = [ ( @@ -145,10 +152,10 @@ def __init__(self, http: httpx2.AsyncClient) -> None: self._http = http async def _call( - self, name: str, args: dict[str, Any], *, organization_id: str | None = None + self, name: str, args: dict[str, Any], *, organization_id: UUID | str | None = None ) -> KnowledgeResult: if organization_id is not None: - args = {**args, "organization_id": organization_id} + args = {**args, "organization_id": str(organization_id)} call = _body("tools/call", {"name": name, "arguments": args}, 2) frames = [ ( diff --git a/proofline/fixes.py b/proofline/fixes.py index 85e437b5..37786001 100644 --- a/proofline/fixes.py +++ b/proofline/fixes.py @@ -3,13 +3,18 @@ from __future__ import annotations from typing import Any +from uuid import UUID from ._mcp import AsyncMcpTools, KnowledgeResult, McpTools class FixesClient(McpTools): def list_open_findings( - self, repository: str, pull_request_number: int, *, organization_id: str | None = None + self, + repository: str, + pull_request_number: int, + *, + organization_id: UUID | str | None = None, ) -> KnowledgeResult: return self._call( "list_open_findings", @@ -18,24 +23,24 @@ def list_open_findings( ) def get_fix_context( - self, finding_id: str, *, organization_id: str | None = None + self, finding_id: UUID | str, *, organization_id: UUID | str | None = None ) -> KnowledgeResult: return self._call( - "get_fix_context", {"finding_id": finding_id}, organization_id=organization_id + "get_fix_context", {"finding_id": str(finding_id)}, organization_id=organization_id ) def request_reverification( self, - review_id: str, - finding_ids: list[str], + review_id: UUID | str, + finding_ids: list[UUID | str], commit_sha: str, *, note: str | None = None, - organization_id: str | None = None, + organization_id: UUID | str | None = None, ) -> KnowledgeResult: args: dict[str, Any] = { - "review_id": review_id, - "finding_ids": finding_ids, + "review_id": str(review_id), + "finding_ids": [str(item) for item in finding_ids], "commit_sha": commit_sha, } if note is not None: @@ -43,16 +48,20 @@ def request_reverification( return self._call("request_reverification", args, organization_id=organization_id) def get_reverification( - self, request_id: str, *, organization_id: str | None = None + self, request_id: UUID | str, *, organization_id: UUID | str | None = None ) -> KnowledgeResult: return self._call( - "get_reverification", {"request_id": request_id}, organization_id=organization_id + "get_reverification", {"request_id": str(request_id)}, organization_id=organization_id ) class AsyncFixesClient(AsyncMcpTools): async def list_open_findings( - self, repository: str, pull_request_number: int, *, organization_id: str | None = None + self, + repository: str, + pull_request_number: int, + *, + organization_id: UUID | str | None = None, ) -> KnowledgeResult: return await self._call( "list_open_findings", @@ -61,24 +70,24 @@ async def list_open_findings( ) async def get_fix_context( - self, finding_id: str, *, organization_id: str | None = None + self, finding_id: UUID | str, *, organization_id: UUID | str | None = None ) -> KnowledgeResult: return await self._call( - "get_fix_context", {"finding_id": finding_id}, organization_id=organization_id + "get_fix_context", {"finding_id": str(finding_id)}, organization_id=organization_id ) async def request_reverification( self, - review_id: str, - finding_ids: list[str], + review_id: UUID | str, + finding_ids: list[UUID | str], commit_sha: str, *, note: str | None = None, - organization_id: str | None = None, + organization_id: UUID | str | None = None, ) -> KnowledgeResult: args: dict[str, Any] = { - "review_id": review_id, - "finding_ids": finding_ids, + "review_id": str(review_id), + "finding_ids": [str(item) for item in finding_ids], "commit_sha": commit_sha, } if note is not None: @@ -86,8 +95,8 @@ async def request_reverification( return await self._call("request_reverification", args, organization_id=organization_id) async def get_reverification( - self, request_id: str, *, organization_id: str | None = None + self, request_id: UUID | str, *, organization_id: UUID | str | None = None ) -> KnowledgeResult: return await self._call( - "get_reverification", {"request_id": request_id}, organization_id=organization_id + "get_reverification", {"request_id": str(request_id)}, organization_id=organization_id ) diff --git a/proofline/knowledge.py b/proofline/knowledge.py index 2a301cf5..0f747217 100644 --- a/proofline/knowledge.py +++ b/proofline/knowledge.py @@ -1,11 +1,19 @@ """Published knowledge operations through the scoped MCP server.""" +from __future__ import annotations + +from uuid import UUID + from ._mcp import AsyncMcpTools, KnowledgeResult, McpTools, _repository class KnowledgeClient(McpTools): def list_wiki_pages( - self, repository: str, *, commit: str | None = None, organization_id: str | None = None + self, + repository: str, + *, + commit: str | None = None, + organization_id: UUID | str | None = None, ) -> KnowledgeResult: return self._call( "list_wiki_pages", _repository(repository, commit), organization_id=organization_id @@ -17,7 +25,7 @@ def get_wiki_page( page_key: str, *, commit: str | None = None, - organization_id: str | None = None, + organization_id: UUID | str | None = None, ) -> KnowledgeResult: return self._call( "get_wiki_page", @@ -31,7 +39,7 @@ def ask_codebase( question: str, *, commit: str | None = None, - organization_id: str | None = None, + organization_id: UUID | str | None = None, ) -> KnowledgeResult: return self._call( "ask_codebase", @@ -40,37 +48,45 @@ def ask_codebase( ) def get_architecture_document( - self, environment_id: str, *, organization_id: str | None = None + self, environment_id: UUID | str, *, organization_id: UUID | str | None = None ) -> KnowledgeResult: return self._call( "get_architecture_document", - {"environment_id": environment_id}, + {"environment_id": str(environment_id)}, organization_id=organization_id, ) - def get_plan(self, plan_id: str, *, organization_id: str | None = None) -> KnowledgeResult: - return self._call("get_plan", {"plan_id": plan_id}, organization_id=organization_id) + def get_plan( + self, plan_id: UUID | str, *, organization_id: UUID | str | None = None + ) -> KnowledgeResult: + return self._call("get_plan", {"plan_id": str(plan_id)}, organization_id=organization_id) def start_plan_implementation( - self, plan_id: str, *, organization_id: str | None = None + self, plan_id: UUID | str, *, organization_id: UUID | str | None = None ) -> KnowledgeResult: return self._call( - "start_plan_implementation", {"plan_id": plan_id}, organization_id=organization_id + "start_plan_implementation", + {"plan_id": str(plan_id)}, + organization_id=organization_id, ) def get_plan_implementation( - self, implementation_id: str, *, organization_id: str | None = None + self, implementation_id: UUID | str, *, organization_id: UUID | str | None = None ) -> KnowledgeResult: return self._call( "get_plan_implementation", - {"implementation_id": implementation_id}, + {"implementation_id": str(implementation_id)}, organization_id=organization_id, ) class AsyncKnowledgeClient(AsyncMcpTools): async def list_wiki_pages( - self, repository: str, *, commit: str | None = None, organization_id: str | None = None + self, + repository: str, + *, + commit: str | None = None, + organization_id: UUID | str | None = None, ) -> KnowledgeResult: return await self._call( "list_wiki_pages", _repository(repository, commit), organization_id=organization_id @@ -82,7 +98,7 @@ async def get_wiki_page( page_key: str, *, commit: str | None = None, - organization_id: str | None = None, + organization_id: UUID | str | None = None, ) -> KnowledgeResult: return await self._call( "get_wiki_page", @@ -96,7 +112,7 @@ async def ask_codebase( question: str, *, commit: str | None = None, - organization_id: str | None = None, + organization_id: UUID | str | None = None, ) -> KnowledgeResult: return await self._call( "ask_codebase", @@ -105,31 +121,35 @@ async def ask_codebase( ) async def get_architecture_document( - self, environment_id: str, *, organization_id: str | None = None + self, environment_id: UUID | str, *, organization_id: UUID | str | None = None ) -> KnowledgeResult: return await self._call( "get_architecture_document", - {"environment_id": environment_id}, + {"environment_id": str(environment_id)}, organization_id=organization_id, ) async def get_plan( - self, plan_id: str, *, organization_id: str | None = None + self, plan_id: UUID | str, *, organization_id: UUID | str | None = None ) -> KnowledgeResult: - return await self._call("get_plan", {"plan_id": plan_id}, organization_id=organization_id) + return await self._call( + "get_plan", {"plan_id": str(plan_id)}, organization_id=organization_id + ) async def start_plan_implementation( - self, plan_id: str, *, organization_id: str | None = None + self, plan_id: UUID | str, *, organization_id: UUID | str | None = None ) -> KnowledgeResult: return await self._call( - "start_plan_implementation", {"plan_id": plan_id}, organization_id=organization_id + "start_plan_implementation", + {"plan_id": str(plan_id)}, + organization_id=organization_id, ) async def get_plan_implementation( - self, implementation_id: str, *, organization_id: str | None = None + self, implementation_id: UUID | str, *, organization_id: UUID | str | None = None ) -> KnowledgeResult: return await self._call( "get_plan_implementation", - {"implementation_id": implementation_id}, + {"implementation_id": str(implementation_id)}, organization_id=organization_id, ) diff --git a/tests/test_http.py b/tests/test_http.py index 0af9d60f..1e771d5a 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -2463,14 +2463,60 @@ async def invoke(group: str, name: str, *args: Any, **kwargs: Any) -> Any: ("fixes", "get_reverification", (str(REVIEW),), {}, {"request_id": str(REVIEW)}), ] for group, name, args, kwargs, expected in cases: - for target in (first, second): + for target in (first, second, UUID(first), UUID(second)): answer = await invoke(group, name, *args, **kwargs, organization_id=target) - assert answer["structuredContent"]["data"]["organization_id"] == target + assert answer["structuredContent"]["data"]["organization_id"] == str(target) assert observed[-1] == { "name": name, - "arguments": {**expected, "organization_id": target}, + "arguments": {**expected, "organization_id": str(target)}, } + uuid_cases = [ + ( + "knowledge", + "get_architecture_document", + (REVIEW,), + {}, + {"environment_id": str(REVIEW)}, + ), + ("knowledge", "get_plan", (REVIEW,), {}, {"plan_id": str(REVIEW)}), + ( + "knowledge", + "start_plan_implementation", + (REVIEW,), + {}, + {"plan_id": str(REVIEW)}, + ), + ( + "knowledge", + "get_plan_implementation", + (REVIEW,), + {}, + {"implementation_id": str(REVIEW)}, + ), + ("fixes", "get_fix_context", (REVIEW,), {}, {"finding_id": str(REVIEW)}), + ( + "fixes", + "request_reverification", + (REVIEW, [REVIEW], "a" * 40), + {"note": "fixed"}, + { + "review_id": str(REVIEW), + "finding_ids": [str(REVIEW)], + "commit_sha": "a" * 40, + "note": "fixed", + }, + ), + ("fixes", "get_reverification", (REVIEW,), {}, {"request_id": str(REVIEW)}), + ] + for group, name, args, kwargs, expected in uuid_cases: + answer = await invoke(group, name, *args, **kwargs, organization_id=UUID(first)) + assert answer["structuredContent"]["data"]["organization_id"] == first + assert observed[-1] == { + "name": name, + "arguments": {**expected, "organization_id": first}, + } + if mode == "async": concurrent = True answers = await asyncio.gather( @@ -2517,19 +2563,34 @@ async def refused(target: str | None, code: str) -> None: def test_mcp_public_types_accept_legacy_and_targeted_calls(tmp_path: Path) -> None: # Compile consumer code through the public entry point; dynamic HTTP tests # cannot catch a missing keyword in just one sync/async method signature. - lines = ["from proofline import ProoflineClient, AsyncProoflineClient, KnowledgeResult"] + lines = [ + "from uuid import UUID", + "from proofline import ProoflineClient, AsyncProoflineClient, KnowledgeResult", + ] calls = [ 'knowledge.list_wiki_pages("owner/name", commit="a" * 40)', 'knowledge.get_wiki_page("owner/name", "index")', 'knowledge.ask_codebase("owner/name", "How?")', 'knowledge.get_architecture_document("environment-id")', + 'knowledge.get_architecture_document(UUID("00000000-0000-7000-8000-000000000001"))', 'knowledge.get_plan("plan-id")', + 'knowledge.get_plan(UUID("00000000-0000-7000-8000-000000000001"))', 'knowledge.start_plan_implementation("plan-id")', + 'knowledge.start_plan_implementation(UUID("00000000-0000-7000-8000-000000000001"))', 'knowledge.get_plan_implementation("implementation-id")', + 'knowledge.get_plan_implementation(UUID("00000000-0000-7000-8000-000000000001"))', 'fixes.list_open_findings("owner/name", 7)', 'fixes.get_fix_context("finding-id")', + 'fixes.get_fix_context(UUID("00000000-0000-7000-8000-000000000001"))', 'fixes.request_reverification("review-id", ["finding-id"], "a" * 40, note="fixed")', + ( + "fixes.request_reverification(" + 'UUID("00000000-0000-7000-8000-000000000001"), ' + '[UUID("00000000-0000-7000-8000-000000000002")], ' + '"a" * 40, note="fixed")' + ), 'fixes.get_reverification("request-id")', + 'fixes.get_reverification(UUID("00000000-0000-7000-8000-000000000001"))', ] for prefix, kind, wait in [ ("", "ProoflineClient", ""), @@ -2539,14 +2600,36 @@ def test_mcp_public_types_accept_legacy_and_targeted_calls(tmp_path: Path) -> No lines.append(f" result: KnowledgeResult = {wait}client.mcp.list_organizations()") for expression in calls: lines.append(f" result = {wait}client.{expression}") - targeted = expression[:-1] + ', organization_id="00000000-0000-7000-8000-000000000001")' - lines.append(f" result = {wait}client.{targeted}") + targeted_str = ( + expression[:-1] + ', organization_id="00000000-0000-7000-8000-000000000001")' + ) + lines.append(f" result = {wait}client.{targeted_str}") + targeted_uuid = ( + expression[:-1] + ', organization_id=UUID("00000000-0000-7000-8000-000000000001"))' + ) + lines.append(f" result = {wait}client.{targeted_uuid}") fixture = tmp_path / "mcp_types.py" fixture.write_text("\n".join(lines)) # Use the same mypy configuration as the SDK qualification gate. subprocess.run([sys.executable, "-m", "mypy", str(fixture)], check=True) +def test_mcp_body_serializes_uuid_and_rejects_unserializable_types() -> None: + from proofline._mcp import _body + + uid = UUID("00000000-0000-7000-8000-000000000001") + raw = _body("tools/call", {"organization_id": uid, "ids": [uid]}, 1) + decoded = json.loads(raw.decode("utf-8")) + assert decoded["params"]["organization_id"] == str(uid) + assert decoded["params"]["ids"] == [str(uid)] + + class Unserializable: + pass + + with pytest.raises(TypeError): + _body("tools/call", {"custom": Unserializable()}, 1) + + @MODES async def test_export_ndjson_preserves_unicode_line_separators(mode: str) -> None: kwargs = {