@jorgeceballos/mcp-server-oci
Version:
Model Context Protocol server for Oracle Cloud Infrastructure
199 lines (198 loc) • 8.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OCITools = void 0;
const zod_1 = require("zod");
const client_1 = require("../oci/client");
class OCITools {
constructor(profileName = 'DEFAULT') {
/**
* List OCI compartments
*/
this.listCompartments = {
name: 'list_compartments',
description: 'Lists all compartments available in your Oracle Cloud Infrastructure account',
parameters: {},
handler: async () => {
try {
const compartments = await this.ociClient.listCompartments();
return {
content: [{
text: JSON.stringify(compartments.map(compartment => ({
id: compartment.id,
name: compartment.name,
description: compartment.description,
timeCreated: compartment.timeCreated
})), null, 2),
type: "text"
}]
};
}
catch (error) {
console.error('Error listing compartments:', error);
throw new Error(`Failed to list compartments: ${error.message}`);
}
}
};
/**
* List instances in a compartment
*/
this.listInstances = {
name: 'list_instances',
description: 'Lists all compute instances in a specific compartment',
parameters: {
compartmentId: zod_1.z.string().describe('The OCID of the compartment to list instances from')
},
handler: async ({ compartmentId }) => {
try {
const instances = await this.ociClient.listInstances(compartmentId);
return {
content: [{
text: JSON.stringify(instances.map(instance => ({
id: instance.id,
displayName: instance.displayName,
shape: instance.shape,
lifecycleState: instance.lifecycleState,
timeCreated: instance.timeCreated,
availabilityDomain: instance.availabilityDomain,
faultDomain: instance.faultDomain
})), null, 2),
type: "text"
}]
};
}
catch (error) {
console.error('Error listing instances:', error);
throw new Error(`Failed to list instances: ${error.message}`);
}
}
};
/**
* Get instance details
*/
this.getInstance = {
name: 'get_instance',
description: 'Gets detailed information about a specific compute instance',
parameters: {
instanceId: zod_1.z.string().describe('The OCID of the instance to get details for')
},
handler: async ({ instanceId }) => {
try {
const instance = await this.ociClient.getInstance(instanceId);
return {
content: [{
text: JSON.stringify({
id: instance.id,
displayName: instance.displayName,
shape: instance.shape,
lifecycleState: instance.lifecycleState,
timeCreated: instance.timeCreated,
availabilityDomain: instance.availabilityDomain,
faultDomain: instance.faultDomain,
region: instance.region,
shape_config: instance.shapeConfig,
metadata: instance.metadata,
freeformTags: instance.freeformTags,
definedTags: instance.definedTags
}, null, 2),
type: "text"
}]
};
}
catch (error) {
console.error('Error getting instance details:', error);
throw new Error(`Failed to get instance details: ${error.message}`);
}
}
};
/**
* Start an instance
*/
this.startInstance = {
name: 'start_instance',
description: 'Starts a stopped compute instance',
parameters: {
instanceId: zod_1.z.string().describe('The OCID of the instance to start')
},
handler: async ({ instanceId }) => {
try {
await this.ociClient.startInstance(instanceId);
return {
content: [{
text: `Instance ${instanceId} started successfully`,
type: "text"
}]
};
}
catch (error) {
console.error('Error starting instance:', error);
throw new Error(`Failed to start instance: ${error.message}`);
}
}
};
/**
* Stop an instance
*/
this.stopInstance = {
name: 'stop_instance',
description: 'Stops a running compute instance',
parameters: {
instanceId: zod_1.z.string().describe('The OCID of the instance to stop')
},
handler: async ({ instanceId }) => {
try {
await this.ociClient.stopInstance(instanceId);
return {
content: [{
text: `Instance ${instanceId} stopped successfully`,
type: "text"
}]
};
}
catch (error) {
console.error('Error stopping instance:', error);
throw new Error(`Failed to stop instance: ${error.message}`);
}
}
};
/**
* Restart an instance
*/
this.restartInstance = {
name: 'restart_instance',
description: 'Restarts a running compute instance',
parameters: {
instanceId: zod_1.z.string().describe('The OCID of the instance to restart')
},
handler: async ({ instanceId }) => {
try {
await this.ociClient.restartInstance(instanceId);
return {
content: [{
text: `Instance ${instanceId} restarted successfully`,
type: "text"
}]
};
}
catch (error) {
console.error('Error restarting instance:', error);
throw new Error(`Failed to restart instance: ${error.message}`);
}
}
};
this.ociClient = new client_1.OCIClient(profileName);
}
/**
* Get all tools as an array
*/
getAllTools() {
return [
this.listCompartments,
this.listInstances,
this.getInstance,
this.startInstance,
this.stopInstance,
this.restartInstance
];
}
}
exports.OCITools = OCITools;