close_mathematica_session
Terminates a specific Wolfram Language session and releases associated resources to free up system memory and kernel licenses. Use to close active sessions efficiently.
Instructions
Terminates a specific Wolfram Language session and releases all associated resources.
It is good practice to call this tool when you are finished with a session to free up system memory and kernel licenses. Once a session is closed, its ID can no longer be used.
Args: session_id: The unique identifier of the session you wish to close. This must be an ID from an active, open session. Example: 'bee-sloth-auk-mole'.
Returns: A confirmation message indicating that the session was successfully closed.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| session_id | Yes |
Implementation Reference
- wolfram_mathematica.py:156-187 (handler)The core handler function for the 'close_mathematica_session' tool. Decorated with @mcp.tool() which also handles registration in the FastMCP server. Validates session ID using AnimalIdGenerator, terminates the WolframLanguageSession, removes it from the global sessions dictionary, and returns a success message or raises an error.@mcp.tool() def close_mathematica_session(session_id: str) -> str: """ Terminates a specific Wolfram Language session and releases all associated resources. It is good practice to call this tool when you are finished with a session to free up system memory and kernel licenses. Once a session is closed, its ID can no longer be used. Args: session_id: The unique identifier of the session you wish to close. This must be an ID from an active, open session. Example: 'bee-sloth-auk-mole'. Returns: A confirmation message indicating that the session was successfully closed. """ # 验证 session_id if not id_generator.verify(session_id): raise ValueError("Invalid session ID.") session = sessions.get(session_id) if not session: raise ValueError(f"Session with ID '{session_id}' not found or already closed.") try: # 终止会话并从字典中移除 session.terminate() del sessions[session_id] return f"Session '{session_id}' closed successfully." except Exception as e: raise RuntimeError(f"An error occurred while closing session '{session_id}': {e}") from e