mcp-proxy-adapter 4.1.1__py3-none-any.whl → 6.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (200) hide show
  1. mcp_proxy_adapter/__main__.py +32 -0
  2. mcp_proxy_adapter/api/app.py +290 -33
  3. mcp_proxy_adapter/api/handlers.py +32 -6
  4. mcp_proxy_adapter/api/middleware/__init__.py +38 -32
  5. mcp_proxy_adapter/api/middleware/command_permission_middleware.py +148 -0
  6. mcp_proxy_adapter/api/middleware/error_handling.py +9 -0
  7. mcp_proxy_adapter/api/middleware/factory.py +243 -0
  8. mcp_proxy_adapter/api/middleware/logging.py +32 -6
  9. mcp_proxy_adapter/api/middleware/protocol_middleware.py +201 -0
  10. mcp_proxy_adapter/api/middleware/transport_middleware.py +122 -0
  11. mcp_proxy_adapter/api/middleware/unified_security.py +197 -0
  12. mcp_proxy_adapter/api/middleware/user_info_middleware.py +158 -0
  13. mcp_proxy_adapter/commands/__init__.py +19 -4
  14. mcp_proxy_adapter/commands/auth_validation_command.py +408 -0
  15. mcp_proxy_adapter/commands/base.py +66 -32
  16. mcp_proxy_adapter/commands/builtin_commands.py +95 -0
  17. mcp_proxy_adapter/commands/catalog_manager.py +838 -0
  18. mcp_proxy_adapter/commands/cert_monitor_command.py +620 -0
  19. mcp_proxy_adapter/commands/certificate_management_command.py +608 -0
  20. mcp_proxy_adapter/commands/command_registry.py +711 -354
  21. mcp_proxy_adapter/commands/dependency_manager.py +245 -0
  22. mcp_proxy_adapter/commands/echo_command.py +81 -0
  23. mcp_proxy_adapter/commands/health_command.py +8 -1
  24. mcp_proxy_adapter/commands/help_command.py +21 -14
  25. mcp_proxy_adapter/commands/hooks.py +200 -167
  26. mcp_proxy_adapter/commands/key_management_command.py +506 -0
  27. mcp_proxy_adapter/commands/load_command.py +176 -0
  28. mcp_proxy_adapter/commands/plugins_command.py +235 -0
  29. mcp_proxy_adapter/commands/protocol_management_command.py +232 -0
  30. mcp_proxy_adapter/commands/proxy_registration_command.py +409 -0
  31. mcp_proxy_adapter/commands/reload_command.py +48 -50
  32. mcp_proxy_adapter/commands/result.py +1 -0
  33. mcp_proxy_adapter/commands/role_test_command.py +141 -0
  34. mcp_proxy_adapter/commands/roles_management_command.py +697 -0
  35. mcp_proxy_adapter/commands/security_command.py +488 -0
  36. mcp_proxy_adapter/commands/ssl_setup_command.py +366 -0
  37. mcp_proxy_adapter/commands/token_management_command.py +529 -0
  38. mcp_proxy_adapter/commands/transport_management_command.py +144 -0
  39. mcp_proxy_adapter/commands/unload_command.py +158 -0
  40. mcp_proxy_adapter/config.py +394 -14
  41. mcp_proxy_adapter/core/app_factory.py +410 -0
  42. mcp_proxy_adapter/core/app_runner.py +272 -0
  43. mcp_proxy_adapter/core/auth_validator.py +606 -0
  44. mcp_proxy_adapter/core/certificate_utils.py +1045 -0
  45. mcp_proxy_adapter/core/client.py +574 -0
  46. mcp_proxy_adapter/core/client_manager.py +284 -0
  47. mcp_proxy_adapter/core/client_security.py +384 -0
  48. mcp_proxy_adapter/core/config_converter.py +405 -0
  49. mcp_proxy_adapter/core/config_validator.py +218 -0
  50. mcp_proxy_adapter/core/logging.py +19 -3
  51. mcp_proxy_adapter/core/mtls_asgi.py +156 -0
  52. mcp_proxy_adapter/core/mtls_asgi_app.py +187 -0
  53. mcp_proxy_adapter/core/protocol_manager.py +385 -0
  54. mcp_proxy_adapter/core/proxy_client.py +602 -0
  55. mcp_proxy_adapter/core/proxy_registration.py +522 -0
  56. mcp_proxy_adapter/core/role_utils.py +426 -0
  57. mcp_proxy_adapter/core/security_adapter.py +370 -0
  58. mcp_proxy_adapter/core/security_factory.py +239 -0
  59. mcp_proxy_adapter/core/security_integration.py +286 -0
  60. mcp_proxy_adapter/core/server_adapter.py +282 -0
  61. mcp_proxy_adapter/core/server_engine.py +270 -0
  62. mcp_proxy_adapter/core/settings.py +1 -0
  63. mcp_proxy_adapter/core/ssl_utils.py +234 -0
  64. mcp_proxy_adapter/core/transport_manager.py +292 -0
  65. mcp_proxy_adapter/core/unified_config_adapter.py +579 -0
  66. mcp_proxy_adapter/custom_openapi.py +22 -11
  67. mcp_proxy_adapter/examples/__init__.py +13 -4
  68. mcp_proxy_adapter/examples/basic_framework/__init__.py +9 -0
  69. mcp_proxy_adapter/examples/basic_framework/commands/__init__.py +4 -0
  70. mcp_proxy_adapter/examples/basic_framework/hooks/__init__.py +4 -0
  71. mcp_proxy_adapter/examples/basic_framework/main.py +44 -0
  72. mcp_proxy_adapter/examples/commands/__init__.py +5 -0
  73. mcp_proxy_adapter/examples/create_certificates_simple.py +550 -0
  74. mcp_proxy_adapter/examples/debug_request_state.py +112 -0
  75. mcp_proxy_adapter/examples/debug_role_chain.py +158 -0
  76. mcp_proxy_adapter/examples/demo_client.py +275 -0
  77. mcp_proxy_adapter/examples/examples/basic_framework/__init__.py +9 -0
  78. mcp_proxy_adapter/examples/examples/basic_framework/commands/__init__.py +4 -0
  79. mcp_proxy_adapter/examples/examples/basic_framework/hooks/__init__.py +4 -0
  80. mcp_proxy_adapter/examples/examples/basic_framework/main.py +44 -0
  81. mcp_proxy_adapter/examples/examples/full_application/__init__.py +12 -0
  82. mcp_proxy_adapter/examples/examples/full_application/commands/__init__.py +7 -0
  83. mcp_proxy_adapter/examples/examples/full_application/commands/custom_echo_command.py +80 -0
  84. mcp_proxy_adapter/examples/examples/full_application/commands/dynamic_calculator_command.py +90 -0
  85. mcp_proxy_adapter/examples/examples/full_application/hooks/__init__.py +7 -0
  86. mcp_proxy_adapter/examples/examples/full_application/hooks/application_hooks.py +75 -0
  87. mcp_proxy_adapter/examples/examples/full_application/hooks/builtin_command_hooks.py +71 -0
  88. mcp_proxy_adapter/examples/examples/full_application/main.py +173 -0
  89. mcp_proxy_adapter/examples/examples/full_application/proxy_endpoints.py +154 -0
  90. mcp_proxy_adapter/examples/full_application/__init__.py +12 -0
  91. mcp_proxy_adapter/examples/full_application/commands/__init__.py +7 -0
  92. mcp_proxy_adapter/examples/full_application/commands/custom_echo_command.py +80 -0
  93. mcp_proxy_adapter/examples/full_application/commands/dynamic_calculator_command.py +90 -0
  94. mcp_proxy_adapter/examples/full_application/hooks/__init__.py +7 -0
  95. mcp_proxy_adapter/examples/full_application/hooks/application_hooks.py +75 -0
  96. mcp_proxy_adapter/examples/full_application/hooks/builtin_command_hooks.py +71 -0
  97. mcp_proxy_adapter/examples/full_application/main.py +173 -0
  98. mcp_proxy_adapter/examples/full_application/proxy_endpoints.py +154 -0
  99. mcp_proxy_adapter/examples/generate_all_certificates.py +362 -0
  100. mcp_proxy_adapter/examples/generate_certificates.py +177 -0
  101. mcp_proxy_adapter/examples/generate_certificates_and_tokens.py +369 -0
  102. mcp_proxy_adapter/examples/generate_test_configs.py +331 -0
  103. mcp_proxy_adapter/examples/proxy_registration_example.py +334 -0
  104. mcp_proxy_adapter/examples/run_example.py +59 -0
  105. mcp_proxy_adapter/examples/run_full_test_suite.py +318 -0
  106. mcp_proxy_adapter/examples/run_proxy_server.py +146 -0
  107. mcp_proxy_adapter/examples/run_security_tests.py +544 -0
  108. mcp_proxy_adapter/examples/run_security_tests_fixed.py +247 -0
  109. mcp_proxy_adapter/examples/scripts/config_generator.py +740 -0
  110. mcp_proxy_adapter/examples/scripts/create_certificates_simple.py +560 -0
  111. mcp_proxy_adapter/examples/scripts/generate_certificates_and_tokens.py +369 -0
  112. mcp_proxy_adapter/examples/security_test_client.py +782 -0
  113. mcp_proxy_adapter/examples/setup_test_environment.py +328 -0
  114. mcp_proxy_adapter/examples/test_config.py +148 -0
  115. mcp_proxy_adapter/examples/test_config_generator.py +86 -0
  116. mcp_proxy_adapter/examples/test_examples.py +281 -0
  117. mcp_proxy_adapter/examples/universal_client.py +620 -0
  118. mcp_proxy_adapter/main.py +93 -0
  119. mcp_proxy_adapter/utils/config_generator.py +1008 -0
  120. mcp_proxy_adapter/version.py +5 -2
  121. mcp_proxy_adapter-6.0.1.dist-info/METADATA +679 -0
  122. mcp_proxy_adapter-6.0.1.dist-info/RECORD +140 -0
  123. mcp_proxy_adapter-6.0.1.dist-info/entry_points.txt +2 -0
  124. {mcp_proxy_adapter-4.1.1.dist-info → mcp_proxy_adapter-6.0.1.dist-info}/licenses/LICENSE +2 -2
  125. mcp_proxy_adapter/api/middleware/auth.py +0 -146
  126. mcp_proxy_adapter/api/middleware/rate_limit.py +0 -152
  127. mcp_proxy_adapter/commands/reload_settings_command.py +0 -125
  128. mcp_proxy_adapter/examples/README.md +0 -124
  129. mcp_proxy_adapter/examples/basic_server/README.md +0 -60
  130. mcp_proxy_adapter/examples/basic_server/__init__.py +0 -7
  131. mcp_proxy_adapter/examples/basic_server/basic_custom_settings.json +0 -39
  132. mcp_proxy_adapter/examples/basic_server/config.json +0 -35
  133. mcp_proxy_adapter/examples/basic_server/custom_settings_example.py +0 -238
  134. mcp_proxy_adapter/examples/basic_server/server.py +0 -103
  135. mcp_proxy_adapter/examples/custom_commands/README.md +0 -127
  136. mcp_proxy_adapter/examples/custom_commands/__init__.py +0 -27
  137. mcp_proxy_adapter/examples/custom_commands/advanced_hooks.py +0 -250
  138. mcp_proxy_adapter/examples/custom_commands/auto_commands/__init__.py +0 -6
  139. mcp_proxy_adapter/examples/custom_commands/auto_commands/auto_echo_command.py +0 -103
  140. mcp_proxy_adapter/examples/custom_commands/auto_commands/auto_info_command.py +0 -111
  141. mcp_proxy_adapter/examples/custom_commands/config.json +0 -35
  142. mcp_proxy_adapter/examples/custom_commands/custom_health_command.py +0 -169
  143. mcp_proxy_adapter/examples/custom_commands/custom_help_command.py +0 -215
  144. mcp_proxy_adapter/examples/custom_commands/custom_openapi_generator.py +0 -76
  145. mcp_proxy_adapter/examples/custom_commands/custom_settings.json +0 -96
  146. mcp_proxy_adapter/examples/custom_commands/custom_settings_manager.py +0 -241
  147. mcp_proxy_adapter/examples/custom_commands/data_transform_command.py +0 -135
  148. mcp_proxy_adapter/examples/custom_commands/echo_command.py +0 -122
  149. mcp_proxy_adapter/examples/custom_commands/hooks.py +0 -230
  150. mcp_proxy_adapter/examples/custom_commands/intercept_command.py +0 -123
  151. mcp_proxy_adapter/examples/custom_commands/manual_echo_command.py +0 -103
  152. mcp_proxy_adapter/examples/custom_commands/server.py +0 -228
  153. mcp_proxy_adapter/examples/custom_commands/test_hooks.py +0 -176
  154. mcp_proxy_adapter/examples/deployment/README.md +0 -49
  155. mcp_proxy_adapter/examples/deployment/__init__.py +0 -7
  156. mcp_proxy_adapter/examples/deployment/config.development.json +0 -8
  157. mcp_proxy_adapter/examples/deployment/config.json +0 -29
  158. mcp_proxy_adapter/examples/deployment/config.production.json +0 -12
  159. mcp_proxy_adapter/examples/deployment/config.staging.json +0 -11
  160. mcp_proxy_adapter/examples/deployment/docker-compose.yml +0 -31
  161. mcp_proxy_adapter/examples/deployment/run.sh +0 -43
  162. mcp_proxy_adapter/examples/deployment/run_docker.sh +0 -84
  163. mcp_proxy_adapter/schemas/base_schema.json +0 -114
  164. mcp_proxy_adapter/schemas/openapi_schema.json +0 -314
  165. mcp_proxy_adapter/tests/__init__.py +0 -0
  166. mcp_proxy_adapter/tests/api/__init__.py +0 -3
  167. mcp_proxy_adapter/tests/api/test_cmd_endpoint.py +0 -115
  168. mcp_proxy_adapter/tests/api/test_custom_openapi.py +0 -617
  169. mcp_proxy_adapter/tests/api/test_handlers.py +0 -522
  170. mcp_proxy_adapter/tests/api/test_middleware.py +0 -340
  171. mcp_proxy_adapter/tests/api/test_schemas.py +0 -546
  172. mcp_proxy_adapter/tests/api/test_tool_integration.py +0 -531
  173. mcp_proxy_adapter/tests/commands/__init__.py +0 -3
  174. mcp_proxy_adapter/tests/commands/test_config_command.py +0 -211
  175. mcp_proxy_adapter/tests/commands/test_echo_command.py +0 -127
  176. mcp_proxy_adapter/tests/commands/test_help_command.py +0 -136
  177. mcp_proxy_adapter/tests/conftest.py +0 -131
  178. mcp_proxy_adapter/tests/functional/__init__.py +0 -3
  179. mcp_proxy_adapter/tests/functional/test_api.py +0 -253
  180. mcp_proxy_adapter/tests/integration/__init__.py +0 -3
  181. mcp_proxy_adapter/tests/integration/test_cmd_integration.py +0 -129
  182. mcp_proxy_adapter/tests/integration/test_integration.py +0 -255
  183. mcp_proxy_adapter/tests/performance/__init__.py +0 -3
  184. mcp_proxy_adapter/tests/performance/test_performance.py +0 -189
  185. mcp_proxy_adapter/tests/stubs/__init__.py +0 -10
  186. mcp_proxy_adapter/tests/stubs/echo_command.py +0 -104
  187. mcp_proxy_adapter/tests/test_api_endpoints.py +0 -271
  188. mcp_proxy_adapter/tests/test_api_handlers.py +0 -289
  189. mcp_proxy_adapter/tests/test_base_command.py +0 -123
  190. mcp_proxy_adapter/tests/test_batch_requests.py +0 -117
  191. mcp_proxy_adapter/tests/test_command_registry.py +0 -281
  192. mcp_proxy_adapter/tests/test_config.py +0 -127
  193. mcp_proxy_adapter/tests/test_utils.py +0 -65
  194. mcp_proxy_adapter/tests/unit/__init__.py +0 -3
  195. mcp_proxy_adapter/tests/unit/test_base_command.py +0 -436
  196. mcp_proxy_adapter/tests/unit/test_config.py +0 -217
  197. mcp_proxy_adapter-4.1.1.dist-info/METADATA +0 -200
  198. mcp_proxy_adapter-4.1.1.dist-info/RECORD +0 -110
  199. {mcp_proxy_adapter-4.1.1.dist-info → mcp_proxy_adapter-6.0.1.dist-info}/WHEEL +0 -0
  200. {mcp_proxy_adapter-4.1.1.dist-info → mcp_proxy_adapter-6.0.1.dist-info}/top_level.txt +0 -0
