1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*#![allow(dead_code)]
use std::io::Write;
use std::collections::HashMap;
use base64::prelude::BASE64_STANDARD;
use base64::Engine;
use actix_multipart::form::tempfile::TempFile;
use actix_multipart::form::MultipartForm;
use rmcp::{handler::server::{
wrapper::Parameters,
}, schemars, tool, tool_router, ErrorData};
use rmcp::model::{CallToolResult, ErrorCode};
use rmcp::schemars::JsonSchema;
use serde::Deserialize;
use serde_json::json;
use crate::controller::StoreType;
use crate::error::public_archive_error::PublicArchiveError;
use crate::service::public_archive_service::{PublicArchiveForm, Upload, ArchiveResponse};
use crate::tool::McpTool;
#[derive(Debug, Deserialize, JsonSchema)]
struct CreatePublicArchiveRequest {
#[schemars(description = "Base64 encoded content of the files to archive (map of filename to base64 content)")]
files: HashMap<String, String>,
#[schemars(description = "Optional shared target path (directory) for all files in the archive")]
path: Option<String>,
#[schemars(description = "Store archive on memory, disk or network")]
store_type: String,
}
#[derive(Debug, Deserialize, JsonSchema)]
struct UpdatePublicArchiveRequest {
#[schemars(description = "Address of the public archive")]
address: String,
#[schemars(description = "Base64 encoded content of the files to add to archive (map of filename to base64 content)")]
files: HashMap<String, String>,
#[schemars(description = "Optional shared target path (directory) for all files in the archive")]
path: Option<String>,
#[schemars(description = "Store archive on memory, disk or network")]
store_type: String,
}
#[derive(Debug, Deserialize, JsonSchema)]
struct TruncatePublicArchiveRequest {
#[schemars(description = "Address of the public archive")]
address: String,
#[schemars(description = "Path to directory or file within the archive to be deleted")]
path: String,
#[schemars(description = "Store archive on memory, disk or network")]
store_type: String,
}
#[derive(Debug, Deserialize, JsonSchema)]
struct PushPublicArchiveRequest {
#[schemars(description = "Address of the public archive to push from cache to target store type")]
address: String,
#[schemars(description = "Target store type: memory, disk or network (defaults to network)")]
store_type: String,
}
#[derive(Debug, Deserialize, JsonSchema)]
struct GetStatusPublicArchiveRequest {
#[schemars(description = "Id of upload")]
id: String,
}
impl From<ArchiveResponse> for CallToolResult {
fn from(res: ArchiveResponse) -> CallToolResult {
CallToolResult::structured(json!(res))
}
}
impl From<Upload> for CallToolResult {
fn from(upload: Upload) -> CallToolResult {
CallToolResult::structured(json!(upload))
}
}
impl From<PublicArchiveError> for ErrorData {
fn from(error: PublicArchiveError) -> Self {
ErrorData::new(ErrorCode::INTERNAL_ERROR, error.to_string(), None)
}
}
#[tool_router(router = public_archive_tool_router, vis = "pub")]
impl McpTool {
#[tool(description = "Create a new public archive")]
async fn create_public_archive(
&self,
Parameters(CreatePublicArchiveRequest { files, path, store_type }): Parameters<CreatePublicArchiveRequest>,
) -> Result<CallToolResult, ErrorData> {
let public_archive_form = self.map_to_multipart_form(files, None)?;
Ok(self.public_archive_service.create_public_archive(
path,
public_archive_form,
self.evm_wallet.get_ref().clone(),
StoreType::from(store_type)
).await?.into())
}
#[tool(description = "Update an existing public archive")]
async fn update_public_archive(
&self,
Parameters(UpdatePublicArchiveRequest { address, files, path, store_type }): Parameters<UpdatePublicArchiveRequest>,
) -> Result<CallToolResult, ErrorData> {
let public_archive_form = self.map_to_multipart_form(files, None)?;
Ok(self.public_archive_service.update_public_archive(
address,
path,
public_archive_form,
self.evm_wallet.get_ref().clone(),
StoreType::from(store_type)
).await?.into())
}
#[tool(description = "Truncate an existing public archive (delete file or directory)")]
async fn truncate_public_archive(
&self,
Parameters(TruncatePublicArchiveRequest { address, path, store_type }): Parameters<TruncatePublicArchiveRequest>,
) -> Result<CallToolResult, ErrorData> {
Ok(self.public_archive_service.truncate_public_archive(
address,
path,
self.evm_wallet.get_ref().clone(),
StoreType::from(store_type)
).await?.into())
}
#[tool(description = "Push a staged public archive from cache to a target store type (default: network)")]
async fn push_public_archive(
&self,
Parameters(PushPublicArchiveRequest { address, store_type }): Parameters<PushPublicArchiveRequest>,
) -> Result<CallToolResult, ErrorData> {
Ok(self.public_archive_service.push_public_archive(
address,
self.evm_wallet.get_ref().clone(),
StoreType::from(store_type)
).await?.into())
}
pub(crate) fn map_to_multipart_form(&self, files: HashMap<String, String>, _target_paths: Option<HashMap<String, String>>) -> Result<MultipartForm<PublicArchiveForm>, ErrorData> {
let mut temp_files = Vec::new();
for (name, content_base64) in files {
let content = BASE64_STANDARD.decode(content_base64).map_err(|e|
ErrorData::new(ErrorCode::INVALID_PARAMS, format!("Invalid base64 content for file {}: {}", name, e), None)
)?;
let mut temp_file = tempfile::NamedTempFile::new().map_err(|e|
ErrorData::new(ErrorCode::INTERNAL_ERROR, format!("Failed to create temp file: {}", e), None)
)?;
temp_file.write_all(&content).map_err(|e|
ErrorData::new(ErrorCode::INTERNAL_ERROR, format!("Failed to write to temp file: {}", e), None)
)?;
temp_files.push(TempFile {
file: temp_file,
file_name: Some(name),
content_type: None,
size: content.len(),
});
}
Ok(MultipartForm(PublicArchiveForm { files: temp_files }))
}
}
*/