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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use clap::{Parser, Subcommand};
use claude_utils::{
clipboard::{ClipboardManager, watcher::ClipboardWatcher, processor::{ClipboardProcessor, ProcessorConfig}},
file_manager::{FileManager, FileManagerConfig},
mcp::{auth::{AuthManager, AuthConfig}, server::McpServer},
Result, DEFAULT_HOST, DEFAULT_PORT,
};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tracing::{info, error};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[derive(Parser)]
#[command(
name = "claude-utils",
about = "Cross-platform companion toolkit for Claude Code",
version,
author
)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Start the clipboard daemon
Start {
/// Port to listen on
#[arg(short, long, default_value_t = DEFAULT_PORT)]
port: u16,
/// Host to bind to
#[arg(short = 'H', long, default_value = DEFAULT_HOST)]
host: String,
/// Disable authentication
#[arg(long)]
no_auth: bool,
/// Custom staging directory
#[arg(long)]
staging_dir: Option<PathBuf>,
/// Allow clipboard write operations
#[arg(long)]
write: bool,
/// Enable clipboard watching mode
#[arg(short, long)]
watch: bool,
/// Custom symlink directory (default: ~/Desktop)
#[arg(long)]
symlink_dir: Option<PathBuf>,
/// Disable dual-format clipboard (path + image)
#[arg(long)]
no_dual_format: bool,
/// Disable notifications
#[arg(long)]
no_notifications: bool,
},
/// Show authentication token
Token,
/// Generate MCP configuration
Config {
/// Output path for .mcp.json
#[arg(short, long)]
output: Option<PathBuf>,
},
/// Quick clipboard operations
Clip {
#[command(subcommand)]
action: ClipAction,
},
}
#[derive(Subcommand)]
enum ClipAction {
/// Get current clipboard content
Get {
/// Output format (json, text)
#[arg(short, long, default_value = "json")]
format: String,
},
/// Paste clipboard content (outputs path if image)
Paste,
}
#[tokio::main]
async fn main() -> Result<()> {
// Initialize logging
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "claude_utils=info".into()),
)
.with(tracing_subscriber::fmt::layer())
.init();
let cli = Cli::parse();
match cli.command {
Commands::Start {
port,
host,
no_auth,
staging_dir,
write,
watch,
symlink_dir,
no_dual_format,
no_notifications,
} => {
info!("Starting Claude-Utils clipboard daemon...");
// Initialize components
let clipboard = Arc::new(ClipboardManager::new()?);
let file_config = if let Some(dir) = staging_dir {
FileManagerConfig {
staging_dir: dir,
..Default::default()
}
} else {
FileManagerConfig::default()
};
let file_manager = Arc::new(FileManager::new(file_config).await?);
let auth_config = AuthConfig {
require_auth: !no_auth,
..Default::default()
};
let auth_manager = AuthManager::new(auth_config).await?;
if let Some(token) = auth_manager.get_token().await {
info!("Authentication token: {}", token);
info!("Set CLAUDE_UTILS_TOKEN={} in your environment", token);
}
// Start clipboard watcher if enabled
if watch {
info!("Clipboard watching enabled");
let processor_config = ProcessorConfig {
symlink_dir: symlink_dir.unwrap_or_else(|| {
dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("Desktop")
}),
enable_dual_format: !no_dual_format,
enable_notifications: !no_notifications,
..Default::default()
};
let (watcher, event_rx) = ClipboardWatcher::new(
clipboard.clone(),
Duration::from_millis(500), // Poll every 500ms
);
let processor = ClipboardProcessor::new(
processor_config,
file_manager.clone(),
clipboard.clone(),
);
// Spawn watcher task
tokio::spawn(async move {
watcher.start_watching().await;
});
// Spawn processor task
tokio::spawn(async move {
processor.start_processing(event_rx).await;
});
info!("Clipboard watcher started");
info!("Images will be saved to Desktop with dual-format clipboard");
}
// Start server
let server = McpServer::new(
clipboard,
file_manager,
auth_manager,
port,
host.clone(),
).await?;
info!("Starting MCP server on {}:{}", host, port);
if write {
info!("Write operations enabled");
}
server.run().await?;
}
Commands::Token => {
let auth_manager = AuthManager::new(AuthConfig::default()).await?;
if let Some(token) = auth_manager.get_token().await {
println!("{}", token);
} else {
error!("No authentication token found");
std::process::exit(1);
}
}
Commands::Config { output } => {
let config = serde_json::json!({
"claude-utils": {
"command": "claude-utils",
"args": ["start"],
"env": {
"CLAUDE_UTILS_TOKEN": "${CLAUDE_UTILS_TOKEN}"
}
}
});
let config_str = serde_json::to_string_pretty(&config)?;
if let Some(path) = output {
std::fs::write(&path, config_str)?;
info!("MCP configuration written to {}", path.display());
} else {
println!("{}", config_str);
}
}
Commands::Clip { action } => {
let clipboard = ClipboardManager::new()?;
match action {
ClipAction::Get { format } => {
let content = clipboard.get_content()?;
match format.as_str() {
"json" => {
println!("{}", serde_json::to_string_pretty(&content)?);
}
"text" => {
match &content.content {
claude_utils::clipboard::ClipboardContent::Text { data, .. } => {
println!("{}", data);
}
_ => {
println!("[Image in clipboard]");
}
}
}
_ => {
error!("Unknown format: {}", format);
std::process::exit(1);
}
}
}
ClipAction::Paste => {
let content = clipboard.get_content()?;
match &content.content {
claude_utils::clipboard::ClipboardContent::Text { data, .. } => {
print!("{}", data);
}
claude_utils::clipboard::ClipboardContent::ImagePng { .. } |
claude_utils::clipboard::ClipboardContent::ImageJpeg { .. } => {
// Stage image and output path
let file_manager = FileManager::new(FileManagerConfig::default()).await?;
let image_data = clipboard.get_raw_image()?;
let staged = file_manager.stage_image(&image_data, "png").await?;
print!("{}", staged.path.display());
}
}
}
}
}
}
Ok(())
}