Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#8137] feat(mcp): support policy read operations for Gravitino MCP server (#8151)

FANNG71ff0e7ff806
diff --git a/docs/gravitino-mcp-server.md b/docs/gravitino-mcp-server.md
index f7f1e60aa1..46d3b505cf 100644
--- a/docs/gravitino-mcp-server.md
+++ b/docs/gravitino-mcp-server.md
@@ -55,12 +55,17 @@ Or start a HTTP MCP server by `uv run mcp_server --metalake test --uri http://12
Gravitino MCP server supports the following tools, and you could export tool by tag.
-| Tool name | Description | Tag | Since version |
-|------------------------------|-----------------------------------------------------------------|-----------|---------------|
-| `get_list_of_catalogs` | Retrieve a list of all catalogs in the system. | `catalog` | 1.0.0 |
-| `get_list_of_schemas` | Retrieve a list of schemas belonging to a specific catalog. | `schema` | 1.0.0 |
-| `get_list_of_tables` | Retrieve a list of tables within a specific catalog and schema. | `table` | 1.0.0 |
-| `get_table_metadata_details` | Retrieve comprehensive metadata details for a specific table. | `table` | 1.0.0 |
+| Tool name | Description | Tag | Since version |
+|---------------------------------|---------------------------------------------------------------------|-----------|---------------|
+| `get_list_of_catalogs` | Retrieve a list of all catalogs in the system. | `catalog` | 1.0.0 |
+| `get_list_of_schemas` | Retrieve a list of schemas belonging to a specific catalog. | `schema` | 1.0.0 |
+| `get_list_of_tables` | Retrieve a list of tables within a specific catalog and schema. | `table` | 1.0.0 |
+| `get_table_metadata_details` | Retrieve comprehensive metadata details for a specific table. | `table` | 1.0.0 |
+| `get_list_of_policies` | Retrieve a list of policies in the system. | `policy` | 1.0.0 |
+| `get_policy_detail_information` | Retrieve detailed information for a specific policy by policy name. | `policy` | 1.0.0 |
+| `list_policies_for_metadata` | List all policies associated with a specific metadata item. | `policy` | 1.0.0 |
+| `list_metadata_by_policy` | List all metadata items associated with a specific policy. | `policy` | 1.0.0 |
+| `get_policy_for_metadata` | Get a policy associated with a specific metadata item. | `policy` | 1.0.0 |
### Configuration
diff --git a/mcp-server/mcp_server/client/__init__.py b/mcp-server/mcp_server/client/__init__.py
index 6fbdc469b7..dfddc047dd 100644
--- a/mcp-server/mcp_server/client/__init__.py
+++ b/mcp-server/mcp_server/client/__init__.py
@@ -19,6 +19,7 @@ from mcp_server.client.catalog_operation import CatalogOperation
from mcp_server.client.gravitino_operation import GravitinoOperation
from mcp_server.client.job_operation import JobOperation
from mcp_server.client.model_operation import ModelOperation
+from mcp_server.client.policy_operation import PolicyOperation
from mcp_server.client.schema_operation import SchemaOperation
from mcp_server.client.table_operation import TableOperation
from mcp_server.client.tag_operation import TagOperation
diff --git a/mcp-server/mcp_server/client/gravitino_operation.py b/mcp-server/mcp_server/client/gravitino_operation.py
index 010c31189c..6b0c4fa12b 100644
--- a/mcp-server/mcp_server/client/gravitino_operation.py
+++ b/mcp-server/mcp_server/client/gravitino_operation.py
@@ -21,6 +21,7 @@ from mcp_server.client.catalog_operation import CatalogOperation
from mcp_server.client.fileset_operation import FilesetOperation
from mcp_server.client.job_operation import JobOperation
from mcp_server.client.model_operation import ModelOperation
+from mcp_server.client.policy_operation import PolicyOperation
from mcp_server.client.schema_operation import SchemaOperation
from mcp_server.client.table_operation import TableOperation
from mcp_server.client.tag_operation import TagOperation
@@ -82,6 +83,16 @@ class GravitinoOperation(ABC):
"""
pass
+ @abstractmethod
+ def as_policy_operation(self) -> PolicyOperation:
+ """
+ Access the policy operation interface of this Gravitino operation.
+
+ Returns:
+ PolicyOperation: Interface for performing policy-level operations
+ """
+ pass
+
@abstractmethod
def as_fileset_operation(self) -> FilesetOperation:
"""
diff --git a/mcp-server/mcp_server/client/plain/plain_rest_client_operation.py b/mcp-server/mcp_server/client/plain/plain_rest_client_operation.py
index c21b8898cc..28798d699f 100644
--- a/mcp-server/mcp_server/client/plain/plain_rest_client_operation.py
+++ b/mcp-server/mcp_server/client/plain/plain_rest_client_operation.py
@@ -20,6 +20,8 @@ import httpx
from mcp_server.client import (
CatalogOperation,
GravitinoOperation,
+ ModelOperation,
+ PolicyOperation,
SchemaOperation,
TableOperation,
TagOperation,
@@ -37,6 +39,9 @@ from mcp_server.client.plain.plain_rest_client_job_operation import (
from mcp_server.client.plain.plain_rest_client_model_operation import (
PlainRESTClientModelOperation,
)
+from mcp_server.client.plain.plain_rest_client_policy_operation import (
+ PlainRESTClientPolicyOperation,
+)
from mcp_server.client.plain.plain_rest_client_schema_operation import (
PlainRESTClientSchemaOperation,
)
@@ -52,47 +57,61 @@ from mcp_server.client.plain.plain_rest_client_topic_operation import (
from mcp_server.client.topic_operation import TopicOperation
+# pylint: disable=too-many-instance-attributes
class PlainRESTClientOperation(GravitinoOperation):
def __init__(self, metalake_name: str, uri: str):
- self.metalake_name = metalake_name
- self.rest_client = httpx.AsyncClient(base_url=uri)
+ _rest_client = httpx.AsyncClient(base_url=uri)
+ self._catalog_operation = PlainRESTClientCatalogOperation(
+ metalake_name, _rest_client
+ )
+ self._table_operation = PlainRESTClientTableOperation(
+ metalake_name, _rest_client
+ )
+ self._schema_operation = PlainRESTClientSchemaOperation(
+ metalake_name, _rest_client
+ )
+ self._topic_operation = PlainRESTClientTopicOperation(
+ metalake_name, _rest_client
+ )
+ self._model_operation = PlainRESTClientModelOperation(
+ metalake_name, _rest_client
+ )
+ self._tag_operation = PlainRESTClientTagOperation(
+ metalake_name, _rest_client
+ )
+ self._fileset_operation = PlainRESTClientFilesetOperation(
+ metalake_name, _rest_client
+ )
+ self._job_operation = PlainRESTClientJobOperation(
+ metalake_name, _rest_client
+ )
+ self._policy_operation = PlainRESTClientPolicyOperation(
+ metalake_name, _rest_client
+ )
def as_catalog_operation(self) -> CatalogOperation:
- return PlainRESTClientCatalogOperation(
- metalake_name=self.metalake_name, rest_client=self.rest_client
- )
+ return self._catalog_operation
def as_table_operation(self) -> TableOperation:
- return PlainRESTClientTableOperation(
- metalake_name=self.metalake_name, rest_client=self.rest_client
- )
+ return self._table_operation
def as_schema_operation(self) -> SchemaOperation:
- return PlainRESTClientSchemaOperation(
- metalake_name=self.metalake_name, rest_client=self.rest_client
- )
+ return self._schema_operation
def as_topic_operation(self) -> TopicOperation:
- return PlainRESTClientTopicOperation(
- metalake_name=self.metalake_name, rest_client=self.rest_client
- )
+ return self._topic_operation
- def as_model_operation(self):
- return PlainRESTClientModelOperation(
- metalake_name=self.metalake_name, rest_client=self.rest_client
- )
+ def as_model_operation(self) -> ModelOperation:
+ return self._model_operation
def as_fileset_operation(self):
- return PlainRESTClientFilesetOperation(
- metalake_name=self.metalake_name, rest_client=self.rest_client
- )
+ return self._fileset_operation
def as_tag_operation(self) -> TagOperation:
- return PlainRESTClientTagOperation(
- metalake_name=self.metalake_name, rest_client=self.rest_client
- )
+ return self._tag_operation
def as_job_operation(self) -> JobOperation:
- return PlainRESTClientJobOperation(
- metalake_name=self.metalake_name, rest_client=self.rest_client
- )
+ return self._job_operation
+
+ def as_policy_operation(self) -> PolicyOperation:
+ return self._policy_operation
diff --git a/mcp-server/mcp_server/client/plain/plain_rest_client_policy_operation.py b/mcp-server/mcp_server/client/plain/plain_rest_client_policy_operation.py
new file mode 100644
index 0000000000..7312489861
--- /dev/null
+++ b/mcp-server/mcp_server/client/plain/plain_rest_client_policy_operation.py
@@ -0,0 +1,82 @@
+# 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 import PolicyOperation
+from mcp_server.client.plain.utils import extract_content_from_response
+
+
+class PlainRESTClientPolicyOperation(PolicyOperation):
+ """
+ Implementation of PolicyOperation using a plain REST client.
+ """
+
+ def __init__(self, metalake_name: str, rest_client):
+ self.metalake_name = metalake_name
+ self.rest_client = rest_client
+
+ async def get_list_of_policies(self) -> str:
+ response = await self.rest_client.get(
+ f"/api/metalakes/{self.metalake_name}/policies?details=true"
+ )
+ return extract_content_from_response(response, "policies", [])
+
+ async def load_policy(
+ self,
+ policy_name: str,
+ ) -> str:
+ response = await self.rest_client.get(
+ f"/api/metalakes/{self.metalake_name}/policies/{policy_name}"
+ )
+ return extract_content_from_response(response, "policy", {})
+
+ async def associate_policy_with_metadata(
+ self,
+ metadata_full_name: str,
+ metadata_type: str,
+ policies_to_add: list,
+ policies_to_remove: list,
+ ) -> str:
+ response = await self.rest_client.post(
+ f"/api/metalakes/{self.metalake_name}/objects/{metadata_type}/{metadata_full_name}/policies",
+ json={
+ "policiesToAdd": policies_to_add,
+ "policiesToRemove": policies_to_remove,
+ },
+ )
+ return extract_content_from_response(response, "names", [])
+
+ async def get_policy_for_metadata(
+ self, metadata_full_name: str, metadata_type: str, policy_name: str
+ ) -> str:
+ response = await self.rest_client.get(
+ f"/api/metalakes/{self.metalake_name}/objects/{metadata_type}/{metadata_full_name}/policies/{policy_name}",
+ )
+ return extract_content_from_response(response, "policy", {})
+
+ async def list_policies_for_metadata(
+ self, metadata_full_name: str, metadata_type: str
+ ) -> str:
+ response = await self.rest_client.get(
+ f"/api/metalakes/{self.metalake_name}/objects/{metadata_type}/{metadata_full_name}/policies?details=true",
+ )
+ return extract_content_from_response(response, "policies", [])
+
+ async def list_metadata_by_policy(self, policy_name: str) -> str:
+ response = await self.rest_client.get(
+ f"/api/metalakes/{self.metalake_name}/policies/{policy_name}/objects"
+ )
+ return extract_content_from_response(response, "metadataObjects", [])
diff --git a/mcp-server/mcp_server/client/policy_operation.py b/mcp-server/mcp_server/client/policy_operation.py
new file mode 100644
index 0000000000..6634b8676c
--- /dev/null
+++ b/mcp-server/mcp_server/client/policy_operation.py
@@ -0,0 +1,125 @@
+# 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 abc import ABC, abstractmethod
+
+
+class PolicyOperation(ABC):
+ """
+ Abstract base class for Gravitino policy operations.
+ """
+
+ @abstractmethod
+ async def get_list_of_policies(self) -> str:
+ """
+ Retrieve the list of policies.
+
+ Returns:
+ str: JSON-formatted string containing policy list information
+ """
+ pass
+
+ @abstractmethod
+ async def load_policy(self, policy_name: str) -> str:
+ """
+ Load detailed information of a specific policy.
+
+ Args:
+ policy_name: Name of the policy
+
+ Returns:
+ str: JSON-formatted string containing full policy metadata
+ """
+ pass
+
+ @abstractmethod
+ async def associate_policy_with_metadata(
+ self,
+ metadata_full_name: str,
+ metadata_type: str,
+ policies_to_add: list,
+ policies_to_remove: list,
+ ) -> str:
+ """
+ Associate policies with metadata.
+
+ Args:
+ metadata_full_name: Full name of the metadata object to associate policies with.
+ It's typically in the format "catalog.schema.table" or "catalog.schema" or "catalog"
+ or "catalog.schema.fileset". The "model", "topic" are also supported and the format
+ is the same as for "catalog.schema.table".
+ metadata_type: Type of the metadata (e.g., "table", "column")
+ policies_to_add: List of policy names to associate with the metadata
+ policies_to_remove: List of policy names to disassociate from the metadata
+
+ Returns:
+ str: JSON formatted string containing list of policy names that were
+ successfully associated with the metadata
+ """
+ pass
+
+ @abstractmethod
+ async def get_policy_for_metadata(
+ self, metadata_full_name: str, metadata_type: str, policy_name: str
+ ) -> str:
+ """
+ Get the policy associated with a specific metadata item.
+
+ Args:
+ metadata_full_name: Full name of the metadata object to associate policies with.
+ It's typically in the format "catalog.schema.table" or "catalog.schema" or "catalog"
+ or "catalog.schema.fileset". The "model", "topic" are also supported and the format
+ is the same as for "catalog.schema.table".
+ metadata_type: Type of the metadata (e.g., "table", "column")
+ policy_name: Name of the policy
+
+ Returns:
+ str: JSON formatted string containing policy metadata
+ """
+ pass
+
+ @abstractmethod
+ async def list_policies_for_metadata(
+ self, metadata_full_name: str, metadata_type: str
+ ) -> str:
+ """
+ List all policies associated with a specific metadata item.
+
+ Args:
+ metadata_full_name: Full name of the metadata object to associate policies with.
+ It's typically in the format "catalog.schema.table" or "catalog.schema" or "catalog"
+ or "catalog.schema.fileset". The "model", "topic" are also supported and the format
+ is the same as for "catalog.schema.table".
+ metadata_type: Type of the metadata (e.g., "table", "column")
+
+ Returns:
+ str: JSON formatted string containing list of policy metadata associated with the metadata
+ """
+ pass
+
+ @abstractmethod
+ async def list_metadata_by_policy(self, policy_name: str) -> str:
+ """
+ List all metadata items associated with a specific policy.
+
+ Args:
+ policy_name: Name of the policy to filter metadata by
+
+ Returns:
+ str: JSON formatted string containing list of metadata items associated with the policy
+ """
+ pass
diff --git a/mcp-server/mcp_server/tools/__init__.py b/mcp-server/mcp_server/tools/__init__.py
index 73ece0e368..69fabbf31c 100644
--- a/mcp-server/mcp_server/tools/__init__.py
+++ b/mcp-server/mcp_server/tools/__init__.py
@@ -22,6 +22,7 @@ from mcp_server.tools.fileset import load_fileset_tools
from mcp_server.tools.job import load_job_tool
from mcp_server.tools.metadata import load_metadata_tool
from mcp_server.tools.model import load_model_tools
+from mcp_server.tools.policy import load_policy_tools
from mcp_server.tools.schema import load_schema_tools
from mcp_server.tools.table import load_table_tools
from mcp_server.tools.tag import load_tag_tool
@@ -38,3 +39,4 @@ def load_tools(mcp: FastMCP):
load_fileset_tools(mcp)
load_tag_tool(mcp)
load_metadata_tool(mcp)
+ load_policy_tools(mcp)
diff --git a/mcp-server/mcp_server/tools/policy.py b/mcp-server/mcp_server/tools/policy.py
new file mode 100644
index 0000000000..6507446778
--- /dev/null
+++ b/mcp-server/mcp_server/tools/policy.py
@@ -0,0 +1,393 @@
+# 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
+
+
+def load_policy_tools(mcp: FastMCP):
+ @mcp.tool(tags={"policy"})
+ async def get_list_of_policies(
+ ctx: Context,
+ ) -> str:
+ """
+ Retrieve the list of available policy information.
+
+ Parameters:
+ ctx (Context): The request context object containing lifespan context
+ and connector information.
+ Returns:
+ str: A JSON string representing an array of policy information objects, each object
+ contains the policy name, comment, policy type, enabled status, content, supported object types,
+ inherited policy, and audit information.
+
+ Example Response:
+ [
+ {
+ "name": "my_policy1",
+ "comment": "This is a test policy",
+ "policyType": "custom",
+ "enabled": true,
+ "content": {
+ "customRules": {
+ "rule1": 123
+ },
+ "properties": {
+ "key1": "value1"
+ },
+ "supportedObjectTypes": [
+ "fileset",
+ "schema",
+ "topic",
+ "table",
+ "model",
+ "catalog"
+ ]
+ },
+ "inherited": null,
+ "audit": {
+ "creator": "anonymous",
+ "createTime": "2025-08-18T08:29:30.016501Z"
+ }
+ }
+ ]
+ """
+ client = ctx.request_context.lifespan_context.rest_client()
+ return await client.as_policy_operation().get_list_of_policies()
+
+ @mcp.tool(tags={"policy"})
+ async def get_policy_detail_information(
+ ctx: Context,
+ policy_name: str,
+ ) -> str:
+ """
+ Retrieve detailed information for a specific policy by policy name.
+
+ Parameters:
+ ctx : Context
+ The request context object containing lifespan context and connector
+ information.
+ policy_name : str
+ The unique identifier for the policy. Must be one of the names
+ returned by `get_list_of_policies`.
+
+ Returns:
+ str : A structured JSON object containing the policy configuration with the following fields:
+ name: Unique policy identifier (string)
+ comment: Descriptive text about the policy (string)
+ policyType: Classification of policy (e.g., "custom") (string)
+ enabled: Activation status (boolean)
+ content: Rule definitions and scope settings (dict)
+ - customRules: Key-value pairs of rule identifiers and settings (dict)
+ - properties: Additional configuration properties (dict)
+ - supportedObjectTypes: Entities this policy applies to (list of string)
+ inherited: Whether the policy is inherited from parent metadata (boolean)
+ audit: Creation metadata (dict)
+ - creator: User or service that created the policy (string)
+ - createTime: ISO 8601 timestamp of creation (string)
+
+ Example Response:
+ {
+ "name": "my_policy1",
+ "comment": "This is a test policy",
+ "policyType": "custom",
+ "enabled": true,
+ "content": {
+ "customRules": {
+ "rule1": 123
+ },
+ "properties": {
+ "key1": "value1"
+ },
+ "supportedObjectTypes": [
+ "fileset",
+ "schema",
+ "topic",
+ "table",
+ "model",
+ "catalog"
+ ]
+ },
+ "inherited": null,
+ "audit": {
+ "creator": "anonymous",
+ "createTime": "2025-08-18T08:29:30.016501Z"
+ }
+ }
+ """
+
+ client = ctx.request_context.lifespan_context.rest_client()
+ return await client.as_policy_operation().load_policy(policy_name)
+
+ @mcp.tool(tags={"policy"})
+ async def associate_policy_with_metadata(
+ ctx: Context,
+ metadata_full_name: str,
+ metadata_type: str,
+ policies_to_add: list,
+ ) -> str:
+ """
+ Associate policies with metadata.
+
+ Args:
+ ctx (Context): The request context object containing lifespan context
+ and connector information.
+ metadata_full_name (str): Full name of the metadata object to associate policies with.
+ It's typically in the format "catalog.schema.table" or "catalog.schema" or "catalog"
+ or "catalog.schema.fileset". The "model", "topic" are also supported and the format
+ is the same as for "catalog.schema.table". For more, please see tool
+ `get_metadata_fullname_formats`.
+
+ metadata_type (str): Type of the metadata object (e.g., "table", "schema", "catalog",
+ "fileset", "model", "topic"). For More information, please see
+ tool `list_all_metadata_types`.
+ policies_to_add (list): List of policy names to associate with the metadata.
+
+ Example input:
+ metadata_full_name: "catalog.schema.table"
+ metadata_type: "table"
+ policies_to_add: ["policy1", "policy2"]
+
+ Returns:
+ str: JSON-formatted string containing the names of associated policies.
+
+ Example Return Value:
+ [
+ "policy1",
+ "policy2"
+ ]
+ """
+ client = ctx.request_context.lifespan_context.rest_client()
+ return (
+ await client.as_policy_operation().associate_policy_with_metadata(
+ metadata_full_name,
+ metadata_type,
+ policies_to_add,
+ [],
+ )
+ )
+
+ @mcp.tool(tags={"policy"})
+ async def disassociate_policy_from_metadata(
+ ctx: Context,
+ metadata_full_name: str,
+ metadata_type: str,
+ policies_to_remove: list,
+ ) -> str:
+ """
+ Disassociate policies from metadata.
+
+ Args:
+ ctx (Context): The request context object containing lifespan context
+ and connector information.
+ metadata_full_name (str): Full name of the metadata object to disassociate policies from.
+ For more, please see tool `get_metadata_fullname_formats`.
+ metadata_type (str): Type of the metadata object (e.g., "table", "schema", "catalog",
+ "fileset", "model", "topic"). For More information, please see
+ tool `list_all_metadata_types`.
+ policies_to_remove (list): List of policy names to disassociate from the metadata.
+
+ Example input:
+ metadata_full_name: "catalog.schema.table"
+ metadata_type: "table"
+ policies_to_remove: ["policy1", "policy2"]
+
+ Returns:
+ str: JSON-formatted string containing the names of associated policies.
+
+ Example Return Value:
+ [
+ "policy3"
+ ]
+ """
+ client = ctx.request_context.lifespan_context.rest_client()
+ return (
+ await client.as_policy_operation().associate_policy_with_metadata(
+ metadata_full_name, metadata_type, [], policies_to_remove
+ )
+ )
+
+ @mcp.tool(tags={"policy"})
+ async def list_policies_for_metadata(
+ ctx: Context, metadata_full_name: str, metadata_type: str
+ ) -> str:
+ """
+ List all policies associated with a specific metadata item.
+
+ Args:
+ ctx (Context): The request context object containing lifespan context
+ and connector information.
+ metadata_full_name (str): Full name of the metadata item. For more, please see tool
+ `get_metadata_fullname_formats`.
+ metadata_type (str): Type of the metadata (e.g., "table", "column"). For More information, please see
+ tool `list_all_metadata_types`.
+
+ Example input:
+ metadata_full_name: "catalog.schema.table"
+ metadata_type: "table"
+
+ Returns:
+ str: JSON-formatted string containing the list of policies associated with the metadata.
+
+ Example Return Value:
+ [
+ {
+ "name": "my_policy1",
+ "comment": "This is a test policy",
+ "policyType": "custom",
+ "enabled": true,
+ "content": {
+ "customRules": {
+ "rule1": 123
+ },
+ "properties": {
+ "key1": "value1"
+ },
+ "supportedObjectTypes": [
+ "fileset",
+ "model",
+ "topic",
+ "schema",
+ "table",
+ "catalog"
+ ]
+ },
+ "inherited": false,
+ "audit": {
+ "creator": "anonymous",
+ "createTime": "2025-08-18T08:29:30.016501Z"
+ }
+ }
+ ]
+ """
+ client = ctx.request_context.lifespan_context.rest_client()
+ return await client.as_policy_operation().list_policies_for_metadata(
+ metadata_full_name, metadata_type
+ )
+
+ @mcp.tool(tags={"policy"})
+ async def list_metadata_by_policy(ctx: Context, policy_name: str) -> str:
+ """
+ List all metadata items associated with a specific policy.
+
+ The child metadata items which inherit the associate from the parent metadata
+ will be excluded in the result. For example, if the policy is associated with
+ the catalog, the catalog's child metadata items will be excluded in the result.
+
+ Args:
+ ctx (Context): The request context object containing lifespan context
+ and connector information.
+ policy_name (str): Name of the policy to filter metadata by.
+
+ Returns:
+ str: JSON-formatted string containing the list of metadata items associated with the policy.
+
+ Example Return Value:
+ [
+ {
+ "fullName": "model_catalog",
+ "type": "catalog"
+ },
+ {
+ "fullName": "catalog.schema.table",
+ "type": "table"
+ },
+ {
+ "fullName": "catalog.schema.fileset",
+ "type": "fileset"
+ },
+ {
+ "fullName": "catalog.schema.model",
+ "type": "model"
+ },
+ {
+ "fullName": "catalog.schema.topic",
+ "type": "topic"
+ }
+ {
+ "fullName": "catalog.schema",
+ "type": "schema"
+ }
+ ...
+ ]
+ """
+ client = ctx.request_context.lifespan_context.rest_client()
+ return await client.as_policy_operation().list_metadata_by_policy(
+ policy_name
+ )
+
+ @mcp.tool(tags={"policy"})
+ async def get_policy_for_metadata(
+ ctx: Context,
+ metadata_full_name: str,
+ metadata_type: str,
+ policy_name: str,
+ ) -> str:
+ """
+ Get the policy associated with a specific metadata item by policy name.
+
+ This tool is used to check whether a policy is associated with the metadata item.
+
+ Args:
+ ctx (Context): The request context object containing lifespan context
+ and connector information.
+ metadata_full_name (str): Full name of the metadata item. For more, please see tool
+ `get_metadata_fullname_formats`.
+ metadata_type (str): Type of the metadata (e.g., "table", "column"). For More information, please see
+ tool `list_all_metadata_types`.
+ policy_name (str): Name of the policy to filter metadata by.
+
+ Example input:
+ metadata_full_name: "catalog.schema.table"
+ metadata_type: "table"
+ policy_name: "policy_a"
+
+ Returns:
+ str: JSON-formatted string containing the detailed policy information associated with the metadata.
+
+ Example Return Value:
+ {
+ "name": "my_policy1",
+ "comment": "This is a test policy",
+ "policyType": "custom",
+ "enabled": true,
+ "content": {
+ "customRules": {
+ "rule1": 123
+ },
+ "properties": {
+ "key1": "value1"
+ },
+ "supportedObjectTypes": [
+ "fileset",
+ "schema",
+ "topic",
+ "table",
+ "model",
+ "catalog"
+ ]
+ },
+ "inherited": false,
+ "audit": {
+ "creator": "anonymous",
+ "createTime": "2025-08-18T08:29:30.016501Z"
+ }
+ }
+ """
+ client = ctx.request_context.lifespan_context.rest_client()
+ return await client.as_policy_operation().get_policy_for_metadata(
+ metadata_full_name, metadata_type, policy_name
+ )
diff --git a/mcp-server/mcp_server/tools/tag.py b/mcp-server/mcp_server/tools/tag.py
index 95395b7891..d708b27a5b 100644
--- a/mcp-server/mcp_server/tools/tag.py
+++ b/mcp-server/mcp_server/tools/tag.py
@@ -193,8 +193,7 @@ def load_tag_tool(mcp: FastMCP):
client = ctx.request_context.lifespan_context.rest_client()
return await client.as_tag_operation().delete_tag(name)
- # Disable the associate_tag_with_metadata tool by default as it will change metadata state.
- @mcp.tool(tags={"tag"}, enabled=False)
+ @mcp.tool(tags={"tag"})
async def associate_tag_with_metadata(
ctx: Context,
metadata_full_name: str,
@@ -202,7 +201,7 @@ def load_tag_tool(mcp: FastMCP):
tags_to_associate: list,
) -> str:
"""
- Associate or disassociate tags with metadata.
+ Associate tags with metadata.
Args:
ctx (Context): The request context object containing lifespan context
@@ -241,8 +240,7 @@ def load_tag_tool(mcp: FastMCP):
[],
)
- # Disable the disassociate_tag_from_metadata tool by default as it will change metadata state.
- @mcp.tool(tags={"tag"}, enabled=False)
+ @mcp.tool(tags={"tag"})
async def disassociate_tag_from_metadata(
ctx: Context,
metadata_full_name: str,
diff --git a/mcp-server/tests/unit/tools/mock_operation.py b/mcp-server/tests/unit/tools/mock_operation.py
index db8258ef51..0831dfb69b 100644
--- a/mcp-server/tests/unit/tools/mock_operation.py
+++ b/mcp-server/tests/unit/tools/mock_operation.py
@@ -19,6 +19,7 @@ from mcp_server.client import (
CatalogOperation,
GravitinoOperation,
ModelOperation,
+ PolicyOperation,
SchemaOperation,
TableOperation,
TagOperation,
@@ -56,6 +57,9 @@ class MockOperation(GravitinoOperation):
def as_job_operation(self) -> JobOperation:
return MockJobOperation()
+ def as_policy_operation(self) -> PolicyOperation:
+ return MockPolicyOperation()
+
class MockCatalogOperation(CatalogOperation):
async def get_list_of_catalogs(self) -> str:
@@ -103,6 +107,41 @@ class MockFilesetOperation(FilesetOperation):
return "mock_files_in_fileset"
+class MockPolicyOperation(PolicyOperation):
+ async def associate_policy_with_metadata(
+ self,
+ metadata_full_name: str,
+ metadata_type: str,
+ policies_to_add: list,
+ policies_to_remove: list,
+ ) -> str:
+ return (
+ f"associate_policy_with_metadata: {metadata_full_name}, {metadata_type}, "
+ f"{policies_to_add}, {policies_to_remove}"
+ )
+
+ async def get_policy_for_metadata(
+ self, metadata_full_name: str, metadata_type: str, policy_name: str
+ ) -> str:
+ return f"get_policy_for_metadata: {metadata_full_name}, {metadata_type}, {policy_name}"
+
+ async def list_policies_for_metadata(
+ self, metadata_full_name: str, metadata_type: str
+ ) -> str:
+ return (
+ f"list_policies_for_metadata: {metadata_full_name}, {metadata_type}"
+ )
+
+ async def list_metadata_by_policy(self, policy_name: str) -> str:
+ return f"list_metadata_by_policy: {policy_name}"
+
+ async def get_list_of_policies(self) -> str:
+ return "mock_policies"
+
+ async def load_policy(self, policy_name: str) -> str:
+ return f"mock_policy: {policy_name}"
+
+
class MockModelOperation(ModelOperation):
async def get_list_of_models(
self, catalog_name: str, schema_name: str
diff --git a/mcp-server/tests/unit/tools/test_policy.py b/mcp-server/tests/unit/tools/test_policy.py
new file mode 100644
index 0000000000..3b49d2a246
--- /dev/null
+++ b/mcp-server/tests/unit/tools/test_policy.py
@@ -0,0 +1,140 @@
+# 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.
+
+import asyncio
+import unittest
+
+from fastmcp import Client
+
+from mcp_server.client.factory import RESTClientFactory
+from mcp_server.core import Setting
+from mcp_server.server import GravitinoMCPServer
+from tests.unit.tools import MockOperation
+
+
+class TestPolicyTool(unittest.TestCase):
+
+ def setUp(self):
+ RESTClientFactory.set_rest_client(MockOperation)
+ server = GravitinoMCPServer(Setting("mock_metalake"))
+ self.mcp = server.mcp
+
+ def test_list_policies(self):
+ async def _test_list_policies(mcp_server):
+ async with Client(mcp_server) as client:
+ result = await client.call_tool("get_list_of_policies", {})
+ self.assertEqual("mock_policies", result.content[0].text)
+
+ asyncio.run(_test_list_policies(self.mcp))
+
+ def test_get_policy_detail_information(self):
+ async def _test_get_policy_detail_information(mcp_server):
+ async with Client(mcp_server) as client:
+ result = await client.call_tool(
+ "get_policy_detail_information",
+ {"policy_name": "mock_name"},
+ )
+ self.assertEqual(
+ "mock_policy: mock_name", result.content[0].text
+ )
+
+ asyncio.run(_test_get_policy_detail_information(self.mcp))
+
+ def test_list_policies_for_metadata(self):
+ async def _test_list_policies_for_metadata(mcp_server):
+ async with Client(mcp_server) as client:
+ result = await client.call_tool(
+ "list_policies_for_metadata",
+ {
+ "metadata_full_name": "catalog.db.table",
+ "metadata_type": "table",
+ },
+ )
+ self.assertEqual(
+ "list_policies_for_metadata: catalog.db.table, table",
+ result.content[0].text,
+ )
+
+ asyncio.run(_test_list_policies_for_metadata(self.mcp))
+
+ def test_list_metadata_for_policy(self):
+ async def _test_list_metadata_for_policy(mcp_server):
+ async with Client(mcp_server) as client:
+ result = await client.call_tool(
+ "list_metadata_by_policy",
+ {"policy_name": "mock_policy"},
+ )
+ self.assertEqual(
+ "list_metadata_by_policy: mock_policy",
+ result.content[0].text,
+ )
+
+ asyncio.run(_test_list_metadata_for_policy(self.mcp))
+
+ def test_disassociate_policy_from_metadata(self):
+ async def _test_disassociate_policy_from_metadata(mcp_server):
+ async with Client(mcp_server) as client:
+ result = await client.call_tool(
+ "disassociate_policy_from_metadata",
+ {
+ "metadata_full_name": "catalog.db.table",
+ "metadata_type": "table",
+ "policies_to_remove": ["mock_policy"],
+ },
+ )
+ self.assertEqual(
+ """associate_policy_with_metadata: catalog.db.table, table, [], ['mock_policy']""",
+ result.content[0].text,
+ )
+
+ asyncio.run(_test_disassociate_policy_from_metadata(self.mcp))
+
+ def test_associate_policy_with_metadata(self):
+ async def _test_associate_policy_with_metadata(mcp_server):
+ async with Client(mcp_server) as client:
+ result = await client.call_tool(
+ "associate_policy_with_metadata",
+ {
+ "metadata_full_name": "catalog.db.table",
+ "metadata_type": "table",
+ "policies_to_add": ["mock_policy"],
+ },
+ )
+ self.assertEqual(
+ """associate_policy_with_metadata: catalog.db.table, table, ['mock_policy'], []""",
+ result.content[0].text,
+ )
+
+ asyncio.run(_test_associate_policy_with_metadata(self.mcp))
+
+ def test_get_policy_for_metadata(self):
+ async def _test_get_policy_for_metadata(mcp_server):
+ async with Client(mcp_server) as client:
+ result = await client.call_tool(
+ "get_policy_for_metadata",
+ {
+ "metadata_full_name": "catalog.db.table",
+ "metadata_type": "table",
+ "policy_name": "mock_policy",
+ },
+ )
+ self.assertEqual(
+ """get_policy_for_metadata: catalog.db.table, table, mock_policy""",
+ result.content[0].text,
+ )
+
+ asyncio.run(_test_get_policy_for_metadata(self.mcp))