mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-21 11:56:38 +00:00
feat: extend rustfs mcp with bucket creation and deletion (#416)
* feat: extend rustfs mcp with bucket creation and deletion * update file to fix pipeline error * change variable name to fix pipeline error
This commit is contained in:
@@ -54,6 +54,18 @@ pub struct UploadFileRequest {
|
||||
pub cache_control: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
||||
pub struct CreateBucketReqeust {
|
||||
#[schemars(description = "Name of the S3 bucket to create")]
|
||||
pub bucket_name: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
||||
pub struct DeleteBucketReqeust {
|
||||
#[schemars(description = "Name of the S3 bucket to delete")]
|
||||
pub bucket_name: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, JsonSchema)]
|
||||
pub struct GetObjectRequest {
|
||||
#[schemars(description = "Name of the S3 bucket")]
|
||||
@@ -110,6 +122,53 @@ impl RustfsMcpServer {
|
||||
})
|
||||
}
|
||||
|
||||
#[tool(description = "Create a new S3 bucket with the specified name")]
|
||||
pub async fn create_bucket(&self, Parameters(req): Parameters<CreateBucketReqeust>) -> String {
|
||||
info!("Executing create_bucket tool for bucket: {}", req.bucket_name);
|
||||
|
||||
match self.s3_client.create_bucket(&req.bucket_name).await {
|
||||
Ok(_) => {
|
||||
format!("Successfully created bucket: {}", req.bucket_name)
|
||||
}
|
||||
Err(e) => {
|
||||
format!("Failed to create bucket '{}': {:?}", req.bucket_name, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tool(description = "Delete an existing S3 bucket with the specified name")]
|
||||
pub async fn delete_bucket(&self, Parameters(req): Parameters<DeleteBucketReqeust>) -> String {
|
||||
info!("Executing delete_bucket tool for bucket: {}", req.bucket_name);
|
||||
|
||||
// check if bucket is empty, if not, can not delete bucket directly.
|
||||
let object_result = match self
|
||||
.s3_client
|
||||
.list_objects_v2(&req.bucket_name, ListObjectsOptions::default())
|
||||
.await
|
||||
{
|
||||
Ok(result) => result,
|
||||
Err(e) => {
|
||||
error!("Failed to list objects in bucket '{}': {:?}", req.bucket_name, e);
|
||||
return format!("Failed to list objects in bucket '{}': {:?}", req.bucket_name, e);
|
||||
}
|
||||
};
|
||||
|
||||
if !object_result.objects.is_empty() {
|
||||
error!("Bucket '{}' is not empty", req.bucket_name);
|
||||
return format!("Failed to delete bucket '{}': bucket is not empty", req.bucket_name);
|
||||
}
|
||||
|
||||
// delete the bucket.
|
||||
match self.s3_client.delete_bucket(&req.bucket_name).await {
|
||||
Ok(_) => {
|
||||
format!("Successfully deleted bucket: {}", req.bucket_name)
|
||||
}
|
||||
Err(e) => {
|
||||
format!("Failed to delete bucket '{}': {:?}", req.bucket_name, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tool(description = "List all S3 buckets accessible with the configured credentials")]
|
||||
pub async fn list_buckets(&self) -> String {
|
||||
info!("Executing list_buckets tool");
|
||||
@@ -667,4 +726,20 @@ mod tests {
|
||||
assert_eq!(read_mode_deser, GetObjectMode::Read);
|
||||
assert_eq!(download_mode_deser, GetObjectMode::Download);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bucket_creation() {
|
||||
let request = CreateBucketReqeust {
|
||||
bucket_name: "test-bucket".to_string(),
|
||||
};
|
||||
assert_eq!(request.bucket_name, "test-bucket");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bucket_deletion() {
|
||||
let request = DeleteBucketReqeust {
|
||||
bucket_name: "test-bucket".to_string(),
|
||||
};
|
||||
assert_eq!(request.bucket_name, "test-bucket");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user