|
1 | 1 | import os |
2 | 2 | from logging import Logger |
| 3 | +from typing import Annotated |
3 | 4 |
|
4 | 5 | import oci |
5 | 6 | from fastmcp import FastMCP |
@@ -44,9 +45,9 @@ def launch_instance( |
44 | 45 | compartment_id: str, |
45 | 46 | display_name: str, |
46 | 47 | availability_domain: str, |
47 | | - shape: str, |
48 | 48 | image_id: str, |
49 | 49 | subnet_id: str, |
| 50 | + shape: Annotated[str, "Instance shape"] = "VM.Standard.A1.Flex", |
50 | 51 | ): |
51 | 52 | compute = get_compute_client() |
52 | 53 | launch_details = oci.core.models.LaunchInstanceDetails( |
@@ -81,13 +82,86 @@ def get_instance(instance_id: str): |
81 | 82 | # } |
82 | 83 |
|
83 | 84 |
|
| 85 | +@mcp.tool |
| 86 | +def get_image(image_id: str): |
| 87 | + compute = get_compute_client() |
| 88 | + return compute.get_image(image_id).data |
| 89 | + |
| 90 | + |
| 91 | +@mcp.tool |
| 92 | +def list_images(compartment_id: str, operating_system: str = None): |
| 93 | + """List images, optionally filtered by operating system""" |
| 94 | + compute = get_compute_client() |
| 95 | + images = compute.list_images(compartment_id).data |
| 96 | + |
| 97 | + if operating_system: |
| 98 | + images = [img for img in images if img.operating_system == operating_system] |
| 99 | + |
| 100 | + return [ |
| 101 | + { |
| 102 | + "id": img.id, |
| 103 | + "display_name": img.display_name, |
| 104 | + "operating_system": img.operating_system, |
| 105 | + "operating_system_version": img.operating_system_version, |
| 106 | + } |
| 107 | + for img in images |
| 108 | + ] |
| 109 | + |
| 110 | + |
84 | 111 | @mcp.tool |
85 | 112 | def instance_action(instance_id: str, action: str): |
86 | 113 | compute = get_compute_client() |
87 | 114 | response = compute.instance_action(instance_id, action) |
88 | 115 | return {"status": action, "opc_request_id": response.headers.get("opc-request-id")} |
89 | 116 |
|
90 | 117 |
|
| 118 | +@mcp.tool |
| 119 | +def update_instance_details( |
| 120 | + instance_id: str, |
| 121 | + ocpus: Annotated[int, "Number of CPUs allocated to the instance"], |
| 122 | + memory_in_gbs: Annotated[ |
| 123 | + int, "Amount of memory in gigabytes (GB) allocated to the instance" |
| 124 | + ], |
| 125 | +): |
| 126 | + """Update instance details; this may restart the instance so warn the user""" |
| 127 | + compute_client = get_compute_client() |
| 128 | + |
| 129 | + shape_config_details = oci.core.models.UpdateInstanceShapeConfigDetails( |
| 130 | + ocpus=ocpus, memory_in_gbs=memory_in_gbs |
| 131 | + ) |
| 132 | + |
| 133 | + update_instance_details = oci.core.models.UpdateInstanceDetails( |
| 134 | + shape_config=shape_config_details |
| 135 | + ) |
| 136 | + |
| 137 | + try: |
| 138 | + compute_client.update_instance( |
| 139 | + instance_id=instance_id, update_instance_details=update_instance_details |
| 140 | + ) |
| 141 | + |
| 142 | + get_response = compute_client.get_instance(instance_id) |
| 143 | + |
| 144 | + final_response = oci.wait_until( |
| 145 | + client=compute_client, |
| 146 | + response=get_response, |
| 147 | + evaluate_response=lambda x: x.data.shape_config.ocpus == ocpus |
| 148 | + and x.data.shape_config.memory_in_gbs == memory_in_gbs, |
| 149 | + max_interval_seconds=5, |
| 150 | + max_wait_seconds=240, |
| 151 | + ) |
| 152 | + |
| 153 | + return { |
| 154 | + "instance_id": instance_id, |
| 155 | + "ocpus": final_response.data.shape_config.ocpus, |
| 156 | + "memory_in_gbs": final_response.data.shape_config.memory_in_gbs, |
| 157 | + } |
| 158 | + except oci.exceptions.ServiceError as e: |
| 159 | + return { |
| 160 | + "error_code": e.code, |
| 161 | + "message": e.message, |
| 162 | + } |
| 163 | + |
| 164 | + |
91 | 165 | if __name__ == "__main__": |
92 | 166 |
|
93 | 167 | # MCP spec: OpenAPI exposed at /openapi.json, native MCP at /mcp/v1 |
|
0 commit comments