aip-agents-binary 0.6.0__py3-none-any.whl → 0.6.2__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/langgraph_react_agent.py +194 -2
- aip_agents/examples/hello_world_ptc.py +49 -0
- aip_agents/ptc/__init__.py +48 -0
- aip_agents/ptc/doc_gen.py +122 -0
- aip_agents/ptc/exceptions.py +39 -0
- aip_agents/ptc/executor.py +143 -0
- aip_agents/ptc/mcp/__init__.py +45 -0
- aip_agents/ptc/mcp/sandbox_bridge.py +668 -0
- aip_agents/ptc/mcp/templates/__init__.py +1 -0
- aip_agents/ptc/mcp/templates/mcp_client.py.template +239 -0
- aip_agents/ptc/naming.py +184 -0
- aip_agents/ptc/payload.py +26 -0
- aip_agents/ptc/prompt_builder.py +571 -0
- aip_agents/ptc/ptc_helper.py +16 -0
- aip_agents/ptc/sandbox_bridge.py +58 -0
- aip_agents/ptc/template_utils.py +33 -0
- aip_agents/ptc/templates/__init__.py +1 -0
- aip_agents/ptc/templates/ptc_helper.py.template +134 -0
- aip_agents/sandbox/__init__.py +43 -0
- aip_agents/sandbox/defaults.py +9 -0
- aip_agents/sandbox/e2b_runtime.py +267 -0
- aip_agents/sandbox/template_builder.py +131 -0
- aip_agents/sandbox/types.py +24 -0
- aip_agents/sandbox/validation.py +50 -0
- aip_agents/tools/__init__.py +2 -0
- aip_agents/tools/execute_ptc_code.py +308 -0
- {aip_agents_binary-0.6.0.dist-info → aip_agents_binary-0.6.2.dist-info}/METADATA +1 -1
- {aip_agents_binary-0.6.0.dist-info → aip_agents_binary-0.6.2.dist-info}/RECORD +30 -282
- aip_agents/__init__.pyi +0 -19
- aip_agents/a2a/__init__.pyi +0 -3
- aip_agents/a2a/server/__init__.pyi +0 -4
- aip_agents/a2a/server/base_executor.pyi +0 -73
- aip_agents/a2a/server/google_adk_executor.pyi +0 -51
- aip_agents/a2a/server/langflow_executor.pyi +0 -43
- aip_agents/a2a/server/langgraph_executor.pyi +0 -47
- aip_agents/a2a/types.pyi +0 -132
- aip_agents/agent/__init__.pyi +0 -9
- aip_agents/agent/base_agent.pyi +0 -221
- aip_agents/agent/base_langgraph_agent.pyi +0 -233
- aip_agents/agent/google_adk_agent.pyi +0 -141
- aip_agents/agent/google_adk_constants.pyi +0 -3
- aip_agents/agent/hitl/__init__.pyi +0 -6
- aip_agents/agent/hitl/config.pyi +0 -15
- aip_agents/agent/hitl/langgraph_hitl_mixin.pyi +0 -42
- aip_agents/agent/hitl/manager.pyi +0 -200
- aip_agents/agent/hitl/models.pyi +0 -3
- aip_agents/agent/hitl/prompt/__init__.pyi +0 -4
- aip_agents/agent/hitl/prompt/base.pyi +0 -24
- aip_agents/agent/hitl/prompt/deferred.pyi +0 -30
- aip_agents/agent/hitl/registry.pyi +0 -101
- aip_agents/agent/interface.pyi +0 -81
- aip_agents/agent/interfaces.pyi +0 -44
- aip_agents/agent/langflow_agent.pyi +0 -133
- aip_agents/agent/langgraph_memory_enhancer_agent.pyi +0 -49
- aip_agents/agent/langgraph_react_agent.pyi +0 -131
- aip_agents/agent/system_instruction_context.pyi +0 -13
- aip_agents/clients/__init__.pyi +0 -4
- aip_agents/clients/langflow/__init__.pyi +0 -4
- aip_agents/clients/langflow/client.pyi +0 -140
- aip_agents/clients/langflow/types.pyi +0 -7
- aip_agents/constants.pyi +0 -7
- aip_agents/examples/__init__.pyi +0 -0
- aip_agents/examples/compare_streaming_client.pyi +0 -48
- aip_agents/examples/compare_streaming_server.pyi +0 -18
- aip_agents/examples/demo_memory_recall.pyi +0 -58
- aip_agents/examples/hello_world_a2a_google_adk_client.pyi +0 -9
- aip_agents/examples/hello_world_a2a_google_adk_client_agent.pyi +0 -9
- aip_agents/examples/hello_world_a2a_google_adk_client_streaming.pyi +0 -9
- aip_agents/examples/hello_world_a2a_google_adk_server.pyi +0 -15
- aip_agents/examples/hello_world_a2a_langchain_client.pyi +0 -5
- aip_agents/examples/hello_world_a2a_langchain_client_agent.pyi +0 -5
- aip_agents/examples/hello_world_a2a_langchain_client_lm_invoker.pyi +0 -5
- aip_agents/examples/hello_world_a2a_langchain_client_streaming.pyi +0 -5
- aip_agents/examples/hello_world_a2a_langchain_reference_client_streaming.pyi +0 -5
- aip_agents/examples/hello_world_a2a_langchain_reference_server.pyi +0 -15
- aip_agents/examples/hello_world_a2a_langchain_server.pyi +0 -15
- aip_agents/examples/hello_world_a2a_langchain_server_lm_invoker.pyi +0 -15
- aip_agents/examples/hello_world_a2a_langflow_client.pyi +0 -9
- aip_agents/examples/hello_world_a2a_langflow_server.pyi +0 -14
- aip_agents/examples/hello_world_a2a_langgraph_artifact_client.pyi +0 -5
- aip_agents/examples/hello_world_a2a_langgraph_artifact_client_streaming.pyi +0 -5
- aip_agents/examples/hello_world_a2a_langgraph_artifact_server.pyi +0 -16
- aip_agents/examples/hello_world_a2a_langgraph_client.pyi +0 -9
- aip_agents/examples/hello_world_a2a_langgraph_client_agent.pyi +0 -9
- aip_agents/examples/hello_world_a2a_langgraph_client_agent_lm_invoker.pyi +0 -2
- aip_agents/examples/hello_world_a2a_langgraph_client_streaming.pyi +0 -9
- aip_agents/examples/hello_world_a2a_langgraph_client_streaming_lm_invoker.pyi +0 -5
- aip_agents/examples/hello_world_a2a_langgraph_client_streaming_tool_streaming.pyi +0 -5
- aip_agents/examples/hello_world_a2a_langgraph_server.pyi +0 -14
- aip_agents/examples/hello_world_a2a_langgraph_server_lm_invoker.pyi +0 -15
- aip_agents/examples/hello_world_a2a_langgraph_server_tool_streaming.pyi +0 -15
- aip_agents/examples/hello_world_a2a_mcp_langgraph.pyi +0 -48
- aip_agents/examples/hello_world_a2a_three_level_agent_hierarchy_client.pyi +0 -48
- aip_agents/examples/hello_world_a2a_three_level_agent_hierarchy_server.pyi +0 -45
- aip_agents/examples/hello_world_a2a_with_metadata_langchain_client.pyi +0 -5
- aip_agents/examples/hello_world_a2a_with_metadata_langchain_server_lm_invoker.pyi +0 -15
- aip_agents/examples/hello_world_google_adk.pyi +0 -5
- aip_agents/examples/hello_world_google_adk_mcp_http.pyi +0 -5
- aip_agents/examples/hello_world_google_adk_mcp_http_stream.pyi +0 -5
- aip_agents/examples/hello_world_google_adk_mcp_sse.pyi +0 -5
- aip_agents/examples/hello_world_google_adk_mcp_sse_stream.pyi +0 -5
- aip_agents/examples/hello_world_google_adk_mcp_stdio.pyi +0 -5
- aip_agents/examples/hello_world_google_adk_mcp_stdio_stream.pyi +0 -5
- aip_agents/examples/hello_world_google_adk_stream.pyi +0 -5
- aip_agents/examples/hello_world_langchain.pyi +0 -5
- aip_agents/examples/hello_world_langchain_lm_invoker.pyi +0 -2
- aip_agents/examples/hello_world_langchain_mcp_http.pyi +0 -5
- aip_agents/examples/hello_world_langchain_mcp_http_interactive.pyi +0 -16
- aip_agents/examples/hello_world_langchain_mcp_http_stream.pyi +0 -5
- aip_agents/examples/hello_world_langchain_mcp_multi_server.pyi +0 -18
- aip_agents/examples/hello_world_langchain_mcp_sse.pyi +0 -5
- aip_agents/examples/hello_world_langchain_mcp_sse_stream.pyi +0 -5
- aip_agents/examples/hello_world_langchain_mcp_stdio.pyi +0 -5
- aip_agents/examples/hello_world_langchain_mcp_stdio_stream.pyi +0 -5
- aip_agents/examples/hello_world_langchain_stream.pyi +0 -5
- aip_agents/examples/hello_world_langchain_stream_lm_invoker.pyi +0 -5
- aip_agents/examples/hello_world_langflow_agent.pyi +0 -35
- aip_agents/examples/hello_world_langgraph.pyi +0 -5
- aip_agents/examples/hello_world_langgraph_gl_connector_twitter.pyi +0 -5
- aip_agents/examples/hello_world_langgraph_mcp_http.pyi +0 -5
- aip_agents/examples/hello_world_langgraph_mcp_http_stream.pyi +0 -5
- aip_agents/examples/hello_world_langgraph_mcp_sse.pyi +0 -5
- aip_agents/examples/hello_world_langgraph_mcp_sse_stream.pyi +0 -5
- aip_agents/examples/hello_world_langgraph_mcp_stdio.pyi +0 -5
- aip_agents/examples/hello_world_langgraph_mcp_stdio_stream.pyi +0 -5
- aip_agents/examples/hello_world_langgraph_stream.pyi +0 -5
- aip_agents/examples/hello_world_langgraph_stream_lm_invoker.pyi +0 -5
- aip_agents/examples/hello_world_model_switch_cli.pyi +0 -30
- aip_agents/examples/hello_world_multi_agent_adk.pyi +0 -6
- aip_agents/examples/hello_world_multi_agent_langchain.pyi +0 -5
- aip_agents/examples/hello_world_multi_agent_langgraph.pyi +0 -5
- aip_agents/examples/hello_world_multi_agent_langgraph_lm_invoker.pyi +0 -5
- aip_agents/examples/hello_world_pii_logger.pyi +0 -5
- aip_agents/examples/hello_world_sentry.pyi +0 -21
- aip_agents/examples/hello_world_step_limits.pyi +0 -17
- aip_agents/examples/hello_world_stock_a2a_server.pyi +0 -17
- aip_agents/examples/hello_world_tool_output_client.pyi +0 -5
- aip_agents/examples/hello_world_tool_output_server.pyi +0 -19
- aip_agents/examples/hitl_demo.pyi +0 -67
- aip_agents/examples/pii_demo_langgraph_client.pyi +0 -5
- aip_agents/examples/pii_demo_langgraph_server.pyi +0 -20
- aip_agents/examples/pii_demo_multi_agent_client.pyi +0 -5
- aip_agents/examples/pii_demo_multi_agent_server.pyi +0 -40
- aip_agents/examples/todolist_planning_a2a_langchain_client.pyi +0 -5
- aip_agents/examples/todolist_planning_a2a_langgraph_server.pyi +0 -19
- aip_agents/examples/tools/__init__.pyi +0 -9
- aip_agents/examples/tools/adk_arithmetic_tools.pyi +0 -24
- aip_agents/examples/tools/adk_weather_tool.pyi +0 -18
- aip_agents/examples/tools/data_generator_tool.pyi +0 -15
- aip_agents/examples/tools/data_visualization_tool.pyi +0 -19
- aip_agents/examples/tools/image_artifact_tool.pyi +0 -26
- aip_agents/examples/tools/langchain_arithmetic_tools.pyi +0 -17
- aip_agents/examples/tools/langchain_currency_exchange_tool.pyi +0 -20
- aip_agents/examples/tools/langchain_graph_artifact_tool.pyi +0 -25
- aip_agents/examples/tools/langchain_weather_tool.pyi +0 -19
- aip_agents/examples/tools/langgraph_streaming_tool.pyi +0 -43
- aip_agents/examples/tools/mock_retrieval_tool.pyi +0 -13
- aip_agents/examples/tools/pii_demo_tools.pyi +0 -54
- aip_agents/examples/tools/random_chart_tool.pyi +0 -20
- aip_agents/examples/tools/serper_tool.pyi +0 -16
- aip_agents/examples/tools/stock_tools.pyi +0 -36
- aip_agents/examples/tools/table_generator_tool.pyi +0 -22
- aip_agents/examples/tools/time_tool.pyi +0 -15
- aip_agents/examples/tools/weather_forecast_tool.pyi +0 -14
- aip_agents/guardrails/__init__.pyi +0 -6
- aip_agents/guardrails/engines/__init__.pyi +0 -4
- aip_agents/guardrails/engines/base.pyi +0 -61
- aip_agents/guardrails/engines/nemo.pyi +0 -46
- aip_agents/guardrails/engines/phrase_matcher.pyi +0 -48
- aip_agents/guardrails/exceptions.pyi +0 -23
- aip_agents/guardrails/manager.pyi +0 -42
- aip_agents/guardrails/middleware.pyi +0 -87
- aip_agents/guardrails/schemas.pyi +0 -43
- aip_agents/guardrails/utils.pyi +0 -19
- aip_agents/mcp/__init__.pyi +0 -0
- aip_agents/mcp/client/__init__.pyi +0 -5
- aip_agents/mcp/client/base_mcp_client.pyi +0 -148
- aip_agents/mcp/client/connection_manager.pyi +0 -51
- aip_agents/mcp/client/google_adk/__init__.pyi +0 -3
- aip_agents/mcp/client/google_adk/client.pyi +0 -75
- aip_agents/mcp/client/langchain/__init__.pyi +0 -3
- aip_agents/mcp/client/langchain/client.pyi +0 -48
- aip_agents/mcp/client/persistent_session.pyi +0 -122
- aip_agents/mcp/client/session_pool.pyi +0 -101
- aip_agents/mcp/client/transports.pyi +0 -132
- aip_agents/mcp/utils/__init__.pyi +0 -0
- aip_agents/mcp/utils/config_validator.pyi +0 -82
- aip_agents/memory/__init__.pyi +0 -5
- aip_agents/memory/adapters/__init__.pyi +0 -4
- aip_agents/memory/adapters/base_adapter.pyi +0 -150
- aip_agents/memory/adapters/mem0.pyi +0 -22
- aip_agents/memory/base.pyi +0 -60
- aip_agents/memory/constants.pyi +0 -25
- aip_agents/memory/factory.pyi +0 -24
- aip_agents/memory/guidance.pyi +0 -3
- aip_agents/memory/simple_memory.pyi +0 -23
- aip_agents/middleware/__init__.pyi +0 -5
- aip_agents/middleware/base.pyi +0 -75
- aip_agents/middleware/manager.pyi +0 -84
- aip_agents/middleware/todolist.pyi +0 -125
- aip_agents/schema/__init__.pyi +0 -9
- aip_agents/schema/a2a.pyi +0 -40
- aip_agents/schema/agent.pyi +0 -65
- aip_agents/schema/hitl.pyi +0 -89
- aip_agents/schema/langgraph.pyi +0 -28
- aip_agents/schema/model_id.pyi +0 -54
- aip_agents/schema/step_limit.pyi +0 -63
- aip_agents/schema/storage.pyi +0 -21
- aip_agents/sentry/__init__.pyi +0 -3
- aip_agents/sentry/sentry.pyi +0 -48
- aip_agents/storage/__init__.pyi +0 -8
- aip_agents/storage/base.pyi +0 -58
- aip_agents/storage/clients/__init__.pyi +0 -3
- aip_agents/storage/clients/minio_client.pyi +0 -137
- aip_agents/storage/config.pyi +0 -29
- aip_agents/storage/providers/__init__.pyi +0 -5
- aip_agents/storage/providers/base.pyi +0 -88
- aip_agents/storage/providers/memory.pyi +0 -79
- aip_agents/storage/providers/object_storage.pyi +0 -98
- aip_agents/tools/__init__.pyi +0 -9
- aip_agents/tools/browser_use/__init__.pyi +0 -14
- aip_agents/tools/browser_use/action_parser.pyi +0 -18
- aip_agents/tools/browser_use/browser_use_tool.pyi +0 -50
- aip_agents/tools/browser_use/llm_config.pyi +0 -52
- aip_agents/tools/browser_use/minio_storage.pyi +0 -109
- aip_agents/tools/browser_use/schemas.pyi +0 -32
- aip_agents/tools/browser_use/session.pyi +0 -4
- aip_agents/tools/browser_use/session_errors.pyi +0 -53
- aip_agents/tools/browser_use/steel_session_recording.pyi +0 -63
- aip_agents/tools/browser_use/streaming.pyi +0 -81
- aip_agents/tools/browser_use/structured_data_parser.pyi +0 -86
- aip_agents/tools/browser_use/structured_data_recovery.pyi +0 -43
- aip_agents/tools/browser_use/types.pyi +0 -45
- aip_agents/tools/code_sandbox/__init__.pyi +0 -3
- aip_agents/tools/code_sandbox/constant.pyi +0 -4
- aip_agents/tools/code_sandbox/e2b_cloud_sandbox_extended.pyi +0 -102
- aip_agents/tools/code_sandbox/e2b_sandbox_tool.pyi +0 -29
- aip_agents/tools/constants.pyi +0 -138
- aip_agents/tools/document_loader/__init__.pyi +0 -7
- aip_agents/tools/document_loader/base_reader.pyi +0 -75
- aip_agents/tools/document_loader/docx_reader_tool.pyi +0 -10
- aip_agents/tools/document_loader/excel_reader_tool.pyi +0 -26
- aip_agents/tools/document_loader/pdf_reader_tool.pyi +0 -11
- aip_agents/tools/document_loader/pdf_splitter.pyi +0 -18
- aip_agents/tools/gl_connector/__init__.pyi +0 -3
- aip_agents/tools/gl_connector/tool.pyi +0 -74
- aip_agents/tools/gl_connector_tools.pyi +0 -39
- aip_agents/tools/memory_search/__init__.pyi +0 -5
- aip_agents/tools/memory_search/base.pyi +0 -69
- aip_agents/tools/memory_search/mem0.pyi +0 -19
- aip_agents/tools/memory_search/schema.pyi +0 -15
- aip_agents/tools/memory_search_tool.pyi +0 -3
- aip_agents/tools/time_tool.pyi +0 -16
- aip_agents/tools/tool_config_injector.pyi +0 -26
- aip_agents/tools/web_search/__init__.pyi +0 -3
- aip_agents/tools/web_search/serper_tool.pyi +0 -19
- aip_agents/types/__init__.pyi +0 -36
- aip_agents/types/a2a_events.pyi +0 -3
- aip_agents/utils/__init__.pyi +0 -11
- aip_agents/utils/a2a_connector.pyi +0 -146
- aip_agents/utils/artifact_helpers.pyi +0 -203
- aip_agents/utils/constants.pyi +0 -10
- aip_agents/utils/datetime/__init__.pyi +0 -4
- aip_agents/utils/datetime/normalization.pyi +0 -95
- aip_agents/utils/datetime/timezone.pyi +0 -48
- aip_agents/utils/env_loader.pyi +0 -10
- aip_agents/utils/event_handler_registry.pyi +0 -23
- aip_agents/utils/file_prompt_utils.pyi +0 -21
- aip_agents/utils/final_response_builder.pyi +0 -34
- aip_agents/utils/formatter_llm_client.pyi +0 -71
- aip_agents/utils/langgraph/__init__.pyi +0 -3
- aip_agents/utils/langgraph/converter.pyi +0 -49
- aip_agents/utils/langgraph/tool_managers/__init__.pyi +0 -5
- aip_agents/utils/langgraph/tool_managers/a2a_tool_manager.pyi +0 -35
- aip_agents/utils/langgraph/tool_managers/base_tool_manager.pyi +0 -48
- aip_agents/utils/langgraph/tool_managers/delegation_tool_manager.pyi +0 -56
- aip_agents/utils/langgraph/tool_output_management.pyi +0 -329
- aip_agents/utils/logger.pyi +0 -60
- aip_agents/utils/metadata/__init__.pyi +0 -5
- aip_agents/utils/metadata/activity_metadata_helper.pyi +0 -25
- aip_agents/utils/metadata/activity_narrative/__init__.pyi +0 -7
- aip_agents/utils/metadata/activity_narrative/builder.pyi +0 -35
- aip_agents/utils/metadata/activity_narrative/constants.pyi +0 -10
- aip_agents/utils/metadata/activity_narrative/context.pyi +0 -32
- aip_agents/utils/metadata/activity_narrative/formatters.pyi +0 -48
- aip_agents/utils/metadata/activity_narrative/utils.pyi +0 -12
- aip_agents/utils/metadata/schemas/__init__.pyi +0 -4
- aip_agents/utils/metadata/schemas/activity_schema.pyi +0 -18
- aip_agents/utils/metadata/schemas/thinking_schema.pyi +0 -20
- aip_agents/utils/metadata/thinking_metadata_helper.pyi +0 -4
- aip_agents/utils/metadata_helper.pyi +0 -117
- aip_agents/utils/name_preprocessor/__init__.pyi +0 -6
- aip_agents/utils/name_preprocessor/base_name_preprocessor.pyi +0 -52
- aip_agents/utils/name_preprocessor/google_name_preprocessor.pyi +0 -38
- aip_agents/utils/name_preprocessor/name_preprocessor.pyi +0 -41
- aip_agents/utils/name_preprocessor/openai_name_preprocessor.pyi +0 -34
- aip_agents/utils/pii/__init__.pyi +0 -5
- aip_agents/utils/pii/pii_handler.pyi +0 -96
- aip_agents/utils/pii/pii_helper.pyi +0 -78
- aip_agents/utils/pii/uuid_deanonymizer_mapping.pyi +0 -73
- aip_agents/utils/reference_helper.pyi +0 -81
- aip_agents/utils/sse_chunk_transformer.pyi +0 -166
- aip_agents/utils/step_limit_manager.pyi +0 -112
- aip_agents/utils/token_usage_helper.pyi +0 -60
- {aip_agents_binary-0.6.0.dist-info → aip_agents_binary-0.6.2.dist-info}/WHEEL +0 -0
- {aip_agents_binary-0.6.0.dist-info → aip_agents_binary-0.6.2.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"""Execute PTC Code Tool.
|
|
2
|
+
|
|
3
|
+
This module provides a LangChain tool for executing Python code with MCP tool access
|
|
4
|
+
inside an E2B sandbox. The tool is designed for LLM-generated code that needs to call
|
|
5
|
+
multiple MCP tools programmatically.
|
|
6
|
+
|
|
7
|
+
Authors:
|
|
8
|
+
Putu Ravindra Wiguna (putu.r.wiguna@gdplabs.id)
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import asyncio
|
|
12
|
+
import concurrent.futures
|
|
13
|
+
import json
|
|
14
|
+
from typing import TYPE_CHECKING, Any
|
|
15
|
+
|
|
16
|
+
from langchain_core.callbacks import (
|
|
17
|
+
AsyncCallbackManagerForToolRun,
|
|
18
|
+
CallbackManagerForToolRun,
|
|
19
|
+
)
|
|
20
|
+
from langchain_core.tools import BaseTool
|
|
21
|
+
from pydantic import BaseModel, Field
|
|
22
|
+
|
|
23
|
+
from aip_agents.ptc.naming import sanitize_function_name
|
|
24
|
+
from aip_agents.tools.tool_config_injector import TOOL_CONFIGS_KEY
|
|
25
|
+
from aip_agents.utils.logger import get_logger
|
|
26
|
+
|
|
27
|
+
if TYPE_CHECKING:
|
|
28
|
+
from aip_agents.mcp.client.base_mcp_client import BaseMCPClient
|
|
29
|
+
from aip_agents.ptc.executor import PTCSandboxConfig, PTCSandboxExecutor
|
|
30
|
+
from aip_agents.sandbox.e2b_runtime import E2BSandboxRuntime
|
|
31
|
+
|
|
32
|
+
logger = get_logger(__name__)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class PTCCodeInput(BaseModel):
|
|
36
|
+
"""Input schema for PTCCodeTool."""
|
|
37
|
+
|
|
38
|
+
code: str = Field(
|
|
39
|
+
...,
|
|
40
|
+
description=(
|
|
41
|
+
"Python code to execute. Import MCP tools from the generated `tools` package, "
|
|
42
|
+
"for example: `from tools.yfinance import get_stock_history`. "
|
|
43
|
+
"The code runs in a sandboxed environment with access to all configured MCP tools. "
|
|
44
|
+
"Use print() to output results. The tool returns JSON with keys: "
|
|
45
|
+
"ok, stdout, stderr, exit_code."
|
|
46
|
+
),
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _merge_config_layer(
|
|
51
|
+
merged: dict[str, dict[str, Any]],
|
|
52
|
+
source: dict[str, Any],
|
|
53
|
+
skip_tool_configs_key: bool = True,
|
|
54
|
+
) -> None:
|
|
55
|
+
"""Merge a single layer of tool configs into the merged dict.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
merged: Target dict to merge into (modified in place).
|
|
59
|
+
source: Source dict containing tool configs.
|
|
60
|
+
skip_tool_configs_key: Whether to skip the TOOL_CONFIGS_KEY entry.
|
|
61
|
+
"""
|
|
62
|
+
for name, config in source.items():
|
|
63
|
+
if skip_tool_configs_key and name == TOOL_CONFIGS_KEY:
|
|
64
|
+
continue
|
|
65
|
+
if not isinstance(config, dict):
|
|
66
|
+
continue
|
|
67
|
+
|
|
68
|
+
sanitized = sanitize_function_name(name)
|
|
69
|
+
if sanitized in merged:
|
|
70
|
+
merged[sanitized].update(config)
|
|
71
|
+
else:
|
|
72
|
+
merged[sanitized] = dict(config)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def merge_tool_configs(
|
|
76
|
+
agent_configs: dict[str, Any] | None,
|
|
77
|
+
runtime_configs: dict[str, Any] | None,
|
|
78
|
+
) -> dict[str, dict[str, Any]]:
|
|
79
|
+
"""Merge agent-level and runtime tool configs with sanitized keys.
|
|
80
|
+
|
|
81
|
+
Merges tool configurations from two sources:
|
|
82
|
+
1. Agent-level defaults (from agent.tool_configs)
|
|
83
|
+
2. Runtime overrides (from RunnableConfig.metadata["tool_configs"])
|
|
84
|
+
|
|
85
|
+
Both sources support two formats (matching LangGraphReactAgent behavior):
|
|
86
|
+
- Direct per-tool keys: {"time_tool": {"timezone": "UTC"}}
|
|
87
|
+
- Nested structure: {"tool_configs": {"time_tool": {"timezone": "UTC"}}}
|
|
88
|
+
|
|
89
|
+
The nested "tool_configs" key has higher precedence than direct keys.
|
|
90
|
+
Tool names are sanitized to match sandbox expectations (e.g., "Time Tool" -> "time_tool").
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
agent_configs: Agent-level tool configs (may be None or contain nested dicts)
|
|
94
|
+
runtime_configs: Runtime overrides from metadata (may be None)
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
Merged dict with sanitized tool names as keys and config dicts as values.
|
|
98
|
+
Only includes entries that are dicts (non-dict values are agent-wide defaults).
|
|
99
|
+
"""
|
|
100
|
+
merged: dict[str, dict[str, Any]] = {}
|
|
101
|
+
|
|
102
|
+
# Layer 1: Agent-level per-tool configs (direct keys)
|
|
103
|
+
if agent_configs:
|
|
104
|
+
_merge_config_layer(merged, agent_configs, skip_tool_configs_key=True)
|
|
105
|
+
|
|
106
|
+
# Layer 2: Agent-level per-tool configs (nested tool_configs key)
|
|
107
|
+
if agent_configs:
|
|
108
|
+
nested_agent = agent_configs.get(TOOL_CONFIGS_KEY)
|
|
109
|
+
if isinstance(nested_agent, dict):
|
|
110
|
+
_merge_config_layer(merged, nested_agent, skip_tool_configs_key=False)
|
|
111
|
+
|
|
112
|
+
# Layer 3: Runtime per-tool configs (direct keys, override agent defaults)
|
|
113
|
+
if runtime_configs:
|
|
114
|
+
_merge_config_layer(merged, runtime_configs, skip_tool_configs_key=True)
|
|
115
|
+
|
|
116
|
+
# Layer 4: Runtime per-tool configs (nested tool_configs key, highest precedence)
|
|
117
|
+
if runtime_configs:
|
|
118
|
+
nested_runtime = runtime_configs.get(TOOL_CONFIGS_KEY)
|
|
119
|
+
if isinstance(nested_runtime, dict):
|
|
120
|
+
_merge_config_layer(merged, nested_runtime, skip_tool_configs_key=False)
|
|
121
|
+
|
|
122
|
+
return merged
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class PTCCodeTool(BaseTool):
|
|
126
|
+
"""Tool for executing Python code with MCP tool access in a sandbox.
|
|
127
|
+
|
|
128
|
+
This tool uses BaseTool to properly access runtime config via run_manager.metadata.
|
|
129
|
+
The config parameter is NOT exposed to the LLM schema - it's extracted from
|
|
130
|
+
the callback manager during execution.
|
|
131
|
+
"""
|
|
132
|
+
|
|
133
|
+
name: str = "execute_ptc_code"
|
|
134
|
+
description: str = (
|
|
135
|
+
"Execute Python code that can call MCP tools programmatically. "
|
|
136
|
+
"Import tools from the generated `tools` package (e.g., `from tools.yfinance import get_stock`) "
|
|
137
|
+
"and run normal Python code. Use print() to output results. "
|
|
138
|
+
"Returns JSON with ok, stdout, stderr, and exit_code keys. "
|
|
139
|
+
"This tool is useful for chaining multiple MCP tool calls with local data processing."
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
# Input schema for LangChain tool invocation
|
|
143
|
+
args_schema: type[BaseModel] = PTCCodeInput
|
|
144
|
+
|
|
145
|
+
# Internal attributes (not exposed to LLM)
|
|
146
|
+
_ptc_executor: "PTCSandboxExecutor" = None # type: ignore[assignment]
|
|
147
|
+
_ptc_runtime: "E2BSandboxRuntime" = None # type: ignore[assignment]
|
|
148
|
+
_agent_tool_configs: dict[str, Any] | None = None
|
|
149
|
+
|
|
150
|
+
def __init__(
|
|
151
|
+
self,
|
|
152
|
+
executor: "PTCSandboxExecutor",
|
|
153
|
+
runtime: "E2BSandboxRuntime",
|
|
154
|
+
agent_tool_configs: dict[str, Any] | None = None,
|
|
155
|
+
**kwargs: Any,
|
|
156
|
+
) -> None:
|
|
157
|
+
"""Initialize the PTC code tool.
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
executor: The PTC sandbox executor.
|
|
161
|
+
runtime: The E2B sandbox runtime.
|
|
162
|
+
agent_tool_configs: Optional agent-level tool configs.
|
|
163
|
+
**kwargs: Additional keyword arguments passed to BaseTool.
|
|
164
|
+
"""
|
|
165
|
+
super().__init__(**kwargs)
|
|
166
|
+
# Store as private attributes to avoid Pydantic field issues
|
|
167
|
+
object.__setattr__(self, "_ptc_executor", executor)
|
|
168
|
+
object.__setattr__(self, "_ptc_runtime", runtime)
|
|
169
|
+
object.__setattr__(self, "_agent_tool_configs", agent_tool_configs)
|
|
170
|
+
|
|
171
|
+
def _run(
|
|
172
|
+
self,
|
|
173
|
+
code: str,
|
|
174
|
+
run_manager: CallbackManagerForToolRun | None = None,
|
|
175
|
+
) -> str:
|
|
176
|
+
"""Execute code synchronously (wraps async version)."""
|
|
177
|
+
# Extract runtime metadata from run_manager
|
|
178
|
+
runtime_metadata = None
|
|
179
|
+
if run_manager and hasattr(run_manager, "metadata"):
|
|
180
|
+
runtime_metadata = run_manager.metadata
|
|
181
|
+
|
|
182
|
+
# Run async version in sync context
|
|
183
|
+
try:
|
|
184
|
+
asyncio.get_running_loop()
|
|
185
|
+
except RuntimeError:
|
|
186
|
+
return asyncio.run(self._execute(code, runtime_metadata))
|
|
187
|
+
|
|
188
|
+
# Already in async context - run in thread
|
|
189
|
+
def run_in_new_loop() -> str:
|
|
190
|
+
new_loop = asyncio.new_event_loop()
|
|
191
|
+
asyncio.set_event_loop(new_loop)
|
|
192
|
+
try:
|
|
193
|
+
return new_loop.run_until_complete(self._execute(code, runtime_metadata))
|
|
194
|
+
finally:
|
|
195
|
+
new_loop.close()
|
|
196
|
+
|
|
197
|
+
with concurrent.futures.ThreadPoolExecutor() as executor_service:
|
|
198
|
+
future = executor_service.submit(run_in_new_loop)
|
|
199
|
+
return future.result()
|
|
200
|
+
|
|
201
|
+
async def _arun(
|
|
202
|
+
self,
|
|
203
|
+
code: str,
|
|
204
|
+
run_manager: AsyncCallbackManagerForToolRun | None = None,
|
|
205
|
+
) -> str:
|
|
206
|
+
"""Execute code asynchronously."""
|
|
207
|
+
# Extract runtime metadata from run_manager
|
|
208
|
+
runtime_metadata = None
|
|
209
|
+
if run_manager and hasattr(run_manager, "metadata"):
|
|
210
|
+
runtime_metadata = run_manager.metadata
|
|
211
|
+
|
|
212
|
+
return await self._execute(code, runtime_metadata)
|
|
213
|
+
|
|
214
|
+
async def _execute(
|
|
215
|
+
self,
|
|
216
|
+
code: str,
|
|
217
|
+
runtime_metadata: dict[str, Any] | None,
|
|
218
|
+
) -> str:
|
|
219
|
+
"""Internal execution logic."""
|
|
220
|
+
try:
|
|
221
|
+
logger.info("Executing PTC code in sandbox")
|
|
222
|
+
result = await self._ptc_executor.execute_code(code)
|
|
223
|
+
|
|
224
|
+
if result.exit_code == 0:
|
|
225
|
+
logger.info("PTC code execution completed successfully")
|
|
226
|
+
payload = {
|
|
227
|
+
"ok": True,
|
|
228
|
+
"stdout": result.stdout,
|
|
229
|
+
"stderr": "",
|
|
230
|
+
"exit_code": 0,
|
|
231
|
+
}
|
|
232
|
+
return json.dumps(payload)
|
|
233
|
+
|
|
234
|
+
logger.warning(f"PTC code execution failed with exit code {result.exit_code}")
|
|
235
|
+
payload = {
|
|
236
|
+
"ok": False,
|
|
237
|
+
"stdout": result.stdout,
|
|
238
|
+
"stderr": result.stderr,
|
|
239
|
+
"exit_code": result.exit_code,
|
|
240
|
+
}
|
|
241
|
+
return json.dumps(payload)
|
|
242
|
+
|
|
243
|
+
except Exception as e:
|
|
244
|
+
logger.error(f"PTC code execution failed: {e}")
|
|
245
|
+
payload = {
|
|
246
|
+
"ok": False,
|
|
247
|
+
"stdout": "",
|
|
248
|
+
"stderr": f"Execution failed: {type(e).__name__}: {e}",
|
|
249
|
+
"exit_code": 1,
|
|
250
|
+
}
|
|
251
|
+
return json.dumps(payload)
|
|
252
|
+
|
|
253
|
+
async def cleanup(self) -> None:
|
|
254
|
+
"""Clean up the sandbox runtime."""
|
|
255
|
+
await self._ptc_runtime.cleanup()
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def create_execute_ptc_code_tool(
|
|
259
|
+
mcp_client: "BaseMCPClient | None",
|
|
260
|
+
config: "PTCSandboxConfig | None" = None, # noqa: F821
|
|
261
|
+
agent_tool_configs: dict[str, Any] | None = None,
|
|
262
|
+
) -> PTCCodeTool:
|
|
263
|
+
r"""Create a tool that executes Python code with MCP tool access.
|
|
264
|
+
|
|
265
|
+
The code runs inside an E2B sandbox with access to generated MCP tool modules.
|
|
266
|
+
This tool is designed for LLM-generated code that needs to call multiple tools
|
|
267
|
+
programmatically in a single execution.
|
|
268
|
+
|
|
269
|
+
Args:
|
|
270
|
+
mcp_client: The MCP client with configured servers.
|
|
271
|
+
config: Optional sandbox executor configuration.
|
|
272
|
+
agent_tool_configs: Optional agent-level tool configs (from agent.tool_configs).
|
|
273
|
+
These are merged with runtime overrides from RunnableConfig.metadata.
|
|
274
|
+
|
|
275
|
+
Returns:
|
|
276
|
+
PTCCodeTool configured for PTC code execution.
|
|
277
|
+
|
|
278
|
+
Example:
|
|
279
|
+
```python
|
|
280
|
+
from aip_agents.mcp.client import LangchainMCPClient
|
|
281
|
+
from aip_agents.tools.execute_ptc_code import create_execute_ptc_code_tool
|
|
282
|
+
|
|
283
|
+
mcp_client = LangchainMCPClient()
|
|
284
|
+
await mcp_client.add_server("yfinance", {...})
|
|
285
|
+
|
|
286
|
+
tool = create_execute_ptc_code_tool(mcp_client)
|
|
287
|
+
result = await tool.ainvoke({"code": "from tools.yfinance import get_stock\\nprint(get_stock('AAPL'))"})
|
|
288
|
+
```
|
|
289
|
+
"""
|
|
290
|
+
# Import here to avoid circular dependencies and allow lazy loading
|
|
291
|
+
from aip_agents.ptc.executor import PTCSandboxConfig, PTCSandboxExecutor
|
|
292
|
+
from aip_agents.sandbox.e2b_runtime import E2BSandboxRuntime
|
|
293
|
+
|
|
294
|
+
# Use provided config or create default
|
|
295
|
+
sandbox_config = config or PTCSandboxConfig()
|
|
296
|
+
|
|
297
|
+
# Create runtime and executor
|
|
298
|
+
runtime = E2BSandboxRuntime(
|
|
299
|
+
template=sandbox_config.sandbox_template,
|
|
300
|
+
ptc_packages=sandbox_config.ptc_packages,
|
|
301
|
+
)
|
|
302
|
+
executor = PTCSandboxExecutor(mcp_client, runtime, sandbox_config)
|
|
303
|
+
|
|
304
|
+
return PTCCodeTool(
|
|
305
|
+
executor=executor,
|
|
306
|
+
runtime=runtime,
|
|
307
|
+
agent_tool_configs=agent_tool_configs,
|
|
308
|
+
)
|