dsh-dragonbyte-client 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +158 -2
- package/client.js +201 -0
- package/cordis.patch.yml +3 -0
- package/package.json +58 -4
- package/src/desktop.mjs +421 -0
- package/src/host-main.mjs +275 -0
- package/src/host.mjs +325 -0
- package/src/protocol.v1.json +72 -0
- package/src/schema.mjs +118 -0
- package/src/wire.mjs +129 -0
package/src/schema.mjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// A JSON Schema subset validator, shipped rather than depended on.
|
|
2
|
+
//
|
|
3
|
+
// ⚠ THE THREE HALVES RUN IN DIFFERENT IMAGES AND MUST AGREE. The bridge
|
|
4
|
+
// container copies `src` and `server` from this package and nothing else; the
|
|
5
|
+
// tenant side is a store-linked bundle that resolves `/opt/dsh/node_modules`.
|
|
6
|
+
// A dependency reachable from one and not the other means two validators that
|
|
7
|
+
// disagree about what is valid, which is the failure a shared schema exists to
|
|
8
|
+
// prevent. So the schema and this reader travel with the package.
|
|
9
|
+
//
|
|
10
|
+
// Supported keywords, and nothing more: type, required, properties,
|
|
11
|
+
// additionalProperties, items, enum, const, pattern, minLength, maxLength,
|
|
12
|
+
// minimum, maximum, minItems, maxItems. Anything else is a bug in the schema and
|
|
13
|
+
// THROWS rather than being skipped — a silently ignored `anyOf` is a schema that
|
|
14
|
+
// does not do what its author believes.
|
|
15
|
+
|
|
16
|
+
const KEYWORDS = new Set([
|
|
17
|
+
'type', 'required', 'properties', 'additionalProperties', 'items',
|
|
18
|
+
'enum', 'const', 'pattern', 'minLength', 'maxLength',
|
|
19
|
+
'minimum', 'maximum', 'minItems', 'maxItems',
|
|
20
|
+
'title', 'description', '$id', 'examples', 'default',
|
|
21
|
+
// ⚠ NOT A JSON SCHEMA KEYWORD. `frames` groups the per-direction property
|
|
22
|
+
// and requirement lists in protocol.v1.json so one artifact can describe
|
|
23
|
+
// three hops; it is read by `frameSchema` and never by `validate`.
|
|
24
|
+
'frames',
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
function typeOf(value) {
|
|
28
|
+
if (value === null) return 'null';
|
|
29
|
+
if (Array.isArray(value)) return 'array';
|
|
30
|
+
if (Number.isInteger(value)) return 'integer';
|
|
31
|
+
return typeof value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function typeMatches(token, value) {
|
|
35
|
+
const actual = typeOf(value);
|
|
36
|
+
if (token === 'number') return actual === 'integer' || actual === 'number';
|
|
37
|
+
return actual === token;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function check(schema, value, path, errors) {
|
|
41
|
+
for (const keyword of Object.keys(schema)) {
|
|
42
|
+
if (!KEYWORDS.has(keyword)) throw new TypeError(`unsupported schema keyword at ${path || '/'}: ${keyword}`);
|
|
43
|
+
}
|
|
44
|
+
if (schema.type !== undefined) {
|
|
45
|
+
const tokens = Array.isArray(schema.type) ? schema.type : [schema.type];
|
|
46
|
+
if (!tokens.some((token) => typeMatches(token, value))) {
|
|
47
|
+
errors.push({ path, message: `expected ${tokens.join(' or ')}, got ${typeOf(value)}` });
|
|
48
|
+
// Every keyword below assumes the type held.
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (schema.const !== undefined && value !== schema.const) errors.push({ path, message: `must be ${JSON.stringify(schema.const)}` });
|
|
53
|
+
if (schema.enum !== undefined && !schema.enum.includes(value)) {
|
|
54
|
+
errors.push({ path, message: `must be one of ${schema.enum.map((v) => JSON.stringify(v)).join(', ')}` });
|
|
55
|
+
}
|
|
56
|
+
if (typeof value === 'string') {
|
|
57
|
+
if (schema.pattern !== undefined && !new RegExp(schema.pattern).test(value)) errors.push({ path, message: `must match ${schema.pattern}` });
|
|
58
|
+
if (schema.minLength !== undefined && value.length < schema.minLength) errors.push({ path, message: `must be at least ${schema.minLength} characters` });
|
|
59
|
+
if (schema.maxLength !== undefined && value.length > schema.maxLength) errors.push({ path, message: `must be at most ${schema.maxLength} characters` });
|
|
60
|
+
}
|
|
61
|
+
if (typeof value === 'number') {
|
|
62
|
+
if (schema.minimum !== undefined && value < schema.minimum) errors.push({ path, message: `must be >= ${schema.minimum}` });
|
|
63
|
+
if (schema.maximum !== undefined && value > schema.maximum) errors.push({ path, message: `must be <= ${schema.maximum}` });
|
|
64
|
+
}
|
|
65
|
+
if (Array.isArray(value)) {
|
|
66
|
+
if (schema.minItems !== undefined && value.length < schema.minItems) errors.push({ path, message: `must have at least ${schema.minItems} items` });
|
|
67
|
+
if (schema.maxItems !== undefined && value.length > schema.maxItems) errors.push({ path, message: `must have at most ${schema.maxItems} items` });
|
|
68
|
+
if (schema.items !== undefined) value.forEach((item, index) => check(schema.items, item, `${path}/${index}`, errors));
|
|
69
|
+
}
|
|
70
|
+
if (typeOf(value) === 'object') {
|
|
71
|
+
for (const key of schema.required ?? []) {
|
|
72
|
+
if (!Object.hasOwn(value, key)) errors.push({ path: `${path}/${key}`, message: 'is required' });
|
|
73
|
+
}
|
|
74
|
+
for (const [key, subschema] of Object.entries(schema.properties ?? {})) {
|
|
75
|
+
if (Object.hasOwn(value, key)) check(subschema, value[key], `${path}/${key}`, errors);
|
|
76
|
+
}
|
|
77
|
+
// Absent means true — see protocol.v1.json. Never set false on a frame.
|
|
78
|
+
if (schema.additionalProperties === false) {
|
|
79
|
+
for (const key of Object.keys(value)) {
|
|
80
|
+
if (!Object.hasOwn(schema.properties ?? {}, key)) errors.push({ path: `${path}/${key}`, message: 'is not a known field' });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** @returns {string|null} the first failure, or null when `value` conforms. */
|
|
87
|
+
export function validate(schema, value) {
|
|
88
|
+
const errors = [];
|
|
89
|
+
check(schema, value, '', errors);
|
|
90
|
+
if (!errors.length) return null;
|
|
91
|
+
const [first] = errors;
|
|
92
|
+
return `${first.path || '/'} ${first.message}`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The property map for one frame of one hop.
|
|
97
|
+
*
|
|
98
|
+
* `protocol.v1.json` groups requirement and property lists by direction, so a
|
|
99
|
+
* receiver asks for exactly the frame it accepts instead of validating against
|
|
100
|
+
* the union of everything the protocol can carry. An unknown direction or frame
|
|
101
|
+
* THROWS: a typo here is a programming error, not an input to tolerate.
|
|
102
|
+
*
|
|
103
|
+
* @param {object} protocol the parsed protocol.v1.json
|
|
104
|
+
* @param {string} direction e.g. `host-to-bridge`
|
|
105
|
+
* @param {string} frame e.g. `catalog`
|
|
106
|
+
* @returns {object} a JSON Schema describing that frame
|
|
107
|
+
*/
|
|
108
|
+
export function frameSchema(protocol, direction, frame) {
|
|
109
|
+
const hop = protocol.frames?.[direction];
|
|
110
|
+
if (hop === undefined) throw new TypeError(`unknown frame direction ${JSON.stringify(direction)}`);
|
|
111
|
+
const entry = hop[frame];
|
|
112
|
+
if (entry === undefined) throw new TypeError(`unknown frame ${JSON.stringify(frame)} in ${direction}`);
|
|
113
|
+
return {
|
|
114
|
+
type: 'object',
|
|
115
|
+
required: entry.required,
|
|
116
|
+
properties: Object.fromEntries(entry.properties.map((name) => [name, protocol.properties[name]])),
|
|
117
|
+
};
|
|
118
|
+
}
|
package/src/wire.mjs
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// The one contract both halves import. The host validates what it sends; the
|
|
2
|
+
// tenant side validates what it receives; the server forwards without reading,
|
|
3
|
+
// so a version skew between an old host and a new tenant fails here with
|
|
4
|
+
// `bad_message` instead of inside a tool call.
|
|
5
|
+
//
|
|
6
|
+
// A breaking change is a new `v`, never a mutated field. Adding an optional
|
|
7
|
+
// field is the only additive change allowed.
|
|
8
|
+
|
|
9
|
+
export const WIRE_VERSION = 1;
|
|
10
|
+
|
|
11
|
+
// ⚠ THE FRAMES ARE A SHARED ARTIFACT, NOT A CONVENTION. Each hop used to check
|
|
12
|
+
// its own fields ad hoc, so an unknown `type` was answered `unknown message X`
|
|
13
|
+
// only after the receiver had already switched on it, and a malformed field was
|
|
14
|
+
// described by whichever branch happened to read it. One JSON file now names
|
|
15
|
+
// every frame in every direction, and each receiver validates the frame it
|
|
16
|
+
// accepts before reading a single field.
|
|
17
|
+
export { frameSchema, validate as validateSchema } from './schema.mjs';
|
|
18
|
+
import PROTOCOL from './protocol.v1.json' with { type: 'json' };
|
|
19
|
+
import { frameSchema, validate as validateSchema } from './schema.mjs';
|
|
20
|
+
|
|
21
|
+
export const MAX_CATALOG_BYTES = 256 * 1024;
|
|
22
|
+
export const MAX_ARGS_BYTES = 512 * 1024;
|
|
23
|
+
export const MAX_RESULT_BYTES = 4 * 1024 * 1024;
|
|
24
|
+
export const MAX_FRAME_BYTES = MAX_RESULT_BYTES + 64 * 1024;
|
|
25
|
+
|
|
26
|
+
const EMAIL_PATTERN = /^[^\s@]{1,64}@[^\s@]{1,190}$/;
|
|
27
|
+
// Mirrors the `mcp__<server>__<tool>` shape dsh-mcp-oauth already publishes, so
|
|
28
|
+
// a model's habit of reading that prefix as "an MCP tool" stays true.
|
|
29
|
+
const SERVER_NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
|
30
|
+
const TOOL_NAME = /^[A-Za-z0-9_.-]{1,64}$/;
|
|
31
|
+
|
|
32
|
+
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
33
|
+
|
|
34
|
+
function invalid(message) {
|
|
35
|
+
const error = new Error(message);
|
|
36
|
+
error.code = 'bad_message';
|
|
37
|
+
return error;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function publicToolName(server, tool) {
|
|
41
|
+
return `mcp__dragonbyte_${server}__${tool}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The wire name a public tool name maps back to, or null when it is not ours. */
|
|
45
|
+
export function parsePublicToolName(name) {
|
|
46
|
+
const match = /^mcp__dragonbyte_([a-z][a-z0-9-]{0,31})__(.+)$/.exec(String(name || ''));
|
|
47
|
+
if (!match || !TOOL_NAME.test(match[2])) return null;
|
|
48
|
+
return { server: match[1], tool: match[2] };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Throws `bad_message` rather than returning false: every caller would throw. */
|
|
52
|
+
export function assertEmail(email) {
|
|
53
|
+
const value = String(email ?? '').trim().toLowerCase();
|
|
54
|
+
if (!EMAIL_PATTERN.test(value)) throw invalid('invalid email');
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Validate one frame of one hop against the shared schema.
|
|
60
|
+
*
|
|
61
|
+
* ⚠ THE VERSION CHECK COMES FIRST AND IS DELIBERATELY NOT `const: 1` IN THE
|
|
62
|
+
* SCHEMA. Version skew between a laptop, a bridge and an immutable tenant
|
|
63
|
+
* bundle is normal, so it must fail with a message that names it rather than
|
|
64
|
+
* with a field-by-field complaint from a schema that assumed today's version.
|
|
65
|
+
*
|
|
66
|
+
* @param {object} message the decoded frame
|
|
67
|
+
* @param {string} direction e.g. `host-to-bridge`
|
|
68
|
+
* @param {string} frame e.g. `catalog`
|
|
69
|
+
* @returns {object} the same message, for chaining
|
|
70
|
+
*/
|
|
71
|
+
export function assertFrame(message, direction, frame) {
|
|
72
|
+
if (message?.v !== WIRE_VERSION) throw invalid(`unsupported wire version ${JSON.stringify(message?.v)}`);
|
|
73
|
+
const problem = validateSchema(frameSchema(PROTOCOL, direction, frame), message);
|
|
74
|
+
if (problem !== null) throw invalid(`${direction} ${frame}: ${problem}`);
|
|
75
|
+
return message;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function validateCatalog(catalog) {
|
|
79
|
+
if (!isObject(catalog) || !Array.isArray(catalog.servers)) throw invalid('catalog.servers must be an array');
|
|
80
|
+
if (catalog.servers.length > 32) throw invalid('catalog names more than 32 servers');
|
|
81
|
+
const seenServers = new Set();
|
|
82
|
+
let tools = 0;
|
|
83
|
+
const servers = catalog.servers.map((server) => {
|
|
84
|
+
if (!isObject(server) || !SERVER_NAME.test(server.name || '')) throw invalid('invalid server name');
|
|
85
|
+
if (seenServers.has(server.name)) throw invalid(`duplicate server ${server.name}`);
|
|
86
|
+
seenServers.add(server.name);
|
|
87
|
+
if (!Array.isArray(server.tools)) throw invalid(`server ${server.name} has no tools array`);
|
|
88
|
+
const seenTools = new Set();
|
|
89
|
+
const entries = server.tools.map((tool) => {
|
|
90
|
+
if (!isObject(tool) || !TOOL_NAME.test(tool.name || '')) throw invalid(`invalid tool name on ${server.name}`);
|
|
91
|
+
if (seenTools.has(tool.name)) throw invalid(`duplicate tool ${server.name}/${tool.name}`);
|
|
92
|
+
seenTools.add(tool.name);
|
|
93
|
+
if (tool.description !== undefined && typeof tool.description !== 'string') throw invalid('tool description must be a string');
|
|
94
|
+
// ⚠ NOT validated further on purpose. An MCP input schema is whatever the
|
|
95
|
+
// server said, and this bridge does not re-implement JSON Schema: a
|
|
96
|
+
// stricter check here would reject a valid server and a looser one would
|
|
97
|
+
// give the model a schema the server never agreed to.
|
|
98
|
+
if (!isObject(tool.inputSchema ?? {})) throw invalid('tool inputSchema must be an object');
|
|
99
|
+
tools += 1;
|
|
100
|
+
return { name: tool.name, description: String(tool.description ?? ''), inputSchema: tool.inputSchema ?? { type: 'object' } };
|
|
101
|
+
});
|
|
102
|
+
return { name: server.name, label: String(server.label ?? server.name).slice(0, 80), tools: entries };
|
|
103
|
+
});
|
|
104
|
+
if (tools === 0) throw invalid('catalog carries no tools');
|
|
105
|
+
if (tools > 256) throw invalid('catalog carries more than 256 tools');
|
|
106
|
+
const value = { servers };
|
|
107
|
+
if (Buffer.byteLength(JSON.stringify(value)) > MAX_CATALOG_BYTES) throw invalid('catalog exceeds 256 KiB');
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function validateArgs(args) {
|
|
112
|
+
if (!isObject(args)) throw invalid('tool arguments must be an object');
|
|
113
|
+
if (Buffer.byteLength(JSON.stringify(args)) > MAX_ARGS_BYTES) throw invalid('tool arguments exceed 512 KiB');
|
|
114
|
+
return args;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function validateResult(result) {
|
|
118
|
+
if (!isObject(result) || !Array.isArray(result.content)) throw invalid('result.content must be an array');
|
|
119
|
+
for (const item of result.content) {
|
|
120
|
+
if (!isObject(item) || typeof item.type !== 'string') throw invalid('result content item has no type');
|
|
121
|
+
}
|
|
122
|
+
if (result.structuredContent !== undefined && !isObject(result.structuredContent)) throw invalid('structuredContent must be an object');
|
|
123
|
+
if (Buffer.byteLength(JSON.stringify(result)) > MAX_RESULT_BYTES) throw invalid('result exceeds 4 MiB');
|
|
124
|
+
return {
|
|
125
|
+
content: result.content,
|
|
126
|
+
...(result.structuredContent === undefined ? {} : { structuredContent: result.structuredContent }),
|
|
127
|
+
...(result.isError === true ? { isError: true } : {}),
|
|
128
|
+
};
|
|
129
|
+
}
|