diff --git a/proofline/_mcp.py b/proofline/_mcp.py index f80c5e00..8f3f59b3 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 {obj.__class__.__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,7 +103,7 @@ 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} @@ -145,7 +152,7 @@ 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} diff --git a/proofline/fixes.py b/proofline/fixes.py index 85e437b5..9b4c00ad 100644 --- a/proofline/fixes.py +++ b/proofline/fixes.py @@ -2,14 +2,20 @@ from __future__ import annotations +from collections.abc import Sequence 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,7 +24,7 @@ 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 @@ -26,12 +32,12 @@ def get_fix_context( def request_reverification( self, - review_id: str, - finding_ids: list[str], + review_id: UUID | str, + finding_ids: Sequence[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, @@ -43,7 +49,7 @@ 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 @@ -52,7 +58,11 @@ def get_reverification( 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,7 +71,7 @@ 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 @@ -69,12 +79,12 @@ async def get_fix_context( async def request_reverification( self, - review_id: str, - finding_ids: list[str], + review_id: UUID | str, + finding_ids: Sequence[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, @@ -86,7 +96,7 @@ 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 diff --git a/proofline/knowledge.py b/proofline/knowledge.py index 2a301cf5..7d071fa0 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,7 +48,7 @@ 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", @@ -48,18 +56,20 @@ def get_architecture_document( organization_id=organization_id, ) - def get_plan(self, plan_id: str, *, organization_id: str | None = None) -> KnowledgeResult: + def get_plan( + self, plan_id: UUID | str, *, organization_id: UUID | str | None = None + ) -> KnowledgeResult: return self._call("get_plan", {"plan_id": 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 ) 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", @@ -70,7 +80,11 @@ def get_plan_implementation( 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 +96,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 +110,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,7 +119,7 @@ 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", @@ -114,19 +128,19 @@ async def get_architecture_document( ) 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) 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 ) 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", diff --git a/tests/test_http.py b/tests/test_http.py index 0af9d60f..a2f2dfb8 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -2586,3 +2586,198 @@ def respond(request: Request, index: int) -> Reply: assert len(exported.rows) == 1 assert exported.rows[0]["claim"] == row["claim"] assert exported.next_cursor == "next-page" + + +@MODES +async def test_mcp_uuid_parameters_serialize(mode: str) -> None: + observed: list[dict[str, Any]] = [] + + def respond(request: Request, index: int) -> Reply: + body = json.loads(request.body) + if body["method"] == "initialize": + return Reply( + body={"jsonrpc": "2.0", "id": 1, "result": {"protocolVersion": "2025-11-25"}} + ) + if body["method"] == "notifications/initialized": + return Reply(status=202, body=b"") + params = body["params"] + observed.append(params) + return Reply( + body={ + "jsonrpc": "2.0", + "id": 2, + "result": { + "content": [{"type": "text", "text": "{}"}], + "structuredContent": {"arguments": params["arguments"]}, + }, + } + ) + + cases = [ + # Knowledge methods: + ( + "knowledge", + "list_wiki_pages", + ("owner/name",), + {"commit": "a" * 40, "organization_id": ORG}, + {"repository": "owner/name", "commit": "a" * 40, "organization_id": str(ORG)}, + ), + ( + "knowledge", + "get_wiki_page", + ("owner/name", "index"), + {"commit": "a" * 40, "organization_id": ORG}, + { + "repository": "owner/name", + "commit": "a" * 40, + "page_key": "index", + "organization_id": str(ORG), + }, + ), + ( + "knowledge", + "ask_codebase", + ("owner/name", "How?"), + {"commit": "a" * 40, "organization_id": ORG}, + { + "repository": "owner/name", + "commit": "a" * 40, + "question": "How?", + "organization_id": str(ORG), + }, + ), + ( + "knowledge", + "get_architecture_document", + (ASSESSMENT,), + {"organization_id": ORG}, + {"environment_id": str(ASSESSMENT), "organization_id": str(ORG)}, + ), + ( + "knowledge", + "get_plan", + (REVIEW,), + {"organization_id": ORG}, + {"plan_id": str(REVIEW), "organization_id": str(ORG)}, + ), + ( + "knowledge", + "start_plan_implementation", + (REVIEW,), + {"organization_id": ORG}, + {"plan_id": str(REVIEW), "organization_id": str(ORG)}, + ), + ( + "knowledge", + "get_plan_implementation", + (REVIEW,), + {"organization_id": ORG}, + {"implementation_id": str(REVIEW), "organization_id": str(ORG)}, + ), + # Fixes methods: + ( + "fixes", + "list_open_findings", + ("owner/name", 7), + {"organization_id": ORG}, + {"repository": "owner/name", "pull_request_number": 7, "organization_id": str(ORG)}, + ), + ( + "fixes", + "get_fix_context", + (FINDING,), + {"organization_id": ORG}, + {"finding_id": str(FINDING), "organization_id": str(ORG)}, + ), + ( + "fixes", + "request_reverification", + (REVIEW, [FINDING], "a" * 40), + {"note": "fixed", "organization_id": ORG}, + { + "review_id": str(REVIEW), + "finding_ids": [str(FINDING)], + "commit_sha": "a" * 40, + "note": "fixed", + "organization_id": str(ORG), + }, + ), + ( + "fixes", + "request_reverification", + (REVIEW, (FINDING,), "a" * 40), + {"note": "fixed", "organization_id": ORG}, + { + "review_id": str(REVIEW), + "finding_ids": [str(FINDING)], + "commit_sha": "a" * 40, + "note": "fixed", + "organization_id": str(ORG), + }, + ), + ( + "fixes", + "get_reverification", + (REVIEW,), + {"organization_id": ORG}, + {"request_id": str(REVIEW), "organization_id": str(ORG)}, + ), + ] + + with Server(respond) as server: + async with session(mode, server.url) as client: + + async def invoke(group: str, name: str, *args: Any, **kwargs: Any) -> Any: + result = getattr(getattr(client, group), name)(*args, **kwargs) + return await result if inspect.isawaitable(result) else result + + for group, name, args, kwargs, expected in cases: + observed.clear() + answer = await invoke(group, name, *args, **kwargs) + assert answer["structuredContent"]["arguments"] == expected + assert len(observed) == 1 + assert observed[0]["name"] == name + assert observed[0]["arguments"] == expected + + +@MODES +async def test_mcp_unserializable_object_raises_type_error(mode: str) -> None: + class CustomObject: + pass + + with Server(lambda request, index: Reply(status=200, body=b"{}")) as server: + async with session(mode, server.url) as client: + + async def invoke(group: str, name: str, *args: Any, **kwargs: Any) -> Any: + result = getattr(getattr(client, group), name)(*args, **kwargs) + return await result if inspect.isawaitable(result) else result + + with pytest.raises(TypeError, match="is not JSON serializable"): + await invoke("fixes", "get_fix_context", CustomObject(), organization_id=ORG) # type: ignore[arg-type] + + with pytest.raises(TypeError, match="is not JSON serializable"): + await invoke("knowledge", "get_plan", CustomObject(), organization_id=ORG) # type: ignore[arg-type] + + # Passing a set for finding_ids still raises TypeError + with pytest.raises(TypeError, match="is not JSON serializable"): + await invoke( + "fixes", + "request_reverification", + REVIEW, + {FINDING}, # type: ignore[arg-type] + "a" * 40, + organization_id=ORG, + ) + + # Cyclic containers still raise ValueError + cyclic: list[Any] = [] + cyclic.append(cyclic) + with pytest.raises(ValueError, match="Circular reference detected"): + await invoke( + "fixes", + "request_reverification", + REVIEW, + cyclic, + "a" * 40, + organization_id=ORG, + )