@@ -1,260 +1,293 @@
1
1
  """
2
- Module for command execution hooks.
2
+ Module for command registration hooks.
3
3
 
4
- This module provides a hook system that allows intercepting command execution
5
- before and after the actual command runs.
4
+ This module provides a hook system for registering custom commands
5
+ that will be called during system initialization.
6
6
  """
7
7
 
8
- from typing import Any, Callable, Dict, List, Optional, Union
9
- from dataclasses import dataclass
8
+ from typing import Callable, List, Optional, Dict, Any, Union
10
9
  from enum import Enum
11
-
10
+ from dataclasses import dataclass
12
11
  from mcp_proxy_adapter.core.logging import logger
13
12
 
14
13
 
15
14
  class HookType(Enum):
16
- """Types of hooks."""
15
+ """Types of hooks that can be registered."""
16
+ CUSTOM_COMMANDS = "custom_commands"
17
+ BEFORE_INIT = "before_init"
18
+ AFTER_INIT = "after_init"
19
+ BEFORE_COMMAND = "before_command"
20
+ AFTER_COMMAND = "after_command"
17
21
  BEFORE_EXECUTION = "before_execution"
18
22
  AFTER_EXECUTION = "after_execution"
19
23
 
20
24
 
21
25
  @dataclass
22
26
  class HookContext:
