|
| 1 | +"""Claude Desktop/Code adapter for MCP host configuration. |
| 2 | +
|
| 3 | +Claude Desktop and Claude Code share the same configuration format: |
| 4 | +- Supports 'type' field for transport discrimination |
| 5 | +- Mutually exclusive: command XOR url (never both) |
| 6 | +- Standard field set: command, args, env, url, headers, type |
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Any, Dict, FrozenSet |
| 10 | + |
| 11 | +from hatch.mcp_host_config.adapters.base import AdapterValidationError, BaseAdapter |
| 12 | +from hatch.mcp_host_config.fields import CLAUDE_FIELDS |
| 13 | +from hatch.mcp_host_config.models import MCPServerConfig |
| 14 | + |
| 15 | + |
| 16 | +class ClaudeAdapter(BaseAdapter): |
| 17 | + """Adapter for Claude Desktop and Claude Code hosts. |
| 18 | + |
| 19 | + Claude uses a strict validation model: |
| 20 | + - Local servers: command (required), args, env |
| 21 | + - Remote servers: url (required), headers, env |
| 22 | + - Never both command and url |
| 23 | + |
| 24 | + Supports the 'type' field for explicit transport discrimination. |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, variant: str = "desktop"): |
| 28 | + """Initialize Claude adapter. |
| 29 | + |
| 30 | + Args: |
| 31 | + variant: Either "desktop" or "code" to specify the Claude variant. |
| 32 | + """ |
| 33 | + if variant not in ("desktop", "code"): |
| 34 | + raise ValueError(f"Invalid Claude variant: {variant}. Must be 'desktop' or 'code'") |
| 35 | + self._variant = variant |
| 36 | + |
| 37 | + @property |
| 38 | + def host_name(self) -> str: |
| 39 | + """Return the host identifier.""" |
| 40 | + return f"claude-{self._variant}" |
| 41 | + |
| 42 | + def get_supported_fields(self) -> FrozenSet[str]: |
| 43 | + """Return fields supported by Claude.""" |
| 44 | + return CLAUDE_FIELDS |
| 45 | + |
| 46 | + def validate(self, config: MCPServerConfig) -> None: |
| 47 | + """Validate configuration for Claude. |
| 48 | + |
| 49 | + Claude requires exactly one transport: |
| 50 | + - stdio (command) |
| 51 | + - sse (url) |
| 52 | + |
| 53 | + Having both command and url is invalid. |
| 54 | + """ |
| 55 | + has_command = config.command is not None |
| 56 | + has_url = config.url is not None |
| 57 | + has_http_url = config.httpUrl is not None |
| 58 | + |
| 59 | + # Claude doesn't support httpUrl |
| 60 | + if has_http_url: |
| 61 | + raise AdapterValidationError( |
| 62 | + "httpUrl is not supported (use 'url' for remote servers)", |
| 63 | + field="httpUrl", |
| 64 | + host_name=self.host_name |
| 65 | + ) |
| 66 | + |
| 67 | + # Must have exactly one transport |
| 68 | + if not has_command and not has_url: |
| 69 | + raise AdapterValidationError( |
| 70 | + "Either 'command' (local) or 'url' (remote) must be specified", |
| 71 | + host_name=self.host_name |
| 72 | + ) |
| 73 | + |
| 74 | + if has_command and has_url: |
| 75 | + raise AdapterValidationError( |
| 76 | + "Cannot specify both 'command' and 'url' - choose one transport", |
| 77 | + host_name=self.host_name |
| 78 | + ) |
| 79 | + |
| 80 | + # Validate type consistency if specified |
| 81 | + if config.type is not None: |
| 82 | + if config.type == "stdio" and not has_command: |
| 83 | + raise AdapterValidationError( |
| 84 | + "type='stdio' requires 'command' field", |
| 85 | + field="type", |
| 86 | + host_name=self.host_name |
| 87 | + ) |
| 88 | + if config.type in ("sse", "http") and not has_url: |
| 89 | + raise AdapterValidationError( |
| 90 | + f"type='{config.type}' requires 'url' field", |
| 91 | + field="type", |
| 92 | + host_name=self.host_name |
| 93 | + ) |
| 94 | + |
| 95 | + def serialize(self, config: MCPServerConfig) -> Dict[str, Any]: |
| 96 | + """Serialize configuration for Claude format. |
| 97 | + |
| 98 | + Returns a dictionary suitable for Claude's config.json format. |
| 99 | + """ |
| 100 | + # Validate before serializing |
| 101 | + self.validate(config) |
| 102 | + |
| 103 | + # Filter to supported fields |
| 104 | + return self.filter_fields(config) |
| 105 | + |
0 commit comments