import { DevOpsApiClient } from '../api/index.js';
/**
* Docker-related MCP tools
*/
export class DockerTools {
constructor(private client: DevOpsApiClient) {}
/**
* Search for Docker images by keyword
*/
async searchImages(keyword: string): Promise<string> {
try {
const response = await this.client.searchDockerImages(keyword);
if (!response.success || !response.data) {
return `Error: ${response.error || 'Failed to search Docker images'}`;
}
if (response.data.length === 0) {
return `No Docker images found for keyword: "${keyword}"`;
}
const results = response.data
.map((img, index) => {
const parts = [
`${index + 1}. ${img.name}`,
img.description ? ` Description: ${img.description}` : null,
img.official ? ` Official: Yes` : null,
img.stars !== undefined ? ` Stars: ${img.stars}` : null,
];
return parts.filter(Boolean).join('\n');
})
.join('\n\n');
return `Found ${response.data.length} Docker image(s) for "${keyword}":\n\n${results}`;
} catch (error) {
return `Error searching Docker images: ${error instanceof Error ? error.message : String(error)}`;
}
}
/**
* Get tags for a specific Docker image
*/
async getImageTags(imageName: string): Promise<string> {
try {
const response = await this.client.getDockerImageTags(imageName);
if (!response.success || !response.data) {
return `Error: ${response.error || 'Failed to get Docker image tags'}`;
}
if (response.data.length === 0) {
return `No tags found for Docker image: "${imageName}"`;
}
const results = response.data
.map((tag, index) => {
const parts = [
`${index + 1}. ${tag.name}`,
tag.size ? ` Size: ${this.formatBytes(tag.size)}` : null,
tag.lastUpdated ? ` Last Updated: ${tag.lastUpdated}` : null,
tag.digest ? ` Digest: ${tag.digest.substring(0, 20)}...` : null,
];
return parts.filter(Boolean).join('\n');
})
.join('\n\n');
return `Found ${response.data.length} tag(s) for "${imageName}":\n\n${results}`;
} catch (error) {
return `Error getting Docker image tags: ${error instanceof Error ? error.message : String(error)}`;
}
}
/**
* Format bytes to human-readable size
*/
private formatBytes(bytes: number): string {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
}
}