Skip to main content
Glama

execute_code

Run Python code for financial analysis and calculations, with results returned via print statements.

Instructions

Execute python code can be used in scenarios such as analysis or calculation, and the final result can be printed using the print function.

Input Schema

NameRequiredDescriptionDefault
codeYescode to be executed

Input Schema (JSON Schema)

{ "properties": { "code": { "description": "code to be executed", "type": "string" } }, "required": [ "code" ], "type": "object" }

Implementation Reference

  • Core handler logic for the 'execute_code' tool: reads input code and executes it via exec_code helper, setting the result as output.
    async def async_execute(self): """Execute the provided Python code asynchronously. The method reads the ``code`` field from ``input_dict``, delegates execution to :func:`exec_code`, and stores the textual result in the operation output. """ self.set_output(exec_code(self.input_dict["code"]))
  • Defines the ToolCall schema for 'execute_code', including input schema requiring a 'code' string.
    def build_tool_call(self) -> ToolCall: """Build the tool call schema used by FlowLLM. Returns: ToolCall: The tool call definition including description and input schema. """ return ToolCall( **{ "description": self.get_prompt("tool_description"), "input_schema": { "code": { "type": "string", "description": "code to be executed", "required": True, }, }, }, )
  • Registers the ExecuteCodeOp class (providing the 'execute_code' tool) using the @C.register_op() decorator.
    @C.register_op() class ExecuteCodeOp(BaseAsyncToolOp):
  • Helper function that executes arbitrary Python code, captures stdout, and returns output or exception message.
    def exec_code(code: str) -> str: """Execute arbitrary Python code and capture its printed output. The code is executed in the current global context and any text written to ``stdout`` is captured and returned as a string. If an exception occurs, its string representation is returned instead. Args: code: Python source code to execute. Returns: Captured ``stdout`` output, or the exception message if execution fails. """ try: redirected_output = StringIO() with contextlib.redirect_stdout(redirected_output): exec(code) return redirected_output.getvalue() except Exception as e: # noqa: BLE001 return str(e)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/FlowLLM-AI/finance-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server