aip-agents-binary 0.5.25b9__py3-none-any.whl → 0.6.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.
- aip_agents/agent/base_langgraph_agent.py +137 -68
- aip_agents/agent/base_langgraph_agent.pyi +3 -2
- aip_agents/agent/langgraph_react_agent.py +252 -16
- aip_agents/agent/langgraph_react_agent.pyi +40 -1
- aip_agents/examples/compare_streaming_client.py +2 -2
- aip_agents/examples/compare_streaming_server.py +1 -1
- aip_agents/examples/hello_world_ptc.py +51 -0
- aip_agents/examples/hello_world_ptc.pyi +5 -0
- aip_agents/examples/hello_world_tool_output_client.py +9 -0
- aip_agents/examples/todolist_planning_a2a_langchain_client.py +2 -2
- aip_agents/examples/todolist_planning_a2a_langgraph_server.py +1 -1
- aip_agents/guardrails/engines/base.py +6 -6
- aip_agents/mcp/client/connection_manager.py +36 -1
- aip_agents/mcp/client/connection_manager.pyi +3 -0
- aip_agents/mcp/client/persistent_session.py +318 -68
- aip_agents/mcp/client/persistent_session.pyi +9 -0
- aip_agents/mcp/client/transports.py +33 -2
- aip_agents/mcp/client/transports.pyi +9 -0
- aip_agents/ptc/__init__.py +48 -0
- aip_agents/ptc/__init__.pyi +10 -0
- aip_agents/ptc/doc_gen.py +122 -0
- aip_agents/ptc/doc_gen.pyi +40 -0
- aip_agents/ptc/exceptions.py +39 -0
- aip_agents/ptc/exceptions.pyi +22 -0
- aip_agents/ptc/executor.py +143 -0
- aip_agents/ptc/executor.pyi +73 -0
- aip_agents/ptc/mcp/__init__.py +45 -0
- aip_agents/ptc/mcp/__init__.pyi +7 -0
- aip_agents/ptc/mcp/sandbox_bridge.py +668 -0
- aip_agents/ptc/mcp/sandbox_bridge.pyi +47 -0
- aip_agents/ptc/mcp/templates/__init__.py +1 -0
- aip_agents/ptc/mcp/templates/__init__.pyi +0 -0
- aip_agents/ptc/mcp/templates/mcp_client.py.template +239 -0
- aip_agents/ptc/naming.py +184 -0
- aip_agents/ptc/naming.pyi +76 -0
- aip_agents/ptc/payload.py +26 -0
- aip_agents/ptc/payload.pyi +15 -0
- aip_agents/ptc/prompt_builder.py +571 -0
- aip_agents/ptc/prompt_builder.pyi +55 -0
- aip_agents/ptc/ptc_helper.py +16 -0
- aip_agents/ptc/ptc_helper.pyi +1 -0
- aip_agents/ptc/sandbox_bridge.py +58 -0
- aip_agents/ptc/sandbox_bridge.pyi +25 -0
- aip_agents/ptc/template_utils.py +33 -0
- aip_agents/ptc/template_utils.pyi +13 -0
- aip_agents/ptc/templates/__init__.py +1 -0
- aip_agents/ptc/templates/__init__.pyi +0 -0
- aip_agents/ptc/templates/ptc_helper.py.template +134 -0
- aip_agents/sandbox/__init__.py +43 -0
- aip_agents/sandbox/__init__.pyi +5 -0
- aip_agents/sandbox/defaults.py +9 -0
- aip_agents/sandbox/defaults.pyi +2 -0
- aip_agents/sandbox/e2b_runtime.py +267 -0
- aip_agents/sandbox/e2b_runtime.pyi +51 -0
- aip_agents/sandbox/template_builder.py +131 -0
- aip_agents/sandbox/template_builder.pyi +36 -0
- aip_agents/sandbox/types.py +24 -0
- aip_agents/sandbox/types.pyi +14 -0
- aip_agents/sandbox/validation.py +50 -0
- aip_agents/sandbox/validation.pyi +20 -0
- aip_agents/tools/__init__.py +2 -0
- aip_agents/tools/__init__.pyi +2 -1
- aip_agents/tools/browser_use/browser_use_tool.py +8 -0
- aip_agents/tools/browser_use/streaming.py +2 -0
- aip_agents/tools/execute_ptc_code.py +305 -0
- aip_agents/tools/execute_ptc_code.pyi +87 -0
- aip_agents/utils/langgraph/tool_managers/delegation_tool_manager.py +26 -1
- aip_agents/utils/langgraph/tool_output_management.py +80 -0
- aip_agents/utils/langgraph/tool_output_management.pyi +37 -0
- {aip_agents_binary-0.5.25b9.dist-info → aip_agents_binary-0.6.1.dist-info}/METADATA +51 -48
- {aip_agents_binary-0.5.25b9.dist-info → aip_agents_binary-0.6.1.dist-info}/RECORD +73 -27
- {aip_agents_binary-0.5.25b9.dist-info → aip_agents_binary-0.6.1.dist-info}/WHEEL +0 -0
- {aip_agents_binary-0.5.25b9.dist-info → aip_agents_binary-0.6.1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""PTC (Programmatic Tool Calling) core module (MCP-only).
|
|
2
|
+
|
|
3
|
+
This module provides the core PTC functionality for MCP tools, including
|
|
4
|
+
executor, prompt builder, and sandbox bridge.
|
|
5
|
+
|
|
6
|
+
Authors:
|
|
7
|
+
Putu Ravindra Wiguna (putu.r.wiguna@gdplabs.id)
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from aip_agents.ptc.exceptions import PTCError, PTCToolError
|
|
11
|
+
from aip_agents.ptc.prompt_builder import PromptConfig, build_ptc_prompt, compute_ptc_prompt_hash
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
# Exceptions
|
|
15
|
+
"PTCError",
|
|
16
|
+
"PTCToolError",
|
|
17
|
+
# Executor
|
|
18
|
+
"PTCSandboxConfig",
|
|
19
|
+
"PTCSandboxExecutor",
|
|
20
|
+
# Prompt builder
|
|
21
|
+
"PromptConfig",
|
|
22
|
+
"build_ptc_prompt",
|
|
23
|
+
"compute_ptc_prompt_hash",
|
|
24
|
+
# Sandbox bridge
|
|
25
|
+
"build_sandbox_payload",
|
|
26
|
+
"wrap_ptc_code",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def __getattr__(name: str):
|
|
31
|
+
"""Lazy import to avoid circular dependencies."""
|
|
32
|
+
if name == "PTCSandboxConfig":
|
|
33
|
+
from aip_agents.ptc.executor import PTCSandboxConfig
|
|
34
|
+
|
|
35
|
+
return PTCSandboxConfig
|
|
36
|
+
elif name == "PTCSandboxExecutor":
|
|
37
|
+
from aip_agents.ptc.executor import PTCSandboxExecutor
|
|
38
|
+
|
|
39
|
+
return PTCSandboxExecutor
|
|
40
|
+
elif name == "build_sandbox_payload":
|
|
41
|
+
from aip_agents.ptc.sandbox_bridge import build_sandbox_payload
|
|
42
|
+
|
|
43
|
+
return build_sandbox_payload
|
|
44
|
+
elif name == "wrap_ptc_code":
|
|
45
|
+
from aip_agents.ptc.sandbox_bridge import wrap_ptc_code
|
|
46
|
+
|
|
47
|
+
return wrap_ptc_code
|
|
48
|
+
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from aip_agents.ptc.exceptions import PTCError as PTCError, PTCToolError as PTCToolError
|
|
2
|
+
from aip_agents.ptc.prompt_builder import PromptConfig as PromptConfig, build_ptc_prompt as build_ptc_prompt, compute_ptc_prompt_hash as compute_ptc_prompt_hash
|
|
3
|
+
|
|
4
|
+
__all__ = ['PTCError', 'PTCToolError', 'PTCSandboxConfig', 'PTCSandboxExecutor', 'PromptConfig', 'build_ptc_prompt', 'compute_ptc_prompt_hash', 'build_sandbox_payload', 'wrap_ptc_code']
|
|
5
|
+
|
|
6
|
+
# Names in __all__ with no definition:
|
|
7
|
+
# PTCSandboxConfig
|
|
8
|
+
# PTCSandboxExecutor
|
|
9
|
+
# build_sandbox_payload
|
|
10
|
+
# wrap_ptc_code
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""Documentation generation utilities for PTC.
|
|
2
|
+
|
|
3
|
+
Shared constants and helpers for generating tool documentation in sandbox payloads.
|
|
4
|
+
|
|
5
|
+
Authors:
|
|
6
|
+
Putu Ravindra Wiguna (putu.r.wiguna@gdplabs.id)
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from aip_agents.ptc.naming import sanitize_function_name
|
|
12
|
+
|
|
13
|
+
# Documentation limits (fixed constants per plan)
|
|
14
|
+
DOC_DESC_LIMIT = 120 # Tool description trim limit
|
|
15
|
+
DOC_PARAM_DESC_LIMIT = 80 # Parameter description trim limit
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def json_type_to_display(json_type: Any) -> str:
|
|
19
|
+
"""Convert JSON type to display string.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
json_type: JSON schema type.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
Human-readable type string.
|
|
26
|
+
"""
|
|
27
|
+
if isinstance(json_type, list):
|
|
28
|
+
return "any"
|
|
29
|
+
type_map = {
|
|
30
|
+
"string": "str",
|
|
31
|
+
"integer": "int",
|
|
32
|
+
"number": "float",
|
|
33
|
+
"boolean": "bool",
|
|
34
|
+
"array": "list",
|
|
35
|
+
"object": "dict",
|
|
36
|
+
"null": "None",
|
|
37
|
+
}
|
|
38
|
+
return type_map.get(str(json_type), "any")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def trim_text(text: str | None, limit: int) -> str:
|
|
42
|
+
"""Trim text to limit with ellipsis if needed.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
text: Text to trim.
|
|
46
|
+
limit: Maximum length before trimming.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
Trimmed text.
|
|
50
|
+
"""
|
|
51
|
+
if not text:
|
|
52
|
+
return ""
|
|
53
|
+
if len(text) > limit:
|
|
54
|
+
return text[:limit] + "..."
|
|
55
|
+
return text
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def render_tool_doc(
|
|
59
|
+
func_name: str,
|
|
60
|
+
signature: str,
|
|
61
|
+
description: str,
|
|
62
|
+
schema: dict[str, Any],
|
|
63
|
+
is_stub: bool = False,
|
|
64
|
+
example_code: str | None = None,
|
|
65
|
+
example_heading: str = "## Example",
|
|
66
|
+
) -> str:
|
|
67
|
+
"""Render markdown documentation for a tool.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
func_name: Sanitized function name.
|
|
71
|
+
signature: Full function signature.
|
|
72
|
+
description: Tool description.
|
|
73
|
+
schema: Input schema for parameters.
|
|
74
|
+
is_stub: Whether this is a stub documentation.
|
|
75
|
+
example_code: Optional example code block content (without ```python).
|
|
76
|
+
example_heading: Heading for the example section.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
Markdown documentation string.
|
|
80
|
+
"""
|
|
81
|
+
if is_stub:
|
|
82
|
+
desc = "Details unavailable because tool definitions are not loaded yet."
|
|
83
|
+
else:
|
|
84
|
+
desc = trim_text(description, DOC_DESC_LIMIT) or "No description available."
|
|
85
|
+
|
|
86
|
+
lines = [
|
|
87
|
+
f"# {func_name}",
|
|
88
|
+
"",
|
|
89
|
+
f"**Description:** {desc}",
|
|
90
|
+
"",
|
|
91
|
+
f"**Signature:** `{signature}`",
|
|
92
|
+
"",
|
|
93
|
+
]
|
|
94
|
+
|
|
95
|
+
# Add parameters section
|
|
96
|
+
properties = schema.get("properties", {})
|
|
97
|
+
required = set(schema.get("required", []))
|
|
98
|
+
|
|
99
|
+
if properties:
|
|
100
|
+
lines.append("## Parameters")
|
|
101
|
+
lines.append("")
|
|
102
|
+
for prop_name, prop_schema in sorted(properties.items()):
|
|
103
|
+
safe_param = sanitize_function_name(prop_name)
|
|
104
|
+
prop_type = json_type_to_display(prop_schema.get("type", "any"))
|
|
105
|
+
is_required = "required" if prop_name in required else "optional"
|
|
106
|
+
|
|
107
|
+
# Trim param description
|
|
108
|
+
raw_param_desc = prop_schema.get("description", "")
|
|
109
|
+
param_desc = trim_text(raw_param_desc, DOC_PARAM_DESC_LIMIT)
|
|
110
|
+
|
|
111
|
+
lines.append(f"- **{safe_param}** ({prop_type}, {is_required}): {param_desc}")
|
|
112
|
+
lines.append("")
|
|
113
|
+
|
|
114
|
+
# Add example section
|
|
115
|
+
if example_code:
|
|
116
|
+
lines.append(example_heading)
|
|
117
|
+
lines.append("")
|
|
118
|
+
lines.append("```python")
|
|
119
|
+
lines.append(example_code)
|
|
120
|
+
lines.append("```")
|
|
121
|
+
|
|
122
|
+
return "\n".join(lines)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from aip_agents.ptc.naming import sanitize_function_name as sanitize_function_name
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
DOC_DESC_LIMIT: int
|
|
5
|
+
DOC_PARAM_DESC_LIMIT: int
|
|
6
|
+
|
|
7
|
+
def json_type_to_display(json_type: Any) -> str:
|
|
8
|
+
"""Convert JSON type to display string.
|
|
9
|
+
|
|
10
|
+
Args:
|
|
11
|
+
json_type: JSON schema type.
|
|
12
|
+
|
|
13
|
+
Returns:
|
|
14
|
+
Human-readable type string.
|
|
15
|
+
"""
|
|
16
|
+
def trim_text(text: str | None, limit: int) -> str:
|
|
17
|
+
"""Trim text to limit with ellipsis if needed.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
text: Text to trim.
|
|
21
|
+
limit: Maximum length before trimming.
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
Trimmed text.
|
|
25
|
+
"""
|
|
26
|
+
def render_tool_doc(func_name: str, signature: str, description: str, schema: dict[str, Any], is_stub: bool = False, example_code: str | None = None, example_heading: str = '## Example') -> str:
|
|
27
|
+
"""Render markdown documentation for a tool.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
func_name: Sanitized function name.
|
|
31
|
+
signature: Full function signature.
|
|
32
|
+
description: Tool description.
|
|
33
|
+
schema: Input schema for parameters.
|
|
34
|
+
is_stub: Whether this is a stub documentation.
|
|
35
|
+
example_code: Optional example code block content (without ```python).
|
|
36
|
+
example_heading: Heading for the example section.
|
|
37
|
+
|
|
38
|
+
Returns:
|
|
39
|
+
Markdown documentation string.
|
|
40
|
+
"""
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""PTC-specific exceptions.
|
|
2
|
+
|
|
3
|
+
This module defines exceptions for Programmatic Tool Calling operations.
|
|
4
|
+
|
|
5
|
+
Authors:
|
|
6
|
+
Putu Ravindra Wiguna (putu.r.wiguna@gdplabs.id)
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PTCError(Exception):
|
|
11
|
+
"""Base exception for PTC errors."""
|
|
12
|
+
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class PTCToolError(PTCError):
|
|
17
|
+
"""Error during tool execution.
|
|
18
|
+
|
|
19
|
+
Attributes:
|
|
20
|
+
server_name: The MCP server where the error occurred.
|
|
21
|
+
tool_name: The tool that failed.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
message: str,
|
|
27
|
+
server_name: str | None = None,
|
|
28
|
+
tool_name: str | None = None,
|
|
29
|
+
) -> None:
|
|
30
|
+
"""Initialize PTCToolError.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
message: Error message.
|
|
34
|
+
server_name: The MCP server name (optional).
|
|
35
|
+
tool_name: The tool name (optional).
|
|
36
|
+
"""
|
|
37
|
+
super().__init__(message)
|
|
38
|
+
self.server_name = server_name
|
|
39
|
+
self.tool_name = tool_name
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
|
|
3
|
+
class PTCError(Exception):
|
|
4
|
+
"""Base exception for PTC errors."""
|
|
5
|
+
|
|
6
|
+
class PTCToolError(PTCError):
|
|
7
|
+
"""Error during tool execution.
|
|
8
|
+
|
|
9
|
+
Attributes:
|
|
10
|
+
server_name: The MCP server where the error occurred.
|
|
11
|
+
tool_name: The tool that failed.
|
|
12
|
+
"""
|
|
13
|
+
server_name: Incomplete
|
|
14
|
+
tool_name: Incomplete
|
|
15
|
+
def __init__(self, message: str, server_name: str | None = None, tool_name: str | None = None) -> None:
|
|
16
|
+
"""Initialize PTCToolError.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
message: Error message.
|
|
20
|
+
server_name: The MCP server name (optional).
|
|
21
|
+
tool_name: The tool name (optional).
|
|
22
|
+
"""
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"""PTC Executor implementations (MCP-only).
|
|
2
|
+
|
|
3
|
+
This module provides the sandboxed executor for Programmatic Tool Calling
|
|
4
|
+
with MCP tools.
|
|
5
|
+
|
|
6
|
+
Authors:
|
|
7
|
+
Putu Ravindra Wiguna (putu.r.wiguna@gdplabs.id)
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from dataclasses import dataclass, field
|
|
13
|
+
|
|
14
|
+
from aip_agents.mcp.client.base_mcp_client import BaseMCPClient
|
|
15
|
+
from aip_agents.ptc.exceptions import PTCToolError
|
|
16
|
+
from aip_agents.ptc.prompt_builder import PromptConfig
|
|
17
|
+
from aip_agents.sandbox.defaults import DEFAULT_PTC_PACKAGES, DEFAULT_PTC_TEMPLATE
|
|
18
|
+
from aip_agents.utils.logger import get_logger
|
|
19
|
+
|
|
20
|
+
# Lazy import to avoid circular dependencies
|
|
21
|
+
# These are only needed for PTCSandboxExecutor
|
|
22
|
+
try:
|
|
23
|
+
from aip_agents.ptc.sandbox_bridge import build_sandbox_payload, wrap_ptc_code
|
|
24
|
+
from aip_agents.sandbox.e2b_runtime import E2BSandboxRuntime
|
|
25
|
+
from aip_agents.sandbox.types import SandboxExecutionResult
|
|
26
|
+
|
|
27
|
+
_SANDBOX_DEPS_AVAILABLE = True
|
|
28
|
+
except ImportError:
|
|
29
|
+
_SANDBOX_DEPS_AVAILABLE = False
|
|
30
|
+
|
|
31
|
+
logger = get_logger(__name__)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class PTCSandboxConfig:
|
|
36
|
+
"""Configuration for PTC sandbox executor (MCP-only).
|
|
37
|
+
|
|
38
|
+
Attributes:
|
|
39
|
+
enabled: Whether PTC is enabled. When False, PTC is disabled.
|
|
40
|
+
default_tool_timeout: Default timeout per tool call in seconds.
|
|
41
|
+
sandbox_template: Optional E2B sandbox template ID.
|
|
42
|
+
sandbox_timeout: Sandbox execution timeout in seconds (hard cap/TTL).
|
|
43
|
+
ptc_packages: List of packages to install in sandbox. None or empty list skips install.
|
|
44
|
+
prompt: Prompt configuration for PTC usage guidance.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
enabled: bool = False
|
|
48
|
+
default_tool_timeout: float = 60.0
|
|
49
|
+
sandbox_template: str | None = DEFAULT_PTC_TEMPLATE
|
|
50
|
+
sandbox_timeout: float = 300.0
|
|
51
|
+
ptc_packages: list[str] | None = field(default_factory=lambda: list(DEFAULT_PTC_PACKAGES))
|
|
52
|
+
prompt: PromptConfig = field(default_factory=PromptConfig)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class PTCSandboxExecutor:
|
|
56
|
+
r"""Executes PTC code inside an E2B sandbox (MCP-only).
|
|
57
|
+
|
|
58
|
+
This executor is used for LLM-generated code that requires sandboxing.
|
|
59
|
+
It builds a sandbox payload (MCP server config + generated tool modules)
|
|
60
|
+
and executes the code using the E2B runtime.
|
|
61
|
+
|
|
62
|
+
Example:
|
|
63
|
+
runtime = E2BSandboxRuntime()
|
|
64
|
+
executor = PTCSandboxExecutor(mcp_client, runtime)
|
|
65
|
+
result = await executor.execute_code("from tools.yfinance import get_stock\nprint(get_stock('AAPL'))")
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
def __init__(
|
|
69
|
+
self,
|
|
70
|
+
mcp_client: BaseMCPClient,
|
|
71
|
+
runtime: E2BSandboxRuntime,
|
|
72
|
+
config: PTCSandboxConfig | None = None,
|
|
73
|
+
) -> None:
|
|
74
|
+
"""Initialize PTCSandboxExecutor.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
mcp_client: The MCP client with configured servers.
|
|
78
|
+
runtime: The E2B sandbox runtime instance.
|
|
79
|
+
config: Optional sandbox executor configuration.
|
|
80
|
+
|
|
81
|
+
Raises:
|
|
82
|
+
ImportError: If sandbox dependencies are not available.
|
|
83
|
+
"""
|
|
84
|
+
if not _SANDBOX_DEPS_AVAILABLE:
|
|
85
|
+
raise ImportError(
|
|
86
|
+
"Sandbox dependencies not available. "
|
|
87
|
+
"PTCSandboxExecutor requires sandbox_bridge and e2b_runtime modules."
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
self._mcp_client = mcp_client
|
|
91
|
+
self._runtime = runtime
|
|
92
|
+
self._config = config or PTCSandboxConfig()
|
|
93
|
+
|
|
94
|
+
async def execute_code(
|
|
95
|
+
self,
|
|
96
|
+
code: str,
|
|
97
|
+
) -> SandboxExecutionResult:
|
|
98
|
+
"""Execute code inside the sandbox with MCP access.
|
|
99
|
+
|
|
100
|
+
This method:
|
|
101
|
+
1. Builds the sandbox payload (MCP config + generated tool modules)
|
|
102
|
+
2. Wraps the user code with necessary imports and setup
|
|
103
|
+
3. Executes the code in the E2B sandbox
|
|
104
|
+
4. Returns the execution result (stdout/stderr/exit_code)
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
code: Python code to execute in the sandbox.
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
SandboxExecutionResult with stdout, stderr, and exit_code.
|
|
111
|
+
|
|
112
|
+
Raises:
|
|
113
|
+
PTCToolError: If sandbox execution fails.
|
|
114
|
+
"""
|
|
115
|
+
try:
|
|
116
|
+
logger.info("Building sandbox payload")
|
|
117
|
+
payload = await build_sandbox_payload(
|
|
118
|
+
self._mcp_client,
|
|
119
|
+
self._config.default_tool_timeout,
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
logger.info("Wrapping PTC code")
|
|
123
|
+
wrapped_code = wrap_ptc_code(code)
|
|
124
|
+
|
|
125
|
+
logger.info(f"Executing code in sandbox (timeout: {self._config.sandbox_timeout}s)")
|
|
126
|
+
result = await self._runtime.execute(
|
|
127
|
+
code=wrapped_code,
|
|
128
|
+
timeout=self._config.sandbox_timeout,
|
|
129
|
+
files=payload.files if payload.files else None,
|
|
130
|
+
env=payload.env,
|
|
131
|
+
template=self._config.sandbox_template,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
if result.exit_code == 0:
|
|
135
|
+
logger.info("Sandbox execution completed successfully")
|
|
136
|
+
else:
|
|
137
|
+
logger.warning(f"Sandbox execution failed with exit code {result.exit_code}")
|
|
138
|
+
|
|
139
|
+
return result
|
|
140
|
+
|
|
141
|
+
except Exception as exc:
|
|
142
|
+
logger.error(f"Sandbox execution failed: {exc}")
|
|
143
|
+
raise PTCToolError(f"Sandbox execution failed: {exc}") from exc
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
from _typeshed import Incomplete
|
|
2
|
+
from aip_agents.mcp.client.base_mcp_client import BaseMCPClient as BaseMCPClient
|
|
3
|
+
from aip_agents.ptc.exceptions import PTCToolError as PTCToolError
|
|
4
|
+
from aip_agents.ptc.prompt_builder import PromptConfig as PromptConfig
|
|
5
|
+
from aip_agents.ptc.sandbox_bridge import build_sandbox_payload as build_sandbox_payload, wrap_ptc_code as wrap_ptc_code
|
|
6
|
+
from aip_agents.sandbox.defaults import DEFAULT_PTC_PACKAGES as DEFAULT_PTC_PACKAGES, DEFAULT_PTC_TEMPLATE as DEFAULT_PTC_TEMPLATE
|
|
7
|
+
from aip_agents.sandbox.e2b_runtime import E2BSandboxRuntime as E2BSandboxRuntime
|
|
8
|
+
from aip_agents.sandbox.types import SandboxExecutionResult as SandboxExecutionResult
|
|
9
|
+
from aip_agents.utils.logger import get_logger as get_logger
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
|
|
12
|
+
logger: Incomplete
|
|
13
|
+
|
|
14
|
+
@dataclass
|
|
15
|
+
class PTCSandboxConfig:
|
|
16
|
+
"""Configuration for PTC sandbox executor (MCP-only).
|
|
17
|
+
|
|
18
|
+
Attributes:
|
|
19
|
+
enabled: Whether PTC is enabled. When False, PTC is disabled.
|
|
20
|
+
default_tool_timeout: Default timeout per tool call in seconds.
|
|
21
|
+
sandbox_template: Optional E2B sandbox template ID.
|
|
22
|
+
sandbox_timeout: Sandbox execution timeout in seconds (hard cap/TTL).
|
|
23
|
+
ptc_packages: List of packages to install in sandbox. None or empty list skips install.
|
|
24
|
+
prompt: Prompt configuration for PTC usage guidance.
|
|
25
|
+
"""
|
|
26
|
+
enabled: bool = ...
|
|
27
|
+
default_tool_timeout: float = ...
|
|
28
|
+
sandbox_template: str | None = ...
|
|
29
|
+
sandbox_timeout: float = ...
|
|
30
|
+
ptc_packages: list[str] | None = field(default_factory=Incomplete)
|
|
31
|
+
prompt: PromptConfig = field(default_factory=PromptConfig)
|
|
32
|
+
|
|
33
|
+
class PTCSandboxExecutor:
|
|
34
|
+
'''Executes PTC code inside an E2B sandbox (MCP-only).
|
|
35
|
+
|
|
36
|
+
This executor is used for LLM-generated code that requires sandboxing.
|
|
37
|
+
It builds a sandbox payload (MCP server config + generated tool modules)
|
|
38
|
+
and executes the code using the E2B runtime.
|
|
39
|
+
|
|
40
|
+
Example:
|
|
41
|
+
runtime = E2BSandboxRuntime()
|
|
42
|
+
executor = PTCSandboxExecutor(mcp_client, runtime)
|
|
43
|
+
result = await executor.execute_code("from tools.yfinance import get_stock\\nprint(get_stock(\'AAPL\'))")
|
|
44
|
+
'''
|
|
45
|
+
def __init__(self, mcp_client: BaseMCPClient, runtime: E2BSandboxRuntime, config: PTCSandboxConfig | None = None) -> None:
|
|
46
|
+
"""Initialize PTCSandboxExecutor.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
mcp_client: The MCP client with configured servers.
|
|
50
|
+
runtime: The E2B sandbox runtime instance.
|
|
51
|
+
config: Optional sandbox executor configuration.
|
|
52
|
+
|
|
53
|
+
Raises:
|
|
54
|
+
ImportError: If sandbox dependencies are not available.
|
|
55
|
+
"""
|
|
56
|
+
async def execute_code(self, code: str) -> SandboxExecutionResult:
|
|
57
|
+
"""Execute code inside the sandbox with MCP access.
|
|
58
|
+
|
|
59
|
+
This method:
|
|
60
|
+
1. Builds the sandbox payload (MCP config + generated tool modules)
|
|
61
|
+
2. Wraps the user code with necessary imports and setup
|
|
62
|
+
3. Executes the code in the E2B sandbox
|
|
63
|
+
4. Returns the execution result (stdout/stderr/exit_code)
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
code: Python code to execute in the sandbox.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
SandboxExecutionResult with stdout, stderr, and exit_code.
|
|
70
|
+
|
|
71
|
+
Raises:
|
|
72
|
+
PTCToolError: If sandbox execution fails.
|
|
73
|
+
"""
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Programmatic Tool Calling (PTC) module for MCP tools.
|
|
2
|
+
|
|
3
|
+
This module provides programmatic tool calling capabilities for MCP tools,
|
|
4
|
+
allowing code-based tool invocation instead of JSON tool calls.
|
|
5
|
+
|
|
6
|
+
Authors:
|
|
7
|
+
Putu Ravindra Wiguna (putu.r.wiguna@gdplabs.id)
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from aip_agents.ptc.exceptions import PTCError, PTCToolError
|
|
11
|
+
from aip_agents.ptc.mcp.sandbox_bridge import (
|
|
12
|
+
ServerConfig,
|
|
13
|
+
build_mcp_payload,
|
|
14
|
+
)
|
|
15
|
+
from aip_agents.ptc.naming import (
|
|
16
|
+
json_type_to_python,
|
|
17
|
+
sanitize_function_name,
|
|
18
|
+
sanitize_module_name,
|
|
19
|
+
sanitize_param_name,
|
|
20
|
+
schema_to_params,
|
|
21
|
+
)
|
|
22
|
+
from aip_agents.ptc.payload import SandboxPayload
|
|
23
|
+
from aip_agents.ptc.prompt_builder import (
|
|
24
|
+
build_ptc_prompt,
|
|
25
|
+
compute_ptc_prompt_hash,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
# Exceptions
|
|
30
|
+
"PTCError",
|
|
31
|
+
"PTCToolError",
|
|
32
|
+
# Naming utilities
|
|
33
|
+
"json_type_to_python",
|
|
34
|
+
"sanitize_function_name",
|
|
35
|
+
"sanitize_module_name",
|
|
36
|
+
"sanitize_param_name",
|
|
37
|
+
"schema_to_params",
|
|
38
|
+
# Prompt builder
|
|
39
|
+
"build_ptc_prompt",
|
|
40
|
+
"compute_ptc_prompt_hash",
|
|
41
|
+
# Sandbox Bridge
|
|
42
|
+
"SandboxPayload",
|
|
43
|
+
"ServerConfig",
|
|
44
|
+
"build_mcp_payload",
|
|
45
|
+
]
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
from aip_agents.ptc.exceptions import PTCError as PTCError, PTCToolError as PTCToolError
|
|
2
|
+
from aip_agents.ptc.mcp.sandbox_bridge import ServerConfig as ServerConfig, build_mcp_payload as build_mcp_payload
|
|
3
|
+
from aip_agents.ptc.naming import json_type_to_python as json_type_to_python, sanitize_function_name as sanitize_function_name, sanitize_module_name as sanitize_module_name, sanitize_param_name as sanitize_param_name, schema_to_params as schema_to_params
|
|
4
|
+
from aip_agents.ptc.payload import SandboxPayload as SandboxPayload
|
|
5
|
+
from aip_agents.ptc.prompt_builder import build_ptc_prompt as build_ptc_prompt, compute_ptc_prompt_hash as compute_ptc_prompt_hash
|
|
6
|
+
|
|
7
|
+
__all__ = ['PTCError', 'PTCToolError', 'json_type_to_python', 'sanitize_function_name', 'sanitize_module_name', 'sanitize_param_name', 'schema_to_params', 'build_ptc_prompt', 'compute_ptc_prompt_hash', 'SandboxPayload', 'ServerConfig', 'build_mcp_payload']
|