---
title: "Building a Math & Calculator MCP Server: Supercharge Your AI Assistant with Mathematical Tools"
published: false
description: "Learn how to create a Model Context Protocol (MCP) server that adds powerful mathematical capabilities to Claude, VSCode, and other AI assistants"
tags: ai, mcp, typescript, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/placeholder.jpg
canonical_url: null
---
# Building a Math & Calculator MCP Server: Supercharge Your AI Assistant with Mathematical Tools
## šÆ Introduction
Have you ever wished your AI assistant could perform complex calculations, statistical analysis, or unit conversions seamlessly? With the **Model Context Protocol (MCP)**, you can extend AI assistants like Claude with custom tools!
In this article, I'll show you how I built a **Math & Calculator MCP Server** that provides 6 powerful mathematical tools to any MCP-compatible client.
**š Full Repository:** [https://github.com/Ankluna72/Math-Calculator-MCP-Server-](https://github.com/Ankluna72/Math-Calculator-MCP-Server-)
---
## š¤ What is the Model Context Protocol (MCP)?
The **Model Context Protocol** is an open standard developed by Anthropic that allows AI assistants to connect to external tools and data sources. Think of it as a universal adapter that lets your AI assistant:
- š Access custom tools and functions
- š Query databases and APIs
- š§ Perform specialized operations
- š Integrate with your existing systems
MCP servers expose **tools** that AI models can discover and use automatically, making your assistant infinitely more capable.
---
## šļø What We're Building
Our **Math & Calculator MCP Server** includes 6 comprehensive tools:
### 1. š¢ **Calculator**
Basic arithmetic: add, subtract, multiply, divide, power, sqrt, modulo
### 2. š **Statistics**
Mean, median, mode, standard deviation, variance
### 3. š **Unit Converter**
Length, weight, and temperature conversions
### 4. š **Equation Solver**
Quadratic equations with real and complex solutions
### 5. šÆ **Percentage Calculator**
All percentage operations you need
### 6. š **Trigonometry**
Sin, cos, tan and their inverse functions
---
## š Implementation
### Project Setup
```bash
mkdir math-calculator-mcp-server
cd math-calculator-mcp-server
npm init -y
npm install @modelcontextprotocol/sdk
npm install -D typescript @types/node
```
### TypeScript Configuration
Create `tsconfig.json`:
```json
{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true
},
"include": ["src/**/*"]
}
```
### Core Server Implementation
The magic happens in `src/index.ts`. Here's the structure:
```typescript
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
// Define tools with JSON schemas
const TOOLS = [
{
name: "calculate",
description: "Perform basic arithmetic operations",
inputSchema: {
type: "object",
properties: {
operation: {
type: "string",
enum: ["add", "subtract", "multiply", "divide", "power", "sqrt"]
},
a: { type: "number" },
b: { type: "number" }
},
required: ["operation", "a"]
}
},
// ... more tools
];
// Create server instance
const server = new Server(
{
name: "math-calculator-mcp-server",
version: "1.0.0",
},
{
capabilities: {
tools: {},
},
}
);
```
### Handling Tool Requests
The server responds to two main requests:
**1. List Available Tools:**
```typescript
server.setRequestHandler(ListToolsRequestSchema, async () => {
return { tools: TOOLS };
});
```
**2. Execute Tools:**
```typescript
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
switch (name) {
case "calculate":
const result = performCalculation(args);
return {
content: [{
type: "text",
text: JSON.stringify({ result })
}]
};
// ... handle other tools
}
});
```
### Example: Statistics Tool
```typescript
function statistics(numbers: number[], operation: string) {
const sum = numbers.reduce((acc, val) => acc + val, 0);
const mean = sum / numbers.length;
const sorted = [...numbers].sort((a, b) => a - b);
const median = sorted.length % 2 === 0
? (sorted[sorted.length / 2 - 1] + sorted[sorted.length / 2]) / 2
: sorted[Math.floor(sorted.length / 2)];
const variance = numbers.reduce(
(acc, val) => acc + Math.pow(val - mean, 2), 0
) / numbers.length;
const stddev = Math.sqrt(variance);
return { mean, median, variance, stddev };
}
```
---
## āļø Connecting to Claude Desktop
Edit your Claude config file:
**Windows:** `%APPDATA%\Claude\claude_desktop_config.json`
```json
{
"mcpServers": {
"math-calculator": {
"command": "node",
"args": [
"C:\\path\\to\\Math-Calculator-MCP-Server-\\dist\\index.js"
]
}
}
}
```
Restart Claude Desktop, and your tools are ready!
---
## š® Usage Examples
Once connected, Claude can use your tools naturally:
**User:** "Calculate the mean of [10, 20, 30, 40, 50]"
**Claude:** Uses the `statistics` tool ā Returns: `{ mean: 30 }`
**User:** "Convert 100 kilometers to miles"
**Claude:** Uses `convert_units` ā Returns: `62.1371 miles`
**User:** "Solve x² - 5x + 6 = 0"
**Claude:** Uses `solve_equation` ā Returns: `[3, 2]`
---
## š Key Takeaways
### ā
What I Learned:
1. **MCP is powerful:** Extending AI capabilities is easier than I thought
2. **Type safety matters:** TypeScript + JSON Schema = robust tools
3. **Error handling is crucial:** Always validate inputs and handle edge cases
4. **Documentation is key:** Clear schemas help the AI use tools correctly
### š What's Next:
- Add matrix operations and linear algebra
- Implement calculus (derivatives, integrals)
- Add graphing capabilities
- Support for custom formulas
---
## š¦ Try It Yourself!
1. **Clone the repository:**
```bash
git clone https://github.com/Ankluna72/Math-Calculator-MCP-Server-.git
cd Math-Calculator-MCP-Server-
npm install
npm run build
```
2. **Configure your MCP client** (Claude Desktop, VSCode with Cline, etc.)
3. **Start using mathematical tools** in your AI conversations!
---
## š¤ Contributing
Found a bug? Have an idea for a new tool? Contributions are welcome!
- **Repository:** [https://github.com/Ankluna72/Math-Calculator-MCP-Server-](https://github.com/Ankluna72/Math-Calculator-MCP-Server-)
- **Issues:** Open an issue for bugs or feature requests
- **Pull Requests:** Fork, code, and submit PRs!
---
## š Resources
- [Model Context Protocol Documentation](https://modelcontextprotocol.io/)
- [MCP SDK on GitHub](https://github.com/modelcontextprotocol/sdk)
- [Anthropic's MCP Announcement](https://www.anthropic.com/mcp)
---
## šØāš» About the Author
**Jefferson Rosas Chambilla** - Software developer passionate about AI, mathematics, and building tools that make technology more accessible.
Connect with me:
- GitHub: [@Ankluna72](https://github.com/Ankluna72)
- Repository: [Math-Calculator-MCP-Server-](https://github.com/Ankluna72/Math-Calculator-MCP-Server-)
---
## š Conclusion
Building an MCP server opens up endless possibilities for extending AI assistants. Whether you need mathematical tools, database access, or custom APIs, MCP makes it possible.
**What will you build with MCP?** Drop a comment below! š
ā If you found this helpful, please star the repository and share with others!
---
**Tags:** #AI #MCP #TypeScript #Claude #Tutorial #MachineLearning #Developer #OpenSource