chuk-tool-processor 0.7.0__py3-none-any.whl → 0.10__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.

Potentially problematic release.


This version of chuk-tool-processor might be problematic. Click here for more details.

Files changed (39) hide show
  1. chuk_tool_processor/__init__.py +114 -0
  2. chuk_tool_processor/core/__init__.py +31 -0
  3. chuk_tool_processor/core/exceptions.py +218 -12
  4. chuk_tool_processor/core/processor.py +391 -43
  5. chuk_tool_processor/execution/wrappers/__init__.py +42 -0
  6. chuk_tool_processor/execution/wrappers/caching.py +43 -10
  7. chuk_tool_processor/execution/wrappers/circuit_breaker.py +370 -0
  8. chuk_tool_processor/execution/wrappers/rate_limiting.py +31 -1
  9. chuk_tool_processor/execution/wrappers/retry.py +93 -53
  10. chuk_tool_processor/logging/__init__.py +5 -8
  11. chuk_tool_processor/logging/context.py +2 -5
  12. chuk_tool_processor/mcp/__init__.py +3 -0
  13. chuk_tool_processor/mcp/mcp_tool.py +8 -3
  14. chuk_tool_processor/mcp/models.py +87 -0
  15. chuk_tool_processor/mcp/setup_mcp_http_streamable.py +38 -2
  16. chuk_tool_processor/mcp/setup_mcp_sse.py +38 -2
  17. chuk_tool_processor/mcp/setup_mcp_stdio.py +92 -12
  18. chuk_tool_processor/mcp/stream_manager.py +109 -6
  19. chuk_tool_processor/mcp/transport/http_streamable_transport.py +18 -5
  20. chuk_tool_processor/mcp/transport/sse_transport.py +16 -3
  21. chuk_tool_processor/models/__init__.py +20 -0
  22. chuk_tool_processor/models/tool_call.py +34 -1
  23. chuk_tool_processor/models/tool_export_mixin.py +4 -4
  24. chuk_tool_processor/models/tool_spec.py +350 -0
  25. chuk_tool_processor/models/validated_tool.py +22 -2
  26. chuk_tool_processor/observability/__init__.py +30 -0
  27. chuk_tool_processor/observability/metrics.py +312 -0
  28. chuk_tool_processor/observability/setup.py +105 -0
  29. chuk_tool_processor/observability/tracing.py +346 -0
  30. chuk_tool_processor/py.typed +0 -0
  31. chuk_tool_processor/registry/interface.py +7 -7
  32. chuk_tool_processor/registry/providers/__init__.py +2 -1
  33. chuk_tool_processor/registry/tool_export.py +1 -6
  34. chuk_tool_processor-0.10.dist-info/METADATA +2326 -0
  35. chuk_tool_processor-0.10.dist-info/RECORD +69 -0
  36. chuk_tool_processor-0.7.0.dist-info/METADATA +0 -1230
  37. chuk_tool_processor-0.7.0.dist-info/RECORD +0 -61
  38. {chuk_tool_processor-0.7.0.dist-info → chuk_tool_processor-0.10.dist-info}/WHEEL +0 -0
  39. {chuk_tool_processor-0.7.0.dist-info → chuk_tool_processor-0.10.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,114 @@
1
+ """
2
+ CHUK Tool Processor - Async-native framework for processing LLM tool calls.
3
+
4
+ This package provides a production-ready framework for:
5
+ - Processing tool calls from various LLM output formats
6
+ - Executing tools with timeouts, retries, and rate limiting
7
+ - Connecting to remote MCP servers
8
+ - Caching results and circuit breaking
9
+
10
+ Quick Start:
11
+ >>> import asyncio
12
+ >>> from chuk_tool_processor import ToolProcessor
13
+ >>>
14
+ >>> async def main():
15
+ ... async with ToolProcessor() as processor:
16
+ ... llm_output = '<tool name="calculator" args=\'{"a": 5, "b": 3}\'/>'
17
+ ... results = await processor.process(llm_output)
18
+ ... print(results[0].result)
19
+ >>>
20
+ >>> asyncio.run(main())
21
+ """
22
+
23
+ from typing import TYPE_CHECKING
24
+
25
+ # Version
26
+ __version__ = "0.9.7"
27
+
28
+ # Core processor
29
+ from chuk_tool_processor.core.processor import ToolProcessor
30
+
31
+ # Execution strategies
32
+ from chuk_tool_processor.execution.strategies.inprocess_strategy import InProcessStrategy
33
+ from chuk_tool_processor.execution.strategies.subprocess_strategy import SubprocessStrategy
34
+ from chuk_tool_processor.execution.strategies.subprocess_strategy import SubprocessStrategy as IsolatedStrategy
35
+
36
+ # MCP setup helpers
37
+ from chuk_tool_processor.mcp import (
38
+ setup_mcp_http_streamable,
39
+ setup_mcp_sse,
40
+ setup_mcp_stdio,
41
+ )
42
+
43
+ # Stream manager for advanced MCP usage
44
+ from chuk_tool_processor.mcp.stream_manager import StreamManager
45
+
46
+ # Models (commonly used)
47
+ from chuk_tool_processor.models.tool_call import ToolCall
48
+ from chuk_tool_processor.models.tool_result import ToolResult
49
+
50
+ # Registry functions
51
+ from chuk_tool_processor.registry import (
52
+ ToolRegistryProvider,
53
+ get_default_registry,
54
+ initialize,
55
+ )
56
+ from chuk_tool_processor.registry.auto_register import register_fn_tool
57
+
58
+ # Decorators for registering tools
59
+ from chuk_tool_processor.registry.decorators import register_tool
60
+
61
+ # Type checking imports (not available at runtime)
62
+ if TYPE_CHECKING:
63
+ # Advanced models for type hints
64
+ # Execution strategies
65
+ from chuk_tool_processor.execution.strategies.inprocess_strategy import InProcessStrategy
66
+ from chuk_tool_processor.execution.strategies.subprocess_strategy import SubprocessStrategy
67
+
68
+ # Retry config
69
+ from chuk_tool_processor.execution.wrappers.retry import RetryConfig
70
+ from chuk_tool_processor.models.streaming_tool import StreamingTool
71
+ from chuk_tool_processor.models.tool_spec import ToolSpec
72
+ from chuk_tool_processor.models.validated_tool import ValidatedTool
73
+
74
+ # Registry interface
75
+ from chuk_tool_processor.registry.interface import ToolRegistryInterface
76
+
77
+ # Public API
78
+ __all__ = [
79
+ # Version
80
+ "__version__",
81
+ # Core classes
82
+ "ToolProcessor",
83
+ "StreamManager",
84
+ # Models
85
+ "ToolCall",
86
+ "ToolResult",
87
+ # Registry
88
+ "initialize",
89
+ "get_default_registry",
90
+ "ToolRegistryProvider",
91
+ # Decorators
92
+ "register_tool",
93
+ "register_fn_tool",
94
+ # Execution strategies
95
+ "InProcessStrategy",
96
+ "IsolatedStrategy",
97
+ "SubprocessStrategy",
98
+ # MCP setup
99
+ "setup_mcp_stdio",
100
+ "setup_mcp_sse",
101
+ "setup_mcp_http_streamable",
102
+ ]
103
+
104
+ # Type checking exports (documentation only)
105
+ if TYPE_CHECKING:
106
+ __all__ += [
107
+ "ValidatedTool",
108
+ "StreamingTool",
109
+ "ToolSpec",
110
+ "InProcessStrategy",
111
+ "SubprocessStrategy",
112
+ "ToolRegistryInterface",
113
+ "RetryConfig",
114
+ ]
@@ -1 +1,32 @@
1
1
  # chuk_tool_processor/core/__init__.py
2
+ """Core functionality for the tool processor."""
3
+
4
+ from chuk_tool_processor.core.exceptions import (
5
+ ErrorCode,
6
+ MCPConnectionError,
7
+ MCPError,
8
+ MCPTimeoutError,
9
+ ParserError,
10
+ ToolCircuitOpenError,
11
+ ToolExecutionError,
12
+ ToolNotFoundError,
13
+ ToolProcessorError,
14
+ ToolRateLimitedError,
15
+ ToolTimeoutError,
16
+ ToolValidationError,
17
+ )
18
+
19
+ __all__ = [
20
+ "ErrorCode",
21
+ "ToolProcessorError",
22
+ "ToolNotFoundError",
23
+ "ToolExecutionError",
24
+ "ToolTimeoutError",
25
+ "ToolValidationError",
26
+ "ParserError",
27
+ "ToolRateLimitedError",
28
+ "ToolCircuitOpenError",
29
+ "MCPError",
30
+ "MCPConnectionError",
31
+ "MCPTimeoutError",
32
+ ]
@@ -1,51 +1,257 @@
1
1
  # chuk_tool_processor/exceptions.py
2
+ from enum import Enum
2
3
  from typing import Any
3
4
 
4
5
 
6
+ class ErrorCode(str, Enum):
7
+ """Machine-readable error codes for tool processor errors."""
8
+
9
+ # Tool registry errors
10
+ TOOL_NOT_FOUND = "TOOL_NOT_FOUND"
11
+ TOOL_REGISTRATION_FAILED = "TOOL_REGISTRATION_FAILED"
12
+
13
+ # Execution errors
14
+ TOOL_EXECUTION_FAILED = "TOOL_EXECUTION_FAILED"
15
+ TOOL_TIMEOUT = "TOOL_TIMEOUT"
16
+ TOOL_CANCELLED = "TOOL_CANCELLED"
17
+
18
+ # Validation errors
19
+ TOOL_VALIDATION_ERROR = "TOOL_VALIDATION_ERROR"
20
+ TOOL_ARGUMENT_ERROR = "TOOL_ARGUMENT_ERROR"
21
+ TOOL_RESULT_ERROR = "TOOL_RESULT_ERROR"
22
+
23
+ # Rate limiting and circuit breaker
24
+ TOOL_RATE_LIMITED = "TOOL_RATE_LIMITED"
25
+ TOOL_CIRCUIT_OPEN = "TOOL_CIRCUIT_OPEN"
26
+
27
+ # Parser errors
28
+ PARSER_ERROR = "PARSER_ERROR"
29
+ PARSER_INVALID_FORMAT = "PARSER_INVALID_FORMAT"
30
+
31
+ # MCP errors
32
+ MCP_CONNECTION_FAILED = "MCP_CONNECTION_FAILED"
33
+ MCP_TRANSPORT_ERROR = "MCP_TRANSPORT_ERROR"
34
+ MCP_SERVER_ERROR = "MCP_SERVER_ERROR"
35
+ MCP_TIMEOUT = "MCP_TIMEOUT"
36
+
37
+ # System errors
38
+ RESOURCE_EXHAUSTED = "RESOURCE_EXHAUSTED"
39
+ CONFIGURATION_ERROR = "CONFIGURATION_ERROR"
40
+
41
+
5
42
  class ToolProcessorError(Exception):
6
- """Base exception for all tool processor errors."""
43
+ """Base exception for all tool processor errors with machine-readable codes."""
7
44
 
8
- pass
45
+ def __init__(
46
+ self,
47
+ message: str,
48
+ code: ErrorCode | None = None,
49
+ details: dict[str, Any] | None = None,
50
+ original_error: Exception | None = None,
51
+ ):
52
+ super().__init__(message)
53
+ self.code = code or ErrorCode.TOOL_EXECUTION_FAILED
54
+ self.details = details or {}
55
+ self.original_error = original_error
56
+
57
+ def to_dict(self) -> dict[str, Any]:
58
+ """Convert exception to a structured dictionary for logging/monitoring."""
59
+ result = {
60
+ "error": self.__class__.__name__,
61
+ "code": self.code.value,
62
+ "message": str(self),
63
+ "details": self.details,
64
+ }
65
+ if self.original_error:
66
+ result["original_error"] = {
67
+ "type": type(self.original_error).__name__,
68
+ "message": str(self.original_error),
69
+ }
70
+ return result
9
71
 
10
72
 
11
73
  class ToolNotFoundError(ToolProcessorError):
12
74
  """Raised when a requested tool is not found in the registry."""
13
75
 
14
- def __init__(self, tool_name: str):
76
+ def __init__(self, tool_name: str, available_tools: list[str] | None = None):
15
77
  self.tool_name = tool_name
16
- super().__init__(f"Tool '{tool_name}' not found in registry")
78
+ details: dict[str, Any] = {"tool_name": tool_name}
79
+ if available_tools:
80
+ details["available_tools"] = available_tools
81
+ super().__init__(
82
+ f"Tool '{tool_name}' not found in registry",
83
+ code=ErrorCode.TOOL_NOT_FOUND,
84
+ details=details,
85
+ )
17
86
 
18
87
 
19
88
  class ToolExecutionError(ToolProcessorError):
20
89
  """Raised when a tool execution fails."""
21
90
 
22
- def __init__(self, tool_name: str, original_error: Exception | None = None):
91
+ def __init__(
92
+ self,
93
+ tool_name: str,
94
+ original_error: Exception | None = None,
95
+ details: dict[str, Any] | None = None,
96
+ ):
23
97
  self.tool_name = tool_name
24
- self.original_error = original_error
25
98
  message = f"Tool '{tool_name}' execution failed"
26
99
  if original_error:
27
100
  message += f": {str(original_error)}"
28
- super().__init__(message)
101
+
102
+ error_details = {"tool_name": tool_name}
103
+ if details:
104
+ error_details.update(details)
105
+
106
+ super().__init__(
107
+ message,
108
+ code=ErrorCode.TOOL_EXECUTION_FAILED,
109
+ details=error_details,
110
+ original_error=original_error,
111
+ )
29
112
 
30
113
 
31
114
  class ToolTimeoutError(ToolExecutionError):
32
115
  """Raised when a tool execution times out."""
33
116
 
34
- def __init__(self, tool_name: str, timeout: float):
117
+ def __init__(self, tool_name: str, timeout: float, attempts: int = 1):
35
118
  self.timeout = timeout
36
- super().__init__(tool_name, Exception(f"Execution timed out after {timeout}s"))
119
+ self.attempts = attempts
120
+ # Call ToolProcessorError.__init__ directly to set the right code
121
+ ToolProcessorError.__init__(
122
+ self,
123
+ f"Tool '{tool_name}' timed out after {timeout}s (attempts: {attempts})",
124
+ code=ErrorCode.TOOL_TIMEOUT,
125
+ details={"tool_name": tool_name, "timeout": timeout, "attempts": attempts},
126
+ )
127
+ self.tool_name = tool_name
37
128
 
38
129
 
39
130
  class ToolValidationError(ToolProcessorError):
40
131
  """Raised when tool arguments or results fail validation."""
41
132
 
42
- def __init__(self, tool_name: str, errors: dict[str, Any]):
133
+ def __init__(
134
+ self,
135
+ tool_name: str,
136
+ errors: dict[str, Any],
137
+ validation_type: str = "arguments",
138
+ ):
43
139
  self.tool_name = tool_name
44
140
  self.errors = errors
45
- super().__init__(f"Validation failed for tool '{tool_name}': {errors}")
141
+ self.validation_type = validation_type
142
+ super().__init__(
143
+ f"Validation failed for tool '{tool_name}' {validation_type}: {errors}",
144
+ code=ErrorCode.TOOL_VALIDATION_ERROR,
145
+ details={"tool_name": tool_name, "validation_type": validation_type, "errors": errors},
146
+ )
46
147
 
47
148
 
48
149
  class ParserError(ToolProcessorError):
49
150
  """Raised when parsing tool calls from raw input fails."""
50
151
 
51
- pass
152
+ def __init__(
153
+ self,
154
+ message: str,
155
+ parser_name: str | None = None,
156
+ input_sample: str | None = None,
157
+ ):
158
+ self.parser_name = parser_name
159
+ self.input_sample = input_sample
160
+ details = {}
161
+ if parser_name:
162
+ details["parser_name"] = parser_name
163
+ if input_sample:
164
+ # Truncate sample for logging
165
+ details["input_sample"] = input_sample[:200] + "..." if len(input_sample) > 200 else input_sample
166
+ super().__init__(
167
+ message,
168
+ code=ErrorCode.PARSER_ERROR,
169
+ details=details,
170
+ )
171
+
172
+
173
+ class ToolRateLimitedError(ToolProcessorError):
174
+ """Raised when a tool call is rate limited."""
175
+
176
+ def __init__(
177
+ self,
178
+ tool_name: str,
179
+ retry_after: float | None = None,
180
+ limit: int | None = None,
181
+ ):
182
+ self.tool_name = tool_name
183
+ self.retry_after = retry_after
184
+ self.limit = limit
185
+ message = f"Tool '{tool_name}' rate limited"
186
+ if retry_after:
187
+ message += f" (retry after {retry_after}s)"
188
+ super().__init__(
189
+ message,
190
+ code=ErrorCode.TOOL_RATE_LIMITED,
191
+ details={"tool_name": tool_name, "retry_after": retry_after, "limit": limit},
192
+ )
193
+
194
+
195
+ class ToolCircuitOpenError(ToolProcessorError):
196
+ """Raised when a tool circuit breaker is open."""
197
+
198
+ def __init__(
199
+ self,
200
+ tool_name: str,
201
+ failure_count: int,
202
+ reset_timeout: float | None = None,
203
+ ):
204
+ self.tool_name = tool_name
205
+ self.failure_count = failure_count
206
+ self.reset_timeout = reset_timeout
207
+ message = f"Tool '{tool_name}' circuit breaker is open (failures: {failure_count})"
208
+ if reset_timeout:
209
+ message += f" (reset in {reset_timeout}s)"
210
+ super().__init__(
211
+ message,
212
+ code=ErrorCode.TOOL_CIRCUIT_OPEN,
213
+ details={"tool_name": tool_name, "failure_count": failure_count, "reset_timeout": reset_timeout},
214
+ )
215
+
216
+
217
+ class MCPError(ToolProcessorError):
218
+ """Base class for MCP-related errors."""
219
+
220
+ def __init__(
221
+ self,
222
+ message: str,
223
+ code: ErrorCode,
224
+ server_name: str | None = None,
225
+ details: dict[str, Any] | None = None,
226
+ ):
227
+ error_details = details or {}
228
+ if server_name:
229
+ error_details["server_name"] = server_name
230
+ super().__init__(message, code=code, details=error_details)
231
+
232
+
233
+ class MCPConnectionError(MCPError):
234
+ """Raised when MCP connection fails."""
235
+
236
+ def __init__(self, server_name: str, reason: str | None = None):
237
+ message = f"Failed to connect to MCP server '{server_name}'"
238
+ if reason:
239
+ message += f": {reason}"
240
+ super().__init__(
241
+ message,
242
+ code=ErrorCode.MCP_CONNECTION_FAILED,
243
+ server_name=server_name,
244
+ details={"reason": reason} if reason else None,
245
+ )
246
+
247
+
248
+ class MCPTimeoutError(MCPError):
249
+ """Raised when MCP operation times out."""
250
+
251
+ def __init__(self, server_name: str, operation: str, timeout: float):
252
+ super().__init__(
253
+ f"MCP operation '{operation}' on server '{server_name}' timed out after {timeout}s",
254
+ code=ErrorCode.MCP_TIMEOUT,
255
+ server_name=server_name,
256
+ details={"operation": operation, "timeout": timeout},
257
+ )