calc-mcp-server 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/.claude/commands/opsx/apply.md +152 -0
  2. package/.claude/commands/opsx/archive.md +157 -0
  3. package/.claude/commands/opsx/bulk-archive.md +242 -0
  4. package/.claude/commands/opsx/continue.md +114 -0
  5. package/.claude/commands/opsx/explore.md +174 -0
  6. package/.claude/commands/opsx/ff.md +94 -0
  7. package/.claude/commands/opsx/new.md +69 -0
  8. package/.claude/commands/opsx/onboard.md +534 -0
  9. package/.claude/commands/opsx/sync.md +134 -0
  10. package/.claude/commands/opsx/verify.md +164 -0
  11. package/.claude/settings.local.json +8 -0
  12. package/.claude/skills/npm-publish/SKILL.md +164 -0
  13. package/.claude/skills/openspec-apply-change/SKILL.md +156 -0
  14. package/.claude/skills/openspec-archive-change/SKILL.md +161 -0
  15. package/.claude/skills/openspec-bulk-archive-change/SKILL.md +246 -0
  16. package/.claude/skills/openspec-continue-change/SKILL.md +118 -0
  17. package/.claude/skills/openspec-explore/SKILL.md +289 -0
  18. package/.claude/skills/openspec-ff-change/SKILL.md +101 -0
  19. package/.claude/skills/openspec-new-change/SKILL.md +74 -0
  20. package/.claude/skills/openspec-onboard/SKILL.md +538 -0
  21. package/.claude/skills/openspec-sync-specs/SKILL.md +138 -0
  22. package/.claude/skills/openspec-verify-change/SKILL.md +168 -0
  23. package/CLAUDE.md +92 -0
  24. package/README.md +319 -0
  25. package/build/engines/decimal.d.ts +10 -0
  26. package/build/engines/decimal.d.ts.map +1 -0
  27. package/build/engines/decimal.js +61 -0
  28. package/build/engines/decimal.js.map +1 -0
  29. package/build/engines/programmer.d.ts +18 -0
  30. package/build/engines/programmer.d.ts.map +1 -0
  31. package/build/engines/programmer.js +103 -0
  32. package/build/engines/programmer.js.map +1 -0
  33. package/build/errors/handler.d.ts +10 -0
  34. package/build/errors/handler.d.ts.map +1 -0
  35. package/build/errors/handler.js +37 -0
  36. package/build/errors/handler.js.map +1 -0
  37. package/build/errors/types.d.ts +25 -0
  38. package/build/errors/types.d.ts.map +1 -0
  39. package/build/errors/types.js +2 -0
  40. package/build/errors/types.js.map +1 -0
  41. package/build/index.d.ts +3 -0
  42. package/build/index.d.ts.map +1 -0
  43. package/build/index.js +16 -0
  44. package/build/index.js.map +1 -0
  45. package/build/mcp/server.d.ts +3 -0
  46. package/build/mcp/server.d.ts.map +1 -0
  47. package/build/mcp/server.js +270 -0
  48. package/build/mcp/server.js.map +1 -0
  49. package/build/mcp/tools/ascii.d.ts +11 -0
  50. package/build/mcp/tools/ascii.d.ts.map +1 -0
  51. package/build/mcp/tools/ascii.js +93 -0
  52. package/build/mcp/tools/ascii.js.map +1 -0
  53. package/build/mcp/tools/basic.d.ts +6 -0
  54. package/build/mcp/tools/basic.d.ts.map +1 -0
  55. package/build/mcp/tools/basic.js +34 -0
  56. package/build/mcp/tools/basic.js.map +1 -0
  57. package/build/mcp/tools/conversion.d.ts +8 -0
  58. package/build/mcp/tools/conversion.d.ts.map +1 -0
  59. package/build/mcp/tools/conversion.js +81 -0
  60. package/build/mcp/tools/conversion.js.map +1 -0
  61. package/build/mcp/tools/programmer.d.ts +6 -0
  62. package/build/mcp/tools/programmer.d.ts.map +1 -0
  63. package/build/mcp/tools/programmer.js +29 -0
  64. package/build/mcp/tools/programmer.js.map +1 -0
  65. package/build/parser/ast.d.ts +47 -0
  66. package/build/parser/ast.d.ts.map +1 -0
  67. package/build/parser/ast.js +2 -0
  68. package/build/parser/ast.js.map +1 -0
  69. package/build/parser/lexer.d.ts +24 -0
  70. package/build/parser/lexer.d.ts.map +1 -0
  71. package/build/parser/lexer.js +168 -0
  72. package/build/parser/lexer.js.map +1 -0
  73. package/build/parser/parser.d.ts +14 -0
  74. package/build/parser/parser.d.ts.map +1 -0
  75. package/build/parser/parser.js +115 -0
  76. package/build/parser/parser.js.map +1 -0
  77. package/docs/plans/2025-02-24-mcp-calculator-design.md +344 -0
  78. package/docs/plans/2025-02-24-mcp-calculator-implementation.md +2626 -0
  79. package/openspec/changes/archive/2026-02-24-simplify-ascii-tools/.openspec.yaml +2 -0
  80. package/openspec/changes/archive/2026-02-24-simplify-ascii-tools/design.md +46 -0
  81. package/openspec/changes/archive/2026-02-24-simplify-ascii-tools/proposal.md +21 -0
  82. package/openspec/changes/archive/2026-02-24-simplify-ascii-tools/specs/ascii-conversion/spec.md +22 -0
  83. package/openspec/changes/archive/2026-02-24-simplify-ascii-tools/tasks.md +24 -0
  84. package/openspec/config.yaml +20 -0
  85. package/openspec/specs/ascii-conversion/spec.md +43 -0
  86. package/package.json +40 -0
  87. package/src/engines/decimal.ts +69 -0
  88. package/src/engines/programmer.ts +112 -0
  89. package/src/errors/handler.ts +55 -0
  90. package/src/errors/types.ts +37 -0
  91. package/src/index.ts +20 -0
  92. package/src/mcp/server.ts +287 -0
  93. package/src/mcp/tools/ascii.ts +116 -0
  94. package/src/mcp/tools/basic.ts +44 -0
  95. package/src/mcp/tools/conversion.ts +95 -0
  96. package/src/mcp/tools/programmer.ts +36 -0
  97. package/src/parser/ast.ts +51 -0
  98. package/src/parser/lexer.ts +216 -0
  99. package/src/parser/parser.ts +154 -0
  100. package/test/integration/ascii.test.ts +450 -0
  101. package/test/integration/basic-calculate.test.ts +272 -0
  102. package/test/integration/conversion.test.ts +357 -0
  103. package/test/integration/programmer-calculate.test.ts +363 -0
  104. package/test/unit/decimal-engine.test.ts +134 -0
  105. package/test/unit/error-handler.test.ts +173 -0
  106. package/test/unit/lexer.test.ts +176 -0
  107. package/test/unit/parser.test.ts +197 -0
  108. package/test/unit/programmer-engine.test.ts +234 -0
  109. package/tsconfig.json +20 -0
  110. package/vitest.config.ts +13 -0
