Skip to content

Commit f03e16b

Browse files
author
LittleCoinCoin
committed
feat(kiro): implement KiroHostStrategy for configuration management
Implement KiroHostStrategy class with @register_host_strategy(MCPHostType.KIRO) decorator for automatic registration. Supports full CRUD operations: - get_config_path(): Returns ~/.kiro/settings/mcp.json (user-level only per constraint) - get_config_key(): Returns 'mcpServers' for consistency with other hosts - is_host_available(): Checks for .kiro/settings directory existence - validate_server_config(): Accepts both local (command) and remote (url) servers - read_configuration(): Reads and parses Kiro config with error handling - write_configuration(): Writes config while preserving non-MCP settings (read-modify-write pattern) Supports cross-platform path resolution and proper error handling with logging.
1 parent a505e28 commit f03e16b

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

hatch/mcp_host_config/strategies.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,87 @@ def write_configuration(self, config: HostConfiguration, no_backup: bool = False
409409
return False
410410

411411

412+
@register_host_strategy(MCPHostType.KIRO)
413+
class KiroHostStrategy(MCPHostStrategy):
414+
"""Configuration strategy for Kiro IDE."""
415+
416+
def get_config_path(self) -> Optional[Path]:
417+
"""Get Kiro configuration path (user-level only per constraint)."""
418+
return Path.home() / ".kiro" / "settings" / "mcp.json"
419+
420+
def get_config_key(self) -> str:
421+
"""Kiro uses 'mcpServers' key."""
422+
return "mcpServers"
423+
424+
def is_host_available(self) -> bool:
425+
"""Check if Kiro is available by checking for settings directory."""
426+
kiro_dir = Path.home() / ".kiro" / "settings"
427+
return kiro_dir.exists()
428+
429+
def validate_server_config(self, server_config: MCPServerConfig) -> bool:
430+
"""Kiro validation - supports both local and remote servers."""
431+
return server_config.command is not None or server_config.url is not None
432+
433+
def read_configuration(self) -> HostConfiguration:
434+
"""Read Kiro configuration file."""
435+
config_path = self.get_config_path()
436+
if not config_path or not config_path.exists():
437+
return HostConfiguration(servers={})
438+
439+
try:
440+
with open(config_path, 'r', encoding='utf-8') as f:
441+
data = json.load(f)
442+
443+
servers = {}
444+
mcp_servers = data.get(self.get_config_key(), {})
445+
446+
for name, config in mcp_servers.items():
447+
try:
448+
servers[name] = MCPServerConfig(**config)
449+
except Exception as e:
450+
logger.warning(f"Invalid server config for {name}: {e}")
451+
continue
452+
453+
return HostConfiguration(servers=servers)
454+
455+
except Exception as e:
456+
logger.error(f"Failed to read Kiro configuration: {e}")
457+
return HostConfiguration(servers={})
458+
459+
def write_configuration(self, config: HostConfiguration, no_backup: bool = False) -> bool:
460+
"""Write configuration to Kiro."""
461+
config_path = self.get_config_path()
462+
if not config_path:
463+
return False
464+
465+
try:
466+
# Ensure directory exists
467+
config_path.parent.mkdir(parents=True, exist_ok=True)
468+
469+
# Read existing configuration to preserve other settings
470+
existing_data = {}
471+
if config_path.exists():
472+
with open(config_path, 'r', encoding='utf-8') as f:
473+
existing_data = json.load(f)
474+
475+
# Update MCP servers section
476+
servers_data = {}
477+
for name, server_config in config.servers.items():
478+
servers_data[name] = server_config.model_dump(exclude_unset=True)
479+
480+
existing_data[self.get_config_key()] = servers_data
481+
482+
# Write updated configuration
483+
with open(config_path, 'w', encoding='utf-8') as f:
484+
json.dump(existing_data, f, indent=2)
485+
486+
return True
487+
488+
except Exception as e:
489+
logger.error(f"Failed to write Kiro configuration: {e}")
490+
return False
491+
492+
412493
@register_host_strategy(MCPHostType.GEMINI)
413494
class GeminiHostStrategy(MCPHostStrategy):
414495
"""Configuration strategy for Google Gemini CLI MCP integration."""

0 commit comments

Comments
 (0)