Skip to content

Commit a00e8b2

Browse files
committed
add update and image tools to compute server (oracle#14)
Signed-off-by: Richard Gebhardt <[email protected]>
1 parent 6c0a3d0 commit a00e8b2

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

oci_compute.py

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from logging import Logger
3+
from typing import Annotated
34

45
import oci
56
from fastmcp import FastMCP
@@ -44,9 +45,9 @@ def launch_instance(
4445
compartment_id: str,
4546
display_name: str,
4647
availability_domain: str,
47-
shape: str,
4848
image_id: str,
4949
subnet_id: str,
50+
shape: Annotated[str, "Instance shape"] = "VM.Standard.A1.Flex",
5051
):
5152
compute = get_compute_client()
5253
launch_details = oci.core.models.LaunchInstanceDetails(
@@ -81,13 +82,86 @@ def get_instance(instance_id: str):
8182
# }
8283

8384

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+
84111
@mcp.tool
85112
def instance_action(instance_id: str, action: str):
86113
compute = get_compute_client()
87114
response = compute.instance_action(instance_id, action)
88115
return {"status": action, "opc_request_id": response.headers.get("opc-request-id")}
89116

90117

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+
91165
if __name__ == "__main__":
92166

93167
# MCP spec: OpenAPI exposed at /openapi.json, native MCP at /mcp/v1

0 commit comments

Comments
 (0)