23
- """Context passed to hook functions."""
24
- command_name: str
25
- params: Dict[str, Any]
27
+ """Context object passed to hook functions."""
26
28
  hook_type: HookType
27
- standard_processing: bool = True
29
+ command_name: Optional[str] = None
30
+ params: Optional[Dict[str, Any]] = None
28
31
  result: Optional[Any] = None
32
+ registry: Optional[Any] = None
33
+ metadata: Optional[Dict[str, Any]] = None
34
+ standard_processing: bool = True
35
+
36
+ def __post_init__(self):
37
+ """Initialize metadata if not provided."""
38
+ if self.metadata is None:
39
+ self.metadata = {}
29
40
 
30
41
 
31
42
  class CommandHooks:
32
43
  """
33
- Manages command execution hooks.
44
+ Hook system for command registration.
34
45
  """
35
46
 
36
47
  def __init__(self):
37
- """Initialize hooks manager."""
38
- self._before_hooks: Dict[str, List[Callable]] = {}
39
- self._after_hooks: Dict[str, List[Callable]] = {}
40
- self._global_before_hooks: List[Callable] = []
41
- self._global_after_hooks: List[Callable] = []
48
+ """
49
+ Initialize command hooks.
50
+ """
51
+ self._custom_commands_hooks: List[Callable] = []
52
+ self._before_init_hooks: List[Callable] = []
53
+ self._after_init_hooks: List[Callable] = []
54
+ self._before_command_hooks: List[Callable] = []
55
+ self._after_command_hooks: List[Callable] = []
42
56
 
