mchades commented on code in PR #14893: URL: https://github.com/apache/gravitino/pull/14893#discussion_r4545400778
########## mcp-server/mcp_server/client/plain/plain_rest_client_domain_operation.py: ########## @@ -0,0 +1,107 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from mcp_server.client.domain_operation import DomainOperation +from mcp_server.client.plain.utils import ( + encode_path_segment, + extract_content_from_response, +) + + +class PlainRESTClientDomainOperation(DomainOperation): + """Read domain snapshots without projecting away governance information.""" + + def __init__(self, metalake_name: str, rest_client): + self._metalake_name = metalake_name + self._rest_client = rest_client + + @property + def _path(self) -> str: + # Metalake discovery can construct a client without a selected metalake. + # Validate the scope only when a domain request actually needs it. + return ( + f"/api/metalakes/{encode_path_segment(self._metalake_name)}/domains" + ) + + @property + def _object_path(self) -> str: + return ( + f"/api/metalakes/{encode_path_segment(self._metalake_name)}/objects" + ) + + async def _list(self, path: str, details: bool) -> str: + response = await self._rest_client.get( + path, params={"details": str(details).lower()} + ) + return self._extract(response, "domains" if details else "names", []) + + async def list_of_domains(self, details: bool = True) -> str: + return await self._list(self._path, details) + + async def get_domain_by_name(self, domain_name: str) -> str: + response = await self._rest_client.get( + f"{self._path}/{encode_path_segment(domain_name)}" + ) + return self._extract(response, "domain", {}) + + async def list_subdomains( + self, domain_name: str, details: bool = True + ) -> str: + return await self._list( + f"{self._path}/{encode_path_segment(domain_name)}/subdomains", + details, + ) + + async def list_domain_members( + self, + domain_name: str, + types: list[str] | None = None, + domains: list[str] | None = None, + name_contains: str | None = None, + ) -> str: + # Repeated query parameters preserve commas and other characters in domain names. + params = [] + if types: + params.append(("types", ",".join(types))) + if domains: + params.extend(("domains", domain) for domain in domains) + if name_contains is not None: + params.append(("nameContains", name_contains)) Review Comment: `name_contains=""` is forwarded as `nameContains=`, but the server rejects it in `DomainMemberFilter.Builder#withNameContains` ("nameContains must not be blank"). Agents commonly fill optional string args with `""`, so this turns a harmless call into an error. How about treating empty/blank as unset (e.g. `if name_contains and name_contains.strip():`), consistent with "Omitted or empty type/domain lists leave those filters unset"? The test at `test_domain.py:250-253` would need updating accordingly. ########## mcp-server/mcp_server/client/plain/plain_rest_client_domain_operation.py: ########## @@ -0,0 +1,107 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from mcp_server.client.domain_operation import DomainOperation +from mcp_server.client.plain.utils import ( + encode_path_segment, + extract_content_from_response, +) + + +class PlainRESTClientDomainOperation(DomainOperation): + """Read domain snapshots without projecting away governance information.""" + + def __init__(self, metalake_name: str, rest_client): + self._metalake_name = metalake_name + self._rest_client = rest_client + + @property + def _path(self) -> str: + # Metalake discovery can construct a client without a selected metalake. + # Validate the scope only when a domain request actually needs it. + return ( + f"/api/metalakes/{encode_path_segment(self._metalake_name)}/domains" + ) + + @property + def _object_path(self) -> str: + return ( + f"/api/metalakes/{encode_path_segment(self._metalake_name)}/objects" + ) + + async def _list(self, path: str, details: bool) -> str: + response = await self._rest_client.get( + path, params={"details": str(details).lower()} + ) + return self._extract(response, "domains" if details else "names", []) + + async def list_of_domains(self, details: bool = True) -> str: + return await self._list(self._path, details) + + async def get_domain_by_name(self, domain_name: str) -> str: + response = await self._rest_client.get( + f"{self._path}/{encode_path_segment(domain_name)}" + ) + return self._extract(response, "domain", {}) + + async def list_subdomains( + self, domain_name: str, details: bool = True + ) -> str: + return await self._list( + f"{self._path}/{encode_path_segment(domain_name)}/subdomains", + details, + ) + + async def list_domain_members( + self, + domain_name: str, + types: list[str] | None = None, + domains: list[str] | None = None, + name_contains: str | None = None, + ) -> str: + # Repeated query parameters preserve commas and other characters in domain names. + params = [] + if types: + params.append(("types", ",".join(types))) + if domains: + params.extend(("domains", domain) for domain in domains) + if name_contains is not None: + params.append(("nameContains", name_contains)) + + response = await self._rest_client.get( + f"{self._path}/{encode_path_segment(domain_name)}/objects", + params=params, + ) + return self._extract(response, "members", []) + + async def list_domains_for_metadata( + self, metadata_full_name: str, metadata_type: str, details: bool = True + ) -> str: + return await self._list( + f"{self._object_path}/{encode_path_segment(metadata_type)}" + f"/{encode_path_segment(metadata_full_name)}/domains", + details, + ) + + @staticmethod + def _extract(response, field: str, default) -> str: + # Preserve structured Gravitino errors before checking HTTP status. + # Gateways may return JSON without a Gravitino error code; these must + # never turn authentication failures into successful empty results. + content = extract_content_from_response(response, field, default) + response.raise_for_status() Review Comment: `response.json()` runs before `raise_for_status()`, so a non-JSON error body (e.g. a gateway 502 HTML page) surfaces as `JSONDecodeError` rather than an HTTP status error. Consider checking `response.is_error` first and only parsing JSON when the body is JSON. Also, no other plain REST operation calls `raise_for_status()`; if this behavior is desired, it might be better placed in `utils.extract_content_from_response` so all tool groups behave consistently. ########## mcp-server/mcp_server/tools/domain.py: ########## @@ -0,0 +1,121 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from fastmcp import Context, FastMCP + +_READONLY_TOOL_ANNOTATIONS = { + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, +} + + +def load_domain_tools(mcp: FastMCP): + """Register only read-only domain tools.""" + + @mcp.tool( + tags={"domain"}, + annotations=_READONLY_TOOL_ANNOTATIONS, + ) + async def list_of_domains(ctx: Context, details: bool = True) -> str: + """List domains; details includes backingTag, status, parent and audit. + + Returns a JSON string; empty lists are returned as []. + Authorization and errors are enforced by the Gravitino server. + """ + client = ctx.request_context.lifespan_context.rest_client() + return await client.as_domain_operation().list_of_domains(details) + + @mcp.tool( + tags={"domain"}, + annotations=_READONLY_TOOL_ANNOTATIONS, + ) + async def get_domain_by_name(ctx: Context, domain_name: str) -> str: + """Load a domain snapshot by name. + + Returns a JSON string containing the complete domain snapshot. + Authorization and errors are enforced by the Gravitino server. + """ + client = ctx.request_context.lifespan_context.rest_client() + return await client.as_domain_operation().get_domain_by_name( + domain_name + ) + + @mcp.tool( + tags={"domain"}, + annotations=_READONLY_TOOL_ANNOTATIONS, + ) + async def list_subdomains( + ctx: Context, domain_name: str, details: bool = True + ) -> str: + """List immediate subdomains, as names or complete snapshots. + + Returns a JSON string; empty lists are returned as []. + Authorization and errors are enforced by the Gravitino server. + """ + client = ctx.request_context.lifespan_context.rest_client() + return await client.as_domain_operation().list_subdomains( + domain_name, details + ) + + @mcp.tool( + tags={"domain"}, + annotations=_READONLY_TOOL_ANNOTATIONS, + ) + async def list_domain_members( + ctx: Context, + domain_name: str, + types: list[str] | None = None, + domains: list[str] | None = None, + name_contains: str | None = None, + ) -> str: + """List recorded leaf members and all applicable relations. + + types filters leaf object types (for example TABLE and TOPIC). + domains filters source domain names; name_contains is a literal substring. Review Comment: nit: the server treats `nameContains` as a case-insensitive full-name substring (see `DomainMembershipOperations#listMembers` javadoc), so "literal substring" may mislead agents. Same wording in `domain_operation.py` and the docs. ########## mcp-server/mcp_server/client/plain/plain_rest_client_domain_operation.py: ########## @@ -0,0 +1,107 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from mcp_server.client.domain_operation import DomainOperation +from mcp_server.client.plain.utils import ( + encode_path_segment, + extract_content_from_response, +) + + +class PlainRESTClientDomainOperation(DomainOperation): + """Read domain snapshots without projecting away governance information.""" + + def __init__(self, metalake_name: str, rest_client): + self._metalake_name = metalake_name + self._rest_client = rest_client + + @property + def _path(self) -> str: + # Metalake discovery can construct a client without a selected metalake. + # Validate the scope only when a domain request actually needs it. + return ( + f"/api/metalakes/{encode_path_segment(self._metalake_name)}/domains" Review Comment: The comment says the scope is validated only when a request needs it, but nothing validates it here: if `metalake_name` is `None`, `encode_path_segment` turns it into the literal string `"None"` and the request goes to `/api/metalakes/None/domains`. Either add an explicit check (raise a clear error when no metalake is selected) or drop the comment. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
