execute_mathematica_code
Execute Wolfram Language code within a secure session using a provided session ID. Perform calculations, symbolic computations, and data visualizations by submitting valid code strings to the Mathematica kernel.
Instructions
Executes a string of Wolfram Language code within a specific, active session.
To use this tool, you must provide a valid 'session_id' obtained from a previous call to 'create_session'. The code will be executed in the context of that session, meaning it can access variables and functions defined in previous calls within the same session.
Args: session_id: The unique identifier for an active session, provided by 'create_session'. Example: 'bee-sloth-auk-mole'. code: A string containing the Wolfram Language code to be executed. The code should be syntactically correct. Example 1 (simple calculation): 'Total[Range[100]]' Example 2 (symbolic computation): 'Solve[x^2 - 5x + 6 == 0, x]' Example 3 (data visualization): 'Plot[Sin[x], {x, 0, 2 Pi}]'
Returns: The direct result of the code execution from the Wolfram Engine. The data type can vary (e.g., integer, list, string, or a complex expression). For plots, it may return a representation of the graphics object.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | ||
| session_id | Yes |
Implementation Reference
- wolfram_mathematica.py:115-153 (handler)The @mcp.tool() decorator registers the handler function execute_mathematica_code, which verifies the session ID using AnimalIdGenerator, retrieves the WolframLanguageSession from the sessions dict, evaluates the provided code using session.evaluate(wlexpr(code)), and returns the result or raises an error.@mcp.tool() async def execute_mathematica_code(session_id: str, code: str) -> Any: """ Executes a string of Wolfram Language code within a specific, active session. To use this tool, you must provide a valid 'session_id' obtained from a previous call to 'create_session'. The code will be executed in the context of that session, meaning it can access variables and functions defined in previous calls within the same session. Args: session_id: The unique identifier for an active session, provided by 'create_session'. Example: 'bee-sloth-auk-mole'. code: A string containing the Wolfram Language code to be executed. The code should be syntactically correct. Example 1 (simple calculation): 'Total[Range[100]]' Example 2 (symbolic computation): 'Solve[x^2 - 5x + 6 == 0, x]' Example 3 (data visualization): 'Plot[Sin[x], {x, 0, 2 Pi}]' Returns: The direct result of the code execution from the Wolfram Engine. The data type can vary (e.g., integer, list, string, or a complex expression). For plots, it may return a representation of the graphics object. """ # 验证 session_id 格式和校验和 if not id_generator.verify(session_id): raise ValueError("Invalid session ID. It might be malformed or tampered with.") # 查找会话 session = sessions.get(session_id) if not session: raise ValueError(f"Session with ID '{session_id}' not found or has been closed.") try: # 将字符串代码转换为 wlexpr 对象并执行 result = session.evaluate(wlexpr(code)) return result except Exception as e: raise RuntimeError(f"An error occurred during execution in session '{session_id}': {e}") from e