43
- def register_before_hook(self, command_name: str, hook: Callable[[HookContext], None]) -> None:
57
+ def register_custom_commands_hook(self, hook_func: Callable) -> None:
44
58
  """
45
- Register a hook to be executed before command execution.
59
+ Register a hook function for custom commands registration.
46
60
 
47
61
  Args:
48
- command_name: Name of the command to hook into
49
- hook: Hook function that takes HookContext as parameter
62
+ hook_func: Function that registers custom commands.
63
+ Should accept registry as parameter.
50
64
  """
51
- if command_name not in self._before_hooks:
52
- self._before_hooks[command_name] = []
53
- self._before_hooks[command_name].append(hook)
54
- logger.debug(f"Registered before hook for command: {command_name}")
65
+ self._custom_commands_hooks.append(hook_func)
66
+ logger.debug(f"Registered custom commands hook: {hook_func.__name__}")
55
67
 
56
- def register_after_hook(self, command_name: str, hook: Callable[[HookContext], None]) -> None:
68
+ def register_before_init_hook(self, hook_func: Callable) -> None:
57
69
  """
58
- Register a hook to be executed after command execution.
70
+ Register a hook function to be called before system initialization.
59
71
 
60
72
  Args:
61
- command_name: Name of the command to hook into
62
- hook: Hook function that takes HookContext as parameter
73
+ hook_func: Function to call before initialization.
63
74
  """
