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.