|
| 1 | +import argparse |
| 2 | +from logging import Logger |
| 3 | + |
| 4 | +import oci |
| 5 | +from fastmcp import FastMCP |
| 6 | + |
| 7 | +logger = Logger("oci_compute_mcp", level="INFO") |
| 8 | + |
| 9 | +mcp = FastMCP("oci_compute") |
| 10 | + |
| 11 | + |
| 12 | +def get_compute_client(): |
| 13 | + logger.info("entering get_compute_client") |
| 14 | + config = oci.config.from_file() |
| 15 | + return oci.core.ComputeClient(config) |
| 16 | + |
| 17 | + |
| 18 | +@mcp.tool |
| 19 | +def list_instances(compartment_id: str): |
| 20 | + compute = get_compute_client() |
| 21 | + instances = compute.list_instances(compartment_id).data |
| 22 | + return [ |
| 23 | + { |
| 24 | + "instance_id": inst.id, |
| 25 | + "display_name": inst.display_name, |
| 26 | + "lifecycle_state": inst.lifecycle_state, |
| 27 | + "shape": inst.shape, |
| 28 | + } |
| 29 | + for inst in instances |
| 30 | + ] |
| 31 | + |
| 32 | + |
| 33 | +@mcp.tool |
| 34 | +def launch_instance( |
| 35 | + compartment_id: str, |
| 36 | + display_name: str, |
| 37 | + availability_domain: str, |
| 38 | + shape: str, |
| 39 | + image_id: str, |
| 40 | + subnet_id: str, |
| 41 | +): |
| 42 | + compute = get_compute_client() |
| 43 | + launch_details = oci.core.models.LaunchInstanceDetails( |
| 44 | + compartment_id=compartment_id, |
| 45 | + display_name=display_name, |
| 46 | + availability_domain=availability_domain, |
| 47 | + shape=shape, |
| 48 | + image_id=image_id, |
| 49 | + subnet_id=subnet_id, |
| 50 | + ) |
| 51 | + instance = compute.launch_instance(launch_details).data |
| 52 | + return { |
| 53 | + "id": instance.id, |
| 54 | + "display_name": instance.display_name, |
| 55 | + "lifecycle_state": instance.lifecycle_state, |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +@mcp.tool |
| 60 | +def get_instance(instance_id: str): |
| 61 | + compute = get_compute_client() |
| 62 | + return compute.get_instance(instance_id).data |
| 63 | + |
| 64 | + |
| 65 | +# @mcp.tool |
| 66 | +# def terminate_instance(instance_id: str): |
| 67 | +# compute = get_compute_client() |
| 68 | +# response = compute.terminate_instance(instance_id) |
| 69 | +# return { |
| 70 | +# "status": "terminated", |
| 71 | +# "opc_request_id": response.headers.get("opc-request-id"), |
| 72 | +# } |
| 73 | + |
| 74 | + |
| 75 | +@mcp.tool |
| 76 | +def instance_action(instance_id: str, action: str): |
| 77 | + compute = get_compute_client() |
| 78 | + response = compute.instance_action(instance_id, action) |
| 79 | + return {"status": action, "opc_request_id": response.headers.get("opc-request-id")} |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + parser = argparse.ArgumentParser( |
| 84 | + description="Oracle Cloud Infrastructure Compute MCP server" |
| 85 | + ) |
| 86 | + parser.add_argument("port", type=int, help="port number") |
| 87 | + |
| 88 | + args = parser.parse_args() |
| 89 | + |
| 90 | + # MCP spec: OpenAPI exposed at /openapi.json, native MCP at /mcp/v1 |
| 91 | + # mcp.run(transport="http", host="127.0.0.1", port=8000, path="/mcp") |
| 92 | + |
| 93 | + mcp.run(transport="sse", host="127.0.0.1", port=args.port) |
0 commit comments