Skip to main content
Glama

Math Operations MCP Server

by iamkhanwasim
MIT License
  • Apple
  • Linux
mcp_server.py3.7 kB
#!/usr/bin/env python3 """ MCP Server for Math Operations Exposes addition and subtraction operations as MCP tools for AI applications. """ import asyncio import logging from mcp.server import Server from mcp.types import Tool, TextContent from mcp.server.stdio import stdio_server # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger("math-mcp-server") # Create MCP server instance app = Server("math-operations-mcp") @app.list_tools() async def list_tools() -> list[Tool]: """ List available MCP tools. Returns: List of available math operation tools """ return [ Tool( name="add", description="Add two numbers together. Returns the sum of a and b.", inputSchema={ "type": "object", "properties": { "a": { "type": "number", "description": "First number to add" }, "b": { "type": "number", "description": "Second number to add" } }, "required": ["a", "b"] } ), Tool( name="subtract", description="Subtract b from a. Returns the difference (a - b).", inputSchema={ "type": "object", "properties": { "a": { "type": "number", "description": "Number to subtract from" }, "b": { "type": "number", "description": "Number to subtract" } }, "required": ["a", "b"] } ) ] @app.call_tool() async def call_tool(name: str, arguments: dict) -> list[TextContent]: """ Handle tool calls from AI applications. Args: name: Name of the tool to call arguments: Tool arguments Returns: List of TextContent with the operation result """ try: # Extract parameters a = float(arguments.get("a", 0)) b = float(arguments.get("b", 0)) # Execute the requested operation if name == "add": result = a + b operation = "addition" logger.info(f"Executing add: {a} + {b} = {result}") elif name == "subtract": result = a - b operation = "subtraction" logger.info(f"Executing subtract: {a} - {b} = {result}") else: raise ValueError(f"Unknown tool: {name}") # Return formatted result response = { "result": result, "operation": operation, "inputs": {"a": a, "b": b} } return [ TextContent( type="text", text=f"Result: {result}\nOperation: {operation}\nInputs: a={a}, b={b}" ) ] except Exception as e: logger.error(f"Error executing tool {name}: {str(e)}") return [ TextContent( type="text", text=f"Error: {str(e)}" ) ] async def main(): """ Main entry point for the MCP server. Runs the server using stdio transport. """ logger.info("Starting Math Operations MCP Server") async with stdio_server() as (read_stream, write_stream): await app.run( read_stream, write_stream, app.create_initialization_options() ) if __name__ == "__main__": asyncio.run(main())

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/iamkhanwasim/mcp-fastapi'

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