Skip to main content
Glama

BC Calculator MCP Server

by cthunter01
PROJECT_SUMMARY.mdโ€ข10.1 kB
# BC Calculator MCP Server - Project Summary ## What We're Building A production-ready MCP server that provides arbitrary precision mathematical calculations using the Unix `bc` calculator, with enterprise-grade security, performance, and reliability. ## ๐Ÿ“‹ Planning Documents Created 1. **[ARCHITECTURE.md](ARCHITECTURE.md)** - System Design (583 lines) - Complete component architecture - Security model - Process management flow - Mermaid diagrams 2. **[IMPLEMENTATION_GUIDE.md](IMPLEMENTATION_GUIDE.md)** - Developer Guide (688 lines) - Step-by-step implementation - Code skeletons for all files - Testing strategy - Deployment instructions 3. **[README.md](README.md)** - User Documentation (429 lines) - Installation guide - Usage examples - API reference - Troubleshooting 4. **[QUICK_REFERENCE.md](QUICK_REFERENCE.md)** - Cheat Sheet (373 lines) - Security checklist - Code patterns - Testing checklist - Common fixes ## ๐Ÿ—๏ธ Architecture Overview ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ BC Calculator MCP Server โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ MCP Server (index.ts) โ”‚ โ”‚ โ”‚ โ”‚ โ€ข Tool: calculate โ”‚ โ”‚ โ”‚ โ”‚ โ€ข Tool: calculate_advanced โ”‚ โ”‚ โ”‚ โ”‚ โ€ข Tool: set_precision โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Input Validator (input-validator.ts) โ”‚ โ”‚ โ”‚ โ”‚ โ€ข Security: Whitelist/Blacklist โ”‚ โ”‚ โ”‚ โ”‚ โ€ข Max length: 10KB โ”‚ โ”‚ โ”‚ โ”‚ โ€ข No command injection โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ BC Process Pool (bc-process-pool.ts) โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ BC Proc โ”‚ โ”‚ BC Proc โ”‚ โ”‚ BC Proc โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ #1 โ”‚ โ”‚ #2 โ”‚ โ”‚ #3 โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ Ready โ”‚ โ”‚ Busy โ”‚ โ”‚ Ready โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚ โ”‚ Each process (bc-process.ts): โ”‚ โ”‚ โ€ข Spawned with -l flag (math library) โ”‚ โ”‚ โ€ข Precision: 20 decimals (configurable) โ”‚ โ”‚ โ€ข Timeout: 30 seconds โ”‚ โ”‚ โ€ข Auto-respawn on crash โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ## ๐Ÿ” Security Design **Multi-Layer Protection**: 1. **Input Validation Layer** - Character whitelist: `[0-9a-zA-Z+\-*\/^().,;\s=<>!&|%{}[\]]` - Blacklist patterns: `system()`, `exec()`, backticks, pipes, redirects - Maximum expression length: 10,000 characters 2. **Process Isolation Layer** - No shell execution (`shell: false`) - Direct BC binary spawn - Separate process per calculation 3. **Resource Protection Layer** - 30-second timeout per calculation - Process pool size limit (3) - Automatic cleanup on timeout/failure ## ๐Ÿ“ File Structure ``` /home/travis/.local/share/Roo-Code/MCP/bc-calculator/ โ”œโ”€โ”€ package.json # Dependencies & scripts โ”œโ”€โ”€ tsconfig.json # TypeScript config โ”œโ”€โ”€ README.md # User docs โ”œโ”€โ”€ ARCHITECTURE.md # System design โ”œโ”€โ”€ IMPLEMENTATION_GUIDE.md # Dev guide โ”œโ”€โ”€ QUICK_REFERENCE.md # Cheat sheet โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ types.ts # Type definitions โ”‚ โ”œโ”€โ”€ input-validator.ts # Security validation โ”‚ โ”œโ”€โ”€ bc-process.ts # Single BC process wrapper โ”‚ โ”œโ”€โ”€ bc-process-pool.ts # Process pool manager โ”‚ โ””โ”€โ”€ index.ts # MCP server main โ””โ”€โ”€ build/ # Compiled output โ””โ”€โ”€ index.js # Executable server ``` ## ๐ŸŽฏ Implementation Roadmap ### Phase 1: Foundation (Todos 1-3) - Create project directory structure - Initialize package.json with dependencies - Configure TypeScript (tsconfig.json) ### Phase 2: Core Components (Todos 4-6, 13) - Design and implement type definitions - Build input validator with security rules - Create BC process wrapper with I/O handling - Implement process pool manager ### Phase 3: MCP Integration (Todos 7-9) - Implement `calculate` tool - Implement `calculate_advanced` tool - Implement `set_precision` tool ### Phase 4: Error & Resource Management (Todos 10-14) - Add comprehensive error handling - Implement timeout mechanism - Add process recovery logic - Create request queue system ### Phase 5: Deployment (Todos 15-16) - Build TypeScript to JavaScript - Auto-install to MCP settings - Verify server registration ### Phase 6: Testing & Documentation (Todos 17-20) - Test basic arithmetic - Test advanced features - Write unit tests - Create final documentation ## ๐Ÿ”ง Technology Stack | Component | Technology | Purpose | |-----------|-----------|---------| | Language | TypeScript 5.3+ | Type safety & modern syntax | | Runtime | Node.js 18+ | Server execution | | Protocol | MCP SDK 1.0.4+ | MCP compliance | | Calculator | bc (GNU) | Arbitrary precision math | | Process Mgmt | child_process | BC process spawning | | Validation | Regex + Whitelist | Input security | ## ๐Ÿš€ Key Features ### Functional โœ… Arbitrary precision (0-100 decimal places) โœ… Basic arithmetic (+, -, *, /, ^, %) โœ… Math library (sqrt, sin, cos, arctan, log, exp) โœ… Variables and assignments โœ… Control flow (if, while, for) โœ… Multi-line scripts โœ… Three concurrent processes ### Non-Functional โœ… Security: Command injection prevention โœ… Performance: <50ms for typical calculations โœ… Reliability: Auto-recovery from crashes โœ… Usability: Clear error messages โœ… Maintainability: Well-documented code โœ… Testability: Comprehensive test suite ## ๐Ÿ“Š Expected Performance | Operation | Target | Notes | |-----------|--------|-------| | Simple calc (2+2) | <10ms | Fastest path | | Division w/ precision | <20ms | Common use case | | Math functions | <50ms | Using -l library | | Complex scripts | <200ms | Multi-statement | | Process spawn | <100ms | One-time cost | | Concurrent requests | 3 parallel | Pool size limit | ## ๐Ÿงช Test Coverage Plan ### Unit Tests - Input validation edge cases - Dangerous pattern detection - Expression sanitization - Error message formatting ### Integration Tests - All mathematical operators - All math library functions - Variable assignment - Multi-line scripts - Precision handling - Timeout scenarios - Concurrent requests - Process failure recovery ### Security Tests - Command injection attempts - Shell metacharacter blocking - File access prevention - Resource exhaustion ## ๐Ÿ“ Configuration ### MCP Settings Entry ```json { "mcpServers": { "bc-calculator": { "command": "node", "args": ["/home/travis/.local/share/Roo-Code/MCP/bc-calculator/build/index.js"], "disabled": false, "alwaysAllow": [], "disabledTools": [] } } } ``` ### Default Server Config ```typescript { poolSize: 3, // Concurrent BC processes defaultPrecision: 20, // Decimal places defaultTimeout: 30000, // Milliseconds maxExpressionLength: 10000 // Characters } ``` ## ๐ŸŽ“ Learning Resources - **BC Manual**: `man bc` or https://www.gnu.org/software/bc/manual/html_mono/bc.html - **MCP Docs**: https://modelcontextprotocol.io/ - **Node child_process**: https://nodejs.org/api/child_process.html - **TypeScript**: https://www.typescriptlang.org/docs/ ## โœ… Success Criteria The implementation will be considered successful when: 1. โœ… All 20 todo items are completed 2. โœ… Server builds without TypeScript errors 3. โœ… Server appears in MCP settings 4. โœ… All three tools are functional 5. โœ… Security validation prevents dangerous inputs 6. โœ… Basic test cases pass 7. โœ… Advanced test cases pass 8. โœ… Concurrent requests work correctly 9. โœ… Error handling works as expected 10. โœ… Documentation is complete ## ๐Ÿ“ฆ Deliverables 1. โœ… Planning documents (this and others) 2. โณ Working TypeScript source code 3. โณ Compiled JavaScript executable 4. โณ MCP configuration entry 5. โณ Test suite 6. โณ User documentation 7. โณ Implementation verification ## ๐Ÿ”„ Next Steps Once you approve this plan, I will: 1. Switch to **Code Mode** 2. Implement all components according to the architecture 3. Build and test the server 4. Auto-install to MCP settings 5. Verify functionality with test cases 6. Provide usage examples --- **Ready to proceed?** If you approve this plan, I'll switch to Code mode and begin implementation following the detailed guides in ARCHITECTURE.md and IMPLEMENTATION_GUIDE.md.

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/cthunter01/MCPCalculator'

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