@ontrails/mcp 0.2.4 → 0.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/CHANGELOG.md +30 -0
- package/README.md +22 -2
- package/package.json +8 -3
- package/src/build.ts +85 -67
- package/src/fetch-boundary.ts +102 -0
- package/src/fetch.ts +86 -0
- package/src/index.ts +22 -6
- package/src/permit-resolution.ts +102 -0
- package/src/resource-access.ts +71 -0
- package/src/resources.ts +4 -0
- package/src/server.ts +317 -0
- package/src/stdio.ts +13 -15
- package/src/surface.ts +13 -264
package/src/surface.ts
CHANGED
|
@@ -1,267 +1,22 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
6
|
-
import {
|
|
7
|
-
CallToolRequestSchema,
|
|
8
|
-
ErrorCode,
|
|
9
|
-
ListResourcesRequestSchema,
|
|
10
|
-
ListToolsRequestSchema,
|
|
11
|
-
McpError,
|
|
12
|
-
ReadResourceRequestSchema,
|
|
13
|
-
} from '@modelcontextprotocol/sdk/types.js';
|
|
14
|
-
import type {
|
|
15
|
-
BaseSurfaceOptions,
|
|
16
|
-
Layer,
|
|
17
|
-
OverlayEnvelopeLike,
|
|
18
|
-
ResourceOverrideMap,
|
|
19
|
-
Topo,
|
|
20
|
-
TrailContextInit,
|
|
21
|
-
} from '@ontrails/core';
|
|
1
|
+
/** Protocol-aware stdio entry; portable HTTP consumers use @ontrails/mcp/fetch. */
|
|
2
|
+
import type { Topo } from '@ontrails/core';
|
|
22
3
|
|
|
23
|
-
import
|
|
24
|
-
|
|
25
|
-
McpToolDefinition,
|
|
26
|
-
ResolveMcpPermit,
|
|
27
|
-
} from './build.js';
|
|
28
|
-
import { deriveMcpTools } from './build.js';
|
|
29
|
-
import { buildMcpResources } from './resources.js';
|
|
30
|
-
import type { BuiltMcpResources, McpResourcesConfig } from './resources.js';
|
|
4
|
+
import { createServerFactory } from './server.js';
|
|
5
|
+
import type { CreateServerOptions } from './server.js';
|
|
31
6
|
import { connectStdio } from './stdio.js';
|
|
32
7
|
|
|
33
|
-
// ---------------------------------------------------------------------------
|
|
34
|
-
// Options
|
|
35
|
-
// ---------------------------------------------------------------------------
|
|
36
|
-
|
|
37
|
-
export interface CreateServerOptions extends BaseSurfaceOptions {
|
|
38
|
-
readonly createContext?:
|
|
39
|
-
| (() => TrailContextInit | Promise<TrailContextInit>)
|
|
40
|
-
| undefined;
|
|
41
|
-
readonly description?: string | undefined;
|
|
42
|
-
/**
|
|
43
|
-
* App-authored overlay envelopes (the same collection compile embeds in
|
|
44
|
-
* `trails.lock`). The `surfaces` overlay's `mcp` bindings are the authored,
|
|
45
|
-
* lockable default: list bindings become grouped trailhead tools and scalar
|
|
46
|
-
* bindings become tool synonyms.
|
|
47
|
-
*/
|
|
48
|
-
readonly overlays?: readonly OverlayEnvelopeLike[] | undefined;
|
|
49
|
-
/**
|
|
50
|
-
* Call-site trailhead map. Override-in-context by design: when both this
|
|
51
|
-
* map and overlay `mcp` list bindings are present, the call-site map wins
|
|
52
|
-
* at runtime.
|
|
53
|
-
*/
|
|
54
|
-
readonly trailheads?: McpSurfaceTrailheadMap | undefined;
|
|
55
|
-
readonly layers?: readonly Layer[] | undefined;
|
|
56
|
-
readonly mcpResources?: McpResourcesConfig | false | undefined;
|
|
57
|
-
readonly name?: string | undefined;
|
|
58
|
-
readonly resources?: ResourceOverrideMap | undefined;
|
|
59
|
-
readonly resolvePermit?: ResolveMcpPermit | undefined;
|
|
60
|
-
readonly version?: string | undefined;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export interface SurfaceMcpResult {
|
|
64
|
-
readonly close: () => Promise<void>;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// ---------------------------------------------------------------------------
|
|
68
|
-
// Internal: create MCP server with tool handlers
|
|
69
|
-
// ---------------------------------------------------------------------------
|
|
70
|
-
|
|
71
8
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* When provided, `info.description` is forwarded to the MCP SDK as the
|
|
75
|
-
* server's `instructions` field — the SDK's documented channel for
|
|
76
|
-
* "optional instructions describing how to use the server and its features."
|
|
77
|
-
*/
|
|
78
|
-
const createMcpServer = (
|
|
79
|
-
tools: McpToolDefinition[],
|
|
80
|
-
info: {
|
|
81
|
-
readonly name: string;
|
|
82
|
-
readonly version: string;
|
|
83
|
-
readonly description?: string | undefined;
|
|
84
|
-
},
|
|
85
|
-
mcpResources?: BuiltMcpResources | undefined
|
|
86
|
-
): Server => {
|
|
87
|
-
const server = new Server(
|
|
88
|
-
{ name: info.name, version: info.version },
|
|
89
|
-
{
|
|
90
|
-
capabilities: {
|
|
91
|
-
...(mcpResources === undefined ? {} : { resources: {} }),
|
|
92
|
-
tools: {},
|
|
93
|
-
},
|
|
94
|
-
...(info.description === undefined
|
|
95
|
-
? {}
|
|
96
|
-
: { instructions: info.description }),
|
|
97
|
-
}
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
// Build a lookup map for tool dispatch
|
|
101
|
-
const toolMap = new Map<string, McpToolDefinition>();
|
|
102
|
-
for (const tool of tools) {
|
|
103
|
-
toolMap.set(tool.name, tool);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// Register tools/list handler
|
|
107
|
-
// oxlint-disable-next-line require-await -- MCP SDK requires async handler
|
|
108
|
-
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
109
|
-
tools: tools.map((t) => ({
|
|
110
|
-
_meta: t._meta,
|
|
111
|
-
annotations: t.annotations,
|
|
112
|
-
description: t.description,
|
|
113
|
-
inputSchema: t.inputSchema,
|
|
114
|
-
name: t.name,
|
|
115
|
-
outputSchema: t.outputSchema,
|
|
116
|
-
})),
|
|
117
|
-
}));
|
|
118
|
-
|
|
119
|
-
// Register tools/call handler
|
|
120
|
-
server.setRequestHandler(
|
|
121
|
-
CallToolRequestSchema,
|
|
122
|
-
async (request, requestExtra) => {
|
|
123
|
-
const tool = toolMap.get(request.params.name);
|
|
124
|
-
if (tool === undefined) {
|
|
125
|
-
return {
|
|
126
|
-
content: [
|
|
127
|
-
{
|
|
128
|
-
text: `Unknown tool: ${request.params.name}`,
|
|
129
|
-
type: 'text' as const,
|
|
130
|
-
},
|
|
131
|
-
],
|
|
132
|
-
isError: true,
|
|
133
|
-
} as Record<string, unknown>;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const args = (request.params.arguments ?? {}) as Record<string, unknown>;
|
|
137
|
-
const progressToken = request.params._meta?.progressToken;
|
|
138
|
-
const { authInfo } = requestExtra as {
|
|
139
|
-
readonly authInfo?:
|
|
140
|
-
| {
|
|
141
|
-
readonly accessToken?: string | undefined;
|
|
142
|
-
readonly sessionId?: string | undefined;
|
|
143
|
-
readonly token?: string | undefined;
|
|
144
|
-
}
|
|
145
|
-
| undefined;
|
|
146
|
-
};
|
|
147
|
-
const authorizationToken = authInfo?.accessToken ?? authInfo?.token;
|
|
148
|
-
|
|
149
|
-
const sendProgress =
|
|
150
|
-
progressToken === undefined
|
|
151
|
-
? undefined
|
|
152
|
-
: async (current: number, total: number) => {
|
|
153
|
-
await server.notification({
|
|
154
|
-
method: 'notifications/progress',
|
|
155
|
-
params: {
|
|
156
|
-
progress: current,
|
|
157
|
-
progressToken,
|
|
158
|
-
total,
|
|
159
|
-
},
|
|
160
|
-
});
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
const extra = {
|
|
164
|
-
abortSignal: requestExtra.signal,
|
|
165
|
-
...(authorizationToken === undefined
|
|
166
|
-
? {}
|
|
167
|
-
: { authorization: `Bearer ${authorizationToken}` }),
|
|
168
|
-
progressToken,
|
|
169
|
-
sendProgress,
|
|
170
|
-
...(authInfo?.sessionId === undefined
|
|
171
|
-
? {}
|
|
172
|
-
: { sessionId: authInfo.sessionId }),
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
const result = await tool.handler(args, extra);
|
|
176
|
-
// Spread to satisfy MCP SDK's index-signature requirement
|
|
177
|
-
return { ...result } as Record<string, unknown>;
|
|
178
|
-
}
|
|
179
|
-
);
|
|
180
|
-
|
|
181
|
-
if (mcpResources !== undefined) {
|
|
182
|
-
// oxlint-disable-next-line require-await -- MCP SDK requires async handler
|
|
183
|
-
server.setRequestHandler(ListResourcesRequestSchema, async () => ({
|
|
184
|
-
resources: mcpResources.list.map((resource) => ({
|
|
185
|
-
description: resource.description,
|
|
186
|
-
mimeType: resource.mimeType,
|
|
187
|
-
name: resource.name,
|
|
188
|
-
uri: resource.uri,
|
|
189
|
-
})),
|
|
190
|
-
}));
|
|
191
|
-
|
|
192
|
-
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
|
|
193
|
-
const content = mcpResources.read(request.params.uri);
|
|
194
|
-
if (content === undefined) {
|
|
195
|
-
throw new McpError(
|
|
196
|
-
ErrorCode.InvalidParams,
|
|
197
|
-
`Resource ${request.params.uri} not found`
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
return {
|
|
201
|
-
contents: [content],
|
|
202
|
-
};
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return server;
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
// ---------------------------------------------------------------------------
|
|
210
|
-
// createServer
|
|
211
|
-
// ---------------------------------------------------------------------------
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Build MCP tools from a topo and create an MCP server.
|
|
215
|
-
*
|
|
216
|
-
* @remarks This is a host materialization boundary. Derivation failures are
|
|
217
|
-
* thrown for server bootstrap code after `deriveMcpTools` has already
|
|
218
|
-
* represented the framework error as a Result.
|
|
219
|
-
*
|
|
9
|
+
* Build a server for custom MCP hosts.
|
|
220
10
|
* @example
|
|
221
11
|
* ```ts
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* const server = createServer(graph, { name: 'demo' });
|
|
225
|
-
* await connectStdio(server);
|
|
12
|
+
* const server = createServer(graph);
|
|
226
13
|
* ```
|
|
227
14
|
*/
|
|
228
|
-
export
|
|
229
|
-
graph: Topo,
|
|
230
|
-
options: CreateServerOptions = {}
|
|
231
|
-
): Server => {
|
|
232
|
-
const toolsResult = deriveMcpTools(graph, {
|
|
233
|
-
configValues: options.configValues,
|
|
234
|
-
createContext: options.createContext,
|
|
235
|
-
exclude: options.exclude,
|
|
236
|
-
include: options.include,
|
|
237
|
-
intent: options.intent,
|
|
238
|
-
layers: options.layers,
|
|
239
|
-
overlays: options.overlays,
|
|
240
|
-
resolvePermit: options.resolvePermit,
|
|
241
|
-
resources: options.resources,
|
|
242
|
-
trailheads: options.trailheads,
|
|
243
|
-
validate: options.validate,
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
if (toolsResult.isErr()) {
|
|
247
|
-
throw toolsResult.error;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
const mcpResources =
|
|
251
|
-
options.mcpResources === false
|
|
252
|
-
? undefined
|
|
253
|
-
: buildMcpResources(graph, toolsResult.value, options.mcpResources);
|
|
15
|
+
export { createServer, type CreateServerOptions } from './server.js';
|
|
254
16
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
description: options.description ?? graph.description,
|
|
259
|
-
name: options.name ?? graph.name,
|
|
260
|
-
version: options.version ?? graph.version ?? '0.1.0',
|
|
261
|
-
},
|
|
262
|
-
mcpResources
|
|
263
|
-
);
|
|
264
|
-
};
|
|
17
|
+
export interface SurfaceMcpResult {
|
|
18
|
+
readonly close: () => Promise<void>;
|
|
19
|
+
}
|
|
265
20
|
|
|
266
21
|
// ---------------------------------------------------------------------------
|
|
267
22
|
// surface
|
|
@@ -271,7 +26,7 @@ export const createServer = (
|
|
|
271
26
|
* Build MCP tools from a topo, create a server, and connect via stdio.
|
|
272
27
|
*
|
|
273
28
|
* @remarks Opens the MCP server on stdio. For custom transports, use
|
|
274
|
-
* `createServer(graph)` with
|
|
29
|
+
* `createServer(graph)` with your own adapter.
|
|
275
30
|
*
|
|
276
31
|
* @example
|
|
277
32
|
* ```ts
|
|
@@ -284,12 +39,6 @@ export const surface = async (
|
|
|
284
39
|
graph: Topo,
|
|
285
40
|
options: CreateServerOptions = {}
|
|
286
41
|
): Promise<SurfaceMcpResult> => {
|
|
287
|
-
const
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
return {
|
|
291
|
-
close: async () => {
|
|
292
|
-
await server.close();
|
|
293
|
-
},
|
|
294
|
-
};
|
|
42
|
+
const factory = createServerFactory(graph, options);
|
|
43
|
+
return connectStdio(() => factory());
|
|
295
44
|
};
|