@ontrails/mcp 0.2.4 → 0.4.1
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 +40 -0
- package/README.md +23 -3
- package/package.json +8 -3
- package/src/build.ts +208 -211
- package/src/fetch-boundary.ts +102 -0
- package/src/fetch.ts +86 -0
- package/src/index.ts +22 -6
- package/src/output-rendering.ts +67 -0
- package/src/permit-resolution.ts +102 -0
- package/src/resource-access.ts +71 -0
- package/src/resources.ts +14 -9
- package/src/server.ts +336 -0
- package/src/stdio.ts +13 -15
- package/src/surface.ts +13 -264
package/src/server.ts
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Surface helpers for exposing a topo over MCP.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Server } from '@modelcontextprotocol/server';
|
|
6
|
+
import type { ServerContext } from '@modelcontextprotocol/server';
|
|
7
|
+
import { NotFoundError, PermissionError, Result } from '@ontrails/core';
|
|
8
|
+
import type {
|
|
9
|
+
BaseSurfaceOptions,
|
|
10
|
+
Layer,
|
|
11
|
+
OverlayEnvelopeLike,
|
|
12
|
+
ResourceOverrideMap,
|
|
13
|
+
Topo,
|
|
14
|
+
TrailContextInit,
|
|
15
|
+
} from '@ontrails/core';
|
|
16
|
+
|
|
17
|
+
import type {
|
|
18
|
+
McpSurfaceTrailheadMap,
|
|
19
|
+
McpExtra,
|
|
20
|
+
ResolveMcpPermit,
|
|
21
|
+
McpCredentialPosture,
|
|
22
|
+
} from './build.js';
|
|
23
|
+
import { deriveMcpToolRecords } from './build.js';
|
|
24
|
+
import type { McpToolRecord } from './output-rendering.js';
|
|
25
|
+
import {
|
|
26
|
+
resolveMcpResourcePermit,
|
|
27
|
+
runMcpResourceRequest,
|
|
28
|
+
} from './resource-access.js';
|
|
29
|
+
import type { AuthorizeMcpResourceRead } from './resource-access.js';
|
|
30
|
+
import { buildMcpResources } from './resources.js';
|
|
31
|
+
import type { BuiltMcpResources, McpResourcesConfig } from './resources.js';
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Options
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
export interface CreateServerOptions extends BaseSurfaceOptions {
|
|
38
|
+
/** Optional read policy; permission denials also omit URIs from resource lists. */
|
|
39
|
+
readonly authorizeResourceRead?: AuthorizeMcpResourceRead | undefined;
|
|
40
|
+
readonly createContext?:
|
|
41
|
+
| (() => TrailContextInit | Promise<TrailContextInit>)
|
|
42
|
+
| undefined;
|
|
43
|
+
readonly description?: string | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* App-authored overlay envelopes (the same collection compile embeds in
|
|
46
|
+
* `trails.lock`). The `surfaces` overlay's `mcp` bindings are the authored,
|
|
47
|
+
* lockable default: list bindings become grouped trailhead tools and scalar
|
|
48
|
+
* bindings become tool synonyms.
|
|
49
|
+
*/
|
|
50
|
+
readonly overlays?: readonly OverlayEnvelopeLike[] | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Call-site trailhead map. Override-in-context by design: when both this
|
|
53
|
+
* map and overlay `mcp` list bindings are present, the call-site map wins
|
|
54
|
+
* at runtime.
|
|
55
|
+
*/
|
|
56
|
+
readonly trailheads?: McpSurfaceTrailheadMap | undefined;
|
|
57
|
+
readonly layers?: readonly Layer[] | undefined;
|
|
58
|
+
readonly mcpResources?: McpResourcesConfig | false | undefined;
|
|
59
|
+
readonly name?: string | undefined;
|
|
60
|
+
readonly resources?: ResourceOverrideMap | undefined;
|
|
61
|
+
readonly resolvePermit?: ResolveMcpPermit | undefined;
|
|
62
|
+
/** See `DeriveMcpToolsOptions.credentialPosture`; the fetch entry forces `network`. */
|
|
63
|
+
readonly credentialPosture?: McpCredentialPosture | undefined;
|
|
64
|
+
readonly version?: string | undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
// Internal: create MCP server with tool handlers
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
|
|
71
|
+
const requestCredentials = (
|
|
72
|
+
context: ServerContext,
|
|
73
|
+
credentials?: Pick<McpExtra, 'authorization'>
|
|
74
|
+
): Pick<McpExtra, '_meta' | 'authorization'> => {
|
|
75
|
+
const token = context.http?.authInfo?.token;
|
|
76
|
+
const authorization =
|
|
77
|
+
credentials?.authorization ??
|
|
78
|
+
(token === undefined ? undefined : `Bearer ${token}`);
|
|
79
|
+
return {
|
|
80
|
+
_meta: context.mcpReq._meta,
|
|
81
|
+
...(authorization === undefined ? {} : { authorization }),
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Create an MCP Server instance and register all tools.
|
|
87
|
+
*
|
|
88
|
+
* When provided, `info.description` is forwarded to the MCP SDK as the
|
|
89
|
+
* server's `instructions` field — the SDK's documented channel for
|
|
90
|
+
* "optional instructions describing how to use the server and its features."
|
|
91
|
+
*/
|
|
92
|
+
const createMcpServer = (
|
|
93
|
+
tools: readonly McpToolRecord[],
|
|
94
|
+
info: {
|
|
95
|
+
readonly name: string;
|
|
96
|
+
readonly version: string;
|
|
97
|
+
readonly description?: string | undefined;
|
|
98
|
+
},
|
|
99
|
+
mcpResources:
|
|
100
|
+
| Readonly<Record<keyof McpToolRecord, BuiltMcpResources>>
|
|
101
|
+
| undefined,
|
|
102
|
+
options: CreateServerOptions,
|
|
103
|
+
credentials?: Pick<McpExtra, 'authorization'>
|
|
104
|
+
): Server => {
|
|
105
|
+
const server = new Server(
|
|
106
|
+
{ name: info.name, version: info.version },
|
|
107
|
+
{
|
|
108
|
+
capabilities: {
|
|
109
|
+
...(mcpResources === undefined ? {} : { resources: {} }),
|
|
110
|
+
tools: {},
|
|
111
|
+
},
|
|
112
|
+
...(info.description === undefined
|
|
113
|
+
? {}
|
|
114
|
+
: { instructions: info.description }),
|
|
115
|
+
}
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
// Build a lookup map for tool dispatch
|
|
119
|
+
const toolMap = new Map(tools.map((record) => [record.legacy.name, record]));
|
|
120
|
+
const protocolView = (): keyof McpToolRecord =>
|
|
121
|
+
server.getNegotiatedProtocolVersion() === '2026-07-28'
|
|
122
|
+
? 'modern'
|
|
123
|
+
: 'legacy';
|
|
124
|
+
|
|
125
|
+
// Register tools/list handler
|
|
126
|
+
// oxlint-disable-next-line require-await -- MCP SDK requires async handler
|
|
127
|
+
server.setRequestHandler('tools/list', async () => {
|
|
128
|
+
const view = protocolView();
|
|
129
|
+
return {
|
|
130
|
+
tools: tools.map((record) => {
|
|
131
|
+
const tool = record[view];
|
|
132
|
+
return {
|
|
133
|
+
_meta: tool._meta,
|
|
134
|
+
annotations: tool.annotations,
|
|
135
|
+
description: tool.description,
|
|
136
|
+
inputSchema: { ...tool.inputSchema, type: 'object' as const },
|
|
137
|
+
name: tool.name,
|
|
138
|
+
outputSchema: tool.outputSchema,
|
|
139
|
+
};
|
|
140
|
+
}),
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// Register tools/call handler
|
|
145
|
+
server.setRequestHandler('tools/call', async (request, requestExtra) => {
|
|
146
|
+
const record = toolMap.get(request.params.name);
|
|
147
|
+
if (record === undefined) {
|
|
148
|
+
return {
|
|
149
|
+
content: [
|
|
150
|
+
{
|
|
151
|
+
text: `Unknown tool: ${request.params.name}`,
|
|
152
|
+
type: 'text' as const,
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
isError: true,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const tool = record[protocolView()];
|
|
160
|
+
const args = (request.params.arguments ?? {}) as Record<string, unknown>;
|
|
161
|
+
const progressToken = requestExtra.mcpReq._meta?.progressToken;
|
|
162
|
+
|
|
163
|
+
const sendProgress =
|
|
164
|
+
progressToken === undefined
|
|
165
|
+
? undefined
|
|
166
|
+
: async (current: number, total: number) => {
|
|
167
|
+
await requestExtra.mcpReq.notify({
|
|
168
|
+
method: 'notifications/progress',
|
|
169
|
+
params: { progress: current, progressToken, total },
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const extra = {
|
|
174
|
+
...requestCredentials(requestExtra, credentials),
|
|
175
|
+
abortSignal: requestExtra.mcpReq.signal,
|
|
176
|
+
progressToken,
|
|
177
|
+
sendProgress,
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const result = await tool.handler(args, extra);
|
|
181
|
+
// Copy the readonly content list into the SDK result shape.
|
|
182
|
+
return server.projectCallToolResult(
|
|
183
|
+
{ ...result, content: [...result.content] },
|
|
184
|
+
tool.outputSchema
|
|
185
|
+
);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
if (mcpResources !== undefined) {
|
|
189
|
+
server.setRequestHandler('resources/list', (_request, requestExtra) =>
|
|
190
|
+
runMcpResourceRequest(undefined, async () => {
|
|
191
|
+
const permit = await resolveMcpResourcePermit(
|
|
192
|
+
options,
|
|
193
|
+
requestCredentials(requestExtra, credentials)
|
|
194
|
+
);
|
|
195
|
+
if (permit.isErr()) {
|
|
196
|
+
return permit;
|
|
197
|
+
}
|
|
198
|
+
const resources = [];
|
|
199
|
+
for (const resource of mcpResources[protocolView()].list) {
|
|
200
|
+
if (options.authorizeResourceRead !== undefined) {
|
|
201
|
+
const allowed = await options.authorizeResourceRead({
|
|
202
|
+
permit: permit.value,
|
|
203
|
+
resource,
|
|
204
|
+
});
|
|
205
|
+
if (allowed.isErr()) {
|
|
206
|
+
if (allowed.error instanceof PermissionError) {
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
return allowed;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
resources.push({
|
|
213
|
+
description: resource.description,
|
|
214
|
+
mimeType: resource.mimeType,
|
|
215
|
+
name: resource.name,
|
|
216
|
+
uri: resource.uri,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
return Result.ok({ resources });
|
|
220
|
+
})
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
server.setRequestHandler('resources/read', (request, requestExtra) =>
|
|
224
|
+
runMcpResourceRequest('resources/read', async () => {
|
|
225
|
+
const permit = await resolveMcpResourcePermit(
|
|
226
|
+
options,
|
|
227
|
+
requestCredentials(requestExtra, credentials)
|
|
228
|
+
);
|
|
229
|
+
if (permit.isErr()) {
|
|
230
|
+
return permit;
|
|
231
|
+
}
|
|
232
|
+
const rendering = mcpResources[protocolView()];
|
|
233
|
+
const resource = rendering.list.find(
|
|
234
|
+
(entry) => entry.uri === request.params.uri
|
|
235
|
+
);
|
|
236
|
+
if (resource === undefined) {
|
|
237
|
+
return Result.err(
|
|
238
|
+
new NotFoundError(`Resource ${request.params.uri} not found`)
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
if (options.authorizeResourceRead !== undefined) {
|
|
242
|
+
const allowed = await options.authorizeResourceRead({
|
|
243
|
+
permit: permit.value,
|
|
244
|
+
resource,
|
|
245
|
+
});
|
|
246
|
+
if (allowed.isErr()) {
|
|
247
|
+
return allowed;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
const content = rendering.read(resource.uri);
|
|
251
|
+
if (content === undefined) {
|
|
252
|
+
return Result.err(
|
|
253
|
+
new NotFoundError(`Resource ${resource.uri} not found`)
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
return Result.ok({ contents: [content] });
|
|
257
|
+
})
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return server;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
// ---------------------------------------------------------------------------
|
|
265
|
+
// createServer
|
|
266
|
+
// ---------------------------------------------------------------------------
|
|
267
|
+
|
|
268
|
+
/** Build fresh portable server instances from one derived surface. @internal */
|
|
269
|
+
export const createServerFactory = (
|
|
270
|
+
graph: Topo,
|
|
271
|
+
options: CreateServerOptions = {}
|
|
272
|
+
): ((credentials?: Pick<McpExtra, 'authorization'>) => Server) => {
|
|
273
|
+
const toolsResult = deriveMcpToolRecords(graph, {
|
|
274
|
+
configValues: options.configValues,
|
|
275
|
+
createContext: options.createContext,
|
|
276
|
+
credentialPosture: options.credentialPosture,
|
|
277
|
+
exclude: options.exclude,
|
|
278
|
+
include: options.include,
|
|
279
|
+
intent: options.intent,
|
|
280
|
+
layers: options.layers,
|
|
281
|
+
overlays: options.overlays,
|
|
282
|
+
resolvePermit: options.resolvePermit,
|
|
283
|
+
resources: options.resources,
|
|
284
|
+
trailheads: options.trailheads,
|
|
285
|
+
validate: options.validate,
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
if (toolsResult.isErr()) {
|
|
289
|
+
throw toolsResult.error;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const mcpResources =
|
|
293
|
+
options.mcpResources === false
|
|
294
|
+
? undefined
|
|
295
|
+
: {
|
|
296
|
+
legacy: buildMcpResources(
|
|
297
|
+
graph,
|
|
298
|
+
toolsResult.value.map(({ legacy }) => legacy),
|
|
299
|
+
options.mcpResources
|
|
300
|
+
),
|
|
301
|
+
modern: buildMcpResources(
|
|
302
|
+
graph,
|
|
303
|
+
toolsResult.value.map(({ modern }) => modern),
|
|
304
|
+
options.mcpResources
|
|
305
|
+
),
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
return (credentials) =>
|
|
309
|
+
createMcpServer(
|
|
310
|
+
toolsResult.value,
|
|
311
|
+
{
|
|
312
|
+
description: options.description ?? graph.description,
|
|
313
|
+
name: options.name ?? graph.name,
|
|
314
|
+
version: options.version ?? graph.version ?? '0.1.0',
|
|
315
|
+
},
|
|
316
|
+
mcpResources,
|
|
317
|
+
options,
|
|
318
|
+
credentials
|
|
319
|
+
);
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Build a server for custom MCP hosts. Use surface() for protocol-aware stdio.
|
|
324
|
+
*
|
|
325
|
+
* Derivation failures throw at this host bootstrap boundary after
|
|
326
|
+
* deriveMcpTools has represented the framework failure as a Result.
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* ```ts
|
|
330
|
+
* const server = createServer(graph, { name: 'demo' });
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
333
|
+
export const createServer = (
|
|
334
|
+
graph: Topo,
|
|
335
|
+
options: CreateServerOptions = {}
|
|
336
|
+
): Server => createServerFactory(graph, options)();
|
package/src/stdio.ts
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
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 surface().
|
|
6
|
-
*/
|
|
1
|
+
/** Protocol-aware MCP stdio serving through the official SDK. */
|
|
7
2
|
|
|
8
|
-
import type {
|
|
9
|
-
import {
|
|
3
|
+
import type { McpServerFactory } from '@modelcontextprotocol/server';
|
|
4
|
+
import { serveStdio } from '@modelcontextprotocol/server/stdio';
|
|
5
|
+
import type { StdioServerHandle } from '@modelcontextprotocol/server/stdio';
|
|
10
6
|
|
|
11
7
|
/**
|
|
12
|
-
*
|
|
8
|
+
* Serve stdio using a fresh server factory for protocol-era selection.
|
|
9
|
+
*
|
|
10
|
+
* The SDK may create a discovery probe before pinning a connection instance.
|
|
11
|
+
* Returning one previously connected server from the factory is unsupported.
|
|
13
12
|
*
|
|
14
13
|
* @example
|
|
15
14
|
* ```ts
|
|
16
15
|
* import { connectStdio, createServer } from '@ontrails/mcp';
|
|
17
16
|
*
|
|
18
|
-
* const
|
|
19
|
-
* await
|
|
17
|
+
* const connection = await connectStdio(() => createServer(graph));
|
|
18
|
+
* await connection.close();
|
|
20
19
|
* ```
|
|
21
20
|
*/
|
|
22
|
-
export const connectStdio = async (
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
};
|
|
21
|
+
export const connectStdio = async (
|
|
22
|
+
factory: McpServerFactory
|
|
23
|
+
): Promise<StdioServerHandle> => serveStdio(factory);
|
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
|
};
|