mcp.discovery

Module exports.

Submodules

Classes

MCPServerAnalyzer

Analyzer for discovering and analyzing MCP servers.

MCPServerDiscovery

Placeholder for MCP server discovery.

Functions

create_mcp_config(→ dict[str, Any])

Create MCP configuration using a temporary instance.

get_discovery_report(→ dict[str, Any])

Get discovery report using a temporary instance.

Package Contents

class mcp.discovery.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:

bool

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:
  • **/*.json: JSON configuration files

  • **/*.yaml: YAML configuration files (requires PyYAML)

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:
  1. ~/.mcp/registry.json

  2. ~/.config/mcp/servers.json

  3. /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]
class mcp.discovery.MCPServerDiscovery

Placeholder for MCP server discovery.

This class provides basic structure for MCP server discovery functionality. In a complete implementation, it would scan various sources to find available MCP servers and their capabilities.

discovered_servers

Dictionary mapping server names to their metadata. Would contain server configurations, capabilities, and locations.

Example

Basic discovery usage (placeholder):

discovery = MCPServerDiscovery() servers = await discovery.discover_all() report = discovery.get_discovery_report()

create_mcp_config() dict[str, Any]

Create MCP configuration from discovered servers.

Converts discovered server information into a valid MCPConfig structure that can be used to initialize MCP agents.

Returns:

  • enabled: Whether MCP should be enabled

  • servers: Discovered server configurations

  • auto_discover: Discovery settings

Return type:

Dictionary representing MCPConfig with

Note

Currently returns empty config as placeholder. Real implementation would build proper configurations.

async discover_all() dict[str, Any]

Discover all available MCP servers.

Placeholder method that would scan all configured sources for MCP servers. In a full implementation, this would:

  • Query MCP server registries

  • Scan local installation directories

  • Parse configuration files

  • Check for environment-based servers

Returns:

Dictionary mapping server names to their discovery metadata. Currently returns empty dict as placeholder.

Note

This is a placeholder. Real implementation would perform actual discovery operations.

get_discovery_report() dict[str, Any]

Get a detailed report of discovered servers.

Generates a summary report of all discovered MCP servers, including counts, sources, and basic statistics.

Returns:

  • servers: Total count of discovered servers

  • sources: List of sources where servers were found

  • categories: Server categories discovered (future)

  • capabilities: Aggregate capabilities (future)

Return type:

Dictionary containing

Example

report = discovery.get_discovery_report() print(f”Found {report[‘servers’]} servers”)

discovered_servers
mcp.discovery.create_mcp_config() dict[str, Any]

Create MCP configuration using a temporary instance.

Convenience function that creates a temporary MCPServerDiscovery instance and generates an MCP configuration from discovered servers.

Returns:

MCP configuration dictionary ready for use with MCPConfig.

Example

config = create_mcp_config() mcp_config = MCPConfig(**config)

mcp.discovery.get_discovery_report() dict[str, Any]

Get discovery report using a temporary instance.

Convenience function that creates a temporary MCPServerDiscovery instance and returns its discovery report. Useful for one-off discovery operations.

Returns:

Discovery report dictionary with server counts and sources.

Example

report = get_discovery_report() if report[‘servers’] > 0:

print(“MCP servers available”)