Skip to main content
Glama
nilay320

Tavily Web Search MCP Server

by nilay320

scientific_calculator

Evaluate mathematical expressions with scientific functions, including trigonometry, logarithms, exponentials, and complex numbers, using radians by default.

Instructions

Evaluate mathematical expressions using a scientific calculator.

Supports:

  • Basic arithmetic: +, -, *, /, //, %, **

  • Scientific functions: sin, cos, tan, asin, acos, atan, sinh, cosh, tanh

  • Logarithmic functions: log, log10, log2, ln (natural log)

  • Exponential functions: exp, sqrt, cbrt

  • Constants: pi, e, tau

  • Complex numbers: 1+2j, complex operations

  • Trigonometric functions work with radians by default

  • Use degrees(x) to convert radians to degrees, radians(x) to convert degrees to radians

Examples:

  • "sin(pi/2)" -> 1.0

  • "log10(100)" -> 2.0

  • "sqrt(16)" -> 4.0

  • "2**3" -> 8

  • "exp(1)" -> 2.718281828459045

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
expressionYes

Implementation Reference

  • The complete handler for the 'scientific_calculator' tool. Registered via @mcp.tool() decorator. Safely evaluates mathematical expressions using Python's eval() with a restricted namespace containing math functions (sin, cos, log, etc.), constants (pi, e), and complex number support. Includes comprehensive error handling and result formatting for real and complex numbers.
    @mcp.tool()
    def scientific_calculator(expression: str) -> str:
        """
        Evaluate mathematical expressions using a scientific calculator.
        
        Supports:
        - Basic arithmetic: +, -, *, /, //, %, **
        - Scientific functions: sin, cos, tan, asin, acos, atan, sinh, cosh, tanh
        - Logarithmic functions: log, log10, log2, ln (natural log)
        - Exponential functions: exp, sqrt, cbrt
        - Constants: pi, e, tau
        - Complex numbers: 1+2j, complex operations
        - Trigonometric functions work with radians by default
        - Use degrees(x) to convert radians to degrees, radians(x) to convert degrees to radians
        
        Examples:
        - "sin(pi/2)" -> 1.0
        - "log10(100)" -> 2.0
        - "sqrt(16)" -> 4.0
        - "2**3" -> 8
        - "exp(1)" -> 2.718281828459045
        """
        try:
            # Create a safe namespace with mathematical functions and constants
            safe_dict = {
                # Mathematical functions
                'sin': math.sin, 'cos': math.cos, 'tan': math.tan,
                'asin': math.asin, 'acos': math.acos, 'atan': math.atan,
                'atan2': math.atan2,
                'sinh': math.sinh, 'cosh': math.cosh, 'tanh': math.tanh,
                'asinh': math.asinh, 'acosh': math.acosh, 'atanh': math.atanh,
                'log': math.log, 'log10': math.log10, 'log2': math.log2,
                'ln': math.log,  # Natural logarithm alias
                'exp': math.exp, 'sqrt': cmath.sqrt, 'cbrt': lambda x: x**(1/3),
                'pow': pow, 'abs': abs,
                'ceil': math.ceil, 'floor': math.floor, 'trunc': math.trunc,
                'round': round,
                'degrees': math.degrees, 'radians': math.radians,
                'factorial': math.factorial,
                'gcd': math.gcd, 'lcm': math.lcm if hasattr(math, 'lcm') else lambda a, b: abs(a*b) // math.gcd(a, b),
                
                # Constants
                'pi': math.pi, 'e': math.e, 'tau': math.tau,
                'inf': math.inf, 'nan': math.nan,
                
                # Complex number functions
                'complex': complex, 'real': lambda x: x.real if isinstance(x, complex) else x,
                'imag': lambda x: x.imag if isinstance(x, complex) else 0,
                'conjugate': lambda x: x.conjugate() if hasattr(x, 'conjugate') else x,
                'phase': cmath.phase, 'polar': cmath.polar, 'rect': cmath.rect,
                
                # Allow built-in mathematical operations
                '__builtins__': {}
            }
            
            # Replace common mathematical notation
            expression = expression.replace('^', '**')  # Allow ^ for exponentiation
            expression = expression.replace('mod', '%')  # Allow mod for modulo
            
            # Evaluate the expression
            result = eval(expression, safe_dict)
            
            # Format the result nicely
            if isinstance(result, complex):
                if abs(result.imag) < 1e-10:  # Essentially real
                    real_part = result.real
                    if abs(real_part) < 1e-10:
                        return "0"
                    elif abs(real_part - round(real_part)) < 1e-10:
                        return str(int(round(real_part)))
                    else:
                        return str(real_part)
                else:
                    real_str = str(int(result.real)) if abs(result.real - round(result.real)) < 1e-10 else str(result.real)
                    imag_str = str(int(result.imag)) if abs(result.imag - round(result.imag)) < 1e-10 else str(result.imag)
                    
                    if result.real == 0:
                        return f"{imag_str}j" if result.imag != 1 else "j" if result.imag == 1 else "-j"
                    else:
                        if result.imag >= 0:
                            imag_part = f" + {imag_str}j" if result.imag != 1 else " + j"
                        else:
                            imag_part = f" - {abs(float(imag_str))}j" if result.imag != -1 else " - j"
                        return f"{real_str}{imag_part}"
            elif isinstance(result, float):
                # Round very small numbers to avoid floating point precision issues
                if abs(result) < 1e-10:
                    return "0"
                elif abs(result - round(result)) < 1e-10:
                    return str(int(round(result)))
                else:
                    return str(result)
            else:
                return str(result)
                
        except ZeroDivisionError:
            return "Error: Division by zero"
        except ValueError as e:
            return f"Error: Invalid mathematical operation - {str(e)}"
        except OverflowError:
            return "Error: Result too large to compute"
        except TypeError as e:
            return f"Error: Invalid expression type - {str(e)}"
        except SyntaxError:
            return "Error: Invalid mathematical expression syntax"
        except Exception as e:
            return f"Error: {str(e)}"
  • server.py:29-29 (registration)
    The @mcp.tool() decorator registers the scientific_calculator function as an MCP tool, using its signature and docstring for schema inference.
    @mcp.tool()
  • Function signature defines input schema (expression: str) and output (str), with detailed docstring describing supported operations, examples, and usage.
    def scientific_calculator(expression: str) -> str:

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/nilay320/MCP-Session-Code'

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