anonyma 1.0.0 → 1.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.
- package/CHANGELOG.md +273 -144
- package/README.md +427 -143
- package/dist/ai/index.cjs +522 -0
- package/dist/ai/index.cjs.map +1 -0
- package/dist/ai/index.d.cts +322 -0
- package/dist/ai/index.d.ts +322 -0
- package/dist/ai/index.js +505 -0
- package/dist/ai/index.js.map +1 -0
- package/dist/audit/index.cjs +359 -0
- package/dist/audit/index.cjs.map +1 -0
- package/dist/audit/index.d.cts +287 -0
- package/dist/audit/index.d.ts +287 -0
- package/dist/audit/index.js +340 -0
- package/dist/audit/index.js.map +1 -0
- package/dist/chunk-26IHMQHL.cjs +662 -0
- package/dist/chunk-26IHMQHL.cjs.map +1 -0
- package/dist/chunk-2F2GWGTB.js +174 -0
- package/dist/chunk-2F2GWGTB.js.map +1 -0
- package/dist/chunk-2NNMJ55L.cjs +45 -0
- package/dist/chunk-2NNMJ55L.cjs.map +1 -0
- package/dist/chunk-2YG7R4KJ.cjs +114 -0
- package/dist/chunk-2YG7R4KJ.cjs.map +1 -0
- package/dist/chunk-2ZZHLCEJ.js +237 -0
- package/dist/chunk-2ZZHLCEJ.js.map +1 -0
- package/dist/chunk-3CY6ATKS.cjs +308 -0
- package/dist/chunk-3CY6ATKS.cjs.map +1 -0
- package/dist/chunk-3D5ZCJ7R.js +268 -0
- package/dist/chunk-3D5ZCJ7R.js.map +1 -0
- package/dist/chunk-6LDAIJBL.js +103 -0
- package/dist/chunk-6LDAIJBL.js.map +1 -0
- package/dist/chunk-7HK2V5Q4.js +111 -0
- package/dist/chunk-7HK2V5Q4.js.map +1 -0
- package/dist/chunk-AAP472NG.js +177 -0
- package/dist/chunk-AAP472NG.js.map +1 -0
- package/dist/chunk-AP4JIUNW.js +42 -0
- package/dist/chunk-AP4JIUNW.js.map +1 -0
- package/dist/chunk-BBJPQPGP.js +457 -0
- package/dist/chunk-BBJPQPGP.js.map +1 -0
- package/dist/chunk-CRXRJZEV.cjs +82 -0
- package/dist/chunk-CRXRJZEV.cjs.map +1 -0
- package/dist/chunk-E3PICZYI.js +20 -0
- package/dist/chunk-E3PICZYI.js.map +1 -0
- package/dist/chunk-F7O2FA3F.cjs +1255 -0
- package/dist/chunk-F7O2FA3F.cjs.map +1 -0
- package/dist/chunk-FB56NAAH.cjs +243 -0
- package/dist/chunk-FB56NAAH.cjs.map +1 -0
- package/dist/chunk-G4AN7FND.js +1063 -0
- package/dist/chunk-G4AN7FND.js.map +1 -0
- package/dist/chunk-GS3FRH4K.js +294 -0
- package/dist/chunk-GS3FRH4K.js.map +1 -0
- package/dist/chunk-GYMG5OI3.cjs +186 -0
- package/dist/chunk-GYMG5OI3.cjs.map +1 -0
- package/dist/chunk-IHEIYZPL.cjs +301 -0
- package/dist/chunk-IHEIYZPL.cjs.map +1 -0
- package/dist/chunk-IJAMLYOT.cjs +1087 -0
- package/dist/chunk-IJAMLYOT.cjs.map +1 -0
- package/dist/chunk-ISL5B4LU.cjs +181 -0
- package/dist/chunk-ISL5B4LU.cjs.map +1 -0
- package/dist/chunk-JKW36IPT.js +179 -0
- package/dist/chunk-JKW36IPT.js.map +1 -0
- package/dist/chunk-KOCQBDI4.cjs +179 -0
- package/dist/chunk-KOCQBDI4.cjs.map +1 -0
- package/dist/chunk-OGNLB2KA.js +79 -0
- package/dist/chunk-OGNLB2KA.js.map +1 -0
- package/dist/chunk-PKHZ7CVI.js +28 -0
- package/dist/chunk-PKHZ7CVI.js.map +1 -0
- package/dist/chunk-QYD4F6T7.cjs +114 -0
- package/dist/chunk-QYD4F6T7.cjs.map +1 -0
- package/dist/chunk-RCW5IPPR.js +1288 -0
- package/dist/chunk-RCW5IPPR.js.map +1 -0
- package/dist/chunk-RMMKYHUN.cjs +35 -0
- package/dist/chunk-RMMKYHUN.cjs.map +1 -0
- package/dist/chunk-S4BJ2ADB.cjs +22 -0
- package/dist/chunk-S4BJ2ADB.cjs.map +1 -0
- package/dist/chunk-S7NH6CHY.cjs +462 -0
- package/dist/chunk-S7NH6CHY.cjs.map +1 -0
- package/dist/chunk-SIHPDZXA.js +176 -0
- package/dist/chunk-SIHPDZXA.js.map +1 -0
- package/dist/chunk-TW7PM3UJ.cjs +30 -0
- package/dist/chunk-TW7PM3UJ.cjs.map +1 -0
- package/dist/chunk-UA3EOFBS.cjs +1322 -0
- package/dist/chunk-UA3EOFBS.cjs.map +1 -0
- package/dist/chunk-VE4CCW7W.js +32 -0
- package/dist/chunk-VE4CCW7W.js.map +1 -0
- package/dist/chunk-W6RBCHYF.js +651 -0
- package/dist/chunk-W6RBCHYF.js.map +1 -0
- package/dist/chunk-WLJN7GIS.cjs +189 -0
- package/dist/chunk-WLJN7GIS.cjs.map +1 -0
- package/dist/chunk-YMLR375Z.js +1244 -0
- package/dist/chunk-YMLR375Z.js.map +1 -0
- package/dist/compliance/index.cjs +88 -0
- package/dist/compliance/index.cjs.map +1 -0
- package/dist/compliance/index.d.cts +277 -0
- package/dist/compliance/index.d.ts +277 -0
- package/dist/compliance/index.js +47 -0
- package/dist/compliance/index.js.map +1 -0
- package/dist/crypto.cjs +12 -136
- package/dist/crypto.cjs.map +1 -1
- package/dist/crypto.d.cts +4 -3
- package/dist/crypto.d.ts +4 -3
- package/dist/crypto.js +3 -137
- package/dist/crypto.js.map +1 -1
- package/dist/detectors/index.cjs +146 -1483
- package/dist/detectors/index.cjs.map +1 -1
- package/dist/detectors/index.d.cts +1 -1
- package/dist/detectors/index.d.ts +1 -1
- package/dist/detectors/index.js +3 -1449
- package/dist/detectors/index.js.map +1 -1
- package/dist/engine/index.cjs +441 -0
- package/dist/engine/index.cjs.map +1 -0
- package/dist/engine/index.d.cts +728 -0
- package/dist/engine/index.d.ts +728 -0
- package/dist/engine/index.js +189 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/hash-2PSYNDMM.cjs +14 -0
- package/dist/hash-2PSYNDMM.cjs.map +1 -0
- package/dist/hash-CELLDNKN.js +5 -0
- package/dist/hash-CELLDNKN.js.map +1 -0
- package/dist/index.cjs +153 -2816
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +93 -11
- package/dist/index.d.ts +93 -11
- package/dist/index.js +13 -2791
- package/dist/index.js.map +1 -1
- package/dist/json-CKDvlRtl.d.ts +147 -0
- package/dist/json-s8ny-RCo.d.cts +147 -0
- package/dist/mcp/index.cjs +751 -0
- package/dist/mcp/index.cjs.map +1 -0
- package/dist/mcp/index.d.cts +226 -0
- package/dist/mcp/index.d.ts +226 -0
- package/dist/mcp/index.js +744 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/middleware/express.cjs +93 -0
- package/dist/middleware/express.cjs.map +1 -0
- package/dist/middleware/express.d.cts +90 -0
- package/dist/middleware/express.d.ts +90 -0
- package/dist/middleware/express.js +91 -0
- package/dist/middleware/express.js.map +1 -0
- package/dist/middleware/hono.cjs +58 -0
- package/dist/middleware/hono.cjs.map +1 -0
- package/dist/middleware/hono.d.cts +75 -0
- package/dist/middleware/hono.d.ts +75 -0
- package/dist/middleware/hono.js +56 -0
- package/dist/middleware/hono.js.map +1 -0
- package/dist/middleware/index.cjs +24 -0
- package/dist/middleware/index.cjs.map +1 -0
- package/dist/middleware/index.d.cts +106 -0
- package/dist/middleware/index.d.ts +106 -0
- package/dist/middleware/index.js +15 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/schemas.cjs +5 -5
- package/dist/schemas.cjs.map +1 -1
- package/dist/schemas.d.cts +20 -10
- package/dist/schemas.d.ts +20 -10
- package/dist/schemas.js +5 -5
- package/dist/schemas.js.map +1 -1
- package/dist/session-BdRftyZ5.d.cts +102 -0
- package/dist/session-Cs2zlNQp.d.ts +102 -0
- package/dist/stream.cjs +13 -2262
- package/dist/stream.cjs.map +1 -1
- package/dist/stream.d.cts +1 -1
- package/dist/stream.d.ts +1 -1
- package/dist/stream.js +6 -2255
- package/dist/stream.js.map +1 -1
- package/dist/types--tPm_AjY.d.ts +199 -0
- package/dist/types-0BJH_wqc.d.cts +199 -0
- package/dist/types-CPUhjvl8.d.cts +629 -0
- package/dist/types-Dtht33aq.d.ts +629 -0
- package/dist/{types-D-9HzVuy.d.cts → types-EJTj_BKY.d.cts} +38 -7
- package/dist/{types-D-9HzVuy.d.ts → types-EJTj_BKY.d.ts} +38 -7
- package/dist/types-Zt_bZvwS.d.cts +182 -0
- package/dist/types-Zt_bZvwS.d.ts +182 -0
- package/dist/validators.cjs +39 -181
- package/dist/validators.cjs.map +1 -1
- package/dist/validators.js +1 -174
- package/dist/validators.js.map +1 -1
- package/dist/vault/index.cjs +688 -0
- package/dist/vault/index.cjs.map +1 -0
- package/dist/vault/index.d.cts +385 -0
- package/dist/vault/index.d.ts +385 -0
- package/dist/vault/index.js +669 -0
- package/dist/vault/index.js.map +1 -0
- package/package.json +128 -11
|
@@ -0,0 +1,744 @@
|
|
|
1
|
+
import { isRegulationId, REGULATIONS, checkPolicy } from '../chunk-YMLR375Z.js';
|
|
2
|
+
import { summarizeSpans } from '../chunk-VE4CCW7W.js';
|
|
3
|
+
import { sanitizeJson } from '../chunk-BBJPQPGP.js';
|
|
4
|
+
import { compilePipeline } from '../chunk-G4AN7FND.js';
|
|
5
|
+
import { BUILTIN_CATEGORIES } from '../chunk-3D5ZCJ7R.js';
|
|
6
|
+
import '../chunk-AAP472NG.js';
|
|
7
|
+
import '../chunk-E3PICZYI.js';
|
|
8
|
+
import '../chunk-7HK2V5Q4.js';
|
|
9
|
+
import { PRESET_REGISTRY } from '../chunk-GS3FRH4K.js';
|
|
10
|
+
import '../chunk-RCW5IPPR.js';
|
|
11
|
+
import '../chunk-SIHPDZXA.js';
|
|
12
|
+
import { toHex } from '../chunk-6LDAIJBL.js';
|
|
13
|
+
import { createSessionTokenizer } from '../chunk-2ZZHLCEJ.js';
|
|
14
|
+
import { webCrypto } from '../chunk-PKHZ7CVI.js';
|
|
15
|
+
import { AnonymaError } from '../chunk-2F2GWGTB.js';
|
|
16
|
+
|
|
17
|
+
// src/mcp/definitions.ts
|
|
18
|
+
var MCP_PROTOCOL_VERSIONS = Object.freeze([
|
|
19
|
+
"2025-06-18",
|
|
20
|
+
"2025-03-26",
|
|
21
|
+
"2024-11-05"
|
|
22
|
+
]);
|
|
23
|
+
var PURE = {
|
|
24
|
+
readOnlyHint: true,
|
|
25
|
+
destructiveHint: false,
|
|
26
|
+
idempotentHint: true,
|
|
27
|
+
openWorldHint: false
|
|
28
|
+
};
|
|
29
|
+
var CATEGORY = { type: "string", enum: BUILTIN_CATEGORIES };
|
|
30
|
+
var CATEGORIES = {
|
|
31
|
+
type: "array",
|
|
32
|
+
description: "Categories to detect. Leave it out for the categories of the preset, or for all categories.",
|
|
33
|
+
items: CATEGORY,
|
|
34
|
+
minItems: 1,
|
|
35
|
+
maxItems: BUILTIN_CATEGORIES.length
|
|
36
|
+
};
|
|
37
|
+
var PRESET = {
|
|
38
|
+
type: "string",
|
|
39
|
+
description: "Compliance preset that selects categories and strategies.",
|
|
40
|
+
enum: ["gdpr", "lgpd", "pipeda", "hipaa", "ccpa", "pci-dss", "sox", "ferpa"]
|
|
41
|
+
};
|
|
42
|
+
var TEXT = {
|
|
43
|
+
type: "string",
|
|
44
|
+
description: "The text to process.",
|
|
45
|
+
maxLength: 1e6
|
|
46
|
+
};
|
|
47
|
+
var MIN_CONFIDENCE = {
|
|
48
|
+
type: "number",
|
|
49
|
+
description: "Ignore detections below this confidence (0 to 1).",
|
|
50
|
+
minimum: 0,
|
|
51
|
+
maximum: 1
|
|
52
|
+
};
|
|
53
|
+
var FIELDS = {
|
|
54
|
+
type: "array",
|
|
55
|
+
description: "What was replaced where. Contains no replaced text.",
|
|
56
|
+
items: {
|
|
57
|
+
type: "object",
|
|
58
|
+
properties: {
|
|
59
|
+
path: { type: "string", description: "JSON Pointer of the field; empty for plain text." },
|
|
60
|
+
category: { type: "string" },
|
|
61
|
+
detector: { type: "string" },
|
|
62
|
+
rule: { type: "string" },
|
|
63
|
+
count: { type: "integer", minimum: 0 }
|
|
64
|
+
},
|
|
65
|
+
required: ["path", "category", "detector", "rule", "count"],
|
|
66
|
+
additionalProperties: false
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var STRATEGY = {
|
|
70
|
+
type: "object",
|
|
71
|
+
description: "How detected values are replaced. Defaults to the preset's strategy, or to redaction.",
|
|
72
|
+
properties: {
|
|
73
|
+
strategy: {
|
|
74
|
+
type: "string",
|
|
75
|
+
enum: ["redact", "mask", "pseudonymize", "generalize", "synthesize"]
|
|
76
|
+
},
|
|
77
|
+
label: {
|
|
78
|
+
type: "string",
|
|
79
|
+
description: "redact: the replacement label.",
|
|
80
|
+
minLength: 1,
|
|
81
|
+
maxLength: 64
|
|
82
|
+
},
|
|
83
|
+
maskChar: {
|
|
84
|
+
type: "string",
|
|
85
|
+
description: "mask: the masking character.",
|
|
86
|
+
minLength: 1,
|
|
87
|
+
maxLength: 1
|
|
88
|
+
},
|
|
89
|
+
keepLeading: {
|
|
90
|
+
type: "integer",
|
|
91
|
+
description: "mask: leading characters left visible.",
|
|
92
|
+
minimum: 0
|
|
93
|
+
},
|
|
94
|
+
keepTrailing: {
|
|
95
|
+
type: "integer",
|
|
96
|
+
description: "mask: trailing characters left visible.",
|
|
97
|
+
minimum: 0
|
|
98
|
+
},
|
|
99
|
+
bucketSize: {
|
|
100
|
+
type: "integer",
|
|
101
|
+
description: "generalize: width of the numeric range.",
|
|
102
|
+
minimum: 1
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
required: ["strategy"],
|
|
106
|
+
additionalProperties: false
|
|
107
|
+
};
|
|
108
|
+
var DETECT = {
|
|
109
|
+
name: "anonyma_detect",
|
|
110
|
+
title: "Detect personal data",
|
|
111
|
+
description: "Find personal data (email addresses, phone numbers, government identifiers, payment cards, IP addresses and more) in a text. Returns the position, category and confidence of every detection, never the detected text itself. Use it to decide whether text is safe to store, log or send elsewhere.",
|
|
112
|
+
inputSchema: {
|
|
113
|
+
type: "object",
|
|
114
|
+
properties: {
|
|
115
|
+
text: TEXT,
|
|
116
|
+
categories: CATEGORIES,
|
|
117
|
+
preset: PRESET,
|
|
118
|
+
minConfidence: MIN_CONFIDENCE
|
|
119
|
+
},
|
|
120
|
+
required: ["text"],
|
|
121
|
+
additionalProperties: false
|
|
122
|
+
},
|
|
123
|
+
outputSchema: {
|
|
124
|
+
type: "object",
|
|
125
|
+
properties: {
|
|
126
|
+
found: { type: "boolean" },
|
|
127
|
+
count: { type: "integer", minimum: 0 },
|
|
128
|
+
spans: {
|
|
129
|
+
type: "array",
|
|
130
|
+
items: {
|
|
131
|
+
type: "object",
|
|
132
|
+
properties: {
|
|
133
|
+
start: { type: "integer", minimum: 0 },
|
|
134
|
+
end: { type: "integer", minimum: 0 },
|
|
135
|
+
category: { type: "string" },
|
|
136
|
+
confidence: { type: "number", minimum: 0, maximum: 1 },
|
|
137
|
+
detector: { type: "string" }
|
|
138
|
+
},
|
|
139
|
+
required: ["start", "end", "category", "confidence", "detector"],
|
|
140
|
+
additionalProperties: false
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
required: ["found", "count", "spans"],
|
|
145
|
+
additionalProperties: false
|
|
146
|
+
},
|
|
147
|
+
annotations: PURE
|
|
148
|
+
};
|
|
149
|
+
var ANONYMIZE = {
|
|
150
|
+
name: "anonyma_anonymize",
|
|
151
|
+
title: "Anonymize text or JSON",
|
|
152
|
+
description: "Replace personal data in a text, or in every string of a JSON value, irreversibly. The structure of JSON (keys, nesting, array lengths, non-string values) is preserved. Provide exactly one of `text` and `json`. Use it before writing data to logs, tickets, prompts or files.",
|
|
153
|
+
inputSchema: {
|
|
154
|
+
type: "object",
|
|
155
|
+
properties: {
|
|
156
|
+
text: TEXT,
|
|
157
|
+
json: {
|
|
158
|
+
description: "A JSON object or array. Its string values, its keys and its integers of 12 or more digits are anonymized."
|
|
159
|
+
},
|
|
160
|
+
preset: PRESET,
|
|
161
|
+
categories: CATEGORIES,
|
|
162
|
+
strategy: STRATEGY,
|
|
163
|
+
minConfidence: MIN_CONFIDENCE
|
|
164
|
+
},
|
|
165
|
+
additionalProperties: false
|
|
166
|
+
},
|
|
167
|
+
outputSchema: {
|
|
168
|
+
type: "object",
|
|
169
|
+
properties: {
|
|
170
|
+
text: { type: "string", description: "The anonymized text, when `text` was given." },
|
|
171
|
+
json: { description: "The anonymized JSON value, when `json` was given." },
|
|
172
|
+
replaced: { type: "integer", minimum: 0 },
|
|
173
|
+
fields: FIELDS
|
|
174
|
+
},
|
|
175
|
+
required: ["replaced", "fields"],
|
|
176
|
+
additionalProperties: false
|
|
177
|
+
},
|
|
178
|
+
annotations: PURE
|
|
179
|
+
};
|
|
180
|
+
var TOKENIZE = {
|
|
181
|
+
name: "anonyma_tokenize",
|
|
182
|
+
title: "Replace personal data with tokens",
|
|
183
|
+
description: "Replace personal data in a text with placeholder tokens such as [EMAIL_0001]. Equal values get equal tokens within a session, so the text stays meaningful. The original values stay on the server. Pass the returned `session` to later calls to keep using the same tokens.",
|
|
184
|
+
inputSchema: {
|
|
185
|
+
type: "object",
|
|
186
|
+
properties: {
|
|
187
|
+
text: TEXT,
|
|
188
|
+
session: {
|
|
189
|
+
type: "string",
|
|
190
|
+
description: "Session returned by an earlier call.",
|
|
191
|
+
minLength: 8,
|
|
192
|
+
maxLength: 64
|
|
193
|
+
},
|
|
194
|
+
categories: CATEGORIES,
|
|
195
|
+
preset: PRESET,
|
|
196
|
+
minConfidence: MIN_CONFIDENCE
|
|
197
|
+
},
|
|
198
|
+
required: ["text"],
|
|
199
|
+
additionalProperties: false
|
|
200
|
+
},
|
|
201
|
+
outputSchema: {
|
|
202
|
+
type: "object",
|
|
203
|
+
properties: {
|
|
204
|
+
text: { type: "string" },
|
|
205
|
+
session: { type: "string" },
|
|
206
|
+
replaced: { type: "integer", minimum: 0 },
|
|
207
|
+
fields: FIELDS
|
|
208
|
+
},
|
|
209
|
+
required: ["text", "session", "replaced", "fields"],
|
|
210
|
+
additionalProperties: false
|
|
211
|
+
},
|
|
212
|
+
annotations: {
|
|
213
|
+
readOnlyHint: false,
|
|
214
|
+
destructiveHint: false,
|
|
215
|
+
idempotentHint: true,
|
|
216
|
+
openWorldHint: false
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
var DETOKENIZE = {
|
|
220
|
+
name: "anonyma_detokenize",
|
|
221
|
+
title: "Restore tokens to original values",
|
|
222
|
+
description: "Replace the tokens of a session with the original values. This reveals personal data to the caller and is only available when the server operator has enabled it.",
|
|
223
|
+
inputSchema: {
|
|
224
|
+
type: "object",
|
|
225
|
+
properties: {
|
|
226
|
+
text: TEXT,
|
|
227
|
+
session: {
|
|
228
|
+
type: "string",
|
|
229
|
+
description: "Session returned by anonyma_tokenize.",
|
|
230
|
+
minLength: 8,
|
|
231
|
+
maxLength: 64
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
required: ["text", "session"],
|
|
235
|
+
additionalProperties: false
|
|
236
|
+
},
|
|
237
|
+
outputSchema: {
|
|
238
|
+
type: "object",
|
|
239
|
+
properties: {
|
|
240
|
+
text: { type: "string" },
|
|
241
|
+
restored: { type: "integer", minimum: 0 },
|
|
242
|
+
unresolved: { type: "array", items: { type: "string" } }
|
|
243
|
+
},
|
|
244
|
+
required: ["text", "restored", "unresolved"],
|
|
245
|
+
additionalProperties: false
|
|
246
|
+
},
|
|
247
|
+
annotations: {
|
|
248
|
+
readOnlyHint: true,
|
|
249
|
+
destructiveHint: false,
|
|
250
|
+
idempotentHint: true,
|
|
251
|
+
openWorldHint: false
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
var CHECK_POLICY = {
|
|
255
|
+
name: "anonyma_check_policy",
|
|
256
|
+
title: "Check an anonymization policy",
|
|
257
|
+
description: "Validate an anonyma policy document and check it against the regulations it extends (GDPR, LGPD, PIPEDA, CCPA, HIPAA, PCI DSS). Returns every problem with a JSON Pointer and, for compliance problems, the provision that is violated.",
|
|
258
|
+
inputSchema: {
|
|
259
|
+
type: "object",
|
|
260
|
+
properties: { policy: { type: "object", description: "The policy document." } },
|
|
261
|
+
required: ["policy"],
|
|
262
|
+
additionalProperties: false
|
|
263
|
+
},
|
|
264
|
+
outputSchema: {
|
|
265
|
+
type: "object",
|
|
266
|
+
properties: {
|
|
267
|
+
valid: { type: "boolean" },
|
|
268
|
+
issues: {
|
|
269
|
+
type: "array",
|
|
270
|
+
items: {
|
|
271
|
+
type: "object",
|
|
272
|
+
properties: {
|
|
273
|
+
severity: { type: "string", enum: ["error", "warning"] },
|
|
274
|
+
path: { type: "string" },
|
|
275
|
+
code: { type: "string" },
|
|
276
|
+
message: { type: "string" },
|
|
277
|
+
citation: { type: "string" }
|
|
278
|
+
},
|
|
279
|
+
required: ["severity", "path", "code", "message"],
|
|
280
|
+
additionalProperties: false
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
required: ["valid", "issues"],
|
|
285
|
+
additionalProperties: false
|
|
286
|
+
},
|
|
287
|
+
annotations: PURE
|
|
288
|
+
};
|
|
289
|
+
var MCP_TOOLS = Object.freeze([
|
|
290
|
+
DETECT,
|
|
291
|
+
ANONYMIZE,
|
|
292
|
+
TOKENIZE,
|
|
293
|
+
DETOKENIZE,
|
|
294
|
+
CHECK_POLICY
|
|
295
|
+
]);
|
|
296
|
+
var MCP_RESOURCES = Object.freeze([
|
|
297
|
+
{
|
|
298
|
+
uri: "anonyma://categories",
|
|
299
|
+
name: "categories",
|
|
300
|
+
title: "Detectable categories",
|
|
301
|
+
description: "The categories of personal data the server can detect.",
|
|
302
|
+
mimeType: "application/json"
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
uri: "anonyma://strategies",
|
|
306
|
+
name: "strategies",
|
|
307
|
+
title: "Anonymization strategies",
|
|
308
|
+
description: "The strategies a tool call may request, and what each does to a value.",
|
|
309
|
+
mimeType: "application/json"
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
uri: "anonyma://presets",
|
|
313
|
+
name: "presets",
|
|
314
|
+
title: "Compliance presets",
|
|
315
|
+
description: "The compliance presets with their categories and strategies.",
|
|
316
|
+
mimeType: "application/json"
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
uri: "anonyma://audit/head",
|
|
320
|
+
name: "audit-head",
|
|
321
|
+
title: "Audit chain head",
|
|
322
|
+
description: "Sequence number and hash of the most recent audit record of this server.",
|
|
323
|
+
mimeType: "application/json"
|
|
324
|
+
}
|
|
325
|
+
]);
|
|
326
|
+
var MCP_RESOURCE_TEMPLATES = Object.freeze([
|
|
327
|
+
{
|
|
328
|
+
uriTemplate: "anonyma://regulations/{id}",
|
|
329
|
+
name: "regulation",
|
|
330
|
+
title: "Regulation profile",
|
|
331
|
+
description: "For one regulation (gdpr, lgpd, pipeda, ccpa, hipaa, pci-dss): the categories in scope with the provisions that bring them into scope, the protection required, and the data elements that cannot be detected.",
|
|
332
|
+
mimeType: "application/json"
|
|
333
|
+
}
|
|
334
|
+
]);
|
|
335
|
+
|
|
336
|
+
// src/mcp/schema.ts
|
|
337
|
+
function typeOf(value) {
|
|
338
|
+
if (value === null) return "null";
|
|
339
|
+
if (Array.isArray(value)) return "array";
|
|
340
|
+
if (typeof value === "number") return Number.isInteger(value) ? "integer" : "number";
|
|
341
|
+
return typeof value;
|
|
342
|
+
}
|
|
343
|
+
function validate(schema, value, path = "") {
|
|
344
|
+
const errors = [];
|
|
345
|
+
const at = path === "" ? "/" : path;
|
|
346
|
+
const actual = typeOf(value);
|
|
347
|
+
if (schema.type !== void 0 && actual !== schema.type && !(schema.type === "number" && actual === "integer")) {
|
|
348
|
+
return [`${at}: expected ${schema.type}, got ${actual}`];
|
|
349
|
+
}
|
|
350
|
+
if (schema.enum !== void 0 && !(typeof value === "string" && schema.enum.includes(value))) {
|
|
351
|
+
errors.push(`${at}: must be one of ${schema.enum.join(", ")}`);
|
|
352
|
+
}
|
|
353
|
+
if (typeof value === "string") {
|
|
354
|
+
if (schema.minLength !== void 0 && value.length < schema.minLength) {
|
|
355
|
+
errors.push(`${at}: must have at least ${String(schema.minLength)} characters`);
|
|
356
|
+
}
|
|
357
|
+
if (schema.maxLength !== void 0 && value.length > schema.maxLength) {
|
|
358
|
+
errors.push(`${at}: must have at most ${String(schema.maxLength)} characters`);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (typeof value === "number") {
|
|
362
|
+
if (schema.minimum !== void 0 && value < schema.minimum) {
|
|
363
|
+
errors.push(`${at}: must be at least ${String(schema.minimum)}`);
|
|
364
|
+
}
|
|
365
|
+
if (schema.maximum !== void 0 && value > schema.maximum) {
|
|
366
|
+
errors.push(`${at}: must be at most ${String(schema.maximum)}`);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
if (Array.isArray(value)) {
|
|
370
|
+
if (schema.minItems !== void 0 && value.length < schema.minItems) {
|
|
371
|
+
errors.push(`${at}: must have at least ${String(schema.minItems)} item(s)`);
|
|
372
|
+
}
|
|
373
|
+
if (schema.maxItems !== void 0 && value.length > schema.maxItems) {
|
|
374
|
+
return [...errors, `${at}: must have at most ${String(schema.maxItems)} items`];
|
|
375
|
+
}
|
|
376
|
+
if (schema.items !== void 0) {
|
|
377
|
+
for (const [index, item] of value.entries()) {
|
|
378
|
+
errors.push(...validate(schema.items, item, `${path}/${String(index)}`));
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
if (actual === "object" && schema.type === "object") {
|
|
383
|
+
const record = value;
|
|
384
|
+
const properties = schema.properties ?? {};
|
|
385
|
+
for (const name of schema.required ?? []) {
|
|
386
|
+
if (!Object.hasOwn(record, name)) errors.push(`${path}/${name}: is required`);
|
|
387
|
+
}
|
|
388
|
+
for (const [name, member] of Object.entries(record)) {
|
|
389
|
+
const propertySchema = Object.hasOwn(properties, name) ? properties[name] : void 0;
|
|
390
|
+
if (propertySchema !== void 0) {
|
|
391
|
+
errors.push(...validate(propertySchema, member, `${path}/${name}`));
|
|
392
|
+
} else if (schema.additionalProperties === false) {
|
|
393
|
+
errors.push(`${path}/${name}: is not a known parameter`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return errors;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// src/mcp/server.ts
|
|
401
|
+
var PARSE_ERROR = -32700;
|
|
402
|
+
var INVALID_REQUEST = -32600;
|
|
403
|
+
var METHOD_NOT_FOUND = -32601;
|
|
404
|
+
var INVALID_PARAMS = -32602;
|
|
405
|
+
var INTERNAL_ERROR = -32603;
|
|
406
|
+
var RESOURCE_NOT_FOUND = -32002;
|
|
407
|
+
var MAX_PROBLEMS = 10;
|
|
408
|
+
var invalidRequest = Object.freeze({
|
|
409
|
+
jsonrpc: "2.0",
|
|
410
|
+
id: null,
|
|
411
|
+
error: Object.freeze({ code: INVALID_REQUEST, message: "Invalid request." })
|
|
412
|
+
});
|
|
413
|
+
var RpcError = class extends Error {
|
|
414
|
+
constructor(code, message) {
|
|
415
|
+
super(message);
|
|
416
|
+
this.code = code;
|
|
417
|
+
}
|
|
418
|
+
code;
|
|
419
|
+
};
|
|
420
|
+
var STRATEGY_NOTES = {
|
|
421
|
+
redact: "Replaces the value with a label. Nothing of the value remains.",
|
|
422
|
+
mask: "Replaces characters with a mask character; keepLeading/keepTrailing leave characters visible.",
|
|
423
|
+
pseudonymize: "Replaces the value with a random identifier.",
|
|
424
|
+
generalize: "Replaces a number with a range; other values are redacted.",
|
|
425
|
+
synthesize: "Replaces the value with a made-up value of the same kind."
|
|
426
|
+
};
|
|
427
|
+
function createMcpServer(options = {}) {
|
|
428
|
+
const { allowDetokenize = false, audit, maxSessions = 256, maxJsonLength = 1e6 } = options;
|
|
429
|
+
const serverInfo = options.serverInfo ?? { name: "anonyma", version: "1.1.0" };
|
|
430
|
+
const tools = MCP_TOOLS.filter((tool) => allowDetokenize || tool.name !== "anonyma_detokenize");
|
|
431
|
+
const sessions = /* @__PURE__ */ new Map();
|
|
432
|
+
function specOf(args, forced) {
|
|
433
|
+
const preset = args["preset"];
|
|
434
|
+
const strategy = forced ?? args["strategy"];
|
|
435
|
+
const categories = args["categories"] ?? (strategy !== void 0 && preset !== void 0 ? PRESET_REGISTRY[preset].categories : void 0);
|
|
436
|
+
return {
|
|
437
|
+
...strategy === void 0 && preset !== void 0 ? { preset } : {},
|
|
438
|
+
...categories !== void 0 ? { categories } : {},
|
|
439
|
+
...args["minConfidence"] !== void 0 ? { minConfidence: args["minConfidence"] } : {},
|
|
440
|
+
...strategy !== void 0 ? { defaultStrategy: strategy } : {}
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
async function sessionFor(id) {
|
|
444
|
+
if (typeof id === "string") {
|
|
445
|
+
const existing = sessions.get(id);
|
|
446
|
+
if (existing === void 0)
|
|
447
|
+
throw new AnonymaError("Unknown or expired session.", "MCP_UNKNOWN_SESSION");
|
|
448
|
+
sessions.delete(id);
|
|
449
|
+
sessions.set(id, existing);
|
|
450
|
+
return { id, session: existing };
|
|
451
|
+
}
|
|
452
|
+
const crypto = await webCrypto();
|
|
453
|
+
const fresh = toHex(crypto.getRandomValues(new Uint8Array(16)));
|
|
454
|
+
const tag = toHex(crypto.getRandomValues(new Uint8Array(4)));
|
|
455
|
+
const session = createSessionTokenizer({ tag });
|
|
456
|
+
sessions.set(fresh, session);
|
|
457
|
+
while (sessions.size > maxSessions) {
|
|
458
|
+
const oldest = sessions.keys().next();
|
|
459
|
+
if (oldest.done === true) break;
|
|
460
|
+
sessions.delete(oldest.value);
|
|
461
|
+
}
|
|
462
|
+
return { id: fresh, session };
|
|
463
|
+
}
|
|
464
|
+
async function runTool(tool, args) {
|
|
465
|
+
switch (tool.name) {
|
|
466
|
+
case "anonyma_detect": {
|
|
467
|
+
const pipeline = compilePipeline(specOf(args));
|
|
468
|
+
const spans = pipeline.scan(args["text"]);
|
|
469
|
+
return {
|
|
470
|
+
structured: {
|
|
471
|
+
found: spans.length > 0,
|
|
472
|
+
count: spans.length,
|
|
473
|
+
spans: spans.map(({ start, end, category, confidence, detector }) => ({
|
|
474
|
+
start,
|
|
475
|
+
end,
|
|
476
|
+
category,
|
|
477
|
+
confidence,
|
|
478
|
+
detector
|
|
479
|
+
}))
|
|
480
|
+
},
|
|
481
|
+
operation: "detect",
|
|
482
|
+
fields: summarizeSpans(spans, { rule: "detect" })
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
case "anonyma_anonymize": {
|
|
486
|
+
const hasText = typeof args["text"] === "string";
|
|
487
|
+
const json = args["json"];
|
|
488
|
+
if (hasText === (json !== void 0)) {
|
|
489
|
+
throw new AnonymaError(
|
|
490
|
+
"Provide exactly one of `text` and `json`.",
|
|
491
|
+
"MCP_INVALID_ARGUMENTS"
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
const spec = specOf(args);
|
|
495
|
+
const pipeline = compilePipeline(spec);
|
|
496
|
+
const rule = (spec.defaultStrategy ?? { strategy: spec.preset ?? "redact" }).strategy;
|
|
497
|
+
if (hasText) {
|
|
498
|
+
const result2 = pipeline.transform(args["text"]);
|
|
499
|
+
const fields = summarizeSpans(result2.spans, { rule });
|
|
500
|
+
return {
|
|
501
|
+
structured: { text: result2.text, replaced: result2.spans.length, fields },
|
|
502
|
+
operation: "anonymize",
|
|
503
|
+
fields
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
if (typeof json !== "object" || json === null) {
|
|
507
|
+
throw new AnonymaError("`json` must be an object or an array.", "MCP_INVALID_ARGUMENTS");
|
|
508
|
+
}
|
|
509
|
+
if (JSON.stringify(json).length > maxJsonLength) {
|
|
510
|
+
throw new AnonymaError(
|
|
511
|
+
`\`json\` is larger than ${String(maxJsonLength)} characters.`,
|
|
512
|
+
"MCP_INVALID_ARGUMENTS"
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
const result = sanitizeJson(json, { pipeline, ruleName: () => rule });
|
|
516
|
+
return {
|
|
517
|
+
structured: { json: result.value, replaced: result.replaced, fields: result.fields },
|
|
518
|
+
operation: "anonymize",
|
|
519
|
+
fields: [...result.fields]
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
case "anonyma_tokenize": {
|
|
523
|
+
const { id, session } = await sessionFor(args["session"]);
|
|
524
|
+
const pipeline = compilePipeline(specOf(args, { strategy: "tokenize" }), {
|
|
525
|
+
tokenization: session
|
|
526
|
+
});
|
|
527
|
+
const result = pipeline.transform(args["text"]);
|
|
528
|
+
const fields = summarizeSpans(result.spans, { rule: "tokenize" });
|
|
529
|
+
return {
|
|
530
|
+
structured: { text: result.text, session: id, replaced: result.spans.length, fields },
|
|
531
|
+
operation: "tokenize",
|
|
532
|
+
fields
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
case "anonyma_detokenize": {
|
|
536
|
+
const { session } = await sessionFor(args["session"]);
|
|
537
|
+
const result = session.restore(args["text"]);
|
|
538
|
+
return {
|
|
539
|
+
structured: {
|
|
540
|
+
text: result.text,
|
|
541
|
+
restored: result.restored,
|
|
542
|
+
unresolved: [...result.unresolved]
|
|
543
|
+
},
|
|
544
|
+
operation: "detokenize",
|
|
545
|
+
fields: []
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
default: {
|
|
549
|
+
const { policy, issues } = checkPolicy(args["policy"]);
|
|
550
|
+
return {
|
|
551
|
+
structured: {
|
|
552
|
+
valid: policy !== void 0,
|
|
553
|
+
issues: issues.map(({ severity, path, code, message, reference }) => ({
|
|
554
|
+
severity,
|
|
555
|
+
path,
|
|
556
|
+
code,
|
|
557
|
+
message,
|
|
558
|
+
...reference !== void 0 ? { citation: reference.citation } : {}
|
|
559
|
+
}))
|
|
560
|
+
},
|
|
561
|
+
operation: "policy-load",
|
|
562
|
+
fields: []
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
async function callTool(params) {
|
|
568
|
+
const { name, arguments: args = {} } = params ?? {};
|
|
569
|
+
const tool = tools.find((candidate) => candidate.name === name);
|
|
570
|
+
if (tool === void 0) throw new RpcError(INVALID_PARAMS, "Unknown tool.");
|
|
571
|
+
const problems = validate(tool.inputSchema, args);
|
|
572
|
+
if (problems.length > 0) {
|
|
573
|
+
const shown = problems.slice(0, MAX_PROBLEMS).join("; ");
|
|
574
|
+
const more = problems.length - MAX_PROBLEMS;
|
|
575
|
+
return {
|
|
576
|
+
content: [
|
|
577
|
+
{
|
|
578
|
+
type: "text",
|
|
579
|
+
text: `Invalid arguments: ${shown}${more > 0 ? `; and ${String(more)} more` : ""}`
|
|
580
|
+
}
|
|
581
|
+
],
|
|
582
|
+
isError: true
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
try {
|
|
586
|
+
const outcome = await runTool(tool, args);
|
|
587
|
+
if (audit !== void 0) {
|
|
588
|
+
await audit.record({
|
|
589
|
+
operation: outcome.operation,
|
|
590
|
+
actor: "mcp",
|
|
591
|
+
source: tool.name,
|
|
592
|
+
fields: outcome.fields
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
return {
|
|
596
|
+
content: [{ type: "text", text: JSON.stringify(outcome.structured) }],
|
|
597
|
+
structuredContent: outcome.structured
|
|
598
|
+
};
|
|
599
|
+
} catch (error) {
|
|
600
|
+
if (!(error instanceof AnonymaError)) throw error;
|
|
601
|
+
return { content: [{ type: "text", text: error.message }], isError: true };
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
function readResource(params) {
|
|
605
|
+
const uri = params?.uri;
|
|
606
|
+
const json = (value) => ({
|
|
607
|
+
contents: [{ uri, mimeType: "application/json", text: JSON.stringify(value) }]
|
|
608
|
+
});
|
|
609
|
+
if (uri === "anonyma://categories") return json({ categories: BUILTIN_CATEGORIES });
|
|
610
|
+
if (uri === "anonyma://strategies") return json({ strategies: STRATEGY_NOTES });
|
|
611
|
+
if (uri === "anonyma://presets") {
|
|
612
|
+
return json({
|
|
613
|
+
presets: Object.values(PRESET_REGISTRY).map(
|
|
614
|
+
({ name, description, categories, defaultStrategy, rules }) => ({
|
|
615
|
+
name,
|
|
616
|
+
description,
|
|
617
|
+
categories,
|
|
618
|
+
defaultStrategy,
|
|
619
|
+
rules: rules ?? []
|
|
620
|
+
})
|
|
621
|
+
)
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
if (uri === "anonyma://audit/head")
|
|
625
|
+
return json(audit === void 0 ? { enabled: false } : { enabled: true, ...audit.head() });
|
|
626
|
+
if (typeof uri === "string" && uri.startsWith("anonyma://regulations/")) {
|
|
627
|
+
const id = uri.slice("anonyma://regulations/".length);
|
|
628
|
+
if (isRegulationId(id)) return json(REGULATIONS[id]);
|
|
629
|
+
}
|
|
630
|
+
throw new RpcError(RESOURCE_NOT_FOUND, "Resource not found.");
|
|
631
|
+
}
|
|
632
|
+
async function dispatch(method, params) {
|
|
633
|
+
switch (method) {
|
|
634
|
+
case "initialize": {
|
|
635
|
+
const requested = params?.protocolVersion;
|
|
636
|
+
return {
|
|
637
|
+
protocolVersion: typeof requested === "string" && MCP_PROTOCOL_VERSIONS.includes(requested) ? requested : MCP_PROTOCOL_VERSIONS[0],
|
|
638
|
+
capabilities: {
|
|
639
|
+
tools: { listChanged: false },
|
|
640
|
+
resources: { subscribe: false, listChanged: false }
|
|
641
|
+
},
|
|
642
|
+
serverInfo: { name: serverInfo.name, title: "anonyma", version: serverInfo.version },
|
|
643
|
+
instructions: "Use anonyma_anonymize before writing text or JSON that may contain personal data to logs, files, tickets or other tools. Use anonyma_tokenize when the data must stay meaningful for further work."
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
case "ping":
|
|
647
|
+
return {};
|
|
648
|
+
case "tools/list":
|
|
649
|
+
return { tools };
|
|
650
|
+
case "tools/call":
|
|
651
|
+
return callTool(params);
|
|
652
|
+
case "resources/list":
|
|
653
|
+
return { resources: MCP_RESOURCES };
|
|
654
|
+
case "resources/templates/list":
|
|
655
|
+
return { resourceTemplates: MCP_RESOURCE_TEMPLATES };
|
|
656
|
+
case "resources/read":
|
|
657
|
+
return readResource(params);
|
|
658
|
+
default:
|
|
659
|
+
throw new RpcError(METHOD_NOT_FOUND, "Method not found.");
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
async function handle(message) {
|
|
663
|
+
if (Array.isArray(message)) {
|
|
664
|
+
if (message.length === 0) return invalidRequest;
|
|
665
|
+
const responses = [];
|
|
666
|
+
for (const entry of message) {
|
|
667
|
+
const response = await handleOne(entry);
|
|
668
|
+
if (response !== void 0) responses.push(response);
|
|
669
|
+
}
|
|
670
|
+
return responses.length > 0 ? responses : void 0;
|
|
671
|
+
}
|
|
672
|
+
return handleOne(message);
|
|
673
|
+
}
|
|
674
|
+
async function handleOne(message) {
|
|
675
|
+
if (typeof message !== "object" || message === null || Array.isArray(message)) {
|
|
676
|
+
return invalidRequest;
|
|
677
|
+
}
|
|
678
|
+
const { jsonrpc, id, method, params } = message;
|
|
679
|
+
const isNotification = id === void 0;
|
|
680
|
+
const responseId = typeof id === "string" || typeof id === "number" && Number.isFinite(id) ? id : null;
|
|
681
|
+
if (jsonrpc !== "2.0" || typeof method !== "string" || !isNotification && responseId === null) {
|
|
682
|
+
if (method === void 0 && message["jsonrpc"] === "2.0")
|
|
683
|
+
return void 0;
|
|
684
|
+
return {
|
|
685
|
+
jsonrpc: "2.0",
|
|
686
|
+
id: responseId,
|
|
687
|
+
error: { code: INVALID_REQUEST, message: "Invalid request." }
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
if (isNotification) return void 0;
|
|
691
|
+
try {
|
|
692
|
+
return { jsonrpc: "2.0", id: responseId, result: await dispatch(method, params) };
|
|
693
|
+
} catch (error) {
|
|
694
|
+
const known = error instanceof RpcError;
|
|
695
|
+
return {
|
|
696
|
+
jsonrpc: "2.0",
|
|
697
|
+
id: responseId,
|
|
698
|
+
error: {
|
|
699
|
+
code: known ? error.code : INTERNAL_ERROR,
|
|
700
|
+
message: known ? error.message : "Internal error."
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
async function handleLine(line) {
|
|
706
|
+
let message;
|
|
707
|
+
try {
|
|
708
|
+
message = JSON.parse(line);
|
|
709
|
+
} catch {
|
|
710
|
+
return JSON.stringify({
|
|
711
|
+
jsonrpc: "2.0",
|
|
712
|
+
id: null,
|
|
713
|
+
error: { code: PARSE_ERROR, message: "Parse error." }
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
const response = await handle(message);
|
|
717
|
+
return response === void 0 ? void 0 : JSON.stringify(response);
|
|
718
|
+
}
|
|
719
|
+
return Object.freeze({ handle, handleLine });
|
|
720
|
+
}
|
|
721
|
+
async function serveStdio(server, io) {
|
|
722
|
+
const decoder = new TextDecoder();
|
|
723
|
+
let buffer = "";
|
|
724
|
+
async function consume(line) {
|
|
725
|
+
if (line.trim() === "") return;
|
|
726
|
+
const response = await server.handleLine(line);
|
|
727
|
+
if (response !== void 0) await io.write(`${response}
|
|
728
|
+
`);
|
|
729
|
+
}
|
|
730
|
+
for await (const chunk of io.input) {
|
|
731
|
+
buffer += typeof chunk === "string" ? chunk : decoder.decode(chunk, { stream: true });
|
|
732
|
+
let newline = buffer.indexOf("\n");
|
|
733
|
+
while (newline >= 0) {
|
|
734
|
+
await consume(buffer.slice(0, newline));
|
|
735
|
+
buffer = buffer.slice(newline + 1);
|
|
736
|
+
newline = buffer.indexOf("\n");
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
await consume(buffer + decoder.decode());
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
export { MCP_PROTOCOL_VERSIONS, MCP_RESOURCES, MCP_RESOURCE_TEMPLATES, MCP_TOOLS, createMcpServer, serveStdio };
|
|
743
|
+
//# sourceMappingURL=index.js.map
|
|
744
|
+
//# sourceMappingURL=index.js.map
|