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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! MCP server installation handlers for different tools.
//!
//! This module provides a pluggable handler system for installing MCP servers
//! into different tools' configuration formats (Claude Code, OpenCode, etc.).
use crate::core::file_error::{FileOperation, FileResultExt};
use anyhow::{Context, Result};
use std::future::Future;
use std::path::Path;
use std::pin::Pin;
/// Trait for handling MCP server installation for different tools.
///
/// Each tool (claude-code, opencode, etc.) may have different ways
/// of managing MCP servers. This trait provides a common interface for:
/// - Reading MCP server configurations directly from source
/// - Merging configurations into tool-specific formats
pub trait McpHandler: Send + Sync {
/// Get the name of this MCP handler (e.g., "claude-code", "opencode").
fn name(&self) -> &str;
/// Configure MCP servers by reading directly from source and merging into config file.
///
/// This method reads MCP server configurations directly from source locations
/// (Git worktrees or local paths) and merges them into the tool's config file.
/// Patches from the manifest are applied to each server configuration before merging.
///
/// # Arguments
///
/// * `project_root` - The project root directory
/// * `artifact_base` - The base directory for this tool
/// * `lockfile_entries` - Locked MCP server resources with source information
/// * `cache` - Cache for accessing Git worktrees
/// * `manifest` - Manifest containing patch definitions
///
/// # Returns
///
/// `Ok((applied_patches, changed_count))` where:
/// - `applied_patches`: Vec<(name, AppliedPatches)> for each server
/// - `changed_count`: Number of servers that actually changed (ignoring timestamps)
#[allow(clippy::type_complexity)]
fn configure_mcp_servers(
&self,
project_root: &Path,
artifact_base: &Path,
lockfile_entries: &[crate::lockfile::LockedResource],
cache: &crate::cache::Cache,
manifest: &crate::manifest::Manifest,
) -> Pin<
Box<
dyn Future<
Output = Result<(
Vec<(String, crate::manifest::patches::AppliedPatches)>,
usize,
)>,
> + Send
+ '_,
>,
>;
/// Clean/remove all managed MCP servers for this handler.
///
/// # Arguments
///
/// * `project_root` - The project root directory
/// * `artifact_base` - The base directory for this tool
///
/// # Returns
///
/// `Ok(())` on success, or an error if the cleanup failed.
fn clean_mcp_servers(&self, project_root: &Path, artifact_base: &Path) -> Result<()>;
}
/// MCP handler for Claude Code.
///
/// Claude Code configures MCP servers directly in `.mcp.json` at project root
/// by reading from source locations (no intermediate file copying).
pub struct ClaudeCodeMcpHandler;
impl McpHandler for ClaudeCodeMcpHandler {
fn name(&self) -> &str {
"claude-code"
}
fn configure_mcp_servers(
&self,
project_root: &Path,
_artifact_base: &Path,
lockfile_entries: &[crate::lockfile::LockedResource],
cache: &crate::cache::Cache,
manifest: &crate::manifest::Manifest,
) -> Pin<
Box<
dyn Future<
Output = Result<(
Vec<(String, crate::manifest::patches::AppliedPatches)>,
usize,
)>,
> + Send
+ '_,
>,
> {
let project_root = project_root.to_path_buf();
let entries = lockfile_entries.to_vec();
let cache = cache.clone();
let manifest = manifest.clone();
Box::pin(async move {
if entries.is_empty() {
return Ok((Vec::new(), 0));
}
// Read MCP server configurations directly from source files
let mut mcp_servers: std::collections::HashMap<String, super::McpServerConfig> =
std::collections::HashMap::new();
let mut all_applied_patches = Vec::new();
for entry in &entries {
// Get the source file path
let source_path = if let Some(source_name) = &entry.source {
let url = entry
.url
.as_ref()
.ok_or_else(|| anyhow::anyhow!("MCP server {} has no URL", entry.name))?;
// Check if this is a local directory source
if entry.is_local() {
// Local directory source - use URL as path directly
std::path::PathBuf::from(url).join(&entry.path)
} else {
// Git-based source - get worktree
let sha = entry.resolved_commit.as_deref().ok_or_else(|| {
anyhow::anyhow!("MCP server {} missing resolved commit SHA", entry.name)
})?;
let worktree = cache
.get_or_create_worktree_for_sha(
source_name,
url,
sha,
Some(&entry.name),
)
.await?;
worktree.join(&entry.path)
}
} else {
// Local file - resolve relative to project root
let candidate = Path::new(&entry.path);
if candidate.is_absolute() {
candidate.to_path_buf()
} else {
project_root.join(candidate)
}
};
// Read the MCP server configuration as string first (for patch application)
let json_content =
tokio::fs::read_to_string(&source_path).await.with_file_context(
FileOperation::Read,
&source_path,
"reading MCP server file",
"mcp_handlers",
)?;
// Apply patches if present
let (patched_content, applied_patches) = {
// Look up patches for this MCP server
let lookup_name = entry.lookup_name();
let project_patches = manifest.project_patches.get("mcp-servers", lookup_name);
let private_patches = manifest.private_patches.get("mcp-servers", lookup_name);
if project_patches.is_some() || private_patches.is_some() {
use crate::manifest::patches::apply_patches_to_content_with_origin;
apply_patches_to_content_with_origin(
&json_content,
&source_path.display().to_string(),
project_patches.unwrap_or(&std::collections::BTreeMap::new()),
private_patches.unwrap_or(&std::collections::BTreeMap::new()),
)?
} else {
(json_content, crate::manifest::patches::AppliedPatches::default())
}
};
// Collect applied patches for this server
all_applied_patches.push((entry.name.clone(), applied_patches));
// Parse the patched JSON
let mut config: super::McpServerConfig = serde_json::from_str(&patched_content)
.with_context(|| {
format!("Failed to parse MCP server JSON from {}", source_path.display())
})?;
// Add AGPM metadata
config.agpm_metadata = Some(super::AgpmMetadata {
managed: true,
source: entry.source.clone(),
version: entry.version.clone(),
installed_at: chrono::Utc::now().to_rfc3339(),
dependency_name: Some(entry.name.clone()),
});
// Use lookup_name for the MCP server key (manifest alias if present, otherwise canonical name)
mcp_servers.insert(entry.lookup_name().to_string(), config);
}
// Configure MCP servers by merging into .mcp.json
let mcp_config_path = project_root.join(".mcp.json");
let changed_count = super::merge_mcp_servers(&mcp_config_path, mcp_servers).await?;
Ok((all_applied_patches, changed_count))
})
}
fn clean_mcp_servers(&self, project_root: &Path, _artifact_base: &Path) -> Result<()> {
// Use existing clean_mcp_servers function
super::clean_mcp_servers(project_root)
}
}
/// MCP handler for OpenCode.
///
/// OpenCode configures MCP servers directly in `.opencode/opencode.json`
/// by reading from source locations (no intermediate file copying).
pub struct OpenCodeMcpHandler;
impl McpHandler for OpenCodeMcpHandler {
fn name(&self) -> &str {
"opencode"
}
fn configure_mcp_servers(
&self,
project_root: &Path,
artifact_base: &Path,
lockfile_entries: &[crate::lockfile::LockedResource],
cache: &crate::cache::Cache,
manifest: &crate::manifest::Manifest,
) -> Pin<
Box<
dyn Future<
Output = Result<(
Vec<(String, crate::manifest::patches::AppliedPatches)>,
usize,
)>,
> + Send
+ '_,
>,
> {
let project_root = project_root.to_path_buf();
let artifact_base = artifact_base.to_path_buf();
let entries = lockfile_entries.to_vec();
let cache = cache.clone();
let manifest = manifest.clone();
Box::pin(async move {
if entries.is_empty() {
return Ok((Vec::new(), 0));
}
let mut all_applied_patches = Vec::new();
// Read MCP server configurations directly from source files
let mut mcp_servers: std::collections::HashMap<String, super::McpServerConfig> =
std::collections::HashMap::new();
for entry in &entries {
// Get the source file path
let source_path = if let Some(source_name) = &entry.source {
let url = entry
.url
.as_ref()
.ok_or_else(|| anyhow::anyhow!("MCP server {} has no URL", entry.name))?;
// Check if this is a local directory source
if entry.is_local() {
// Local directory source - use URL as path directly
std::path::PathBuf::from(url).join(&entry.path)
} else {
// Git-based source - get worktree
let sha = entry.resolved_commit.as_deref().ok_or_else(|| {
anyhow::anyhow!("MCP server {} missing resolved commit SHA", entry.name)
})?;
let worktree = cache
.get_or_create_worktree_for_sha(
source_name,
url,
sha,
Some(&entry.name),
)
.await?;
worktree.join(&entry.path)
}
} else {
// Local file - resolve relative to project root
let candidate = Path::new(&entry.path);
if candidate.is_absolute() {
candidate.to_path_buf()
} else {
project_root.join(candidate)
}
};
// Read the MCP server configuration as string first (for patch application)
let json_content =
tokio::fs::read_to_string(&source_path).await.with_file_context(
FileOperation::Read,
&source_path,
"reading MCP server file",
"mcp_handlers",
)?;
// Apply patches if present
let (patched_content, applied_patches) = {
// Look up patches for this MCP server
let lookup_name = entry.lookup_name();
let project_patches = manifest.project_patches.get("mcp-servers", lookup_name);
let private_patches = manifest.private_patches.get("mcp-servers", lookup_name);
if project_patches.is_some() || private_patches.is_some() {
use crate::manifest::patches::apply_patches_to_content_with_origin;
apply_patches_to_content_with_origin(
&json_content,
&source_path.display().to_string(),
project_patches.unwrap_or(&std::collections::BTreeMap::new()),
private_patches.unwrap_or(&std::collections::BTreeMap::new()),
)?
} else {
(json_content, crate::manifest::patches::AppliedPatches::default())
}
};
// Collect applied patches for this server
all_applied_patches.push((entry.name.clone(), applied_patches));
// Parse the patched JSON
let mut config: super::McpServerConfig = serde_json::from_str(&patched_content)
.with_context(|| {
format!("Failed to parse MCP server JSON from {}", source_path.display())
})?;
// Add AGPM metadata
config.agpm_metadata = Some(super::AgpmMetadata {
managed: true,
source: entry.source.clone(),
version: entry.version.clone(),
installed_at: chrono::Utc::now().to_rfc3339(),
dependency_name: Some(entry.name.clone()),
});
// Use lookup_name for the MCP server key (manifest alias if present, otherwise canonical name)
mcp_servers.insert(entry.lookup_name().to_string(), config);
}
// Load or create opencode.json
let opencode_config_path = artifact_base.join("opencode.json");
let mut opencode_config: serde_json::Value = if opencode_config_path.exists() {
crate::utils::read_json_file(&opencode_config_path).with_context(|| {
format!("Failed to read OpenCode config: {}", opencode_config_path.display())
})?
} else {
serde_json::json!({})
};
// Ensure opencode_config is an object
if !opencode_config.is_object() {
opencode_config = serde_json::json!({});
}
// Get or create "mcp" section
let config_obj = opencode_config
.as_object_mut()
.expect("opencode_config must be an object after is_object() check");
let mcp_section = config_obj.entry("mcp").or_insert_with(|| serde_json::json!({}));
// Count how many servers actually changed (ignoring timestamps)
let mut changed_count = 0;
if let Some(mcp_obj) = mcp_section.as_object() {
for (name, new_config) in &mcp_servers {
match mcp_obj.get(name) {
Some(existing_value) => {
// Server exists - check if it's actually different
if let Ok(existing_config) =
serde_json::from_value::<super::McpServerConfig>(
existing_value.clone(),
)
{
// Create copies without the timestamp for comparison
let mut existing_without_time = existing_config;
let mut new_without_time = new_config.clone();
// Remove timestamp from metadata for comparison
if let Some(ref mut meta) = existing_without_time.agpm_metadata {
meta.installed_at.clear();
}
if let Some(ref mut meta) = new_without_time.agpm_metadata {
meta.installed_at.clear();
}
if existing_without_time != new_without_time {
changed_count += 1;
}
} else {
// Different format - count as changed
changed_count += 1;
}
}
None => {
// New server - will be added
changed_count += 1;
}
}
}
} else {
// No existing MCP section - all servers are new
changed_count = mcp_servers.len();
}
// Merge MCP servers into the mcp section
if let Some(mcp_obj) = mcp_section.as_object_mut() {
for (name, server_config) in mcp_servers {
let server_json = serde_json::to_value(&server_config)?;
mcp_obj.insert(name, server_json);
}
}
// Save the updated configuration
crate::utils::write_json_file(&opencode_config_path, &opencode_config, true)
.with_context(|| {
format!("Failed to write OpenCode config: {}", opencode_config_path.display())
})?;
Ok((all_applied_patches, changed_count))
})
}
fn clean_mcp_servers(&self, _project_root: &Path, artifact_base: &Path) -> Result<()> {
let opencode_config_path = artifact_base.join("opencode.json");
let mcp_servers_dir = artifact_base.join("agpm").join("mcp-servers");
// Remove MCP server files from the staging directory
let mut removed_count = 0;
if mcp_servers_dir.exists() {
for entry in std::fs::read_dir(&mcp_servers_dir).with_file_context(
FileOperation::Read,
&mcp_servers_dir,
"reading MCP servers directory",
"mcp_handlers",
)? {
let entry = entry?;
let path = entry.path();
if path.extension().is_some_and(|ext| ext == "json") {
std::fs::remove_file(&path).with_file_context(
FileOperation::Write,
&path,
"removing MCP server file",
"mcp_handlers",
)?;
removed_count += 1;
}
}
}
// Clean up opencode.json by removing only AGPM-managed servers
if opencode_config_path.exists() {
let mut opencode_config: serde_json::Value =
crate::utils::read_json_file(&opencode_config_path).with_context(|| {
format!("Failed to read OpenCode config: {}", opencode_config_path.display())
})?;
if let Some(config_obj) = opencode_config.as_object_mut()
&& let Some(mcp_section) = config_obj.get_mut("mcp")
&& let Some(mcp_obj) = mcp_section.as_object_mut()
{
// Remove only AGPM-managed servers
mcp_obj.retain(|_name, server| {
// Try to parse as McpServerConfig to check metadata
if let Ok(config) =
serde_json::from_value::<super::McpServerConfig>(server.clone())
{
// Keep if not managed by AGPM
config.agpm_metadata.as_ref().is_none_or(|meta| !meta.managed)
} else {
// Keep if we can't parse it (preserve user data)
true
}
});
crate::utils::write_json_file(&opencode_config_path, &opencode_config, true)
.with_context(|| {
format!(
"Failed to write OpenCode config: {}",
opencode_config_path.display()
)
})?;
}
}
if removed_count > 0 {
println!("✓ Removed {removed_count} MCP server(s) from OpenCode");
} else {
println!("No MCP servers found to remove");
}
Ok(())
}
}
/// Concrete MCP handler enum for different tools.
///
/// This enum wraps all supported MCP handlers and provides a unified interface.
pub enum ConcreteMcpHandler {
/// Claude Code MCP handler
ClaudeCode(ClaudeCodeMcpHandler),
/// OpenCode MCP handler
OpenCode(OpenCodeMcpHandler),
}
impl McpHandler for ConcreteMcpHandler {
fn name(&self) -> &str {
match self {
Self::ClaudeCode(h) => h.name(),
Self::OpenCode(h) => h.name(),
}
}
fn configure_mcp_servers(
&self,
project_root: &Path,
artifact_base: &Path,
lockfile_entries: &[crate::lockfile::LockedResource],
cache: &crate::cache::Cache,
manifest: &crate::manifest::Manifest,
) -> Pin<
Box<
dyn Future<
Output = Result<(
Vec<(String, crate::manifest::patches::AppliedPatches)>,
usize,
)>,
> + Send
+ '_,
>,
> {
match self {
Self::ClaudeCode(h) => h.configure_mcp_servers(
project_root,
artifact_base,
lockfile_entries,
cache,
manifest,
),
Self::OpenCode(h) => h.configure_mcp_servers(
project_root,
artifact_base,
lockfile_entries,
cache,
manifest,
),
}
}
fn clean_mcp_servers(&self, project_root: &Path, artifact_base: &Path) -> Result<()> {
match self {
Self::ClaudeCode(h) => h.clean_mcp_servers(project_root, artifact_base),
Self::OpenCode(h) => h.clean_mcp_servers(project_root, artifact_base),
}
}
}
/// Get the appropriate MCP handler for a tool.
///
/// # Arguments
///
/// * `artifact_type` - The tool name (e.g., "claude-code", "opencode")
///
/// # Returns
///
/// An MCP handler for the given tool, or None if no handler exists.
pub fn get_mcp_handler(artifact_type: &str) -> Option<ConcreteMcpHandler> {
match artifact_type {
"claude-code" => Some(ConcreteMcpHandler::ClaudeCode(ClaudeCodeMcpHandler)),
"opencode" => Some(ConcreteMcpHandler::OpenCode(OpenCodeMcpHandler)),
_ => None, // Other tools don't have MCP support
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_mcp_handler_claude_code() {
let handler = get_mcp_handler("claude-code");
assert!(handler.is_some());
let handler = handler.unwrap();
assert_eq!(handler.name(), "claude-code");
}
#[test]
fn test_get_mcp_handler_opencode() {
let handler = get_mcp_handler("opencode");
assert!(handler.is_some());
let handler = handler.unwrap();
assert_eq!(handler.name(), "opencode");
}
#[test]
fn test_get_mcp_handler_unknown() {
let handler = get_mcp_handler("unknown");
assert!(handler.is_none());
}
#[test]
fn test_get_mcp_handler_agpm() {
// AGPM doesn't support MCP servers
let handler = get_mcp_handler("agpm");
assert!(handler.is_none());
}
}