Skip to content

Conversation

@Moctobers
Copy link

This commit implements a comprehensive SymPy MCP tool providing symbolic mathematics capabilities for the MCP Handley Lab framework.

Features

Core Functions

  • transform_expression: Algebraic manipulation (expand, simplify, factor, collect, apart, together, cancel)
  • evaluate_expression: Variable substitution and numerical evaluation
  • to_latex: Convert expressions to LaTeX format with multiple output modes
  • server_info: Server status and capabilities

Key Capabilities

  • Polynomial operations: Expand, factor, and manipulate polynomial expressions
  • Expression simplification: Simplify complex algebraic and trigonometric expressions
  • Numerical evaluation: Substitute values and convert to numerical results
  • LaTeX output: Generate LaTeX code for typesetting (plain, inline, equation modes)
  • Multi-variable support: Handle expressions with multiple variables
  • Comprehensive error handling: Graceful cancellation and detailed error messages

Implementation Details

  • Follows existing codebase patterns (FastMCP, async/await, common utilities)
  • Added SymPy dependency to pyproject.toml and PKGBUILD
  • Version bump from 0.0.0a23 → 0.0.0a24
  • MCP server entry point: mcp-sympy
  • Unified entry point support: python -m mcp_handley_lab sympy

Testing

  • 54 comprehensive unit tests covering all functionality
  • Integration tests for real mathematical workflows
  • JSON-RPC server testing for MCP compatibility

Examples

# Polynomial expansion
transform_expression("(x+1)*(x+2)", "expand") → "x**2 + 3*x + 2"

# LaTeX conversion
to_latex("x^2 + sqrt(2*pi)", mode="inline") → "$x^{2} + \\sqrt{2 \\pi}$"

# Numerical evaluation
evaluate_expression("x^2 + 3*x + 2", {"x": 3}, numerical=True) → "20.0"

🤖 Generated with Claude Code

Jiamin Hou and others added 2 commits July 2, 2025 09:43
This commit implements a comprehensive SymPy MCP tool providing symbolic mathematics capabilities for the MCP Handley Lab framework.

## Features

### Core Functions
- **transform_expression**: Algebraic manipulation (expand, simplify, factor, collect, apart, together, cancel)
- **evaluate_expression**: Variable substitution and numerical evaluation
- **to_latex**: Convert expressions to LaTeX format with multiple output modes
- **server_info**: Server status and capabilities

### Key Capabilities
- **Polynomial operations**: Expand, factor, and manipulate polynomial expressions
- **Expression simplification**: Simplify complex algebraic and trigonometric expressions
- **Numerical evaluation**: Substitute values and convert to numerical results
- **LaTeX output**: Generate LaTeX code for typesetting (plain, inline, equation modes)
- **Multi-variable support**: Handle expressions with multiple variables
- **Comprehensive error handling**: Graceful cancellation and detailed error messages

### Implementation Details
- Follows existing codebase patterns (FastMCP, async/await, common utilities)
- Added SymPy dependency to pyproject.toml and PKGBUILD
- Version bump from 0.0.0a23 → 0.0.0a24
- MCP server entry point: mcp-sympy
- Unified entry point support: python -m mcp_handley_lab sympy

### Testing
- 54 comprehensive unit tests covering all functionality
- Integration tests for real mathematical workflows
- JSON-RPC server testing for MCP compatibility

### Examples
```python
# Polynomial expansion
transform_expression("(x+1)*(x+2)", "expand") → "x**2 + 3*x + 2"

# LaTeX conversion
to_latex("x^2 + sqrt(2*pi)", mode="inline") → "$x^{2} + \\sqrt{2 \\pi}$"

# Numerical evaluation
evaluate_expression("x^2 + 3*x + 2", {"x": 3}, numerical=True) → "20.0"
```

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Following the established pattern from other MCP tools (LLM tools, code2prompt),
all three main SymPy functions now support file output:

### Enhanced Functions
- **transform_expression**: Added `output_file` parameter for saving algebraic results
- **evaluate_expression**: Added `output_file` parameter for saving evaluation results
- **to_latex**: Added `output_file` parameter for saving LaTeX code

### File Output Behavior
- `output_file="-"` (default): Return result directly as text
- `output_file="/path/to/file"`: Save result to specified file and return status message

### Benefits
- **Consistent API**: Matches the pattern used by LLM tools and other file-outputting tools
- **Workflow integration**: Enables saving mathematical results for later use
- **Academic workflows**: Perfect for saving LaTeX expressions to include in papers
- **Tool chaining**: Results can be saved and used by other tools in the pipeline

### Example Usage
```python
# Direct output (existing behavior)
transform_expression("(x+1)*(x+2)", "expand") → "x**2 + 3*x + 2"

# File output (new functionality)
transform_expression("(x+1)*(x+2)", "expand", output_file="/tmp/result.txt")
→ "Transformation result saved to: /tmp/result.txt\nExpression: x**2 + 3*x + 2\nContent: 14 characters, 1 lines"

# LaTeX to file for academic publishing
to_latex("x^2 + sqrt(2*pi)", mode="inline", output_file="/tmp/equation.tex")
→ "LaTeX output saved to: /tmp/equation.tex\nLaTeX: $x^{2} + \\sqrt{2 \\pi}$\nContent: 22 characters, 1 lines"
```

This enhancement significantly improves the tool's utility for academic and research workflows.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants