|
| 1 | +"""Fallback server detection for stdio transport when initialize message is not available.""" |
| 2 | + |
| 3 | +import re |
| 4 | +from pathlib import Path |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | + |
| 8 | +def detect_server_from_command(command: list[str]) -> Optional[dict[str, str]]: |
| 9 | + """ |
| 10 | + Try to detect MCP server info from the command being executed. |
| 11 | +
|
| 12 | + This is a fallback when we don't have initialize message info. |
| 13 | +
|
| 14 | + Args: |
| 15 | + command: Command and arguments list |
| 16 | +
|
| 17 | + Returns: |
| 18 | + Dict with 'name' and 'version' if detected, None otherwise |
| 19 | + """ |
| 20 | + if not command: |
| 21 | + return None |
| 22 | + |
| 23 | + # Get the executable name |
| 24 | + exe = command[0] |
| 25 | + exe_name = Path(exe).name |
| 26 | + exe_path = Path(exe).stem # without extension |
| 27 | + |
| 28 | + # Check if running Python module with -m |
| 29 | + if exe_name in ['python', 'python3', 'python3.exe', 'python.exe']: |
| 30 | + # Look for -m module_name pattern |
| 31 | + for i, arg in enumerate(command[1:], 1): |
| 32 | + if arg == '-m' and i < len(command) - 1: |
| 33 | + module = command[i + 1] |
| 34 | + |
| 35 | + # Special case for mcphawk |
| 36 | + if module == 'mcphawk' and i + 2 < len(command) and command[i + 2] == 'mcp': |
| 37 | + return {'name': 'MCPHawk Query Server', 'version': 'unknown'} |
| 38 | + |
| 39 | + # Extract name from module |
| 40 | + name = extract_server_name(module) |
| 41 | + if name: |
| 42 | + return {'name': name, 'version': 'unknown'} |
| 43 | + |
| 44 | + # Check executable name |
| 45 | + name = extract_server_name(exe_path) |
| 46 | + if name: |
| 47 | + return {'name': name, 'version': 'unknown'} |
| 48 | + |
| 49 | + # Check for .py files in arguments |
| 50 | + for arg in command[1:]: |
| 51 | + if arg.endswith('.py'): |
| 52 | + script_name = Path(arg).stem |
| 53 | + name = extract_server_name(script_name) |
| 54 | + if name: |
| 55 | + return {'name': name, 'version': 'unknown'} |
| 56 | + |
| 57 | + return None |
| 58 | + |
| 59 | + |
| 60 | +def extract_server_name(text: str) -> Optional[str]: |
| 61 | + """ |
| 62 | + Extract a human-readable server name from various text patterns. |
| 63 | +
|
| 64 | + Args: |
| 65 | + text: Text to extract server name from (module name, exe name, etc) |
| 66 | +
|
| 67 | + Returns: |
| 68 | + Human-readable server name or None |
| 69 | + """ |
| 70 | + if not text or not isinstance(text, str): |
| 71 | + return None |
| 72 | + |
| 73 | + # Pattern 1: mcp-server-{name} or mcp_server_{name} |
| 74 | + match = re.match(r'^mcp[-_]server[-_](.+)$', text, re.IGNORECASE) |
| 75 | + if match: |
| 76 | + name_part = match.group(1) |
| 77 | + # Convert to title case, handling both - and _ |
| 78 | + words = re.split(r'[-_]', name_part) |
| 79 | + return f"MCP {' '.join(word.capitalize() for word in words)} Server" |
| 80 | + |
| 81 | + # Pattern 2: {name}-mcp-server or {name}_mcp_server |
| 82 | + match = re.match(r'^(.+?)[-_]mcp[-_]server$', text, re.IGNORECASE) |
| 83 | + if match: |
| 84 | + name_part = match.group(1) |
| 85 | + words = re.split(r'[-_]', name_part) |
| 86 | + return f"{' '.join(word.capitalize() for word in words)} MCP Server" |
| 87 | + |
| 88 | + # Pattern 3: mcp-{name} or mcp_{name} (but not mcp-server) |
| 89 | + match = re.match(r'^mcp[-_](.+)$', text, re.IGNORECASE) |
| 90 | + if match: |
| 91 | + name_part = match.group(1) |
| 92 | + # Skip if it's just "server" without additional parts |
| 93 | + if name_part.lower() == 'server': |
| 94 | + return None |
| 95 | + words = re.split(r'[-_]', name_part) |
| 96 | + return f"MCP {' '.join(word.capitalize() for word in words)}" |
| 97 | + |
| 98 | + # Pattern 4: {name}-mcp or {name}_mcp |
| 99 | + match = re.match(r'^(.+?)[-_]mcp$', text, re.IGNORECASE) |
| 100 | + if match: |
| 101 | + name_part = match.group(1) |
| 102 | + words = re.split(r'[-_]', name_part) |
| 103 | + return f"{' '.join(word.capitalize() for word in words)} MCP" |
| 104 | + |
| 105 | + # Pattern 5: contains 'mcp' somewhere |
| 106 | + if 'mcp' in text.lower(): |
| 107 | + # Clean up and format |
| 108 | + words = re.split(r'[-_]', text) |
| 109 | + # Filter out empty strings from split |
| 110 | + words = [w for w in words if w] |
| 111 | + formatted_words = [] |
| 112 | + for word in words: |
| 113 | + if word.lower() == 'mcp': |
| 114 | + formatted_words.append('MCP') |
| 115 | + else: |
| 116 | + formatted_words.append(word.capitalize()) |
| 117 | + return ' '.join(formatted_words) |
| 118 | + |
| 119 | + return None |
| 120 | + |
| 121 | + |
| 122 | +def merge_server_info( |
| 123 | + detected: Optional[dict[str, str]], |
| 124 | + from_protocol: Optional[dict[str, str]] |
| 125 | +) -> Optional[dict[str, str]]: |
| 126 | + """ |
| 127 | + Merge server info from command detection and protocol messages. |
| 128 | +
|
| 129 | + Protocol info takes precedence as it's more accurate. |
| 130 | +
|
| 131 | + Args: |
| 132 | + detected: Server info detected from command |
| 133 | + from_protocol: Server info from initialize response |
| 134 | +
|
| 135 | + Returns: |
| 136 | + Merged server info or None |
| 137 | + """ |
| 138 | + if from_protocol: |
| 139 | + return from_protocol |
| 140 | + return detected |
0 commit comments