Factory System
The factory system provides a centralized, reflection-based approach to creating instances of both Target
and ResponseGenerator
from configuration.
Overview
The new factory system is located in src/mcp_kit/factory.py
and replaces the previous manual factory methods with a more generic, reflection-based approach.
Core Components
create_object_from_config()
The generic factory function that can create any object type from configuration using reflection.
Parameters:
config
: Configuration object with a 'type' fieldget_class_name
: Function that converts type string to class nameget_module_name
: Function that converts type string to module nameobject_type_name
: Name of object type for error messages
create_target_from_config()
Factory function specifically for creating Target
instances.
Example:
from src.mcp_kit.factory import create_target_from_config
config = {
"type": "mcp",
"name": "example-server",
"url": "http://localhost:8080/mcp"
}
target = create_target_from_config(config)
create_response_generator_from_config()
Factory function specifically for creating ResponseGenerator
instances.
Example:
from src.mcp_kit.factory import create_response_generator_from_config
config = {
"type": "llm",
"model": "gpt-4"
}
generator = create_response_generator_from_config(config)
Naming Conventions
Targets
- Configuration type:
"mcp"
→ Class:McpTarget
→ Module:src.mcp_kit.targets.mcp_target
- Configuration type:
"oas"
→ Class:OasTarget
→ Module:src.mcp_kit.targets.oas_target
- Configuration type:
"mocked"
→ Class:MockedTarget
→ Module:src.mcp_kit.targets.mocked_target
Response Generators
- Configuration type:
"random"
→ Class:RandomResponseGenerator
→ Module:src.mcp_kit.generators
- Configuration type:
"llm"
→ Class:LLMResponseGenerator
→ Module:src.mcp_kit.generators
Benefits
- Eliminates Manual Factories: No need to manually update factory methods when adding new types
- Consistent Error Messages: Standardized error handling across all object types
- Type Safety: Proper type hints and runtime checking
- Extensibility: Easy to add new object types without modifying existing factory code
- Reduced Circular Imports: Centralized factory prevents circular import issues
Migration
The refactoring maintains backward compatibility:
- All existing
from_config()
methods continue to work - Import paths have been updated from
src.mcp_kit.targets.factory
tosrc.mcp_kit.factory
- Error messages have been standardized but tests have been updated accordingly
Adding New Types
To add a new target type:
- Create the class following the naming convention (e.g.,
NewTarget
) - Implement the
from_config()
class method - Place it in the appropriate module (e.g.,
src.mcp_kit.targets.new_target
) - The factory will automatically discover and instantiate it
To add a new generator type:
- Create the class following the naming convention (e.g.,
NewResponseGenerator
) - Implement the
from_config()
class method - Place it in
src.mcp_kit.generators
- The factory will automatically discover and instantiate it