64
- if command_name not in self._after_hooks:
65
- self._after_hooks[command_name] = []
66
- self._after_hooks[command_name].append(hook)
67
- logger.debug(f"Registered after hook for command: {command_name}")
75
+ self._before_init_hooks.append(hook_func)
76
+ logger.debug(f"Registered before init hook: {hook_func.__name__}")
68
77
 
69
- def register_global_before_hook(self, hook: Callable[[HookContext], None]) -> None:
78
+ def register_after_init_hook(self, hook_func: Callable) -> None:
70
79
  """
71
- Register a global hook to be executed before any command.
80
+ Register a hook function to be called after system initialization.
72
81
 
73
82
  Args:
74
- hook: Hook function that takes HookContext as parameter
83
+ hook_func: Function to call after initialization.
75
84
  """
76
- self._global_before_hooks.append(hook)
77
- logger.debug("Registered global before hook")
85
+ self._after_init_hooks.append(hook_func)
86
+ logger.debug(f"Registered after init hook: {hook_func.__name__}")
78
87
 
79
- def register_global_after_hook(self, hook: Callable[[HookContext], None]) -> None:
88
+ def register_before_command_hook(self, hook_func: Callable) -> None:
80
89
  """
81
- Register a global hook to be executed after any command.
90
+ Register a hook function to be called before command execution.
82
91
 
83
92
  Args:
84
- hook: Hook function that takes HookContext as parameter
93
+ hook_func: Function to call before command execution.
94
+ Should accept command_name and params as parameters.
85
95
  """
