evaluate_sage
Execute SageMath code in a persistent session where variables persist across calls, enabling multi-step workflows like transforms, modular arithmetic, and number fields.
Instructions
Run SageMath code in a persistent session; variables persist across calls. Caller code is deny-by-default -- ordinary mathematics is allowed, but imports, external CAS interfaces and file/display/persistence calls are refused.
LAST RESORT for a single self-contained calculation: a dedicated tool exists for most of those and should be preferred, because it validates arguments and returns a typed result instead of a repr string. One exception overrides that steer -- the dedicated tools evaluate in a FRESH namespace and cannot see variables you defined here, so any multi-step workflow that builds an object once and then explores it (a graph and its invariants, a number field, a matrix decomposition) belongs in evaluate_sage across as many calls as it takes. For a one-off, reach for one of these first:
calculus: differentiate_expression, integrate_expression, limit_expression, series_expansion, symbolic_sum, solve_ode
algebra: solve_equation, simplify_expression, expand_expression, factor_expression, find_root
linear algebra: matrix_operation (determinant, inverse, eigenvalues, rank, rref, transpose), matrix_multiply
discrete: number_theory_operation, combinatorics_operation (binomial, partitions, catalan, fibonacci, bell), graph_operation, group_operation
specialised: elliptic_curve_operation, coding_theory_operation, polynomial_ring_operation, boolean_algebra_operation, geometry_operation, vector_calculus_operation
data: statistics_summary, distribution_operation
plots: plot_expression, plot3d_expression, plot_multi_expression
Use evaluate_sage only for what those do not cover, for example:
Transforms: var('t s'); laplace(sin(t), t, s); inverse_laplace(1/(s^2+1), s, t) Modular arithmetic: Mod(17, 5); power_mod(3, 100, 97) Recurrences: var('n'); f = function('f'); desolve_rec(f(n+2)-f(n+1)-f(n), f, [0, 1]) Continued fractions: continued_fraction(pi).convergents()[:10] Number fields: K. = NumberField(x^3 - 2); K.class_number() Any multi-step work that builds on values defined earlier in the same session, since the dedicated tools cannot see them.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | SageMath code to execute | |
| session | No | Workspace to use, as a name or a portable handle. Workspaces have independent variables. A name is scoped to this MCP session; a handle returned by start_sage_session (workspace_token) reaches the same workspace across reconnects and is a bearer credential -- keep it secret. Omit for 'default'. | default |
| timeout | No | Override the evaluation timeout in seconds | |
| want_latex | No | Return LaTeX representation when possible | |
| capture_stdout | No | Capture stdout emitted by Sage code |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| latex | No | LaTeX serialization of the result when requested and supported. | |
| result | No | String representation of the expression result if available. | |
| stdout | No | Captured stdout emitted during execution. | |
| elapsed_ms | Yes | Wall-clock execution time in milliseconds. | |
| result_type | Yes |