# 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.