86
- self._global_after_hooks.append(hook)
87
- logger.debug("Registered global after hook")
96
+ self._before_command_hooks.append(hook_func)
97
+ logger.debug(f"Registered before command hook: {hook_func.__name__}")
88
98
 
89
- def unregister_before_hook(self, command_name: str, hook: Callable[[HookContext], None]) -> None:
99
+ def register_after_command_hook(self, hook_func: Callable) -> None:
90
100
  """
91
- Unregister a before hook for a specific command.
101
+ Register a hook function to be called after command execution.
92
102
 
93
103
  Args:
94
- command_name: Name of the command
95
- hook: Hook function to unregister
104
+ hook_func: Function to call after command execution.
105
+ Should accept command_name, params, and result as parameters.
96
106
  """
97
- if command_name in self._before_hooks:
98
- try:
99
- self._before_hooks[command_name].remove(hook)
100
- logger.debug(f"Unregistered before hook for command: {command_name}")
101
- # Remove the command key if no hooks remain
102
- if not self._before_hooks[command_name]:
103
- del self._before_hooks[command_name]
104
- except ValueError:
105
- logger.warning(f"Hook not found for command: {command_name}")
107
+ self._after_command_hooks.append(hook_func)
108
+ logger.debug(f"Registered after command hook: {hook_func.__name__}")
106
109
 
107
- def unregister_after_hook(self, command_name: str, hook: Callable[[HookContext], None]) -> None:
110
+ def execute_custom_commands_hooks(self, registry) -> int:
108
111
  """
109
- Unregister an after hook for a specific command.
112
+ Execute all registered custom commands hooks.
110
113
 
111
114
  Args:
112
- command_name: Name of the command
113
- hook: Hook function to unregister
115
+ registry: Command registry instance.
116
+
117
+ Returns:
118
+ Number of hooks executed.
114
119
  """
115
- if command_name in self._after_hooks:
120
+ logger.debug("Executing custom commands hooks...")
121
+ hooks_executed = 0
122
+
123
+ for hook_func in self._custom_commands_hooks:
116
124
  try:
117
- self._after_hooks[command_name].remove(hook)
118
- logger.debug(f"Unregistered after hook for command: {command_name}")
119
- # Remove the command key if no hooks remain
120
- if not self._after_hooks[command_name]:
121
- del self._after_hooks[command_name]
122
- except ValueError:
123
- logger.warning(f"Hook not found for command: {command_name}")
125
+ hook_func(registry)
126
+ hooks_executed += 1
127
+ logger.debug(f"Executed custom commands hook: {hook_func.__name__}")
128
+ except Exception as e:
129
+ logger.error(f"Failed to execute custom commands hook {hook_func.__name__}: {e}")
130
+
131
+ logger.info(f"Executed {hooks_executed} custom commands hooks")
132
+ return hooks_executed
124
133
 
125
- def unregister_global_before_hook(self, hook: Callable[[HookContext], None]) -> None:
134
+ def execute_before_init_hooks(self) -> int:
126
135
  """
127
- Unregister a global before hook.
136
+ Execute all registered before initialization hooks.
128
137
 
129
- Args:
130
- hook: Hook function to unregister
138
+ Returns:
139
+ Number of hooks executed.
131
140
  """
132
- try:
133
- self._global_before_hooks.remove(hook)
134
- logger.debug("Unregistered global before hook")
135
- except ValueError:
136
- logger.warning("Global before hook not found")
141
+ logger.debug("Executing before init hooks...")
142
+ hooks_executed = 0
143
+
144
+ for hook_func in self._before_init_hooks:
145
+ try:
146
+ hook_func()
147
+ hooks_executed += 1
148
+ logger.debug(f"Executed before init hook: {hook_func.__name__}")
149
+ except Exception as e:
150
+ logger.error(f"Failed to execute before init hook {hook_func.__name__}: {e}")
151
+
152
+ logger.debug(f"Executed {hooks_executed} before init hooks")
153
+ return hooks_executed
137
154
 