@@ -0,0 +1,344 @@
1
+ # MCP Server 计算器设计文档
2
+
3
+ **日期**: 2025-02-24
4
+ **状态**: 已批准
5
+ **方案**: 自研解析器架构
6
+
7
+ ---
8
+
9
+ ## 概述
10
+
11
+ 为 AI 助手提供计算能力的 MCP Server,支持四则运算和程序员计算功能。采用自研解析器架构,使用 `decimal.js` 处理四则运算精度,`BigInt` 处理程序员计算大数运算。
12
+
13
+ ## 功能范围
14
+
15
+ ### 优先级
16
+ 1. **四则运算** - 基础加减乘除、括号优先级
17
+ 2. **程序员计算** - 进制转换、位运算、ASCII 码、字节序、编码转换
18
+
19
+ ### 使用场景
20
+ - AI 助手增强 - 为 Claude/AI 助手提供精确计算能力
21
+
22
+ ---
23
+
24
+ ## 架构设计
25
+
26
+ ```
27
+ ┌─────────────────────────────────────────────────────────┐
28
+ │ MCP Server │
29
+ ├─────────────────────────────────────────────────────────┤
30
+ │ ┌─────────────────────────────────────────────────┐ │
31
+ │ │ 工具层 (MCP Tools) │ │
32
+ │ │ • basic_calculate(expression) │ │
33
+ │ │ • programmer_calculate(expression) │ │
34
+ │ │ • convert_number(value, from, to) │ │
35
+ │ │ • ascii_encode(text) / ascii_decode(code) │ │
36
+ │ └─────────────────┬───────────────────────────────┘ │
37
+ │ ┌─────────────────▼───────────────────────────────┐ │
38
+ │ │ 表达式解析层 │ │
39
+ │ │ ┌─────────────┐ ┌─────────────────────┐ │ │
40
+ │ │ │ Lexer │───▶│ Parser │ │ │
41
+ │ │ │ (词法) │ │ (语法/AST) │ │ │
42
+ │ │ └─────────────┘ └──────────┬──────────┘ │ │
43
+ │ └──────────────────────────────────┼────────────────┘ │
44
+ │ ┌──────────────────────────────────▼────────────────┐ │
45
+ │ │ 计算引擎层 │ │
46
+ │ │ ┌─────────────────────┐ ┌─────────────────────┐ │ │
47
+ │ │ │ DecimalEngine │ │ ProgrammerEngine │ │ │
48
+ │ │ │ (四则运算) │ │ (程序员计算) │ │ │
49
+ │ │ │ • decimal.js │ │ • BigInt │ │ │
50
+ │ │ └─────────────────────┘ └─────────────────────┘ │ │
51
+ │ └─────────────────────────────────────────────────────┘ │
52
+ └─────────────────────────────────────────────────────────┘
53
+ ```
54
+
55
+ ### 核心设计原则
56
+ - **工具分类**: 按功能类型暴露不同的 MCP 工具
57
+ - **表达式驱动**: 每个工具接受表达式字符串,返回计算结果
58
+ - **双引擎架构**: Decimal 引擎处理四则运算,BigInt 引擎处理程序员计算
59
+ - **统一解析**: 共享 Lexer 和 Parser,不同引擎处理 AST
60
+
61
+ ---
62
+
63
+ ## MCP 工具定义
64
+
65
+ | 工具名 | 功能描述 | 示例输入 |
66
+ |--------|----------|----------|
67
+ | `basic_calculate` | 四则运算表达式计算 | `"2.5 * 3 + (10 - 4) / 2"` |
68
+ | `programmer_calculate` | 程序员计算表达式(位运算、大数) | `"0xFF & 0x0F \| 0x10"` |
69
+ | `convert_base` | 进制转换(2/8/10/16) | `{ value: "255", from: 10, to: 16 }` |
70
+ | `bit_operation` | 单独的位运算操作 | `{ op: "and", a: "0xFF", b: "0x0F" }` |
71
+ | `ascii_encode` | 文本转 ASCII 码 | `"Hello"` |
72
+ | `ascii_decode` | ASCII 码转文本 | `"72 101 108 108 111"` |
73
+ | `endian_convert` | 字节序转换 | `{ value: "0x12345678", from: "big", to: "little" }` |
74
+ | `number_encoding` | 浮点数编码(IEEE 754) | `{ value: 3.14, format: "float32" }` |
75
+
76
+ ### 返回格式(成功)
77
+ ```json
78
+ {
79
+ "success": true,
80
+ "result": "结果值(字符串形式)",
81
+ "details": {
82
+ "expression": "原始表达式",
83
+ "steps": ["计算步骤(可选)"]
84
+ }
85
+ }
86
+ ```
87
+
88
+ ### 返回格式(错误)
89
+ ```json
90
+ {
91
+ "success": false,
92
+ "error": {
93
+ "type": "SyntaxError | DivisionByZero | InvalidBase | ...",
94
+ "message": "人类可读错误信息",
95
+ "position": { "start": 5, "end": 8 },
96
+ "suggestion": "修复建议"
97
+ }
98
+ }
99
+ ```
100
+
101
+ ---
102
+
103
+ ## 表达式语法设计
104
+
105
+ ### 支持的字面量
106
+
107
+ | 类型 | 语法示例 | 说明 |
108
+ |------|----------|------|
109
+ | 十进制整数 | `42` | 普通整数 |
110
+ | 十进制小数 | `3.14`, `0.5` | 浮点数 |
111
+ | 十六进制 | `0xFF`, `0x1a3` | `0x` 前缀 |
112
+ | 二进制 | `0b1011`, `0B1100` | `0b` 前缀 |
113
+ | 八进制 | `0o77`, `0O755` | `0o` 前缀 |
114
+ | ASCII 字符 | `'A'`, `'\n'` | 单引号字符 |
115
+
116
+ ### 运算符优先级(从高到低)
117
+
118
+ | 优先级 | 运算符 | 说明 |
119
+ |--------|--------|------|
120
+ | 1 | `()` | 括号 |
121
+ | 2 | `~`, `!` | 位非、逻辑非 |
122
+ | 3 | `*`, `/`, `%` | 乘、除、取模 |
123
+ | 4 | `+`, `-` | 加、减 |
124
+ | 5 | `<<`, `>>`, `>>>` | 左移、右移、无符号右移 |
125
+ | 6 | `&` | 位与 |
126
+ | 7 | `^` | 位异或 |
127
+ | 8 | `\|` | 位或 |
128
+ | 9 | `&&`, \|\| | 逻辑与、逻辑或 |
129
+
130
+ ### 表达式示例
131
+
132
+ ```
133
+ # 四则运算
134
+ "2 + 3 * 4" # → 14
135
+ "(10 - 3) * 2" # → 14
136
+ "0.1 + 0.2" # → 0.3 (Decimal 精度)
137
+
138
+ # 程序员计算
139
+ "0xFF & 0x0F" # → 15
140
+ "0b1010 << 2" # → 40
141
+ "~0x00" # → -1
142
+ "0xFF | 0x0F0" # → 255
143
+
144
+ # 混合(建议分开,但语法支持)
145
+ "0xFF + 0b10" # → 257
146
+ ```
147
+
148
+ ---
149
+
150
+ ## 核心组件
151
+
152
+ ### 4.1 Lexer(词法分析器)
153
+
154
+ ```typescript
155
+ type TokenType =
156
+ | 'NUMBER' | 'HEX' | 'BINARY' | 'OCTAL'
157
+ | 'OPERATOR' | 'LPAREN' | 'RPAREN'
158
+ | 'CHAR_LITERAL' | 'WHITESPACE' | 'EOF';
159
+
160
+ interface Token {
161
+ type: TokenType;
162
+ value: string;
163
+ position: { start: number; end: number };
164
+ }
165
+
166
+ class Lexer {
167
+ tokenize(expression: string): Token[];
168
+ }
169
+ ```
170
+
171
+ **职责**: 将表达式字符串转换为 Token 流,支持所有字面量格式。
172
+
173
+ ### 4.2 Parser(语法分析器)
174
+
175
+ ```typescript
176
+ type ASTNode =
177
+ | BinaryOpNode | UnaryOpNode | LiteralNode
178
+ | GroupNode | FunctionNode;
179
+
180
+ interface BinaryOpNode {
181
+ type: 'BinaryOp';
182
+ operator: string;
183
+ left: ASTNode;
184
+ right: ASTNode;
185
+ }
186
+
187
+ class Parser {
188
+ parse(tokens: Token[]): ASTNode;
189
+
190
+ private parseExpression(): ASTNode;
191
+ private parseTerm(): ASTNode;
192
+ private parseFactor(): ASTNode;
193
+ }
194
+ ```
195
+
196
+ **职责**: 根据运算符优先级构建 AST,捕获语法错误并提供位置信息。
197
+
198
+ ### 4.3 DecimalEngine(四则运算引擎)
199
+
200
+ ```typescript
201
+ import { Decimal } from 'decimal.js';
202
+
203
+ class DecimalEngine {
204
+ evaluate(ast: ASTNode): Decimal;
205
+
206
+ private visitBinaryOp(node: BinaryOpNode): Decimal;
207
+ private visitUnaryOp(node: UnaryOpNode): Decimal;
208
+ private visitLiteral(node: LiteralNode): Decimal;
209
+ }
210
+ ```
211
+
212
+ **职责**: 遍历 AST,使用 `decimal.js` 执行四则运算,处理除零等错误。
213
+
214
+ ### 4.4 ProgrammerEngine(程序员计算引擎)
215
+
216
+ ```typescript
217
+ class ProgrammerEngine {
218
+ evaluate(ast: ASTNode): bigint;
219
+
220
+ private bitAnd(a: bigint, b: bigint): bigint;
221
+ private bitOr(a: bigint, b: bigint): bigint;
222
+ private bitXor(a: bigint, b: bigint): bigint;
223
+ private bitNot(value: bigint): bigint;
224
+ private shiftLeft(value: bigint, amount: bigint): bigint;
225
+
226
+ private parseLiteral(token: Token): bigint;
227
+ }
228
+ ```
229
+
230
+ **职责**: 遍历 AST,使用 `BigInt` 执行位运算和进制转换。
231
+
232
+ ### 4.5 ErrorHandler(错误处理器)
233
+
234
+ ```typescript
235
+ type ErrorType =
236
+ | 'SyntaxError' | 'DivisionByZero' | 'InvalidNumber'
237
+ | 'OverflowError' | 'InvalidBase' | 'UnmatchedParen';
238
+
239
+ class ErrorHandler {
240
+ createError(type: ErrorType, message: string, position?: Position): ErrorResponse;
241
+
242
+ syntaxError(message: string, start: number, end: number): ErrorResponse;
243
+ runtimeError(type: ErrorType, message: string): ErrorResponse;
244
+ }
245
+ ```
246
+
247
+ **职责**: 统一生成结构化错误,包含位置信息和修复建议。
248
+
249
+ ---
250
+
251
+ ## 项目结构
252
+
253
+ ```
254
+ calculator/
255
+ ├── docs/
256
+ │ └── plans/
257
+ │ └── 2025-02-24-mcp-calculator-design.md
258
+ ├── src/
259
+ │ ├── mcp/
260
+ │ │ ├── server.ts # MCP Server 入口
261
+ │ │ └── tools/ # MCP 工具实现
262
+ │ │ ├── basic.ts
263
+ │ │ ├── programmer.ts
264
+ │ │ ├── conversion.ts
265
+ │ │ ├── ascii.ts
266
+ │ │ └── encoding.ts
267
+ │ ├── parser/
268
+ │ │ ├── lexer.ts
269
+ │ │ ├── parser.ts
270
+ │ │ └── ast.ts
271
+ │ ├── engines/
272
+ │ │ ├── decimal.ts
273
+ │ │ └── programmer.ts
274
+ │ ├── errors/
275
+ │ │ ├── handler.ts
276
+ │ │ └── types.ts
277
+ │ └── index.ts
278
+ ├── test/
279
+ │ ├── unit/
280
+ │ │ ├── lexer.test.ts
281
+ │ │ ├── parser.test.ts
282
+ │ │ ├── decimal-engine.test.ts
283
+ │ │ └── programmer-engine.test.ts
284
+ │ └── integration/
285
+ │ └── mcp-tools.test.ts
286
+ ├── package.json
287
+ ├── tsconfig.json
288
+ ├── vite.config.ts
289
+ └── README.md
290
+ ```
291
+
292
+ ---
293
+
294
+ ## 测试策略
295
+
296
+ ### 单元测试(Vitest)
297
+
298
+ - **Lexer**: Token 化正确性,所有字面量类型
299
+ - **Parser**: 运算符优先级,AST 结构,语法错误捕获
300
+ - **DecimalEngine**: 计算精度(0.1 + 0.2 = 0.3),除零错误
301
+ - **ProgrammerEngine**: 位运算正确性,大数处理
302
+ - **ErrorHandler**: 错误类型,位置信息
303
+
304
+ ### 集成测试
305
+
306
+ - MCP 工具端到端测试
307
+ - 结构化成功/错误响应验证
308
+ - 边界情况处理
309
+
310
+ ### 边界情况覆盖
311
+
312
+ - 精度边界:极大/极小数
313
+ - 大数边界:BigInt 溢出
314
+ - 错误边界:空输入、不匹配括号、非法字符
315
+ - 进制边界:非法前缀、非法进制字符
316
+
317
+ ### 测试覆盖目标
318
+
319
+ | 组件 | 目标覆盖率 |
320
+ |------|------------|
321
+ | Lexer | 100% |
322
+ | Parser | 95%+ |
323
+ | DecimalEngine | 90%+ |
324
+ | ProgrammerEngine | 95%+ |
325
+ | ErrorHandler | 90%+ |
326
+
327
+ ---
328
+
329
+ ## 依赖库
330
+
331
+ - `decimal.js` - 高精度十进制运算
332
+ - `@modelcontextprotocol/sdk` - MCP Server SDK
333
+ - `vitest` - 测试框架
334
+ - `typescript` - 类型系统
335
+
336
+ ---
337
+
338
+ ## 非目标(明确不实现)
339
+
340
+ - 变量赋值和存储
341
+ - 函数定义和调用(除内置转换函数外)
342
+ - 复杂的数学函数(sin/cos/log 等)
343
+ - 历史记录功能
344
+ - 图形化界面