arcade-mcp/toolkits/code_sandbox/arcade_code_sandbox/tools/e2b.py
Nate Barbettini e9ee3bba40
fix: Use tool secrets in toolkits (#271)
~~Note: Don't merge until the correct secrets have been added to Arcade
Cloud.~~

Ready to merge, the feature is already on its way to prod.

---------

Co-authored-by: Eric Gustin <eric@arcade.dev>
2025-03-04 13:35:36 -08:00

52 lines
1.6 KiB
Python

from typing import Annotated
from arcade.sdk import ToolContext, tool
from e2b_code_interpreter import Sandbox
from arcade_code_sandbox.tools.models import E2BSupportedLanguage
# See https://e2b.dev/docs to learn more about E2B
@tool(requires_secrets=["E2B_API_KEY"])
def run_code(
context: ToolContext,
code: Annotated[str, "The code to run"],
language: Annotated[
E2BSupportedLanguage, "The language of the code"
] = E2BSupportedLanguage.PYTHON,
) -> Annotated[str, "The sandbox execution as a JSON string"]:
"""
Run code in a sandbox and return the output.
"""
api_key = context.get_secret("E2B_API_KEY")
with Sandbox(api_key=api_key) as sbx:
execution = sbx.run_code(code=code, language=language)
return str(execution.to_json())
# Note: Not recommended to use tool_choice='generate' with this tool
# since it contains base64 encoded image.
@tool(requires_secrets=["E2B_API_KEY"])
def create_static_matplotlib_chart(
context: ToolContext,
code: Annotated[str, "The Python code to run"],
) -> Annotated[dict, "A dictionary with the following keys: base64_image, logs, error"]:
"""
Run the provided Python code to generate a static matplotlib chart.
The resulting chart is returned as a base64 encoded image.
"""
api_key = context.get_secret("E2B_API_KEY")
with Sandbox(api_key=api_key) as sbx:
execution = sbx.run_code(code=code)
result = {
"base64_image": execution.results[0].png if execution.results else None,
"logs": execution.logs.to_json(),
"error": execution.error.to_json() if execution.error else None,
}
return result