@dataklin/mcp 0.0.0-stage → 1.3.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/LICENSE +13 -0
- package/README.md +34 -2
- package/bin/bridge.mjs +1663 -0
- package/bin/channel-name.mjs +244 -0
- package/bin/dataklin-mcp.mjs +10 -0
- package/package.json +35 -4
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// GENERATED from apps/desktop/src/shared/mcp/channel-name.ts by
|
|
2
|
+
// packages/mcp-bridge/scripts/generate-channel-name.mjs. Don't edit it:
|
|
3
|
+
// change the source and run `pnpm --filter @dataklin/mcp run generate`.
|
|
4
|
+
|
|
5
|
+
// Where the MCP server's per-user local channel is (feature 0135 T06; ADR
|
|
6
|
+
// 0078 §6; PRD R11). mcp-host listens there while the server is on; the
|
|
7
|
+
// `dataklin-mcp` bridge (T04) computes the same place from its own user, so
|
|
8
|
+
// there is no port and no discovery file.
|
|
9
|
+
//
|
|
10
|
+
// - Windows: the named pipe `\\.\pipe\dataklin-mcp-<first 16 hex of the
|
|
11
|
+
// SHA-256 of the user's SID>`.
|
|
12
|
+
// - macOS: the Unix socket `mcp.sock` in
|
|
13
|
+
// `~/Library/Application Support/DataKlin/mcp/`.
|
|
14
|
+
// - Linux: `mcp.sock` in `$XDG_RUNTIME_DIR/dataklin/` when that variable is an
|
|
15
|
+
// absolute path, else in `~/.local/state/dataklin/mcp/`.
|
|
16
|
+
//
|
|
17
|
+
// On macOS and Linux the folder must be the user's own with mode 0700 and
|
|
18
|
+
// the socket 0600 (mcp-host/channel/unix-socket.ts checks both).
|
|
19
|
+
//
|
|
20
|
+
// `DATAKLIN_MCP_CHANNEL` (an absolute socket path, or a pipe path on
|
|
21
|
+
// Windows) replaces the place: tests use it so they never touch the user's
|
|
22
|
+
// real channel. Like `DATAKLIN_MCP_SERVER_KEY` it is a test override only: a
|
|
23
|
+
// packaged app ignores both, and the bridge takes them only with
|
|
24
|
+
// `DATAKLIN_MCP_TEST_OVERRIDES=1` (0136 F01; `withoutTestOverrides`).
|
|
25
|
+
//
|
|
26
|
+
// The bridge is plain JavaScript with no build step and imports a copy of
|
|
27
|
+
// this file generated from it (T04; a unit test keeps the two equal), so it
|
|
28
|
+
// imports nothing, not even `node:` modules: the SHA-256 is passed in, and
|
|
29
|
+
// the TypeScript here is erasable (types only).
|
|
30
|
+
|
|
31
|
+
/** The environment variable that replaces the channel's place (tests). */
|
|
32
|
+
export const MCP_CHANNEL_ENV = 'DATAKLIN_MCP_CHANNEL';
|
|
33
|
+
|
|
34
|
+
/** The switch that lets the bridge take the test overrides; off in use. */
|
|
35
|
+
export const MCP_TEST_OVERRIDES_ENV = 'DATAKLIN_MCP_TEST_OVERRIDES';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The argument main forks mcp-host with, followed by the channel's path:
|
|
39
|
+
* main names the place from its own environment, which mcp-host doesn't get
|
|
40
|
+
* (XDG_RUNTIME_DIR, the test override). Empty when it could not be named;
|
|
41
|
+
* left out on Windows without the override (mcp-host reads the SID itself).
|
|
42
|
+
*/
|
|
43
|
+
export const MCP_CHANNEL_ARG = '--mcp-channel=';
|
|
44
|
+
|
|
45
|
+
/** The socket's file name in its folder. */
|
|
46
|
+
export const MCP_SOCKET_FILE = 'mcp.sock';
|
|
47
|
+
|
|
48
|
+
/** The pipe name's prefix, before the SID digest. */
|
|
49
|
+
export const MCP_PIPE_PREFIX = '\\\\.\\pipe\\dataklin-mcp-';
|
|
50
|
+
|
|
51
|
+
/** How many hex characters of the SID's SHA-256 the pipe name keeps. */
|
|
52
|
+
export const MCP_PIPE_DIGEST_CHARS = 16;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The longest socket path that fits `sockaddr_un.sun_path` on every Unix
|
|
56
|
+
* the app runs on (macOS 104 bytes, Linux 108, both with the final NUL).
|
|
57
|
+
*/
|
|
58
|
+
export const MCP_SOCKET_PATH_MAX_BYTES = 103;
|
|
59
|
+
|
|
60
|
+
/** A SHA-256 of UTF-8 text, as lowercase hex. */
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The fork arguments that tell mcp-host where the channel of `user` is
|
|
64
|
+
* (see {@link MCP_CHANNEL_ARG}).
|
|
65
|
+
*/
|
|
66
|
+
export function mcpChannelArgs(user, sha256Hex) {
|
|
67
|
+
const override = user.env[MCP_CHANNEL_ENV];
|
|
68
|
+
if (user.platform === 'win32' && user.sid === undefined && (override ?? '') === '') return [];
|
|
69
|
+
try {
|
|
70
|
+
return [`${MCP_CHANNEL_ARG}${mcpChannelLocation(user, sha256Hex).path}`];
|
|
71
|
+
} catch {
|
|
72
|
+
return [MCP_CHANNEL_ARG];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const SID_PATTERN = /^S-1-\d+(?:-\d+)+$/i;
|
|
77
|
+
|
|
78
|
+
/** The pipe path for a Windows user's SID. */
|
|
79
|
+
export function mcpPipePath(sid, sha256Hex) {
|
|
80
|
+
const normalized = sid.trim().toUpperCase();
|
|
81
|
+
if (!SID_PATTERN.test(normalized)) throw new Error('Not a Windows SID.');
|
|
82
|
+
return `${MCP_PIPE_PREFIX}${sha256Hex(normalized).slice(0, MCP_PIPE_DIGEST_CHARS).toLowerCase()}`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The SID in `whoami /user /fo csv /nh` output (`"host\user","S-1-5-21-…"`),
|
|
87
|
+
* or `undefined`.
|
|
88
|
+
*/
|
|
89
|
+
export function parseWhoamiSid(output) {
|
|
90
|
+
for (const line of output.split(/\r?\n/)) {
|
|
91
|
+
const fields = line.split(',').map((field) => field.trim().replace(/^"|"$/g, ''));
|
|
92
|
+
const sid = fields.at(-1);
|
|
93
|
+
if (sid !== undefined && SID_PATTERN.test(sid)) return sid.toUpperCase();
|
|
94
|
+
}
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const trimSlashes = (path) => path.replace(/\/+$/, '');
|
|
99
|
+
|
|
100
|
+
/** The folder of an absolute socket path. */
|
|
101
|
+
const dirOf = (path) => {
|
|
102
|
+
const cut = path.lastIndexOf('/');
|
|
103
|
+
return cut <= 0 ? '/' : path.slice(0, cut);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** UTF-8 length, without `Buffer` or `TextEncoder` assumptions. */
|
|
107
|
+
const utf8Bytes = (text) => {
|
|
108
|
+
let bytes = 0;
|
|
109
|
+
for (const char of text) {
|
|
110
|
+
const code = char.codePointAt(0) ?? 0;
|
|
111
|
+
bytes += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4;
|
|
112
|
+
}
|
|
113
|
+
return bytes;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Where the channel of `user` is. Throws with a message for the user when it
|
|
118
|
+
* can't be named: no SID on Windows, no home folder, or a socket path too
|
|
119
|
+
* long for the OS.
|
|
120
|
+
*/
|
|
121
|
+
export function mcpChannelLocation(user, sha256Hex) {
|
|
122
|
+
const override = user.env[MCP_CHANNEL_ENV];
|
|
123
|
+
if (user.platform === 'win32') {
|
|
124
|
+
if (override !== undefined && override !== '') {
|
|
125
|
+
if (!override.startsWith('\\\\.\\pipe\\')) {
|
|
126
|
+
throw new Error(`${MCP_CHANNEL_ENV} must be a pipe path (\\\\.\\pipe\\…) on Windows.`);
|
|
127
|
+
}
|
|
128
|
+
return { kind: 'pipe', path: override };
|
|
129
|
+
}
|
|
130
|
+
if (user.sid === undefined) throw new Error("The Windows user's SID is not known.");
|
|
131
|
+
return { kind: 'pipe', path: mcpPipePath(user.sid, sha256Hex) };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let path;
|
|
135
|
+
if (override !== undefined && override !== '') {
|
|
136
|
+
if (!override.startsWith('/')) throw new Error(`${MCP_CHANNEL_ENV} must be an absolute path.`);
|
|
137
|
+
path = trimSlashes(override);
|
|
138
|
+
} else {
|
|
139
|
+
const home = trimSlashes(user.homedir);
|
|
140
|
+
if (!user.homedir.startsWith('/') || home === '') {
|
|
141
|
+
throw new Error('The home folder is not an absolute path.');
|
|
142
|
+
}
|
|
143
|
+
const runtime = user.env.XDG_RUNTIME_DIR;
|
|
144
|
+
const dir =
|
|
145
|
+
user.platform === 'darwin'
|
|
146
|
+
? `${home}/Library/Application Support/DataKlin/mcp`
|
|
147
|
+
: runtime !== undefined && runtime.startsWith('/') && trimSlashes(runtime) !== ''
|
|
148
|
+
? `${trimSlashes(runtime)}/dataklin`
|
|
149
|
+
: `${home}/.local/state/dataklin/mcp`;
|
|
150
|
+
path = `${dir}/${MCP_SOCKET_FILE}`;
|
|
151
|
+
}
|
|
152
|
+
if (utf8Bytes(path) > MCP_SOCKET_PATH_MAX_BYTES) {
|
|
153
|
+
throw new Error(
|
|
154
|
+
`The MCP socket path is longer than ${String(MCP_SOCKET_PATH_MAX_BYTES)} bytes: ${path}`,
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
return { kind: 'socket', dir: dirOf(path), path };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// --- The server key (feature 0135 F02; decisions.md, 2026-12-03) ------------
|
|
161
|
+
//
|
|
162
|
+
// 32 random bytes the app keeps in a file only the user can read, so the
|
|
163
|
+
// bridge can tell the app from another program on the channel: the app signs
|
|
164
|
+
// the answers the bridge acts on before it holds a proven token (a pairing's
|
|
165
|
+
// answer, `unknown-client`) with HMAC-SHA-256 under it. Its place is derived
|
|
166
|
+
// from the home folder, never the app's data folder (the Store build's
|
|
167
|
+
// AppData is redirected, so the bridge could not find it there):
|
|
168
|
+
//
|
|
169
|
+
// - macOS, Linux: `~/.config/dataklin-mcp/server.key` (the bridge's own
|
|
170
|
+
// folder, 0700; the file 0600).
|
|
171
|
+
// - Windows: `%USERPROFILE%\.dataklin-mcp\server.key`.
|
|
172
|
+
//
|
|
173
|
+
// The file holds 64 lowercase hex characters and a newline. Main creates it
|
|
174
|
+
// once (main/mcp/server-key.ts) and never rewrites a valid one; the bridge
|
|
175
|
+
// only reads it. `DATAKLIN_MCP_SERVER_KEY` (an absolute path) replaces the
|
|
176
|
+
// place, for tests only: a packaged app ignores it, and the bridge only with
|
|
177
|
+
// `DATAKLIN_MCP_TEST_OVERRIDES=1` (F03). On Windows the key's path must be on
|
|
178
|
+
// a drive (`C:\…`): a UNC path (`\\server\share`) is refused, the home
|
|
179
|
+
// folder's included.
|
|
180
|
+
|
|
181
|
+
/** The environment variable that replaces the server key's place (tests). */
|
|
182
|
+
export const MCP_SERVER_KEY_ENV = 'DATAKLIN_MCP_SERVER_KEY';
|
|
183
|
+
|
|
184
|
+
/** The key file's name in its folder. */
|
|
185
|
+
export const MCP_SERVER_KEY_FILE = 'server.key';
|
|
186
|
+
|
|
187
|
+
/** The key file's content: 64 lowercase hex characters, and a newline. */
|
|
188
|
+
export const MCP_SERVER_KEY_PATTERN = /^[0-9a-f]{64}\n?$/;
|
|
189
|
+
|
|
190
|
+
/** A Windows path on a drive: never UNC (`\\server\share`), whose file another machine serves. */
|
|
191
|
+
const WINDOWS_DRIVE_PATH = /^[A-Za-z]:\\/;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Where the server key of `user` is: its folder and the file. Throws with a
|
|
195
|
+
* message for the user when the home folder isn't an absolute path.
|
|
196
|
+
*/
|
|
197
|
+
export function mcpServerKeyLocation(user) {
|
|
198
|
+
const override = user.env[MCP_SERVER_KEY_ENV];
|
|
199
|
+
const windows = user.platform === 'win32';
|
|
200
|
+
const separator = windows ? '\\' : '/';
|
|
201
|
+
if (override !== undefined && override !== '') {
|
|
202
|
+
const absolute = windows ? WINDOWS_DRIVE_PATH.test(override) : override.startsWith('/');
|
|
203
|
+
if (!absolute) {
|
|
204
|
+
throw new Error(
|
|
205
|
+
windows
|
|
206
|
+
? `${MCP_SERVER_KEY_ENV} must be a path on a drive (C:\\…), not a share.`
|
|
207
|
+
: `${MCP_SERVER_KEY_ENV} must be an absolute path.`,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
const cut = override.lastIndexOf(separator);
|
|
211
|
+
return { dir: cut <= 0 ? separator : override.slice(0, cut), path: override };
|
|
212
|
+
}
|
|
213
|
+
if (windows) {
|
|
214
|
+
const home = user.homedir.replace(/\\+$/, '');
|
|
215
|
+
if (!WINDOWS_DRIVE_PATH.test(user.homedir) || home === '') {
|
|
216
|
+
throw new Error(
|
|
217
|
+
'The home folder is not on a drive (C:\\…), so the MCP server key has no place.',
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
const dir = `${home}\\.dataklin-mcp`;
|
|
221
|
+
return { dir, path: `${dir}\\${MCP_SERVER_KEY_FILE}` };
|
|
222
|
+
}
|
|
223
|
+
const home = trimSlashes(user.homedir);
|
|
224
|
+
if (!user.homedir.startsWith('/') || home === '') {
|
|
225
|
+
throw new Error('The home folder is not an absolute path.');
|
|
226
|
+
}
|
|
227
|
+
const dir = `${home}/.config/dataklin-mcp`;
|
|
228
|
+
return { dir, path: `${dir}/${MCP_SERVER_KEY_FILE}` };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** The environment variables that move the channel or the server key: tests only. */
|
|
232
|
+
export const MCP_TEST_OVERRIDE_VARS = [MCP_CHANNEL_ENV, MCP_SERVER_KEY_ENV];
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* `env` without the test overrides ({@link MCP_TEST_OVERRIDE_VARS}): what a
|
|
236
|
+
* packaged app, and a bridge without {@link MCP_TEST_OVERRIDES_ENV}, name the
|
|
237
|
+
* channel and the key from, so nothing in an ordinary environment can point
|
|
238
|
+
* them at another place.
|
|
239
|
+
*/
|
|
240
|
+
export function withoutTestOverrides(env) {
|
|
241
|
+
return Object.fromEntries(
|
|
242
|
+
Object.entries(env).filter(([name]) => !MCP_TEST_OVERRIDE_VARS.includes(name)),
|
|
243
|
+
);
|
|
244
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// dataklin-mcp: connects an AI client to the DataKlin app's MCP server on this
|
|
3
|
+
// computer (feature 0135 T04; ADR 0078 §6). Start it as a stdio MCP server:
|
|
4
|
+
//
|
|
5
|
+
// npx -y --registry=https://registry.npmjs.org/ --@dataklin:registry=https://registry.npmjs.org/ @dataklin/mcp@<DataKlin's version>
|
|
6
|
+
//
|
|
7
|
+
// `--version` prints the version. See README.md.
|
|
8
|
+
import { main } from './bridge.mjs';
|
|
9
|
+
|
|
10
|
+
main(process.argv.slice(2));
|
package/package.json
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dataklin/mcp",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "Connects an AI client to the DataKlin app's MCP server on this computer, over stdio.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"mcp",
|
|
7
|
+
"model-context-protocol",
|
|
8
|
+
"dataklin",
|
|
9
|
+
"database"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://dataklin.com",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://dataklin.com"
|
|
14
|
+
},
|
|
15
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
16
|
+
"author": "Bharath Reddy Kontham",
|
|
17
|
+
"mcpName": "com.dataklin/mcp",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"bin": {
|
|
20
|
+
"dataklin-mcp": "bin/dataklin-mcp.mjs"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"bin/",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=20"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"generate": "node scripts/generate-channel-name.mjs",
|
|
35
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
36
|
+
}
|
|
37
|
+
}
|