138
- def unregister_global_after_hook(self, hook: Callable[[HookContext], None]) -> None:
155
+ def execute_after_init_hooks(self) -> int:
139
156
  """
140
- Unregister a global after hook.
157
+ Execute all registered after initialization hooks.
141
158
 
142
- Args:
143
- hook: Hook function to unregister
159
+ Returns:
160
+ Number of hooks executed.
144
161
  """
145
- try:
146
- self._global_after_hooks.remove(hook)
147
- logger.debug("Unregistered global after hook")
148
- except ValueError:
149
- logger.warning("Global after hook not found")
162
+ logger.debug("Executing after init hooks...")
163
+ hooks_executed = 0
164
+
165
+ for hook_func in self._after_init_hooks:
166
+ try:
167
+ hook_func()
168
+ hooks_executed += 1
169
+ logger.debug(f"Executed after init hook: {hook_func.__name__}")
170
+ except Exception as e:
171
+ logger.error(f"Failed to execute after init hook {hook_func.__name__}: {e}")
172
+
173
+ logger.debug(f"Executed {hooks_executed} after init hooks")
174
+ return hooks_executed
150
175
 
151
- def execute_before_hooks(self, command_name: str, params: Dict[str, Any]) -> HookContext:
176
+ def execute_before_command_hooks(self, command_name: str, params: Dict[str, Any]) -> int:
152
177
  """
153
- Execute all before hooks for a command.
178
+ Execute all registered before command execution hooks.
154
179
 
155
180
  Args:
156
- command_name: Name of the command
181
+ command_name: Name of the command being executed
157
182
  params: Command parameters
158
183
 
159
184
  Returns:
160
- HookContext with execution results
185
+ Number of hooks executed.
161
186
  """
162
- context = HookContext(
163
- command_name=command_name,
164
- params=params,
165
- hook_type=HookType.BEFORE_EXECUTION
166
- )
187
+ logger.debug(f"Executing before command hooks for: {command_name}")
188
+ hooks_executed = 0
167
189
 
168
- # Execute global before hooks
169
- for hook in self._global_before_hooks:
190
+ for hook_func in self._before_command_hooks:
170
191
  try:
171
- hook(context)
192
+ hook_func(command_name, params)
193
+ hooks_executed += 1
194
+ logger.debug(f"Executed before command hook: {hook_func.__name__}")
172
195
  except Exception as e:
173
- logger.error(f"Error in global before hook for command {command_name}: {e}")
196
+ logger.error(f"Failed to execute before command hook {hook_func.__name__}: {e}")
174
197
 
175
- # Execute command-specific before hooks
176
- if command_name in self._before_hooks:
177
- for hook in self._before_hooks[command_name]:
178
- try:
179
- hook(context)
180
- except Exception as e:
181
- logger.error(f"Error in before hook for command {command_name}: {e}")
182
-
183
- return context
198
+ logger.debug(f"Executed {hooks_executed} before command hooks")
199
+ return hooks_executed
184
200
 
185
- def execute_after_hooks(self, command_name: str, params: Dict[str, Any], result: Any) -> HookContext:
201
+ def execute_after_command_hooks(self, command_name: str, params: Dict[str, Any], result: Any) -> int:
186
202
  """
187
- Execute all after hooks for a command.
203
+ Execute all registered after command execution hooks.
188
204
 
189
205
  Args:
190
- command_name: Name of the command
191
- params: Command parameters
206
+ command_name: Name of the command that was executed
207
+ params: Command parameters that were used
192
208
  result: Command execution result
193
209
 
194
210
  Returns:
195
- HookContext with execution results
211
+ Number of hooks executed.
196
212
  """
197
- context = HookContext(
198
- command_name=command_name,
199
- params=params,
200
- hook_type=HookType.AFTER_EXECUTION,
201
- result=result
202
- )
213
+ logger.debug(f"Executing after command hooks for: {command_name}")
214
+ hooks_executed = 0
203
215
 
