mcp.discovery.analyzer¶
MCP server analyzer for component discovery integration.
This module provides analysis capabilities for discovering and configuring MCP servers from various sources. It can analyze dictionaries, objects, and files to extract valid MCP server configurations.
- The analyzer supports:
Dictionary-based configurations (JSON/YAML)
Object attribute extraction
Directory scanning for config files
Registry file parsing
Component info generation for discovery system
- Classes:
MCPServerAnalyzer: Main analyzer for MCP server discovery
Examples
Analyzing server configurations:
from haive.mcp.discovery import MCPServerAnalyzer
analyzer = MCPServerAnalyzer()
# Analyze a dictionary config
config_dict = {
"name": "my_server",
"command": "npx",
"args": ["-y", "@myorg/mcp-server"],
"capabilities": ["data_access"]
}
server_config = analyzer.analyze(config_dict)
if server_config:
print(f"Found server: {server_config.name}")
# Discover from directory
configs = analyzer.discover_from_directory(Path("./mcp_configs"))
print(f"Discovered {len(configs)} servers")
Note
The analyzer is designed to be flexible and handle various configuration formats commonly used for MCP servers.
Attributes¶
Classes¶
Analyzer for discovering and analyzing MCP servers. |
Module Contents¶
- class mcp.discovery.analyzer.MCPServerAnalyzer¶
Analyzer for discovering and analyzing MCP servers.
MCPServerAnalyzer provides comprehensive analysis capabilities for MCP server configurations. It can identify MCP servers from various sources and convert them to standardized MCPServerConfig instances.
The analyzer integrates with haive-core’s component discovery system to automatically find and register MCP servers. It supports multiple configuration formats and can extract server information from objects, dictionaries, and files.
- discovered_servers¶
Dictionary of servers found during analysis
Examples
Basic server analysis:
analyzer = MCPServerAnalyzer() # Check if object can be analyzed if analyzer.can_analyze(my_object): config = analyzer.analyze(my_object) if config: print(f"Server: {config.name}") print(f"Transport: {config.transport}") print(f"Capabilities: {config.capabilities}")
- analyze(obj: Any, source: str | None = None) haive.mcp.config.MCPServerConfig | None ¶
Analyze an object and extract MCP server configuration.
Attempts to extract a valid MCPServerConfig from various object types. Supports dictionaries, MCPServerConfig instances, objects with conversion methods, and arbitrary objects with MCP attributes.
- Parameters:
obj – Object to analyze. Can be: - Dictionary with MCP configuration - MCPServerConfig instance - Object with to_mcp_config() method - Any object with MCP-related attributes
source – Optional source identifier (file path, registry key, etc.) Used for naming and debugging.
- Returns:
- Extracted configuration if successful,
None if the object cannot be converted to a valid config.
- Return type:
Optional[MCPServerConfig]
Examples
Analyzing different object types:
- can_analyze(obj: Any) bool ¶
Check if an object is an MCP server or configuration.
Determines whether an object appears to be an MCP server or configuration by checking for characteristic attributes and patterns.
- Detection criteria:
Dictionary with MCP config keys (command, url, transport)
Class name containing “MCPServer” or “MCP”
Object with MCP-related attributes
- Parameters:
obj – Object to check for MCP characteristics
- Returns:
True if the object appears to be MCP-related
- Return type:
Examples
Checking various objects:
# Dictionary config config = {"command": "npx", "args": [...]} assert analyzer.can_analyze(config) == True # Non-MCP object assert analyzer.can_analyze({"foo": "bar"}) == False # MCP server instance server = MCPServer(...) assert analyzer.can_analyze(server) == True
- create_component_info(server_config: haive.mcp.config.MCPServerConfig) dict[str, Any] ¶
Create component info for registration with component discovery.
- discover_from_directory(directory: pathlib.Path) list[haive.mcp.config.MCPServerConfig] ¶
Discover MCP server configurations from a directory.
Recursively searches a directory for MCP server configuration files. Supports JSON and YAML formats (if PyYAML is available).
- File patterns:
- Configuration formats:
Single server: {“command”: “…”, “capabilities”: […]}
Multiple servers: [{…}, {…}, …]
- Parameters:
directory – Directory path to search recursively
- Returns:
All valid configurations found
- Return type:
List[MCPServerConfig]
Examples
Discovering from a config directory:
Note
Invalid files are skipped with debug logging. YAML support is optional and fails gracefully.
- discover_from_registry(registry_path: pathlib.Path | None = None) list[haive.mcp.config.MCPServerConfig] ¶
Discover MCP servers from a registry file.
Loads MCP server configurations from a JSON registry file. If no path is provided, checks standard registry locations.
- Registry format:
- {
- “servers”: {
- “server_name”: {
“command”: “…”, “capabilities”: […]
}
}
}
- Default locations checked:
~/.mcp/registry.json
~/.config/mcp/servers.json
/etc/mcp/servers.json
- Parameters:
registry_path – Optional path to registry file. If not provided, searches default locations.
- Returns:
Configurations from the registry
- Return type:
List[MCPServerConfig]
Examples
Using a custom registry:
- discovered_servers: dict[str, haive.mcp.config.MCPServerConfig]¶
- mcp.discovery.analyzer.logger¶