|
| 1 | +""" |
| 2 | +Copyright (c) 2025, Oracle and/or its affiliates. |
| 3 | +Licensed under the Universal Permissive License v1.0 as shown at |
| 4 | +https://oss.oracle.com/licenses/upl. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +from logging import Logger |
| 9 | +from typing import Annotated |
| 10 | + |
| 11 | +import oci |
| 12 | +from fastmcp import FastMCP |
| 13 | + |
| 14 | +from . import __project__, __version__ |
| 15 | + |
| 16 | +logger = Logger(__name__, level="INFO") |
| 17 | + |
| 18 | +mcp = FastMCP(name=__project__) |
| 19 | + |
| 20 | + |
| 21 | +def get_faaas_client(): |
| 22 | + """Initialize and return an OCI Fusion Applications client using security token auth.""" |
| 23 | + logger.info("entering get_faaas_client") |
| 24 | + # Load config from default OCI config file (~/.oci/config) or as per env |
| 25 | + config = oci.config.from_file( |
| 26 | + profile_name=os.getenv("OCI_CONFIG_PROFILE", oci.config.DEFAULT_PROFILE) |
| 27 | + ) |
| 28 | + |
| 29 | + # Append project/version user-agent like other servers |
| 30 | + user_agent_name = __project__.split("oracle.", 1)[1].split("-server", 1)[0] |
| 31 | + config["additional_user_agent"] = f"{user_agent_name}/{__version__}" |
| 32 | + |
| 33 | + # Use security token signer (matches pattern used in other OCI MCP servers) |
| 34 | + private_key = oci.signer.load_private_key_from_file(config["key_file"]) |
| 35 | + token_file = config["security_token_file"] |
| 36 | + token = None |
| 37 | + with open(token_file, "r") as f: |
| 38 | + token = f.read() |
| 39 | + signer = oci.auth.signers.SecurityTokenSigner(token, private_key) |
| 40 | + |
| 41 | + # Return Fusion Applications client |
| 42 | + return oci.fusion_apps.FusionApplicationsClient(config, signer=signer) |
| 43 | + |
| 44 | + |
| 45 | +def _to_dict(obj): |
| 46 | + """Best-effort conversion of OCI SDK model objects to plain dicts.""" |
| 47 | + try: |
| 48 | + return oci.util.to_dict(obj) |
| 49 | + except Exception: |
| 50 | + try: |
| 51 | + return obj.__dict__ |
| 52 | + except Exception: |
| 53 | + return obj |
| 54 | + |
| 55 | + |
| 56 | +def _append_items_from_response_data(accum: list[dict], data_obj): |
| 57 | + """ |
| 58 | + Normalize list responses across SDK collections/lists: |
| 59 | + - If response.data has 'items', iterate it |
| 60 | + - If it's already a list, iterate directly |
| 61 | + - Else treat it as a single object |
| 62 | + """ |
| 63 | + try: |
| 64 | + items = getattr(data_obj, "items", None) |
| 65 | + if items is not None: |
| 66 | + for it in items: |
| 67 | + accum.append(_to_dict(it)) |
| 68 | + return |
| 69 | + |
| 70 | + if isinstance(data_obj, list): |
| 71 | + for it in data_obj: |
| 72 | + accum.append(_to_dict(it)) |
| 73 | + return |
| 74 | + |
| 75 | + # Fallback: single object |
| 76 | + accum.append(_to_dict(data_obj)) |
| 77 | + except Exception as e: |
| 78 | + logger.error(f"Error converting response data to dicts: {e}") |
| 79 | + raise |
| 80 | + |
| 81 | + |
| 82 | +@mcp.tool( |
| 83 | + description="Returns a list of Fusion Environment Families in the specified compartment." |
| 84 | +) |
| 85 | +def list_fusion_environment_families( |
| 86 | + compartment_id: Annotated[ |
| 87 | + str, "The ID of the compartment in which to list resources." |
| 88 | + ], |
| 89 | + display_name: Annotated[str, "Filter to match entire display name."] = None, |
| 90 | + lifecycle_state: Annotated[ |
| 91 | + str, |
| 92 | + "Filter by lifecycle state. Allowed: CREATING, UPDATING, ACTIVE, DELETING, DELETED, FAILED", |
| 93 | + ] = None, |
| 94 | +) -> list[dict]: |
| 95 | + client = get_faaas_client() |
| 96 | + |
| 97 | + families: list[dict] = [] |
| 98 | + next_page: str | None = None |
| 99 | + has_next_page = True |
| 100 | + |
| 101 | + while has_next_page: |
| 102 | + response: oci.response.Response = client.list_fusion_environment_families( |
| 103 | + compartment_id=compartment_id, |
| 104 | + display_name=display_name, |
| 105 | + lifecycle_state=lifecycle_state, |
| 106 | + page=next_page, |
| 107 | + ) |
| 108 | + _append_items_from_response_data(families, response.data) |
| 109 | + has_next_page = getattr(response, "has_next_page", False) |
| 110 | + next_page = getattr(response, "next_page", None) |
| 111 | + |
| 112 | + logger.info(f"Found {len(families)} Fusion Environment Families") |
| 113 | + return families |
| 114 | + |
| 115 | + |
| 116 | +@mcp.tool( |
| 117 | + description=( |
| 118 | + "Returns a list of Fusion Environments in the specified compartment " |
| 119 | + "(optionally filtered by family)." |
| 120 | + ) |
| 121 | +) |
| 122 | +def list_fusion_environments( |
| 123 | + compartment_id: Annotated[ |
| 124 | + str, "The ID of the compartment in which to list resources." |
| 125 | + ], |
| 126 | + fusion_environment_family_id: Annotated[ |
| 127 | + str, "Optional Fusion Environment Family OCID" |
| 128 | + ] = None, |
| 129 | + display_name: Annotated[str, "Filter to match entire display name."] = None, |
| 130 | + lifecycle_state: Annotated[ |
| 131 | + str, |
| 132 | + "Filter by lifecycle state. Allowed: CREATING, UPDATING, ACTIVE, INACTIVE, DELETING, DELETED, FAILED", |
| 133 | + ] = None, |
| 134 | +) -> list[dict]: |
| 135 | + client = get_faaas_client() |
| 136 | + |
| 137 | + environments: list[dict] = [] |
| 138 | + next_page: str | None = None |
| 139 | + has_next_page = True |
| 140 | + |
| 141 | + while has_next_page: |
| 142 | + response: oci.response.Response = client.list_fusion_environments( |
| 143 | + compartment_id=compartment_id, |
| 144 | + fusion_environment_family_id=fusion_environment_family_id, |
| 145 | + display_name=display_name, |
| 146 | + lifecycle_state=lifecycle_state, |
| 147 | + page=next_page, |
| 148 | + ) |
| 149 | + _append_items_from_response_data(environments, response.data) |
| 150 | + has_next_page = getattr(response, "has_next_page", False) |
| 151 | + next_page = getattr(response, "next_page", None) |
| 152 | + |
| 153 | + logger.info(f"Found {len(environments)} Fusion Environments") |
| 154 | + return environments |
| 155 | + |
| 156 | + |
| 157 | +@mcp.tool(description="Gets a Fusion Environment by OCID.") |
| 158 | +def get_fusion_environment( |
| 159 | + fusion_environment_id: Annotated[str, "Unique FusionEnvironment identifier (OCID)"], |
| 160 | +) -> dict: |
| 161 | + client = get_faaas_client() |
| 162 | + response: oci.response.Response = client.get_fusion_environment( |
| 163 | + fusion_environment_id |
| 164 | + ) |
| 165 | + return _to_dict(response.data) |
| 166 | + |
| 167 | + |
| 168 | +@mcp.tool(description="Gets the status of a Fusion Environment by OCID.") |
| 169 | +def get_fusion_environment_status( |
| 170 | + fusion_environment_id: Annotated[str, "Unique FusionEnvironment identifier (OCID)"], |
| 171 | +) -> dict: |
| 172 | + client = get_faaas_client() |
| 173 | + response: oci.response.Response = client.get_fusion_environment_status( |
| 174 | + fusion_environment_id |
| 175 | + ) |
| 176 | + return _to_dict(response.data) |
| 177 | + |
| 178 | + |
| 179 | +def main(): |
| 180 | + mcp.run() |
| 181 | + |
| 182 | + |
| 183 | +if __name__ == "__main__": |
| 184 | + main() |
0 commit comments