204
- # Execute command-specific after hooks
205
- if command_name in self._after_hooks:
206
- for hook in self._after_hooks[command_name]:
207
- try:
208
- hook(context)
209
- except Exception as e:
210
- logger.error(f"Error in after hook for command {command_name}: {e}")
211
-
212
- # Execute global after hooks
213
- for hook in self._global_after_hooks:
216
+ for hook_func in self._after_command_hooks:
214
217
  try:
215
- hook(context)
218
+ hook_func(command_name, params, result)
219
+ hooks_executed += 1
220
+ logger.debug(f"Executed after command hook: {hook_func.__name__}")
216
221
  except Exception as e:
217
- logger.error(f"Error in global after hook for command {command_name}: {e}")
222
+ logger.error(f"Failed to execute after command hook {hook_func.__name__}: {e}")
218
223
 
219
- return context
224
+ logger.debug(f"Executed {hooks_executed} after command hooks")
225
+ return hooks_executed
220
226
 
221
- def clear_hooks(self, command_name: Optional[str] = None) -> None:
227
+ def clear_hooks(self) -> None:
222
228
  """
223
- Clear all hooks or hooks for a specific command.
224
-
225
- Args:
226
- command_name: If provided, clear hooks only for this command.
227
- If None, clear all hooks.
229
+ Clear all registered hooks.
228
230
  """
229
- if command_name is None:
230
- # Clear all hooks
231
- self._before_hooks.clear()
232
- self._after_hooks.clear()
233
- self._global_before_hooks.clear()
234
- self._global_after_hooks.clear()
235
- logger.debug("Cleared all hooks")
236
- else:
237
- # Clear hooks for specific command
238
- if command_name in self._before_hooks:
239
- del self._before_hooks[command_name]
240
- if command_name in self._after_hooks:
241
- del self._after_hooks[command_name]
242
- logger.debug(f"Cleared hooks for command: {command_name}")
243
-
244
- def get_hook_info(self) -> Dict[str, Any]:
245
- """
246
- Get information about registered hooks.
247
-
248
- Returns:
249
- Dictionary with hook information
250
- """
251
- return {
252
- "before_hooks": {cmd: len(hooks) for cmd, hooks in self._before_hooks.items()},
253
- "after_hooks": {cmd: len(hooks) for cmd, hooks in self._after_hooks.items()},
254
- "global_before_hooks": len(self._global_before_hooks),
255
- "global_after_hooks": len(self._global_after_hooks)
256
- }
231
+ self._custom_commands_hooks.clear()
232
+ self._before_init_hooks.clear()
233
+ self._after_init_hooks.clear()
234
+ self._before_command_hooks.clear()
235
+ self._after_command_hooks.clear()
236
+ logger.debug("Cleared all hooks")
257
237
 
258
238
 
259
239
  # Global hooks instance
260
- hooks = CommandHooks()
240
+ hooks = CommandHooks()
241
+
242
+
243
+ def register_custom_commands_hook(hook_func: Callable) -> None:
244
+ """
245
+ Register a hook function for custom commands registration.
246
+
247
+ Args:
248
+ hook_func: Function that registers custom commands.
249
+ Should accept registry as parameter.
250
+ """
251
+ hooks.register_custom_commands_hook(hook_func)
252
+
253
+
254
+ def register_before_init_hook(hook_func: Callable) -> None:
255
+ """
256
+ Register a hook function to be called before system initialization.
257
+
258
+ Args:
259
+ hook_func: Function to call before initialization.
260
+ """
261
+ hooks.register_before_init_hook(hook_func)
262
+
263
+
264
+ def register_after_init_hook(hook_func: Callable) -> None:
265
+ """
266
+ Register a hook function to be called after system initialization.
267
+
268
+ Args:
269
+ hook_func: Function to call after initialization.
270
+ """
271
+ hooks.register_after_init_hook(hook_func)
272
+
273
+
274
+ def register_before_command_hook(hook_func: Callable) -> None:
275
+ """
276
+ Register a hook function to be called before command execution.
277
+
278
+ Args:
279
+ hook_func: Function to call before command execution.
280
+ Should accept command_name and params as parameters.
281
+ """
282
+ hooks.register_before_command_hook(hook_func)
283
+
284
+
285
+ def register_after_command_hook(hook_func: Callable) -> None:
286
+ """
287
+ Register a hook function to be called after command execution.
288
+
289
+ Args:
290
+ hook_func: Function to call after command execution.
291
+ Should accept command_name, params, and result as parameters.
292
+ """
293
+ hooks.register_after_command_hook(hook_func)