@ontrails/mcp 1.0.0-beta.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/.turbo/turbo-build.log +1 -0
- package/.turbo/turbo-lint.log +3 -0
- package/.turbo/turbo-typecheck.log +1 -0
- package/CHANGELOG.md +20 -0
- package/README.md +161 -0
- package/dist/annotations.d.ts +19 -0
- package/dist/annotations.d.ts.map +1 -0
- package/dist/annotations.js +29 -0
- package/dist/annotations.js.map +1 -0
- package/dist/blaze.d.ts +36 -0
- package/dist/blaze.d.ts.map +1 -0
- package/dist/blaze.js +96 -0
- package/dist/blaze.js.map +1 -0
- package/dist/build.d.ts +40 -0
- package/dist/build.d.ts.map +1 -0
- package/dist/build.js +190 -0
- package/dist/build.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/progress.d.ts +13 -0
- package/dist/progress.d.ts.map +1 -0
- package/dist/progress.js +51 -0
- package/dist/progress.js.map +1 -0
- package/dist/stdio.d.ts +12 -0
- package/dist/stdio.d.ts.map +1 -0
- package/dist/stdio.js +15 -0
- package/dist/stdio.js.map +1 -0
- package/dist/tool-name.d.ts +15 -0
- package/dist/tool-name.d.ts.map +1 -0
- package/dist/tool-name.js +19 -0
- package/dist/tool-name.js.map +1 -0
- package/package.json +23 -0
- package/src/__tests__/annotations.test.ts +70 -0
- package/src/__tests__/blaze.test.ts +105 -0
- package/src/__tests__/build.test.ts +377 -0
- package/src/__tests__/progress.test.ts +136 -0
- package/src/__tests__/tool-name.test.ts +46 -0
- package/src/annotations.ts +51 -0
- package/src/blaze.ts +146 -0
- package/src/build.ts +321 -0
- package/src/index.ts +24 -0
- package/src/progress.ts +73 -0
- package/src/stdio.ts +17 -0
- package/src/tool-name.ts +19 -0
- package/tsconfig.json +9 -0
- package/tsconfig.tsbuildinfo +1 -0
package/src/blaze.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* blaze() -- the one-liner MCP server launcher.
|
|
3
|
+
*
|
|
4
|
+
* Three lines to expose trails as MCP tools:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* const app = topo("myapp", entity);
|
|
8
|
+
* await blaze(app);
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
13
|
+
import {
|
|
14
|
+
CallToolRequestSchema,
|
|
15
|
+
ListToolsRequestSchema,
|
|
16
|
+
} from '@modelcontextprotocol/sdk/types.js';
|
|
17
|
+
import type { Layer, Topo, TrailContext } from '@ontrails/core';
|
|
18
|
+
|
|
19
|
+
import type { McpToolDefinition } from './build.js';
|
|
20
|
+
import { buildMcpTools } from './build.js';
|
|
21
|
+
import { connectStdio } from './stdio.js';
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// Options
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
export interface BlazeMcpOptions {
|
|
28
|
+
readonly createContext?:
|
|
29
|
+
| (() => TrailContext | Promise<TrailContext>)
|
|
30
|
+
| undefined;
|
|
31
|
+
readonly excludeTrails?: readonly string[] | undefined;
|
|
32
|
+
readonly includeTrails?: readonly string[] | undefined;
|
|
33
|
+
readonly layers?: readonly Layer[] | undefined;
|
|
34
|
+
readonly serverInfo?:
|
|
35
|
+
| {
|
|
36
|
+
readonly name?: string | undefined;
|
|
37
|
+
readonly version?: string | undefined;
|
|
38
|
+
}
|
|
39
|
+
| undefined;
|
|
40
|
+
readonly transport?: 'stdio' | undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
// Internal: create MCP server with tool handlers
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Create an MCP Server instance and register all tools.
|
|
49
|
+
*/
|
|
50
|
+
export const createMcpServer = (
|
|
51
|
+
tools: McpToolDefinition[],
|
|
52
|
+
info: { readonly name: string; readonly version: string }
|
|
53
|
+
): Server => {
|
|
54
|
+
const server = new Server(
|
|
55
|
+
{ name: info.name, version: info.version },
|
|
56
|
+
{ capabilities: { tools: {} } }
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
// Build a lookup map for tool dispatch
|
|
60
|
+
const toolMap = new Map<string, McpToolDefinition>();
|
|
61
|
+
for (const tool of tools) {
|
|
62
|
+
toolMap.set(tool.name, tool);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Register tools/list handler
|
|
66
|
+
// oxlint-disable-next-line require-await -- MCP SDK requires async handler
|
|
67
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
68
|
+
tools: tools.map((t) => ({
|
|
69
|
+
annotations: t.annotations,
|
|
70
|
+
description: t.description,
|
|
71
|
+
inputSchema: t.inputSchema,
|
|
72
|
+
name: t.name,
|
|
73
|
+
})),
|
|
74
|
+
}));
|
|
75
|
+
|
|
76
|
+
// Register tools/call handler
|
|
77
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
78
|
+
const tool = toolMap.get(request.params.name);
|
|
79
|
+
if (tool === undefined) {
|
|
80
|
+
return {
|
|
81
|
+
content: [
|
|
82
|
+
{
|
|
83
|
+
text: `Unknown tool: ${request.params.name}`,
|
|
84
|
+
type: 'text' as const,
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
isError: true,
|
|
88
|
+
} as Record<string, unknown>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const args = (request.params.arguments ?? {}) as Record<string, unknown>;
|
|
92
|
+
const progressToken = request.params._meta?.progressToken;
|
|
93
|
+
|
|
94
|
+
const sendProgress =
|
|
95
|
+
progressToken === undefined
|
|
96
|
+
? undefined
|
|
97
|
+
: async (current: number, total: number) => {
|
|
98
|
+
await server.notification({
|
|
99
|
+
method: 'notifications/progress',
|
|
100
|
+
params: {
|
|
101
|
+
progress: current,
|
|
102
|
+
progressToken: progressToken,
|
|
103
|
+
total,
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const extra = {
|
|
109
|
+
progressToken,
|
|
110
|
+
sendProgress,
|
|
111
|
+
signal: undefined as AbortSignal | undefined,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const result = await tool.handler(args, extra);
|
|
115
|
+
// Spread to satisfy MCP SDK's index-signature requirement
|
|
116
|
+
return { ...result } as Record<string, unknown>;
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
return server;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// ---------------------------------------------------------------------------
|
|
123
|
+
// blaze
|
|
124
|
+
// ---------------------------------------------------------------------------
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Build MCP tools from an App, create a server, and connect via stdio.
|
|
128
|
+
*/
|
|
129
|
+
export const blaze = async (
|
|
130
|
+
app: Topo,
|
|
131
|
+
options: BlazeMcpOptions = {}
|
|
132
|
+
): Promise<void> => {
|
|
133
|
+
const tools = buildMcpTools(app, {
|
|
134
|
+
createContext: options.createContext,
|
|
135
|
+
excludeTrails: options.excludeTrails,
|
|
136
|
+
includeTrails: options.includeTrails,
|
|
137
|
+
layers: options.layers,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const server = createMcpServer(tools, {
|
|
141
|
+
name: options.serverInfo?.name ?? app.name,
|
|
142
|
+
version: options.serverInfo?.version ?? '0.1.0',
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
await connectStdio(server);
|
|
146
|
+
};
|
package/src/build.ts
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build MCP tool definitions from a Trails App.
|
|
3
|
+
*
|
|
4
|
+
* Iterates the topo, generates McpToolDefinition[] with handlers that
|
|
5
|
+
* validate input, compose layers, execute the implementation, and map
|
|
6
|
+
* Results to MCP responses.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
composeLayers,
|
|
11
|
+
createTrailContext,
|
|
12
|
+
validateInput,
|
|
13
|
+
zodToJsonSchema,
|
|
14
|
+
} from '@ontrails/core';
|
|
15
|
+
import type { Layer, Topo, Trail, TrailContext } from '@ontrails/core';
|
|
16
|
+
|
|
17
|
+
import type { McpAnnotations } from './annotations.js';
|
|
18
|
+
import { deriveAnnotations } from './annotations.js';
|
|
19
|
+
import { createMcpProgressCallback } from './progress.js';
|
|
20
|
+
import { deriveToolName } from './tool-name.js';
|
|
21
|
+
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Public types
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
export interface BuildMcpToolsOptions {
|
|
27
|
+
readonly createContext?:
|
|
28
|
+
| (() => TrailContext | Promise<TrailContext>)
|
|
29
|
+
| undefined;
|
|
30
|
+
readonly excludeTrails?: readonly string[] | undefined;
|
|
31
|
+
readonly includeTrails?: readonly string[] | undefined;
|
|
32
|
+
readonly layers?: readonly Layer[] | undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface McpToolDefinition {
|
|
36
|
+
readonly annotations: McpAnnotations | undefined;
|
|
37
|
+
readonly description: string | undefined;
|
|
38
|
+
readonly handler: (
|
|
39
|
+
args: Record<string, unknown>,
|
|
40
|
+
extra: McpExtra
|
|
41
|
+
) => Promise<McpToolResult>;
|
|
42
|
+
readonly inputSchema: Record<string, unknown>;
|
|
43
|
+
readonly name: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface McpExtra {
|
|
47
|
+
readonly progressToken?: string | number | undefined;
|
|
48
|
+
readonly sendProgress?:
|
|
49
|
+
| ((current: number, total: number) => Promise<void>)
|
|
50
|
+
| undefined;
|
|
51
|
+
readonly signal?: AbortSignal | undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface McpToolResult {
|
|
55
|
+
readonly content: readonly McpContent[];
|
|
56
|
+
readonly isError?: boolean | undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface McpContent {
|
|
60
|
+
readonly data?: string | undefined;
|
|
61
|
+
readonly mimeType?: string | undefined;
|
|
62
|
+
readonly text?: string | undefined;
|
|
63
|
+
readonly type: 'text' | 'image' | 'resource';
|
|
64
|
+
readonly uri?: string | undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
// Internal helpers (defined before use)
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
|
|
71
|
+
interface BlobRef {
|
|
72
|
+
readonly data: Uint8Array;
|
|
73
|
+
readonly kind: 'blob';
|
|
74
|
+
readonly mimeType: string;
|
|
75
|
+
readonly name?: string | undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const isBlobRef = (value: unknown): value is BlobRef => {
|
|
79
|
+
if (typeof value !== 'object' || value === null) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
const obj = value as Record<string, unknown>;
|
|
83
|
+
return (
|
|
84
|
+
obj['kind'] === 'blob' &&
|
|
85
|
+
obj['data'] instanceof Uint8Array &&
|
|
86
|
+
typeof obj['mimeType'] === 'string'
|
|
87
|
+
);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const uint8ArrayToBase64 = (bytes: Uint8Array): string => {
|
|
91
|
+
// Use btoa with manual conversion for runtime-agnostic base64
|
|
92
|
+
let binary = '';
|
|
93
|
+
for (const byte of bytes) {
|
|
94
|
+
binary += String.fromCodePoint(byte);
|
|
95
|
+
}
|
|
96
|
+
return btoa(binary);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const blobToContent = (blob: BlobRef): McpContent => {
|
|
100
|
+
if (blob.mimeType.startsWith('image/')) {
|
|
101
|
+
return {
|
|
102
|
+
data: uint8ArrayToBase64(blob.data),
|
|
103
|
+
mimeType: blob.mimeType,
|
|
104
|
+
type: 'image',
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
mimeType: blob.mimeType,
|
|
110
|
+
type: 'resource',
|
|
111
|
+
uri: `blob://${blob.name ?? 'unnamed'}`,
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/** Separate blob fields from non-blob fields in an object. */
|
|
116
|
+
const separateBlobFields = (
|
|
117
|
+
obj: Record<string, unknown>
|
|
118
|
+
): {
|
|
119
|
+
blobContents: McpContent[];
|
|
120
|
+
hasBlobFields: boolean;
|
|
121
|
+
textFields: Record<string, unknown>;
|
|
122
|
+
} => {
|
|
123
|
+
const blobContents: McpContent[] = [];
|
|
124
|
+
const textFields: Record<string, unknown> = {};
|
|
125
|
+
let hasBlobFields = false;
|
|
126
|
+
for (const [key, val] of Object.entries(obj)) {
|
|
127
|
+
if (isBlobRef(val)) {
|
|
128
|
+
hasBlobFields = true;
|
|
129
|
+
blobContents.push(blobToContent(val));
|
|
130
|
+
} else {
|
|
131
|
+
textFields[key] = val;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return { blobContents, hasBlobFields, textFields };
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/** Serialize a mixed blob/text object to MCP content. */
|
|
138
|
+
const serializeMixedObject = (
|
|
139
|
+
obj: Record<string, unknown>
|
|
140
|
+
): readonly McpContent[] | undefined => {
|
|
141
|
+
const { blobContents, hasBlobFields, textFields } = separateBlobFields(obj);
|
|
142
|
+
if (!hasBlobFields) {
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
if (Object.keys(textFields).length > 0) {
|
|
146
|
+
blobContents.unshift({ text: JSON.stringify(textFields), type: 'text' });
|
|
147
|
+
}
|
|
148
|
+
return blobContents;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const serializeOutput = (value: unknown): readonly McpContent[] => {
|
|
152
|
+
if (isBlobRef(value)) {
|
|
153
|
+
return [blobToContent(value)];
|
|
154
|
+
}
|
|
155
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
156
|
+
const mixed = serializeMixedObject(value as Record<string, unknown>);
|
|
157
|
+
if (mixed) {
|
|
158
|
+
return mixed;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return [{ text: JSON.stringify(value), type: 'text' }];
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// ---------------------------------------------------------------------------
|
|
165
|
+
// Handler factory
|
|
166
|
+
// ---------------------------------------------------------------------------
|
|
167
|
+
|
|
168
|
+
/** Create an error result for MCP responses. */
|
|
169
|
+
const mcpError = (message: string): McpToolResult => ({
|
|
170
|
+
content: [{ text: message, type: 'text' }],
|
|
171
|
+
isError: true,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
/** Build a TrailContext from options and MCP extra. */
|
|
175
|
+
const buildTrailContext = async (
|
|
176
|
+
options: BuildMcpToolsOptions,
|
|
177
|
+
extra: McpExtra
|
|
178
|
+
): Promise<TrailContext> => {
|
|
179
|
+
const baseContext =
|
|
180
|
+
options.createContext !== undefined && options.createContext !== null
|
|
181
|
+
? await options.createContext()
|
|
182
|
+
: createTrailContext();
|
|
183
|
+
|
|
184
|
+
const signal = extra.signal ?? baseContext.signal;
|
|
185
|
+
const progressCb = createMcpProgressCallback(extra);
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
...baseContext,
|
|
189
|
+
signal,
|
|
190
|
+
...(progressCb === undefined ? {} : { progress: progressCb }),
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/** Execute a trail and map the result to an MCP response. */
|
|
195
|
+
const executeAndMap = async (
|
|
196
|
+
trail: Trail<unknown, unknown>,
|
|
197
|
+
validatedInput: unknown,
|
|
198
|
+
ctx: TrailContext,
|
|
199
|
+
layers: readonly Layer[]
|
|
200
|
+
): Promise<McpToolResult> => {
|
|
201
|
+
const impl = composeLayers([...layers], trail, trail.implementation);
|
|
202
|
+
try {
|
|
203
|
+
const result = await impl(validatedInput, ctx);
|
|
204
|
+
if (result.isOk()) {
|
|
205
|
+
return { content: serializeOutput(result.value) };
|
|
206
|
+
}
|
|
207
|
+
return mcpError(result.error.message);
|
|
208
|
+
} catch (error: unknown) {
|
|
209
|
+
return mcpError(error instanceof Error ? error.message : String(error));
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const createHandler =
|
|
214
|
+
(
|
|
215
|
+
trail: Trail<unknown, unknown>,
|
|
216
|
+
layers: readonly Layer[],
|
|
217
|
+
options: BuildMcpToolsOptions
|
|
218
|
+
): ((
|
|
219
|
+
args: Record<string, unknown>,
|
|
220
|
+
extra: McpExtra
|
|
221
|
+
) => Promise<McpToolResult>) =>
|
|
222
|
+
async (args, extra): Promise<McpToolResult> => {
|
|
223
|
+
const validated = validateInput(trail.input, args);
|
|
224
|
+
if (validated.isErr()) {
|
|
225
|
+
return mcpError(validated.error.message);
|
|
226
|
+
}
|
|
227
|
+
const ctx = await buildTrailContext(options, extra);
|
|
228
|
+
return executeAndMap(trail, validated.value, ctx, layers);
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
// ---------------------------------------------------------------------------
|
|
232
|
+
// Builder
|
|
233
|
+
// ---------------------------------------------------------------------------
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Build MCP tool definitions from an App's topology.
|
|
237
|
+
*
|
|
238
|
+
* Each trail in the topo becomes an McpToolDefinition with:
|
|
239
|
+
* - A derived tool name (app-prefixed, underscore-delimited)
|
|
240
|
+
* - JSON Schema input from zodToJsonSchema
|
|
241
|
+
* - MCP annotations from trail markers
|
|
242
|
+
* - A handler that validates, composes layers, executes, and maps results
|
|
243
|
+
*/
|
|
244
|
+
/** Check if a trail should be included based on markers and filters. */
|
|
245
|
+
const shouldInclude = (
|
|
246
|
+
trail: Trail<unknown, unknown>,
|
|
247
|
+
options: BuildMcpToolsOptions
|
|
248
|
+
): boolean => {
|
|
249
|
+
if (trail.markers?.['internal'] === true) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
if (options.includeTrails !== undefined && options.includeTrails.length > 0) {
|
|
253
|
+
return options.includeTrails.includes(trail.id);
|
|
254
|
+
}
|
|
255
|
+
if (
|
|
256
|
+
options.excludeTrails !== undefined &&
|
|
257
|
+
options.excludeTrails.includes(trail.id)
|
|
258
|
+
) {
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
return true;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
/** Build a description with optional example input appended. */
|
|
265
|
+
const buildDescription = (
|
|
266
|
+
trail: Trail<unknown, unknown>
|
|
267
|
+
): string | undefined => {
|
|
268
|
+
let { description } = trail;
|
|
269
|
+
if (
|
|
270
|
+
description !== undefined &&
|
|
271
|
+
trail.examples !== undefined &&
|
|
272
|
+
trail.examples.length > 0
|
|
273
|
+
) {
|
|
274
|
+
const [firstExample] = trail.examples;
|
|
275
|
+
if (firstExample !== undefined) {
|
|
276
|
+
description = `${description}\n\nExample input: ${JSON.stringify(firstExample.input)}`;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return description;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
/** Build a single MCP tool definition from a trail. */
|
|
283
|
+
const buildToolDefinition = (
|
|
284
|
+
app: Topo,
|
|
285
|
+
trail: Trail<unknown, unknown>,
|
|
286
|
+
layers: readonly Layer[],
|
|
287
|
+
options: BuildMcpToolsOptions
|
|
288
|
+
): McpToolDefinition => {
|
|
289
|
+
const rawAnnotations = deriveAnnotations(trail);
|
|
290
|
+
const annotations =
|
|
291
|
+
Object.keys(rawAnnotations).length > 0 ? rawAnnotations : undefined;
|
|
292
|
+
return {
|
|
293
|
+
annotations,
|
|
294
|
+
description: buildDescription(trail),
|
|
295
|
+
handler: createHandler(trail, layers, options),
|
|
296
|
+
inputSchema: zodToJsonSchema(trail.input),
|
|
297
|
+
name: deriveToolName(app.name, trail.id),
|
|
298
|
+
};
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
export const buildMcpTools = (
|
|
302
|
+
app: Topo,
|
|
303
|
+
options: BuildMcpToolsOptions = {}
|
|
304
|
+
): McpToolDefinition[] => {
|
|
305
|
+
const layers = options.layers ?? [];
|
|
306
|
+
const tools: McpToolDefinition[] = [];
|
|
307
|
+
|
|
308
|
+
for (const item of app.list()) {
|
|
309
|
+
if (item.kind !== 'trail' && item.kind !== 'hike') {
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
if (!shouldInclude(item as Trail<unknown, unknown>, options)) {
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
tools.push(
|
|
316
|
+
buildToolDefinition(app, item as Trail<unknown, unknown>, layers, options)
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return tools;
|
|
321
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Build
|
|
2
|
+
export {
|
|
3
|
+
buildMcpTools,
|
|
4
|
+
type BuildMcpToolsOptions,
|
|
5
|
+
type McpToolDefinition,
|
|
6
|
+
type McpToolResult,
|
|
7
|
+
type McpContent,
|
|
8
|
+
type McpExtra,
|
|
9
|
+
} from './build.js';
|
|
10
|
+
|
|
11
|
+
// Tool naming
|
|
12
|
+
export { deriveToolName } from './tool-name.js';
|
|
13
|
+
|
|
14
|
+
// Annotations
|
|
15
|
+
export { deriveAnnotations, type McpAnnotations } from './annotations.js';
|
|
16
|
+
|
|
17
|
+
// Progress
|
|
18
|
+
export { createMcpProgressCallback } from './progress.js';
|
|
19
|
+
|
|
20
|
+
// Blaze
|
|
21
|
+
export { blaze, type BlazeMcpOptions } from './blaze.js';
|
|
22
|
+
|
|
23
|
+
// Transport
|
|
24
|
+
export { connectStdio } from './stdio.js';
|
package/src/progress.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridge Trails ProgressCallback to MCP sendProgress notifications.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { ProgressCallback, ProgressEvent } from '@ontrails/core';
|
|
6
|
+
|
|
7
|
+
import type { McpExtra } from './build.js';
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Event handlers
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
type SendFn = (current: number, total: number) => Promise<void>;
|
|
14
|
+
|
|
15
|
+
/** Fire-and-forget a progress send, swallowing transport errors. */
|
|
16
|
+
const fireSend = async (
|
|
17
|
+
send: SendFn,
|
|
18
|
+
current: number,
|
|
19
|
+
total: number
|
|
20
|
+
): Promise<void> => {
|
|
21
|
+
try {
|
|
22
|
+
await send(current, total);
|
|
23
|
+
} catch {
|
|
24
|
+
/* Transport errors are expected and safe to ignore */
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const handleProgress = (event: ProgressEvent, send: SendFn): void => {
|
|
29
|
+
if (event.current !== undefined && event.total !== undefined) {
|
|
30
|
+
fireSend(send, event.current, event.total);
|
|
31
|
+
} else if (event.current !== undefined) {
|
|
32
|
+
fireSend(send, event.current, 0);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const progressHandlers: Record<
|
|
37
|
+
string,
|
|
38
|
+
(event: ProgressEvent, send: SendFn) => void
|
|
39
|
+
> = {
|
|
40
|
+
complete: (_event, send) => fireSend(send, 1, 1),
|
|
41
|
+
error: () => {
|
|
42
|
+
/* No progress notification for errors */
|
|
43
|
+
},
|
|
44
|
+
progress: handleProgress,
|
|
45
|
+
start: (_event, send) => fireSend(send, 0, 1),
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
// Factory
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Create a ProgressCallback that bridges to MCP's sendProgress.
|
|
54
|
+
*
|
|
55
|
+
* Returns `undefined` if the MCP client did not provide a progressToken
|
|
56
|
+
* (meaning no progress reporting was requested).
|
|
57
|
+
*/
|
|
58
|
+
export const createMcpProgressCallback = (
|
|
59
|
+
extra: McpExtra
|
|
60
|
+
): ProgressCallback | undefined => {
|
|
61
|
+
if (extra.progressToken === undefined || extra.progressToken === null) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
if (typeof extra.sendProgress !== 'function') {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const send = extra.sendProgress;
|
|
69
|
+
return (event: ProgressEvent): void => {
|
|
70
|
+
const handler = progressHandlers[event.type];
|
|
71
|
+
handler?.(event, send);
|
|
72
|
+
};
|
|
73
|
+
};
|
package/src/stdio.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thin wrapper around MCP SDK's StdioServerTransport.
|
|
3
|
+
*
|
|
4
|
+
* Exists as a separate function so it can be swapped for other transports
|
|
5
|
+
* (SSE, streamable HTTP) without changing blaze().
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
9
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Connect an MCP server to stdio transport.
|
|
13
|
+
*/
|
|
14
|
+
export const connectStdio = async (server: Server): Promise<void> => {
|
|
15
|
+
const transport = new StdioServerTransport();
|
|
16
|
+
await server.connect(transport);
|
|
17
|
+
};
|
package/src/tool-name.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Derive MCP-safe tool names from app name + trail ID.
|
|
3
|
+
*
|
|
4
|
+
* MCP tool names must be [a-z0-9_]+. We prefix with the app name,
|
|
5
|
+
* replace dots and hyphens with underscores, and lowercase everything.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Convert app name + trail ID to an MCP-safe tool name.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* deriveToolName("myapp", "entity.show") // "myapp_entity_show"
|
|
13
|
+
* deriveToolName("dispatch", "patch.search") // "dispatch_patch_search"
|
|
14
|
+
*/
|
|
15
|
+
export const deriveToolName = (appName: string, trailId: string): string => {
|
|
16
|
+
const prefix = appName.toLowerCase().replaceAll(/[.-]/g, '_');
|
|
17
|
+
const suffix = trailId.toLowerCase().replaceAll(/[.-]/g, '_');
|
|
18
|
+
return `${prefix}_${suffix}`;
|
|
19
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["./src/annotations.ts","./src/blaze.ts","./src/build.ts","./src/index.ts","./src/progress.ts","./src/stdio.ts","./src/tool-name.ts"],"version":"5.9.3"}
|