|
| 1 | +import os |
| 2 | +from logging import Logger |
| 3 | + |
| 4 | +import oci |
| 5 | +from fastmcp import FastMCP |
| 6 | + |
| 7 | +logger = Logger("oci_registry_mcp", level="INFO") |
| 8 | + |
| 9 | +mcp = FastMCP("oci_registry") |
| 10 | + |
| 11 | + |
| 12 | +def get_ocir_client(): |
| 13 | + config = oci.config.from_file( |
| 14 | + profile_name=os.getenv("OCI_CONFIG_PROFILE", oci.config.DEFAULT_PROFILE) |
| 15 | + ) |
| 16 | + private_key = oci.signer.load_private_key_from_file(config["key_file"]) |
| 17 | + token_file = config["security_token_file"] |
| 18 | + token = None |
| 19 | + with open(token_file, "r") as f: |
| 20 | + token = f.read() |
| 21 | + signer = oci.auth.signers.SecurityTokenSigner(token, private_key) |
| 22 | + return oci.artifacts.ArtifactsClient(config, signer=signer) |
| 23 | + |
| 24 | + |
| 25 | +@mcp.tool |
| 26 | +def create_container_repository( |
| 27 | + compartment_id: str, repository_name: str, is_public: bool = False |
| 28 | +): |
| 29 | + ocir_client = get_ocir_client() |
| 30 | + create_repository_details = oci.artifacts.models.CreateContainerRepositoryDetails( |
| 31 | + compartment_id=compartment_id, display_name=repository_name, is_public=is_public |
| 32 | + ) |
| 33 | + try: |
| 34 | + repository = ocir_client.create_container_repository( |
| 35 | + create_repository_details |
| 36 | + ).data |
| 37 | + return { |
| 38 | + "repository_name": repository.display_name, |
| 39 | + "id": repository.id, |
| 40 | + "is_public": repository.is_public, |
| 41 | + } |
| 42 | + except oci.exceptions.ServiceError as e: |
| 43 | + logger.error(f"Failed to create container repository: {e}") |
| 44 | + return {"error": str(e)} |
| 45 | + |
| 46 | + |
| 47 | +@mcp.tool |
| 48 | +def list_container_repositories(compartment_id: str): |
| 49 | + ocir_client = get_ocir_client() |
| 50 | + try: |
| 51 | + repositories = ocir_client.list_container_repositories( |
| 52 | + compartment_id=compartment_id |
| 53 | + ).data.items |
| 54 | + return [ |
| 55 | + { |
| 56 | + "repository_name": repo.display_name, |
| 57 | + "id": repo.id, |
| 58 | + "is_public": repo.is_public, |
| 59 | + } |
| 60 | + for repo in repositories |
| 61 | + ] |
| 62 | + except oci.exceptions.ServiceError as e: |
| 63 | + logger.error(f"Failed to list container repositories: {e}") |
| 64 | + return {"error": str(e)} |
| 65 | + |
| 66 | + |
| 67 | +@mcp.tool |
| 68 | +def get_container_repo_details(repository_id: str): |
| 69 | + ocir_client = get_ocir_client() |
| 70 | + try: |
| 71 | + repository = ocir_client.get_container_repository( |
| 72 | + repository_id=repository_id |
| 73 | + ).data |
| 74 | + return { |
| 75 | + "repository_name": repository.display_name, |
| 76 | + "id": repository.id, |
| 77 | + "is_public": repository.is_public, |
| 78 | + "compartment_id": repository.compartment_id, |
| 79 | + "time_created": repository.time_created.isoformat(), |
| 80 | + } |
| 81 | + except oci.exceptions.ServiceError as e: |
| 82 | + logger.error(f"Failed to get container repository details: {e}") |
| 83 | + return {"error": str(e)} |
| 84 | + |
| 85 | + |
| 86 | +@mcp.tool |
| 87 | +def delete_container_repository(repository_id: str): |
| 88 | + ocir_client = get_ocir_client() |
| 89 | + try: |
| 90 | + ocir_client.delete_container_repository(repository_id=repository_id) |
| 91 | + return {"success": True} |
| 92 | + except oci.exceptions.ServiceError as e: |
| 93 | + logger.error(f"Failed to delete container repository: {e}") |
| 94 | + return {"error": str(e), "success": False} |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + mcp.run() |
0 commit comments