@arkheia/mcp-server 1.2.0 → 1.2.2
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/CLAUDE_MD_TEMPLATE.md +33 -0
- package/README.md +177 -89
- package/dist/index.js +263 -0
- package/dist/memory.js +187 -0
- package/dist/providers.js +320 -0
- package/dist/proxy-client.js +219 -0
- package/dist/tool-registry.js +90 -0
- package/package.json +44 -44
- package/scripts/build.js +33 -33
- package/scripts/setup-claude.js +169 -0
- package/scripts/setup.js +33 -192
- package/bin/arkheia-mcp.js +0 -207
- package/python/mcp_server/__init__.py +0 -0
- package/python/requirements.txt +0 -4
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProxyClient = void 0;
|
|
4
|
+
const url_1 = require("url");
|
|
5
|
+
// Node.js native fetch doesn't have a global logger, use console
|
|
6
|
+
const logger = console;
|
|
7
|
+
// Hosted API defaults
|
|
8
|
+
const HOSTED_API_URL = "https://arkheia-proxy-production.up.railway.app";
|
|
9
|
+
class ProxyClient {
|
|
10
|
+
base_url;
|
|
11
|
+
timeout; // in milliseconds
|
|
12
|
+
hosted_url;
|
|
13
|
+
api_key;
|
|
14
|
+
_local_available; // optimistic; flips on network errors
|
|
15
|
+
constructor(base_url, timeout = 10.0, // in seconds
|
|
16
|
+
hosted_url, api_key) {
|
|
17
|
+
this.base_url = base_url.endsWith("/") ? base_url.slice(0, -1) : base_url;
|
|
18
|
+
this.timeout = timeout * 1000; // Convert to milliseconds for fetch AbortController
|
|
19
|
+
this.hosted_url = (hosted_url || HOSTED_API_URL).endsWith("/")
|
|
20
|
+
? (hosted_url || HOSTED_API_URL).slice(0, -1)
|
|
21
|
+
: (hosted_url || HOSTED_API_URL);
|
|
22
|
+
this.api_key = api_key || process.env.ARKHEIA_API_KEY;
|
|
23
|
+
this._local_available = true; // optimistic; flips on ConnectError
|
|
24
|
+
}
|
|
25
|
+
async verify(prompt, response, model_id, session_id) {
|
|
26
|
+
/**
|
|
27
|
+
* Detect fabrication in a model response.
|
|
28
|
+
*
|
|
29
|
+
* Tries local proxy first. If unavailable, falls back to hosted API.
|
|
30
|
+
* Never raises -- returns UNKNOWN on any error.
|
|
31
|
+
*/
|
|
32
|
+
// Try local proxy first (if last attempt didn't fail with ConnectError)
|
|
33
|
+
if (this._local_available) {
|
|
34
|
+
const result = await this._verify_local(prompt, response, model_id, session_id);
|
|
35
|
+
if (result.error !== "proxy_unavailable" && result.error !== "proxy_timeout") {
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
// Local proxy down -- fall through to hosted
|
|
39
|
+
this._local_available = false;
|
|
40
|
+
logger.info(`Local proxy unavailable, falling back to hosted API at ${this.hosted_url}`);
|
|
41
|
+
}
|
|
42
|
+
// Fallback: hosted API
|
|
43
|
+
if (this.api_key) {
|
|
44
|
+
const result = await this._verify_hosted(prompt, response, model_id);
|
|
45
|
+
if (result.error !== "hosted_unavailable") {
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
// Hosted also failed -- try local once more in case it came back
|
|
49
|
+
this._local_available = true; // Reset local availability for next call
|
|
50
|
+
}
|
|
51
|
+
// No hosted API key and local is down
|
|
52
|
+
if (!this.api_key) {
|
|
53
|
+
logger.warn("Local proxy unavailable and no ARKHEIA_API_KEY set for hosted fallback");
|
|
54
|
+
return _unavailable("no_detection_available");
|
|
55
|
+
}
|
|
56
|
+
return _unavailable("all_detection_paths_failed");
|
|
57
|
+
}
|
|
58
|
+
async _verify_local(prompt, response, model_id, session_id) {
|
|
59
|
+
/** POST /detect/verify on local Enterprise Proxy. */
|
|
60
|
+
const payload = {
|
|
61
|
+
prompt: prompt,
|
|
62
|
+
response: response,
|
|
63
|
+
model_id: model_id,
|
|
64
|
+
};
|
|
65
|
+
if (session_id) {
|
|
66
|
+
payload["session_id"] = session_id;
|
|
67
|
+
}
|
|
68
|
+
const controller = new AbortController();
|
|
69
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
70
|
+
try {
|
|
71
|
+
const resp = await fetch(`${this.base_url}/detect/verify`, {
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers: {
|
|
74
|
+
"Content-Type": "application/json",
|
|
75
|
+
},
|
|
76
|
+
body: JSON.stringify(payload),
|
|
77
|
+
signal: controller.signal,
|
|
78
|
+
});
|
|
79
|
+
clearTimeout(timeoutId);
|
|
80
|
+
if (!resp.ok) {
|
|
81
|
+
logger.error(`ProxyClient: /detect/verify HTTP error: ${resp.status} ${resp.statusText}`);
|
|
82
|
+
return _unavailable(`proxy_http_error_${resp.status}`);
|
|
83
|
+
}
|
|
84
|
+
return await resp.json();
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
clearTimeout(timeoutId);
|
|
88
|
+
if (e.name === 'AbortError') {
|
|
89
|
+
logger.warn(`ProxyClient: /detect/verify timed out for model=${model_id}`);
|
|
90
|
+
return _unavailable("proxy_timeout");
|
|
91
|
+
}
|
|
92
|
+
else if (e.name === 'TypeError' && e.message.includes('fetch failed')) { // Common for network errors like connection refused
|
|
93
|
+
logger.warn(`ProxyClient: cannot connect to proxy at ${this.base_url}`);
|
|
94
|
+
return _unavailable("proxy_unavailable");
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
logger.error(`ProxyClient: /detect/verify unexpected error: ${e.message}`);
|
|
98
|
+
return _unavailable("proxy_error");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async _verify_hosted(prompt, response, model_id) {
|
|
103
|
+
/** POST /v1/detect on hosted Arkheia API (arkheia-proxy-production.up.railway.app). */
|
|
104
|
+
const payload = {
|
|
105
|
+
model: model_id,
|
|
106
|
+
response: response,
|
|
107
|
+
prompt: prompt,
|
|
108
|
+
};
|
|
109
|
+
const headers = { "X-Arkheia-Key": this.api_key || "" };
|
|
110
|
+
const controller = new AbortController();
|
|
111
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
112
|
+
try {
|
|
113
|
+
const resp = await fetch(`${this.hosted_url}/v1/detect`, {
|
|
114
|
+
method: "POST",
|
|
115
|
+
headers: headers,
|
|
116
|
+
body: JSON.stringify(payload),
|
|
117
|
+
signal: controller.signal,
|
|
118
|
+
});
|
|
119
|
+
clearTimeout(timeoutId);
|
|
120
|
+
if (!resp.ok) {
|
|
121
|
+
const status = resp.status;
|
|
122
|
+
if (status === 401) {
|
|
123
|
+
logger.error("ProxyClient: hosted API rejected API key (401)");
|
|
124
|
+
return _unavailable("hosted_auth_failed");
|
|
125
|
+
}
|
|
126
|
+
if (status === 429) {
|
|
127
|
+
logger.warn("ProxyClient: hosted API rate/quota limit (429)");
|
|
128
|
+
return _unavailable("hosted_quota_exceeded");
|
|
129
|
+
}
|
|
130
|
+
logger.error(`ProxyClient: hosted /v1/detect HTTP error: ${status} ${resp.statusText}`);
|
|
131
|
+
return _unavailable(`hosted_http_error_${status}`);
|
|
132
|
+
}
|
|
133
|
+
const data = await resp.json();
|
|
134
|
+
// Map hosted response format to local format
|
|
135
|
+
return {
|
|
136
|
+
risk_level: data.risk || "UNKNOWN",
|
|
137
|
+
confidence: data.confidence || 0.0,
|
|
138
|
+
features_triggered: data.features_triggered || [],
|
|
139
|
+
detection_id: data.detection_id,
|
|
140
|
+
detection_method: data.detection_method,
|
|
141
|
+
evidence_depth_limited: data.evidence_depth_limited ?? true,
|
|
142
|
+
source: "hosted",
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
catch (e) {
|
|
146
|
+
clearTimeout(timeoutId);
|
|
147
|
+
if (e.name === 'AbortError') {
|
|
148
|
+
logger.warn(`ProxyClient: hosted /v1/detect timed out for model=${model_id}`);
|
|
149
|
+
return _unavailable("hosted_timeout");
|
|
150
|
+
}
|
|
151
|
+
else if (e.name === 'TypeError' && e.message.includes('fetch failed')) {
|
|
152
|
+
logger.warn(`ProxyClient: cannot connect to hosted API at ${this.hosted_url}`);
|
|
153
|
+
return _unavailable("hosted_unavailable");
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
logger.error(`ProxyClient: hosted /v1/detect unexpected error: ${e.message}`);
|
|
157
|
+
return _unavailable("hosted_error");
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
async get_audit_log(session_id, limit = 50) {
|
|
162
|
+
/**
|
|
163
|
+
* GET /audit/log
|
|
164
|
+
*
|
|
165
|
+
* Returns audit log dict. Never raises -- returns empty log on any error.
|
|
166
|
+
* Note: audit log is only available from local proxy, not hosted API.
|
|
167
|
+
*/
|
|
168
|
+
const params = new url_1.URLSearchParams({ limit: String(Math.min(limit, 500)) });
|
|
169
|
+
if (session_id) {
|
|
170
|
+
params.append("session_id", session_id);
|
|
171
|
+
}
|
|
172
|
+
const controller = new AbortController();
|
|
173
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
174
|
+
try {
|
|
175
|
+
const resp = await fetch(`${this.base_url}/audit/log?${params.toString()}`, {
|
|
176
|
+
method: "GET",
|
|
177
|
+
signal: controller.signal,
|
|
178
|
+
});
|
|
179
|
+
clearTimeout(timeoutId);
|
|
180
|
+
if (!resp.ok) {
|
|
181
|
+
logger.error(`ProxyClient: /audit/log HTTP error: ${resp.status} ${resp.statusText}`);
|
|
182
|
+
return _empty_log(`proxy_http_error_${resp.status}`);
|
|
183
|
+
}
|
|
184
|
+
return await resp.json();
|
|
185
|
+
}
|
|
186
|
+
catch (e) {
|
|
187
|
+
clearTimeout(timeoutId);
|
|
188
|
+
if (e.name === 'AbortError') {
|
|
189
|
+
logger.warn("ProxyClient: /audit/log timed out");
|
|
190
|
+
return _empty_log("proxy_timeout");
|
|
191
|
+
}
|
|
192
|
+
else if (e.name === 'TypeError' && e.message.includes('fetch failed')) {
|
|
193
|
+
logger.warn(`ProxyClient: cannot connect to proxy at ${this.base_url}`);
|
|
194
|
+
return _empty_log("proxy_unavailable");
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
logger.error(`ProxyClient: /audit/log unexpected error: ${e.message}`);
|
|
198
|
+
return _empty_log("proxy_error");
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
exports.ProxyClient = ProxyClient;
|
|
204
|
+
function _unavailable(error) {
|
|
205
|
+
/** Standard UNKNOWN response when detection is unreachable. */
|
|
206
|
+
return {
|
|
207
|
+
risk_level: "UNKNOWN",
|
|
208
|
+
confidence: 0.0,
|
|
209
|
+
features_triggered: [],
|
|
210
|
+
error: error,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function _empty_log(error) {
|
|
214
|
+
return {
|
|
215
|
+
events: [],
|
|
216
|
+
summary: { LOW: 0, MEDIUM: 0, HIGH: 0, UNKNOWN: 0 },
|
|
217
|
+
error: error,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PolicyViolation = exports.REGISTRY = exports.Permission = void 0;
|
|
4
|
+
exports.check = check;
|
|
5
|
+
var Permission;
|
|
6
|
+
(function (Permission) {
|
|
7
|
+
Permission["READ"] = "read";
|
|
8
|
+
Permission["EXECUTE"] = "execute";
|
|
9
|
+
Permission["WRITE"] = "write";
|
|
10
|
+
Permission["DEPLOY"] = "deploy";
|
|
11
|
+
})(Permission || (exports.Permission = Permission = {}));
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// The allowlist
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
exports.REGISTRY = {
|
|
16
|
+
arkheia_verify: {
|
|
17
|
+
name: "arkheia_verify",
|
|
18
|
+
permissions: [Permission.READ],
|
|
19
|
+
network_egress: true,
|
|
20
|
+
description: "Screen an AI response for fabrication risk",
|
|
21
|
+
},
|
|
22
|
+
arkheia_audit_log: {
|
|
23
|
+
name: "arkheia_audit_log",
|
|
24
|
+
permissions: [Permission.READ],
|
|
25
|
+
network_egress: false,
|
|
26
|
+
description: "Retrieve structured audit evidence",
|
|
27
|
+
},
|
|
28
|
+
run_grok: {
|
|
29
|
+
name: "run_grok",
|
|
30
|
+
permissions: [Permission.READ, Permission.EXECUTE],
|
|
31
|
+
network_egress: true,
|
|
32
|
+
description: "Call xAI Grok API and screen response through Arkheia",
|
|
33
|
+
},
|
|
34
|
+
run_gemini: {
|
|
35
|
+
name: "run_gemini",
|
|
36
|
+
permissions: [Permission.READ, Permission.EXECUTE],
|
|
37
|
+
network_egress: true,
|
|
38
|
+
description: "Call Google Gemini API and screen response through Arkheia",
|
|
39
|
+
},
|
|
40
|
+
run_together: {
|
|
41
|
+
name: "run_together",
|
|
42
|
+
permissions: [Permission.READ, Permission.EXECUTE],
|
|
43
|
+
network_egress: true,
|
|
44
|
+
description: "Call Together AI API and screen response through Arkheia",
|
|
45
|
+
},
|
|
46
|
+
run_ollama: {
|
|
47
|
+
name: "run_ollama",
|
|
48
|
+
permissions: [Permission.READ, Permission.EXECUTE],
|
|
49
|
+
network_egress: false,
|
|
50
|
+
description: "Call local Ollama model and screen response through Arkheia",
|
|
51
|
+
},
|
|
52
|
+
memory_store: {
|
|
53
|
+
name: "memory_store",
|
|
54
|
+
permissions: [Permission.READ, Permission.WRITE],
|
|
55
|
+
network_egress: false,
|
|
56
|
+
description: "Store an entity and observations in the persistent knowledge graph",
|
|
57
|
+
},
|
|
58
|
+
memory_retrieve: {
|
|
59
|
+
name: "memory_retrieve",
|
|
60
|
+
permissions: [Permission.READ],
|
|
61
|
+
network_egress: false,
|
|
62
|
+
description: "Retrieve entities and their observations from the knowledge graph",
|
|
63
|
+
},
|
|
64
|
+
memory_relate: {
|
|
65
|
+
name: "memory_relate",
|
|
66
|
+
permissions: [Permission.READ, Permission.WRITE],
|
|
67
|
+
network_egress: false,
|
|
68
|
+
description: "Store a named relationship between two entities in the knowledge graph",
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
// ---------------------------------------------------------------------------
|
|
72
|
+
// Policy gate
|
|
73
|
+
// ---------------------------------------------------------------------------
|
|
74
|
+
class PolicyViolation extends Error {
|
|
75
|
+
tool_name;
|
|
76
|
+
reason;
|
|
77
|
+
constructor(tool_name, reason) {
|
|
78
|
+
super(`Policy violation for '${tool_name}': ${reason}`);
|
|
79
|
+
this.tool_name = tool_name;
|
|
80
|
+
this.reason = reason;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.PolicyViolation = PolicyViolation;
|
|
84
|
+
function check(tool_name) {
|
|
85
|
+
const policy = exports.REGISTRY[tool_name];
|
|
86
|
+
if (!policy) {
|
|
87
|
+
throw new PolicyViolation(tool_name, `not in allowlist — default deny. Known tools: ${Object.keys(exports.REGISTRY).sort().join(", ")}`);
|
|
88
|
+
}
|
|
89
|
+
return policy;
|
|
90
|
+
}
|
package/package.json
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@arkheia/mcp-server",
|
|
3
|
-
"version": "1.2.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"bin": {
|
|
7
|
-
"mcp-server": "
|
|
8
|
-
},
|
|
9
|
-
"scripts": {
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@arkheia/mcp-server",
|
|
3
|
+
"version": "1.2.2",
|
|
4
|
+
"description": "Arkheia MCP Server — Fabrication detection for AI agents. Know when your AI is making things up.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"mcp-server": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc",
|
|
11
|
+
"start": "node dist/index.js",
|
|
12
|
+
"prepublishOnly": "npm run build",
|
|
13
|
+
"postinstall": "node scripts/setup.js"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/",
|
|
17
|
+
"scripts/",
|
|
18
|
+
"README.md",
|
|
19
|
+
"CLAUDE_MD_TEMPLATE.md"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"mcp",
|
|
23
|
+
"ai",
|
|
24
|
+
"fabrication",
|
|
25
|
+
"detection",
|
|
26
|
+
"hallucination",
|
|
27
|
+
"governance"
|
|
28
|
+
],
|
|
29
|
+
"author": "Arkheia AI <dmurfet@arkheia.ai>",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=18.0.0"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@modelcontextprotocol/sdk": "^1.12.0",
|
|
36
|
+
"sql.js": "^1.11.0",
|
|
37
|
+
"zod": "^3.23.8"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/sql.js": "^1.4.9",
|
|
41
|
+
"@types/node": "^20.14.10",
|
|
42
|
+
"typescript": "^5.5.3"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/scripts/build.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Build script — copies Python MCP server source into npm package.
|
|
4
|
-
* Run before `npm publish`.
|
|
5
|
-
*
|
|
6
|
-
* Usage: node scripts/build.js
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const fs = require("fs");
|
|
10
|
-
const path = require("path");
|
|
11
|
-
|
|
12
|
-
const SRC = path.resolve(__dirname, "..", "..", "mcp_server");
|
|
13
|
-
const DEST = path.resolve(__dirname, "..", "python", "mcp_server");
|
|
14
|
-
|
|
15
|
-
function copyDir(src, dest) {
|
|
16
|
-
if (!fs.existsSync(dest)) {
|
|
17
|
-
fs.mkdirSync(dest, { recursive: true });
|
|
18
|
-
}
|
|
19
|
-
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
20
|
-
const srcPath = path.join(src, entry.name);
|
|
21
|
-
const destPath = path.join(dest, entry.name);
|
|
22
|
-
if (entry.name === "__pycache__" || entry.name === "tests") continue;
|
|
23
|
-
if (entry.isDirectory()) {
|
|
24
|
-
copyDir(srcPath, destPath);
|
|
25
|
-
} else if (entry.name.endsWith(".py")) {
|
|
26
|
-
fs.copyFileSync(srcPath, destPath);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
console.log(`Copying ${SRC} -> ${DEST}`);
|
|
32
|
-
copyDir(SRC, DEST);
|
|
33
|
-
console.log("Build complete. Run `npm publish` from npm-wrapper/.");
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Build script — copies Python MCP server source into npm package.
|
|
4
|
+
* Run before `npm publish`.
|
|
5
|
+
*
|
|
6
|
+
* Usage: node scripts/build.js
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const fs = require("fs");
|
|
10
|
+
const path = require("path");
|
|
11
|
+
|
|
12
|
+
const SRC = path.resolve(__dirname, "..", "..", "mcp_server");
|
|
13
|
+
const DEST = path.resolve(__dirname, "..", "python", "mcp_server");
|
|
14
|
+
|
|
15
|
+
function copyDir(src, dest) {
|
|
16
|
+
if (!fs.existsSync(dest)) {
|
|
17
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
18
|
+
}
|
|
19
|
+
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
20
|
+
const srcPath = path.join(src, entry.name);
|
|
21
|
+
const destPath = path.join(dest, entry.name);
|
|
22
|
+
if (entry.name === "__pycache__" || entry.name === "tests") continue;
|
|
23
|
+
if (entry.isDirectory()) {
|
|
24
|
+
copyDir(srcPath, destPath);
|
|
25
|
+
} else if (entry.name.endsWith(".py")) {
|
|
26
|
+
fs.copyFileSync(srcPath, destPath);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
console.log(`Copying ${SRC} -> ${DEST}`);
|
|
32
|
+
copyDir(SRC, DEST);
|
|
33
|
+
console.log("Build complete. Run `npm publish` from npm-wrapper/.");
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Installs the Arkheia detection protocol into AI CLI instruction files
|
|
4
|
+
* and registers the MCP server with each detected CLI.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* npx @arkheia/mcp-server --setup
|
|
8
|
+
* node scripts/setup-claude.js
|
|
9
|
+
*
|
|
10
|
+
* What it does:
|
|
11
|
+
* 1. Detects which AI CLIs are installed (claude, codex, gemini, grok)
|
|
12
|
+
* 2. Appends detection protocol to instruction files that need it:
|
|
13
|
+
* - Claude Code: ~/.claude/CLAUDE.md
|
|
14
|
+
* - Codex: ~/.codex/CODEX.md
|
|
15
|
+
* - Gemini: not needed (reads protocol from tool descriptions)
|
|
16
|
+
* - Grok: not needed (no instruction file mechanism)
|
|
17
|
+
* 3. Registers the MCP server with each detected CLI
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const fs = require("fs");
|
|
21
|
+
const path = require("path");
|
|
22
|
+
const os = require("os");
|
|
23
|
+
const { execSync, execFileSync } = require("child_process");
|
|
24
|
+
|
|
25
|
+
const MARKER = "# Arkheia Integration — Detection Protocol";
|
|
26
|
+
|
|
27
|
+
const PROTOCOL = `
|
|
28
|
+
# Arkheia Integration — Detection Protocol
|
|
29
|
+
|
|
30
|
+
When Arkheia MCP tools are available in this session, follow this protocol.
|
|
31
|
+
|
|
32
|
+
## Fabrication Detection
|
|
33
|
+
|
|
34
|
+
Call \`arkheia_verify\` on EVERY model response before surfacing it to the user. Do not skip responses that appear obviously correct.
|
|
35
|
+
|
|
36
|
+
## Risk Escalation
|
|
37
|
+
|
|
38
|
+
- **HIGH risk**: Warn the user that the response may be fabricated. Include the \`detection_id\`. Still show the response — never hide or suppress it. Ask the user if they want to verify with a different model.
|
|
39
|
+
- **MEDIUM risk**: Show the response with a brief confidence note.
|
|
40
|
+
- **LOW risk**: Show normally.
|
|
41
|
+
- **UNKNOWN risk**: Flag for human review. Include the \`detection_id\`.
|
|
42
|
+
|
|
43
|
+
## Cross-Model Verification
|
|
44
|
+
|
|
45
|
+
When HIGH or UNKNOWN risk is detected, offer to verify using: \`run_grok\`, \`run_gemini\`, \`run_ollama\`, \`run_together\`.
|
|
46
|
+
|
|
47
|
+
## Audit Trail
|
|
48
|
+
|
|
49
|
+
Use \`arkheia_audit_log\` when the user asks about detection history or for compliance evidence.
|
|
50
|
+
|
|
51
|
+
## Persistent Knowledge Graph
|
|
52
|
+
|
|
53
|
+
Use \`memory_store\` to persist facts, \`memory_retrieve\` to recall them, \`memory_relate\` to track relationships between entities.
|
|
54
|
+
|
|
55
|
+
## Key Rules
|
|
56
|
+
|
|
57
|
+
1. Never hide a response from the user regardless of risk level
|
|
58
|
+
2. Always include the \`detection_id\` when reporting HIGH or UNKNOWN risk
|
|
59
|
+
3. Call \`arkheia_verify\` proactively — do not wait for the user to ask
|
|
60
|
+
4. Audit logging happens automatically through \`arkheia_verify\`
|
|
61
|
+
`;
|
|
62
|
+
|
|
63
|
+
function cmdExists(cmd) {
|
|
64
|
+
try {
|
|
65
|
+
execSync(process.platform === "win32" ? `where ${cmd}` : `which ${cmd}`, { stdio: "pipe" });
|
|
66
|
+
return true;
|
|
67
|
+
} catch { return false; }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function installProtocol(filePath, cliName) {
|
|
71
|
+
const dir = path.dirname(filePath);
|
|
72
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
73
|
+
|
|
74
|
+
if (fs.existsSync(filePath)) {
|
|
75
|
+
const existing = fs.readFileSync(filePath, "utf8");
|
|
76
|
+
if (existing.includes(MARKER)) {
|
|
77
|
+
console.log(` [${cliName}] Detection protocol already installed in ${filePath}`);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
fs.appendFileSync(filePath, "\n" + PROTOCOL);
|
|
81
|
+
console.log(` [${cliName}] Detection protocol appended to ${filePath}`);
|
|
82
|
+
} else {
|
|
83
|
+
fs.writeFileSync(filePath, PROTOCOL.trim() + "\n");
|
|
84
|
+
console.log(` [${cliName}] Detection protocol written to ${filePath}`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// argv carries the real key for the CLI's own config; `display` is the same command with the key shown only as
|
|
89
|
+
// "$ARKHEIA_API_KEY", which is all that is ever printed (Codex B288 R1: a failure used to echo the full key).
|
|
90
|
+
function registerMcp(cli, argv, display) {
|
|
91
|
+
try {
|
|
92
|
+
execFileSync(argv[0], argv.slice(1), { stdio: "inherit", timeout: 15000 });
|
|
93
|
+
console.log(` [${cli}] MCP server registered`);
|
|
94
|
+
} catch {
|
|
95
|
+
console.log(` [${cli}] Auto-registration failed — run manually:`);
|
|
96
|
+
console.log(` ${display}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function main() {
|
|
101
|
+
const apiKey = process.env.ARKHEIA_API_KEY || "";
|
|
102
|
+
const home = os.homedir();
|
|
103
|
+
const detected = [];
|
|
104
|
+
|
|
105
|
+
console.log("\n[arkheia] Setting up detection protocol for installed AI CLIs...\n");
|
|
106
|
+
|
|
107
|
+
// ── Claude Code ──────────────────────────────────────────────
|
|
108
|
+
if (cmdExists("claude")) {
|
|
109
|
+
detected.push("claude");
|
|
110
|
+
console.log("[claude] Detected");
|
|
111
|
+
installProtocol(path.join(home, ".claude", "CLAUDE.md"), "claude");
|
|
112
|
+
if (apiKey) {
|
|
113
|
+
registerMcp("claude", ["claude", "mcp", "add", "arkheia", "-s", "user", "-e", `ARKHEIA_API_KEY=${apiKey}`, "--", "mcp-server"],
|
|
114
|
+
'claude mcp add arkheia -s user -e ARKHEIA_API_KEY="$ARKHEIA_API_KEY" -- mcp-server');
|
|
115
|
+
} else {
|
|
116
|
+
console.log(' [claude] Set ARKHEIA_API_KEY then run: claude mcp add arkheia -s user -e ARKHEIA_API_KEY="$ARKHEIA_API_KEY" -- mcp-server');
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// ── Codex ────────────────────────────────────────────────────
|
|
121
|
+
if (cmdExists("codex")) {
|
|
122
|
+
detected.push("codex");
|
|
123
|
+
console.log("[codex] Detected");
|
|
124
|
+
installProtocol(path.join(home, ".codex", "CODEX.md"), "codex");
|
|
125
|
+
if (apiKey) {
|
|
126
|
+
registerMcp("codex", ["codex", "mcp", "add", "arkheia", "--env", `ARKHEIA_API_KEY=${apiKey}`, "--", "mcp-server"],
|
|
127
|
+
'codex mcp add arkheia --env ARKHEIA_API_KEY="$ARKHEIA_API_KEY" -- mcp-server');
|
|
128
|
+
} else {
|
|
129
|
+
console.log(' [codex] Set ARKHEIA_API_KEY then run: codex mcp add arkheia --env ARKHEIA_API_KEY="$ARKHEIA_API_KEY" -- mcp-server');
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ── Gemini ───────────────────────────────────────────────────
|
|
134
|
+
// Gemini reads the detection protocol directly from tool descriptions.
|
|
135
|
+
// No instruction file needed — just register the MCP server.
|
|
136
|
+
if (cmdExists("gemini")) {
|
|
137
|
+
detected.push("gemini");
|
|
138
|
+
console.log("[gemini] Detected (no instruction file needed — reads protocol from tool descriptions)");
|
|
139
|
+
if (apiKey) {
|
|
140
|
+
registerMcp("gemini", ["gemini", "mcp", "add", "-s", "user", "-e", `ARKHEIA_API_KEY=${apiKey}`, "arkheia", "mcp-server"],
|
|
141
|
+
'gemini mcp add -s user -e ARKHEIA_API_KEY="$ARKHEIA_API_KEY" arkheia mcp-server');
|
|
142
|
+
} else {
|
|
143
|
+
console.log(' [gemini] Set ARKHEIA_API_KEY then run: gemini mcp add -s user -e ARKHEIA_API_KEY="$ARKHEIA_API_KEY" arkheia mcp-server');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ── Grok ─────────────────────────────────────────────────────
|
|
148
|
+
// No instruction file mechanism. Register MCP server only.
|
|
149
|
+
if (cmdExists("grok")) {
|
|
150
|
+
detected.push("grok");
|
|
151
|
+
console.log("[grok] Detected (no instruction file mechanism)");
|
|
152
|
+
if (apiKey) {
|
|
153
|
+
registerMcp("grok", ["grok", "mcp", "add", "arkheia", "-t", "stdio", "-c", "mcp-server", "-e", `ARKHEIA_API_KEY=${apiKey}`],
|
|
154
|
+
'grok mcp add arkheia -t stdio -c mcp-server -e ARKHEIA_API_KEY="$ARKHEIA_API_KEY"');
|
|
155
|
+
} else {
|
|
156
|
+
console.log(' [grok] Set ARKHEIA_API_KEY then run: grok mcp add arkheia -t stdio -c mcp-server -e ARKHEIA_API_KEY="$ARKHEIA_API_KEY"');
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (detected.length === 0) {
|
|
161
|
+
console.log("No AI CLIs detected on PATH (claude, codex, gemini, grok).");
|
|
162
|
+
console.log("Install one and re-run: npx @arkheia/mcp-server --setup");
|
|
163
|
+
} else {
|
|
164
|
+
console.log(`\n[arkheia] Setup complete for: ${detected.join(", ")}`);
|
|
165
|
+
console.log("[arkheia] Restart each CLI to activate Arkheia detection.\n");
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
main();
|