@ontrails/mcp 1.0.0-beta.0 → 1.0.0-beta.10
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/.turbo/turbo-lint.log +1 -1
- package/CHANGELOG.md +133 -0
- package/README.md +39 -103
- package/dist/annotations.d.ts +2 -2
- package/dist/annotations.d.ts.map +1 -1
- package/dist/annotations.js +8 -6
- package/dist/annotations.js.map +1 -1
- package/dist/blaze.d.ts +2 -0
- package/dist/blaze.d.ts.map +1 -1
- package/dist/blaze.js +12 -2
- package/dist/blaze.js.map +1 -1
- package/dist/build.d.ts +4 -1
- package/dist/build.d.ts.map +1 -1
- package/dist/build.js +79 -62
- package/dist/build.js.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/annotations.test.ts +15 -22
- package/src/__tests__/blaze.test.ts +59 -14
- package/src/__tests__/build.test.ts +159 -61
- package/src/annotations.ts +11 -11
- package/src/blaze.ts +16 -2
- package/src/build.ts +119 -91
package/dist/build.js
CHANGED
|
@@ -5,18 +5,44 @@
|
|
|
5
5
|
* validate input, compose layers, execute the implementation, and map
|
|
6
6
|
* Results to MCP responses.
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { Result, ValidationError, executeTrail, isBlobRef, zodToJsonSchema, } from '@ontrails/core';
|
|
9
9
|
import { deriveAnnotations } from './annotations.js';
|
|
10
10
|
import { createMcpProgressCallback } from './progress.js';
|
|
11
11
|
import { deriveToolName } from './tool-name.js';
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Internal helpers (defined before use)
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
/** Concatenate an array of Uint8Array chunks into a single Uint8Array. */
|
|
16
|
+
const concatChunks = (chunks, totalLength) => {
|
|
17
|
+
const result = new Uint8Array(totalLength);
|
|
18
|
+
let offset = 0;
|
|
19
|
+
for (const chunk of chunks) {
|
|
20
|
+
result.set(chunk, offset);
|
|
21
|
+
offset += chunk.length;
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
/** Collect a ReadableStream into a single Uint8Array. */
|
|
26
|
+
const collectStream = async (stream) => {
|
|
27
|
+
const reader = stream.getReader();
|
|
28
|
+
const chunks = [];
|
|
29
|
+
let totalLength = 0;
|
|
30
|
+
for (;;) {
|
|
31
|
+
const { done, value } = await reader.read();
|
|
32
|
+
if (done) {
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
chunks.push(value);
|
|
36
|
+
totalLength += value.length;
|
|
15
37
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
38
|
+
return concatChunks(chunks, totalLength);
|
|
39
|
+
};
|
|
40
|
+
/** Resolve BlobRef data to Uint8Array (handles ReadableStream). */
|
|
41
|
+
const resolveBlobData = (blob) => {
|
|
42
|
+
if (blob.data instanceof ReadableStream) {
|
|
43
|
+
return collectStream(blob.data);
|
|
44
|
+
}
|
|
45
|
+
return blob.data;
|
|
20
46
|
};
|
|
21
47
|
const uint8ArrayToBase64 = (bytes) => {
|
|
22
48
|
// Use btoa with manual conversion for runtime-agnostic base64
|
|
@@ -26,10 +52,11 @@ const uint8ArrayToBase64 = (bytes) => {
|
|
|
26
52
|
}
|
|
27
53
|
return btoa(binary);
|
|
28
54
|
};
|
|
29
|
-
const blobToContent = (blob) => {
|
|
55
|
+
const blobToContent = async (blob) => {
|
|
56
|
+
const bytes = await resolveBlobData(blob);
|
|
30
57
|
if (blob.mimeType.startsWith('image/')) {
|
|
31
58
|
return {
|
|
32
|
-
data: uint8ArrayToBase64(
|
|
59
|
+
data: uint8ArrayToBase64(bytes),
|
|
33
60
|
mimeType: blob.mimeType,
|
|
34
61
|
type: 'image',
|
|
35
62
|
};
|
|
@@ -37,18 +64,18 @@ const blobToContent = (blob) => {
|
|
|
37
64
|
return {
|
|
38
65
|
mimeType: blob.mimeType,
|
|
39
66
|
type: 'resource',
|
|
40
|
-
uri: `blob://${blob.name
|
|
67
|
+
uri: `blob://${blob.name}`,
|
|
41
68
|
};
|
|
42
69
|
};
|
|
43
70
|
/** Separate blob fields from non-blob fields in an object. */
|
|
44
|
-
const separateBlobFields = (obj) => {
|
|
71
|
+
const separateBlobFields = async (obj) => {
|
|
45
72
|
const blobContents = [];
|
|
46
73
|
const textFields = {};
|
|
47
74
|
let hasBlobFields = false;
|
|
48
75
|
for (const [key, val] of Object.entries(obj)) {
|
|
49
76
|
if (isBlobRef(val)) {
|
|
50
77
|
hasBlobFields = true;
|
|
51
|
-
blobContents.push(blobToContent(val));
|
|
78
|
+
blobContents.push(await blobToContent(val));
|
|
52
79
|
}
|
|
53
80
|
else {
|
|
54
81
|
textFields[key] = val;
|
|
@@ -57,8 +84,8 @@ const separateBlobFields = (obj) => {
|
|
|
57
84
|
return { blobContents, hasBlobFields, textFields };
|
|
58
85
|
};
|
|
59
86
|
/** Serialize a mixed blob/text object to MCP content. */
|
|
60
|
-
const serializeMixedObject = (obj) => {
|
|
61
|
-
const { blobContents, hasBlobFields, textFields } = separateBlobFields(obj);
|
|
87
|
+
const serializeMixedObject = async (obj) => {
|
|
88
|
+
const { blobContents, hasBlobFields, textFields } = await separateBlobFields(obj);
|
|
62
89
|
if (!hasBlobFields) {
|
|
63
90
|
return undefined;
|
|
64
91
|
}
|
|
@@ -67,12 +94,12 @@ const serializeMixedObject = (obj) => {
|
|
|
67
94
|
}
|
|
68
95
|
return blobContents;
|
|
69
96
|
};
|
|
70
|
-
const serializeOutput = (value) => {
|
|
97
|
+
const serializeOutput = async (value) => {
|
|
71
98
|
if (isBlobRef(value)) {
|
|
72
|
-
return [blobToContent(value)];
|
|
99
|
+
return [await blobToContent(value)];
|
|
73
100
|
}
|
|
74
101
|
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
75
|
-
const mixed = serializeMixedObject(value);
|
|
102
|
+
const mixed = await serializeMixedObject(value);
|
|
76
103
|
if (mixed) {
|
|
77
104
|
return mixed;
|
|
78
105
|
}
|
|
@@ -87,40 +114,18 @@ const mcpError = (message) => ({
|
|
|
87
114
|
content: [{ text: message, type: 'text' }],
|
|
88
115
|
isError: true,
|
|
89
116
|
});
|
|
90
|
-
|
|
91
|
-
const buildTrailContext = async (options, extra) => {
|
|
92
|
-
const baseContext = options.createContext !== undefined && options.createContext !== null
|
|
93
|
-
? await options.createContext()
|
|
94
|
-
: createTrailContext();
|
|
95
|
-
const signal = extra.signal ?? baseContext.signal;
|
|
117
|
+
const createHandler = (t, layers, options) => async (args, extra) => {
|
|
96
118
|
const progressCb = createMcpProgressCallback(extra);
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const result = await impl(validatedInput, ctx);
|
|
108
|
-
if (result.isOk()) {
|
|
109
|
-
return { content: serializeOutput(result.value) };
|
|
110
|
-
}
|
|
111
|
-
return mcpError(result.error.message);
|
|
112
|
-
}
|
|
113
|
-
catch (error) {
|
|
114
|
-
return mcpError(error instanceof Error ? error.message : String(error));
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
const createHandler = (trail, layers, options) => async (args, extra) => {
|
|
118
|
-
const validated = validateInput(trail.input, args);
|
|
119
|
-
if (validated.isErr()) {
|
|
120
|
-
return mcpError(validated.error.message);
|
|
121
|
-
}
|
|
122
|
-
const ctx = await buildTrailContext(options, extra);
|
|
123
|
-
return executeAndMap(trail, validated.value, ctx, layers);
|
|
119
|
+
const result = await executeTrail(t, args, {
|
|
120
|
+
createContext: options.createContext,
|
|
121
|
+
ctx: progressCb === undefined ? undefined : { progress: progressCb },
|
|
122
|
+
layers,
|
|
123
|
+
signal: extra.signal,
|
|
124
|
+
});
|
|
125
|
+
if (result.isOk()) {
|
|
126
|
+
return { content: await serializeOutput(result.value) };
|
|
127
|
+
}
|
|
128
|
+
return mcpError(result.error.message);
|
|
124
129
|
};
|
|
125
130
|
// ---------------------------------------------------------------------------
|
|
126
131
|
// Builder
|
|
@@ -131,12 +136,12 @@ const createHandler = (trail, layers, options) => async (args, extra) => {
|
|
|
131
136
|
* Each trail in the topo becomes an McpToolDefinition with:
|
|
132
137
|
* - A derived tool name (app-prefixed, underscore-delimited)
|
|
133
138
|
* - JSON Schema input from zodToJsonSchema
|
|
134
|
-
* - MCP annotations from trail
|
|
139
|
+
* - MCP annotations from trail metadata
|
|
135
140
|
* - A handler that validates, composes layers, executes, and maps results
|
|
136
141
|
*/
|
|
137
|
-
/** Check if a trail should be included based on
|
|
142
|
+
/** Check if a trail should be included based on metadata and filters. */
|
|
138
143
|
const shouldInclude = (trail, options) => {
|
|
139
|
-
if (trail.
|
|
144
|
+
if (trail.metadata?.['internal'] === true) {
|
|
140
145
|
return false;
|
|
141
146
|
}
|
|
142
147
|
if (options.includeTrails !== undefined && options.includeTrails.length > 0) {
|
|
@@ -171,20 +176,32 @@ const buildToolDefinition = (app, trail, layers, options) => {
|
|
|
171
176
|
handler: createHandler(trail, layers, options),
|
|
172
177
|
inputSchema: zodToJsonSchema(trail.input),
|
|
173
178
|
name: deriveToolName(app.name, trail.id),
|
|
179
|
+
trailId: trail.id,
|
|
174
180
|
};
|
|
175
181
|
};
|
|
182
|
+
/** Register a trail as an MCP tool, checking for name collisions. */
|
|
183
|
+
const registerTool = (app, trailItem, layers, options, nameToTrailId, tools) => {
|
|
184
|
+
const toolName = deriveToolName(app.name, trailItem.id);
|
|
185
|
+
const existingId = nameToTrailId.get(toolName);
|
|
186
|
+
if (existingId !== undefined) {
|
|
187
|
+
return Result.err(new ValidationError(`MCP tool-name collision: trails "${existingId}" and "${trailItem.id}" both derive the tool name "${toolName}"`));
|
|
188
|
+
}
|
|
189
|
+
nameToTrailId.set(toolName, trailItem.id);
|
|
190
|
+
tools.push(buildToolDefinition(app, trailItem, layers, options));
|
|
191
|
+
return Result.ok();
|
|
192
|
+
};
|
|
193
|
+
/** Filter topo items to eligible trails. */
|
|
194
|
+
const eligibleTrails = (app, options) => app.list().filter((trail) => shouldInclude(trail, options));
|
|
176
195
|
export const buildMcpTools = (app, options = {}) => {
|
|
177
196
|
const layers = options.layers ?? [];
|
|
178
197
|
const tools = [];
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
continue;
|
|
198
|
+
const nameToTrailId = new Map();
|
|
199
|
+
for (const trailItem of eligibleTrails(app, options)) {
|
|
200
|
+
const registered = registerTool(app, trailItem, layers, options, nameToTrailId, tools);
|
|
201
|
+
if (registered.isErr()) {
|
|
202
|
+
return registered;
|
|
185
203
|
}
|
|
186
|
-
tools.push(buildToolDefinition(app, item, layers, options));
|
|
187
204
|
}
|
|
188
|
-
return tools;
|
|
205
|
+
return Result.ok(tools);
|
|
189
206
|
};
|
|
190
207
|
//# sourceMappingURL=build.js.map
|
package/dist/build.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build.js","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"build.js","sourceRoot":"","sources":["../src/build.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,MAAM,EACN,eAAe,EACf,YAAY,EACZ,SAAS,EACT,eAAe,GAChB,MAAM,gBAAgB,CAAC;AAIxB,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,yBAAyB,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAiDhD,8EAA8E;AAC9E,wCAAwC;AACxC,8EAA8E;AAE9E,0EAA0E;AAC1E,MAAM,YAAY,GAAG,CACnB,MAAoB,EACpB,WAAmB,EACP,EAAE;IACd,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,WAAW,CA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AAC,EAAE,CAAC;QACrD,MAAM,UAAU,GAAG,YAAY,CAC7B,GAAG,EACH,SAAS,EACT,MAAM,EACN,OAAO,EACP,aAAa,EACb,KAAK,CACN,CAAC;QACF,IAAI,UAAU,CAAC,KAAK,EAAE,EAAE,CAAC;YACvB,OAAO,UAAU,CAAC;QACpB,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;AAC1B,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ontrails/mcp",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/index.ts",
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
"clean": "rm -rf dist *.tsbuildinfo"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@ontrails/core": "
|
|
17
|
+
"@ontrails/core": "^1.0.0-beta.8"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
20
|
"@modelcontextprotocol/sdk": "^1.12.0",
|
|
21
|
-
"zod": "
|
|
21
|
+
"zod": "^4.3.5"
|
|
22
22
|
}
|
|
23
23
|
}
|
|
@@ -3,36 +3,39 @@ import { describe, expect, test } from 'bun:test';
|
|
|
3
3
|
import { deriveAnnotations } from '../annotations.js';
|
|
4
4
|
|
|
5
5
|
describe('deriveAnnotations', () => {
|
|
6
|
-
test('
|
|
7
|
-
const annotations = deriveAnnotations({
|
|
6
|
+
test('read intent produces readOnlyHint', () => {
|
|
7
|
+
const annotations = deriveAnnotations({ intent: 'read' });
|
|
8
8
|
expect(annotations.readOnlyHint).toBe(true);
|
|
9
9
|
expect(annotations.destructiveHint).toBeUndefined();
|
|
10
10
|
expect(annotations.idempotentHint).toBeUndefined();
|
|
11
11
|
});
|
|
12
12
|
|
|
13
|
-
test('
|
|
14
|
-
const annotations = deriveAnnotations({
|
|
13
|
+
test('destroy intent produces destructiveHint', () => {
|
|
14
|
+
const annotations = deriveAnnotations({ intent: 'destroy' });
|
|
15
15
|
expect(annotations.destructiveHint).toBe(true);
|
|
16
16
|
expect(annotations.readOnlyHint).toBeUndefined();
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
test('idempotent trail produces idempotentHint', () => {
|
|
20
|
-
const annotations = deriveAnnotations({
|
|
20
|
+
const annotations = deriveAnnotations({
|
|
21
|
+
idempotent: true,
|
|
22
|
+
intent: 'write',
|
|
23
|
+
});
|
|
21
24
|
expect(annotations.idempotentHint).toBe(true);
|
|
22
25
|
});
|
|
23
26
|
|
|
24
|
-
test('
|
|
27
|
+
test('read intent with idempotent combines correctly', () => {
|
|
25
28
|
const annotations = deriveAnnotations({
|
|
26
29
|
idempotent: true,
|
|
27
|
-
|
|
30
|
+
intent: 'read',
|
|
28
31
|
});
|
|
29
32
|
expect(annotations.readOnlyHint).toBe(true);
|
|
30
33
|
expect(annotations.idempotentHint).toBe(true);
|
|
31
34
|
expect(annotations.destructiveHint).toBeUndefined();
|
|
32
35
|
});
|
|
33
36
|
|
|
34
|
-
test('
|
|
35
|
-
const annotations = deriveAnnotations({});
|
|
37
|
+
test('write intent produces empty annotations', () => {
|
|
38
|
+
const annotations = deriveAnnotations({ intent: 'write' });
|
|
36
39
|
expect(annotations.readOnlyHint).toBeUndefined();
|
|
37
40
|
expect(annotations.destructiveHint).toBeUndefined();
|
|
38
41
|
expect(annotations.idempotentHint).toBeUndefined();
|
|
@@ -42,29 +45,19 @@ describe('deriveAnnotations', () => {
|
|
|
42
45
|
test('description maps to title', () => {
|
|
43
46
|
const annotations = deriveAnnotations({
|
|
44
47
|
description: 'Show entity details',
|
|
48
|
+
intent: 'write',
|
|
45
49
|
});
|
|
46
50
|
expect(annotations.title).toBe('Show entity details');
|
|
47
51
|
});
|
|
48
52
|
|
|
49
|
-
test('all
|
|
53
|
+
test('all hints plus description', () => {
|
|
50
54
|
const annotations = deriveAnnotations({
|
|
51
55
|
description: 'A trail',
|
|
52
|
-
destructive: true,
|
|
53
56
|
idempotent: true,
|
|
54
|
-
|
|
57
|
+
intent: 'destroy',
|
|
55
58
|
});
|
|
56
|
-
expect(annotations.readOnlyHint).toBe(true);
|
|
57
59
|
expect(annotations.destructiveHint).toBe(true);
|
|
58
60
|
expect(annotations.idempotentHint).toBe(true);
|
|
59
61
|
expect(annotations.title).toBe('A trail');
|
|
60
62
|
});
|
|
61
|
-
|
|
62
|
-
test('false values are not included', () => {
|
|
63
|
-
const annotations = deriveAnnotations({
|
|
64
|
-
destructive: false,
|
|
65
|
-
readOnly: false,
|
|
66
|
-
});
|
|
67
|
-
expect(annotations.readOnlyHint).toBeUndefined();
|
|
68
|
-
expect(annotations.destructiveHint).toBeUndefined();
|
|
69
|
-
});
|
|
70
63
|
});
|
|
@@ -3,14 +3,15 @@ import { describe, expect, test } from 'bun:test';
|
|
|
3
3
|
import { Result, trail, topo } from '@ontrails/core';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
|
|
6
|
-
import { createMcpServer } from '../blaze.js';
|
|
6
|
+
import { blaze, createMcpServer } from '../blaze.js';
|
|
7
7
|
import { buildMcpTools } from '../build.js';
|
|
8
|
+
import type { McpToolDefinition } from '../build.js';
|
|
8
9
|
|
|
9
10
|
// ---------------------------------------------------------------------------
|
|
10
11
|
// Tests
|
|
11
12
|
// ---------------------------------------------------------------------------
|
|
12
13
|
|
|
13
|
-
const requireTool = (tools:
|
|
14
|
+
const requireTool = (tools: McpToolDefinition[], name: string) => {
|
|
14
15
|
const tool = tools.find((entry) => entry.name === name);
|
|
15
16
|
expect(tool).toBeDefined();
|
|
16
17
|
if (!tool) {
|
|
@@ -19,35 +20,79 @@ const requireTool = (tools: ReturnType<typeof buildMcpTools>, name: string) => {
|
|
|
19
20
|
return tool;
|
|
20
21
|
};
|
|
21
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Unwrap buildMcpTools result, throwing on error so test failures surface clearly.
|
|
25
|
+
*/
|
|
26
|
+
const buildTools = (
|
|
27
|
+
...args: Parameters<typeof buildMcpTools>
|
|
28
|
+
): McpToolDefinition[] => {
|
|
29
|
+
const result = buildMcpTools(...args);
|
|
30
|
+
if (result.isErr()) {
|
|
31
|
+
throw result.error;
|
|
32
|
+
}
|
|
33
|
+
return result.value;
|
|
34
|
+
};
|
|
35
|
+
|
|
22
36
|
const createIntegrationTools = () => {
|
|
23
37
|
const greetTrail = trail('greet', {
|
|
24
38
|
description: 'Greet someone',
|
|
25
|
-
implementation: (input) => Result.ok({ greeting: `Hello, ${input.name}!` }),
|
|
26
39
|
input: z.object({ name: z.string() }),
|
|
27
|
-
|
|
40
|
+
intent: 'read',
|
|
41
|
+
run: (input) => Result.ok({ greeting: `Hello, ${input.name}!` }),
|
|
28
42
|
});
|
|
29
43
|
|
|
30
44
|
const deleteTrail = trail('item.delete', {
|
|
31
45
|
description: 'Delete an item',
|
|
32
|
-
destructive: true,
|
|
33
|
-
implementation: (_input) => Result.ok({ deleted: true }),
|
|
34
46
|
input: z.object({ id: z.string() }),
|
|
47
|
+
intent: 'destroy',
|
|
48
|
+
run: (_input) => Result.ok({ deleted: true }),
|
|
35
49
|
});
|
|
36
50
|
|
|
37
|
-
return
|
|
51
|
+
return buildTools(topo('myapp', { deleteTrail, greetTrail }));
|
|
38
52
|
};
|
|
39
53
|
|
|
40
54
|
describe('blaze', () => {
|
|
55
|
+
test('blaze throws on invalid topo', async () => {
|
|
56
|
+
const t = trail('broken', {
|
|
57
|
+
follow: ['nonexistent.trail'],
|
|
58
|
+
input: z.object({}),
|
|
59
|
+
output: z.object({}),
|
|
60
|
+
run: () => Result.ok({}),
|
|
61
|
+
});
|
|
62
|
+
const app = topo('test', { t });
|
|
63
|
+
await expect(blaze(app)).rejects.toThrow(/validation/i);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('blaze skips validation when validate: false', async () => {
|
|
67
|
+
const t = trail('broken', {
|
|
68
|
+
follow: ['nonexistent.trail'],
|
|
69
|
+
input: z.object({}),
|
|
70
|
+
output: z.object({}),
|
|
71
|
+
run: () => Result.ok({}),
|
|
72
|
+
});
|
|
73
|
+
const app = topo('test', { t });
|
|
74
|
+
const result = await Promise.race([
|
|
75
|
+
blaze(app, { validate: false }).then(() => 'resolved' as const),
|
|
76
|
+
// oxlint-disable-next-line avoid-new -- Promise constructor needed for setTimeout-based timeout
|
|
77
|
+
new Promise<'timeout'>((resolve) => {
|
|
78
|
+
setTimeout(() => {
|
|
79
|
+
resolve('timeout');
|
|
80
|
+
}, 50);
|
|
81
|
+
}),
|
|
82
|
+
]);
|
|
83
|
+
expect(['resolved', 'timeout']).toContain(result);
|
|
84
|
+
});
|
|
85
|
+
|
|
41
86
|
test('createMcpServer registers tools that can be listed', () => {
|
|
42
87
|
const echoTrail = trail('echo', {
|
|
43
88
|
description: 'Echo',
|
|
44
|
-
implementation: (input) => Result.ok({ reply: input.message }),
|
|
45
89
|
input: z.object({ message: z.string() }),
|
|
46
|
-
|
|
90
|
+
intent: 'read',
|
|
91
|
+
run: (input) => Result.ok({ reply: input.message }),
|
|
47
92
|
});
|
|
48
93
|
|
|
49
94
|
const app = topo('testapp', { echoTrail });
|
|
50
|
-
const tools =
|
|
95
|
+
const tools = buildTools(app);
|
|
51
96
|
const server = createMcpServer(tools, {
|
|
52
97
|
name: 'testapp',
|
|
53
98
|
version: '0.1.0',
|
|
@@ -60,19 +105,19 @@ describe('blaze', () => {
|
|
|
60
105
|
test('createMcpServer handles multiple tools', () => {
|
|
61
106
|
const echoTrail = trail('echo', {
|
|
62
107
|
description: 'Echo',
|
|
63
|
-
implementation: (input) => Result.ok({ reply: input.message }),
|
|
64
108
|
input: z.object({ message: z.string() }),
|
|
109
|
+
run: (input) => Result.ok({ reply: input.message }),
|
|
65
110
|
});
|
|
66
111
|
|
|
67
112
|
const searchTrail = trail('search', {
|
|
68
113
|
description: 'Search',
|
|
69
|
-
implementation: (input) => Result.ok({ results: [input.query] }),
|
|
70
114
|
input: z.object({ query: z.string() }),
|
|
71
|
-
|
|
115
|
+
intent: 'read',
|
|
116
|
+
run: (input) => Result.ok({ results: [input.query] }),
|
|
72
117
|
});
|
|
73
118
|
|
|
74
119
|
const app = topo('testapp', { echoTrail, searchTrail });
|
|
75
|
-
const tools =
|
|
120
|
+
const tools = buildTools(app);
|
|
76
121
|
|
|
77
122
|
expect(tools).toHaveLength(2);
|
|
78
123
|
|