@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/build.ts
CHANGED
|
@@ -6,8 +6,12 @@
|
|
|
6
6
|
* Results to MCP responses.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
import type {
|
|
10
|
+
EmbeddedResource,
|
|
11
|
+
ImageContent,
|
|
12
|
+
TextContent,
|
|
13
|
+
} from '@modelcontextprotocol/server';
|
|
9
14
|
import {
|
|
10
|
-
AuthError,
|
|
11
15
|
InternalError,
|
|
12
16
|
Result,
|
|
13
17
|
ValidationError,
|
|
@@ -49,8 +53,21 @@ import type {
|
|
|
49
53
|
|
|
50
54
|
import type { McpAnnotations } from './annotations.js';
|
|
51
55
|
import { deriveAnnotations } from './annotations.js';
|
|
56
|
+
import {
|
|
57
|
+
resolveMcpPermit,
|
|
58
|
+
surfaceContextFactory,
|
|
59
|
+
} from './permit-resolution.js';
|
|
52
60
|
import { createMcpProgressCallback } from './progress.js';
|
|
53
61
|
import { deriveToolName } from './tool-name.js';
|
|
62
|
+
import {
|
|
63
|
+
deriveMcpOutputRendering,
|
|
64
|
+
renderLegacyMcpResult,
|
|
65
|
+
} from './output-rendering.js';
|
|
66
|
+
import type {
|
|
67
|
+
McpOutputRendering,
|
|
68
|
+
McpRuntimeResult,
|
|
69
|
+
McpToolRecord,
|
|
70
|
+
} from './output-rendering.js';
|
|
54
71
|
|
|
55
72
|
/**
|
|
56
73
|
* Metadata key used for structured trail examples on derived MCP tools.
|
|
@@ -129,8 +146,24 @@ export interface DeriveMcpToolsOptions extends BaseSurfaceOptions {
|
|
|
129
146
|
readonly layers?: readonly Layer[] | undefined;
|
|
130
147
|
readonly resources?: ResourceOverrideMap | undefined;
|
|
131
148
|
readonly resolvePermit?: ResolveMcpPermit | undefined;
|
|
149
|
+
/**
|
|
150
|
+
* Where callers reach this surface from.
|
|
151
|
+
*
|
|
152
|
+
* `host` (the default) is the in-process posture: the launching process is
|
|
153
|
+
* the caller, so a `createContext` permit and a host-supplied `extra.permit`
|
|
154
|
+
* are accepted as the host's own policy. `network` is the posture the
|
|
155
|
+
* fetch-shaped entry sets: identity enters only through `resolvePermit`,
|
|
156
|
+
* a `createContext` permit is discarded at runtime with a diagnostic,
|
|
157
|
+
* `extra.permit` is ignored, a presented bearer with no resolver is an
|
|
158
|
+
* `AuthError`, and protected trails without a resolver are refused at
|
|
159
|
+
* build time.
|
|
160
|
+
*/
|
|
161
|
+
readonly credentialPosture?: McpCredentialPosture | undefined;
|
|
132
162
|
}
|
|
133
163
|
|
|
164
|
+
/** See {@link DeriveMcpToolsOptions.credentialPosture}. */
|
|
165
|
+
export type McpCredentialPosture = 'host' | 'network';
|
|
166
|
+
|
|
134
167
|
export type McpSurfaceTrailheadTrailSelector = string | readonly string[];
|
|
135
168
|
|
|
136
169
|
/** Surface-side grouped entry over existing trails. */
|
|
@@ -151,10 +184,11 @@ export type McpSurfaceTrailheadMap = Readonly<
|
|
|
151
184
|
Record<string, McpSurfaceTrailheadDefinition>
|
|
152
185
|
>;
|
|
153
186
|
|
|
187
|
+
/** Untrusted request credential candidates; the configured resolver owns verification. */
|
|
154
188
|
export interface ResolveMcpPermitInput {
|
|
189
|
+
readonly _meta?: Readonly<Record<string, unknown>> | undefined;
|
|
155
190
|
readonly authorization?: string | undefined;
|
|
156
191
|
readonly bearerToken?: string | undefined;
|
|
157
|
-
readonly sessionId?: string | undefined;
|
|
158
192
|
}
|
|
159
193
|
|
|
160
194
|
export type ResolveMcpPermit = (
|
|
@@ -182,6 +216,8 @@ export interface McpToolDefinition {
|
|
|
182
216
|
}
|
|
183
217
|
|
|
184
218
|
export interface McpExtra {
|
|
219
|
+
/** Application metadata from this request, never proof of identity by itself. */
|
|
220
|
+
readonly _meta?: Readonly<Record<string, unknown>> | undefined;
|
|
185
221
|
readonly authorization?: string | undefined;
|
|
186
222
|
readonly progressToken?: string | number | undefined;
|
|
187
223
|
readonly sendProgress?:
|
|
@@ -189,7 +225,6 @@ export interface McpExtra {
|
|
|
189
225
|
| undefined;
|
|
190
226
|
readonly abortSignal?: AbortSignal | undefined;
|
|
191
227
|
readonly permit?: BasePermit | undefined;
|
|
192
|
-
readonly sessionId?: string | undefined;
|
|
193
228
|
}
|
|
194
229
|
|
|
195
230
|
export interface McpToolResult {
|
|
@@ -203,13 +238,8 @@ export type McpToolErrorMeta = Omit<SurfaceErrorRendering, 'surface'> & {
|
|
|
203
238
|
readonly surface: 'mcp';
|
|
204
239
|
};
|
|
205
240
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
readonly mimeType?: string | undefined;
|
|
209
|
-
readonly text?: string | undefined;
|
|
210
|
-
readonly type: 'text' | 'image' | 'resource';
|
|
211
|
-
readonly uri?: string | undefined;
|
|
212
|
-
}
|
|
241
|
+
/** MCP content emitted from trail results, validated by the SDK wire boundary. */
|
|
242
|
+
export type McpContent = TextContent | ImageContent | EmbeddedResource;
|
|
213
243
|
|
|
214
244
|
// ---------------------------------------------------------------------------
|
|
215
245
|
// Internal helpers (defined before use)
|
|
@@ -274,20 +304,20 @@ const blobToContent = async (
|
|
|
274
304
|
blob: BlobRef,
|
|
275
305
|
resolveData: BlobDataResolver = resolveBlobData
|
|
276
306
|
): Promise<McpContent> => {
|
|
307
|
+
const bytes = await resolveData(blob);
|
|
308
|
+
const data = uint8ArrayToBase64(bytes);
|
|
277
309
|
if (!blob.mimeType.startsWith('image/')) {
|
|
278
310
|
return {
|
|
279
|
-
|
|
311
|
+
resource: {
|
|
312
|
+
blob: data,
|
|
313
|
+
mimeType: blob.mimeType,
|
|
314
|
+
uri: `blob://${blob.name}`,
|
|
315
|
+
},
|
|
280
316
|
type: 'resource',
|
|
281
|
-
uri: `blob://${blob.name}`,
|
|
282
317
|
};
|
|
283
318
|
}
|
|
284
319
|
|
|
285
|
-
|
|
286
|
-
return {
|
|
287
|
-
data: uint8ArrayToBase64(bytes),
|
|
288
|
-
mimeType: blob.mimeType,
|
|
289
|
-
type: 'image',
|
|
290
|
-
};
|
|
320
|
+
return { data, mimeType: blob.mimeType, type: 'image' };
|
|
291
321
|
};
|
|
292
322
|
|
|
293
323
|
type BlobContentResolver = (blob: BlobRef) => Promise<McpContent>;
|
|
@@ -491,39 +521,6 @@ const serializeOutput = async (
|
|
|
491
521
|
return [{ text: JSON.stringify(value), type: 'text' }];
|
|
492
522
|
};
|
|
493
523
|
|
|
494
|
-
// `wrapAsData` is decided at build time from the schema shape (see
|
|
495
|
-
// `buildMcpOutputSchemaRendering`). It must be threaded through to the runtime
|
|
496
|
-
// because the schema's wrap decision and the runtime value's wrap decision
|
|
497
|
-
// can diverge — e.g. for `z.union([z.object(...), z.string()])` or
|
|
498
|
-
// `z.any()`, the schema declares a `{ data: ... }` envelope but a runtime
|
|
499
|
-
// object value would otherwise be returned unwrapped, breaking the
|
|
500
|
-
// outputSchema/structuredContent contract.
|
|
501
|
-
const toStructuredContent = (
|
|
502
|
-
value: unknown,
|
|
503
|
-
wrapAsData: boolean
|
|
504
|
-
): Record<string, unknown> | undefined => {
|
|
505
|
-
const structuredValue = containsBlobRef(value)
|
|
506
|
-
? toStructuredValue(value)
|
|
507
|
-
: value;
|
|
508
|
-
if (wrapAsData) {
|
|
509
|
-
return { data: structuredValue };
|
|
510
|
-
}
|
|
511
|
-
if (
|
|
512
|
-
structuredValue !== null &&
|
|
513
|
-
typeof structuredValue === 'object' &&
|
|
514
|
-
!Array.isArray(structuredValue)
|
|
515
|
-
) {
|
|
516
|
-
return structuredValue as Record<string, unknown>;
|
|
517
|
-
}
|
|
518
|
-
// When wrapAsData is false the schema's top-level type is `'object'`, and
|
|
519
|
-
// output validation has already constrained the runtime value to that
|
|
520
|
-
// shape — a primitive or array reaching this branch indicates the
|
|
521
|
-
// validation contract was bypassed. Return `undefined` so any future
|
|
522
|
-
// bypass surfaces as a missing `structuredContent` rather than a silently
|
|
523
|
-
// wrapped envelope that contradicts the published `outputSchema`.
|
|
524
|
-
return undefined;
|
|
525
|
-
};
|
|
526
|
-
|
|
527
524
|
// ---------------------------------------------------------------------------
|
|
528
525
|
// Layer input rendering (TRL-474)
|
|
529
526
|
// ---------------------------------------------------------------------------
|
|
@@ -827,65 +824,18 @@ const withMcpSurface = (
|
|
|
827
824
|
progressCb === undefined ? {} : { progress: progressCb }
|
|
828
825
|
);
|
|
829
826
|
|
|
830
|
-
const parseBearerAuthorization = (
|
|
831
|
-
authorization: string | undefined
|
|
832
|
-
): Result<string | undefined, Error> => {
|
|
833
|
-
if (authorization === undefined || authorization.length === 0) {
|
|
834
|
-
return Result.ok();
|
|
835
|
-
}
|
|
836
|
-
const match = authorization.match(/^Bearer\s+(.+)$/i);
|
|
837
|
-
const token = match?.[1]?.trim();
|
|
838
|
-
if (token === undefined || token.length === 0) {
|
|
839
|
-
return Result.err(
|
|
840
|
-
new AuthError('Malformed MCP authorization; expected Bearer token', {
|
|
841
|
-
context: { code: 'invalid_authorization_header' },
|
|
842
|
-
})
|
|
843
|
-
);
|
|
844
|
-
}
|
|
845
|
-
return Result.ok(token);
|
|
846
|
-
};
|
|
847
|
-
|
|
848
|
-
const resolveMcpPermit = async (
|
|
849
|
-
options: DeriveMcpToolsOptions,
|
|
850
|
-
extra: McpExtra
|
|
851
|
-
): Promise<Result<BasePermit | undefined, Error>> => {
|
|
852
|
-
if (extra.permit !== undefined) {
|
|
853
|
-
return Result.ok(extra.permit);
|
|
854
|
-
}
|
|
855
|
-
const token = parseBearerAuthorization(extra.authorization);
|
|
856
|
-
if (token.isErr()) {
|
|
857
|
-
return token;
|
|
858
|
-
}
|
|
859
|
-
if (token.value === undefined) {
|
|
860
|
-
return Result.ok();
|
|
861
|
-
}
|
|
862
|
-
if (options.resolvePermit === undefined) {
|
|
863
|
-
return Result.ok();
|
|
864
|
-
}
|
|
865
|
-
const resolved = await options.resolvePermit({
|
|
866
|
-
authorization: extra.authorization,
|
|
867
|
-
bearerToken: token.value,
|
|
868
|
-
sessionId: extra.sessionId,
|
|
869
|
-
});
|
|
870
|
-
if (resolved.isErr()) {
|
|
871
|
-
return resolved;
|
|
872
|
-
}
|
|
873
|
-
return Result.ok(resolved.value ?? undefined);
|
|
874
|
-
};
|
|
875
|
-
|
|
876
827
|
const createHandler =
|
|
877
828
|
(
|
|
878
829
|
graph: Topo,
|
|
879
830
|
t: Trail<unknown, unknown, unknown>,
|
|
880
831
|
layers: readonly Layer[],
|
|
881
832
|
options: DeriveMcpToolsOptions,
|
|
882
|
-
wrapAsData: boolean,
|
|
883
833
|
layerRenderings: readonly McpLayerInputRendering[]
|
|
884
834
|
): ((
|
|
885
835
|
args: Record<string, unknown>,
|
|
886
836
|
extra: McpExtra
|
|
887
|
-
) => Promise<
|
|
888
|
-
async (args, extra): Promise<
|
|
837
|
+
) => Promise<McpRuntimeResult>) =>
|
|
838
|
+
async (args, extra): Promise<McpRuntimeResult> => {
|
|
889
839
|
const progressCb = createMcpProgressCallback(extra);
|
|
890
840
|
const versionedArgs =
|
|
891
841
|
t.version === undefined
|
|
@@ -903,7 +853,7 @@ const createHandler =
|
|
|
903
853
|
const result = await executeTrail(t, trailInput, {
|
|
904
854
|
abortSignal: extra.abortSignal,
|
|
905
855
|
configValues: options.configValues,
|
|
906
|
-
createContext: options
|
|
856
|
+
createContext: surfaceContextFactory(options),
|
|
907
857
|
ctx: withMcpSurface(progressCb, layers),
|
|
908
858
|
...(Object.keys(layerInputs).length === 0 ? {} : { layerInputs }),
|
|
909
859
|
...(permit === undefined ? {} : { permit }),
|
|
@@ -916,12 +866,15 @@ const createHandler =
|
|
|
916
866
|
: { version: versionedArgs.version }),
|
|
917
867
|
});
|
|
918
868
|
if (result.isOk()) {
|
|
869
|
+
let structuredContent: unknown;
|
|
870
|
+
if (t.output !== undefined) {
|
|
871
|
+
structuredContent = containsBlobRef(result.value)
|
|
872
|
+
? toStructuredValue(result.value)
|
|
873
|
+
: result.value;
|
|
874
|
+
}
|
|
919
875
|
return {
|
|
920
876
|
content: await serializeOutput(result.value),
|
|
921
|
-
structuredContent
|
|
922
|
-
t.output === undefined
|
|
923
|
-
? undefined
|
|
924
|
-
: toStructuredContent(result.value, wrapAsData),
|
|
877
|
+
structuredContent,
|
|
925
878
|
};
|
|
926
879
|
}
|
|
927
880
|
return mcpError(result.error);
|
|
@@ -945,44 +898,12 @@ const buildDescription = (
|
|
|
945
898
|
trail: Trail<unknown, unknown, unknown>
|
|
946
899
|
): string | undefined => trail.description;
|
|
947
900
|
|
|
948
|
-
// MCP requires `outputSchema` to have literal `type: "object"` at the root
|
|
949
|
-
// (see `@modelcontextprotocol/sdk` Tool schema — `outputSchema: z.object({
|
|
950
|
-
// type: z.literal('object'), ... })`). Object-shaped unions like
|
|
951
|
-
// `z.discriminatedUnion(...)` emit as `{ anyOf: [...] }` from
|
|
952
|
-
// `zodToJsonSchema` with no top-level `type`, so we publish them under the
|
|
953
|
-
// data envelope. The shape of `structuredContent` then flows from the
|
|
954
|
-
// `wrapAsData` flag, keeping the runtime aligned with what the schema
|
|
955
|
-
// declares.
|
|
956
|
-
const isMcpStructuredObjectSchema = (
|
|
957
|
-
schema: Record<string, unknown>
|
|
958
|
-
): boolean => schema['type'] === 'object';
|
|
959
|
-
|
|
960
|
-
interface McpOutputSchemaRendering {
|
|
961
|
-
readonly schema: Record<string, unknown>;
|
|
962
|
-
readonly wrapAsData: boolean;
|
|
963
|
-
}
|
|
964
|
-
|
|
965
|
-
const renderMcpOutputSchema = (
|
|
966
|
-
schema: Parameters<typeof zodToJsonSchema>[0]
|
|
967
|
-
): McpOutputSchemaRendering => {
|
|
968
|
-
const raw = zodToJsonSchema(schema);
|
|
969
|
-
if (isMcpStructuredObjectSchema(raw)) {
|
|
970
|
-
return { schema: raw, wrapAsData: false };
|
|
971
|
-
}
|
|
972
|
-
return {
|
|
973
|
-
schema: {
|
|
974
|
-
properties: { data: raw },
|
|
975
|
-
required: ['data'],
|
|
976
|
-
type: 'object',
|
|
977
|
-
},
|
|
978
|
-
wrapAsData: true,
|
|
979
|
-
};
|
|
980
|
-
};
|
|
981
|
-
|
|
982
901
|
const buildMcpOutputSchemaRendering = (
|
|
983
902
|
trail: Trail<unknown, unknown, unknown>
|
|
984
|
-
):
|
|
985
|
-
trail.output === undefined
|
|
903
|
+
): McpOutputRendering | undefined =>
|
|
904
|
+
trail.output === undefined
|
|
905
|
+
? undefined
|
|
906
|
+
: deriveMcpOutputRendering(trail.output);
|
|
986
907
|
|
|
987
908
|
const buildMeta = (
|
|
988
909
|
trail: Trail<unknown, unknown, unknown>
|
|
@@ -1004,13 +925,13 @@ const mergeMeta = (
|
|
|
1004
925
|
return Object.keys(merged).length > 0 ? merged : undefined;
|
|
1005
926
|
};
|
|
1006
927
|
|
|
1007
|
-
/**
|
|
1008
|
-
const
|
|
928
|
+
/** Derive one authored trail's shared execution and protocol renderings. */
|
|
929
|
+
const buildToolRecord = (
|
|
1009
930
|
graph: Topo,
|
|
1010
931
|
trail: Trail<unknown, unknown, unknown>,
|
|
1011
932
|
layers: readonly Layer[],
|
|
1012
933
|
options: DeriveMcpToolsOptions
|
|
1013
|
-
):
|
|
934
|
+
): McpToolRecord => {
|
|
1014
935
|
const rawAnnotations = deriveAnnotations(trail);
|
|
1015
936
|
const annotations =
|
|
1016
937
|
Object.keys(rawAnnotations).length > 0 ? rawAnnotations : undefined;
|
|
@@ -1023,26 +944,35 @@ const buildToolDefinition = (
|
|
|
1023
944
|
const inputRendering = renderMcpInputSchema(trail, attachedLayers);
|
|
1024
945
|
const inputSchema = addMcpVersionInputSchema(trail, inputRendering.schema);
|
|
1025
946
|
const versions = deriveSurfaceTrailVersionRenderings(trail);
|
|
1026
|
-
|
|
947
|
+
const common = {
|
|
1027
948
|
_meta: buildMeta(trail),
|
|
1028
949
|
annotations,
|
|
1029
950
|
description: buildDescription(trail),
|
|
1030
|
-
handler: createHandler(
|
|
1031
|
-
graph,
|
|
1032
|
-
trail,
|
|
1033
|
-
layers,
|
|
1034
|
-
options,
|
|
1035
|
-
rendering?.wrapAsData ?? false,
|
|
1036
|
-
inputRendering.renderings
|
|
1037
|
-
),
|
|
1038
951
|
inputSchema,
|
|
1039
952
|
name: deriveToolName(graph.name, trail.id),
|
|
1040
|
-
outputSchema: rendering?.schema,
|
|
1041
953
|
trailId: trail.id,
|
|
1042
954
|
...(versions === undefined ? {} : { versions }),
|
|
1043
955
|
};
|
|
956
|
+
const execute = createHandler(
|
|
957
|
+
graph,
|
|
958
|
+
trail,
|
|
959
|
+
layers,
|
|
960
|
+
options,
|
|
961
|
+
inputRendering.renderings
|
|
962
|
+
);
|
|
963
|
+
return {
|
|
964
|
+
legacy: {
|
|
965
|
+
...common,
|
|
966
|
+
handler: async (args, extra) =>
|
|
967
|
+
renderLegacyMcpResult(await execute(args, extra), rendering),
|
|
968
|
+
outputSchema: rendering?.legacySchema,
|
|
969
|
+
},
|
|
970
|
+
modern: { ...common, handler: execute, outputSchema: rendering?.schema },
|
|
971
|
+
};
|
|
1044
972
|
};
|
|
1045
973
|
|
|
974
|
+
type ToolRecordFor = (trail: Trail<unknown, unknown, unknown>) => McpToolRecord;
|
|
975
|
+
|
|
1046
976
|
const trailheadSelectors = (
|
|
1047
977
|
selector: McpSurfaceTrailheadTrailSelector
|
|
1048
978
|
): readonly string[] => (typeof selector === 'string' ? [selector] : selector);
|
|
@@ -1056,7 +986,7 @@ const matchesTrailheadSelector = (
|
|
|
1056
986
|
);
|
|
1057
987
|
|
|
1058
988
|
interface TrailheadMemberTool {
|
|
1059
|
-
readonly
|
|
989
|
+
readonly record: McpToolRecord;
|
|
1060
990
|
readonly trail: Trail<unknown, unknown, unknown>;
|
|
1061
991
|
}
|
|
1062
992
|
|
|
@@ -1066,9 +996,9 @@ const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
|
1066
996
|
const buildTrailheadInputSchema = (
|
|
1067
997
|
members: readonly TrailheadMemberTool[]
|
|
1068
998
|
): Record<string, unknown> => ({
|
|
1069
|
-
anyOf: members.map(({
|
|
999
|
+
anyOf: members.map(({ record, trail }) => ({
|
|
1070
1000
|
properties: {
|
|
1071
|
-
input:
|
|
1001
|
+
input: record.legacy.inputSchema,
|
|
1072
1002
|
trail: { const: trail.id },
|
|
1073
1003
|
},
|
|
1074
1004
|
required: ['trail', 'input'],
|
|
@@ -1086,9 +1016,12 @@ const buildTrailheadInputSchema = (
|
|
|
1086
1016
|
});
|
|
1087
1017
|
|
|
1088
1018
|
const buildTrailheadOutputSchema = (
|
|
1089
|
-
members: readonly TrailheadMemberTool[]
|
|
1019
|
+
members: readonly TrailheadMemberTool[],
|
|
1020
|
+
view: keyof McpToolRecord
|
|
1090
1021
|
): Record<string, unknown> => {
|
|
1091
|
-
const outputSchemas = members.map(
|
|
1022
|
+
const outputSchemas = members.map(
|
|
1023
|
+
({ record }) => record[view].outputSchema ?? {}
|
|
1024
|
+
);
|
|
1092
1025
|
return {
|
|
1093
1026
|
properties: {
|
|
1094
1027
|
output:
|
|
@@ -1121,13 +1054,15 @@ const parseJsonTextContent = (
|
|
|
1121
1054
|
|
|
1122
1055
|
const wrapTrailheadResult = (
|
|
1123
1056
|
trailId: string,
|
|
1124
|
-
result:
|
|
1057
|
+
result: McpRuntimeResult
|
|
1125
1058
|
): McpToolResult => {
|
|
1126
1059
|
if (result.isError === true) {
|
|
1127
|
-
return result;
|
|
1060
|
+
return renderLegacyMcpResult(result);
|
|
1128
1061
|
}
|
|
1129
1062
|
const output =
|
|
1130
|
-
result.structuredContent
|
|
1063
|
+
result.structuredContent === undefined
|
|
1064
|
+
? (parseJsonTextContent(result.content) ?? null)
|
|
1065
|
+
: result.structuredContent;
|
|
1131
1066
|
const envelope = { output, trail: trailId };
|
|
1132
1067
|
return {
|
|
1133
1068
|
...(result._meta === undefined ? {} : { _meta: result._meta }),
|
|
@@ -1141,10 +1076,11 @@ const wrapTrailheadResult = (
|
|
|
1141
1076
|
|
|
1142
1077
|
const createTrailheadHandler = (
|
|
1143
1078
|
trailheadId: string,
|
|
1144
|
-
members: readonly TrailheadMemberTool[]
|
|
1079
|
+
members: readonly TrailheadMemberTool[],
|
|
1080
|
+
view: keyof McpToolRecord
|
|
1145
1081
|
): McpToolDefinition['handler'] => {
|
|
1146
1082
|
const byTrailId = new Map(
|
|
1147
|
-
members.map((member) => [member.trail.id, member.
|
|
1083
|
+
members.map((member) => [member.trail.id, member.record[view]])
|
|
1148
1084
|
);
|
|
1149
1085
|
|
|
1150
1086
|
return async (args, extra): Promise<McpToolResult> => {
|
|
@@ -1215,34 +1151,43 @@ const buildTrailheadMeta = (
|
|
|
1215
1151
|
: undefined
|
|
1216
1152
|
);
|
|
1217
1153
|
|
|
1218
|
-
const
|
|
1154
|
+
const buildTrailheadToolRecord = (
|
|
1219
1155
|
graph: Topo,
|
|
1220
1156
|
trailheadId: string,
|
|
1221
1157
|
definition: McpSurfaceTrailheadDefinition,
|
|
1222
1158
|
members: readonly TrailheadMemberTool[]
|
|
1223
|
-
):
|
|
1159
|
+
): McpToolRecord => {
|
|
1224
1160
|
const memberTrailIds = members.map(({ trail }) => trail.id);
|
|
1225
|
-
|
|
1161
|
+
const common = {
|
|
1226
1162
|
_meta: buildTrailheadMeta(trailheadId, definition, memberTrailIds),
|
|
1227
1163
|
annotations: deriveTrailheadAnnotations(definition, members),
|
|
1228
1164
|
description: definition.description,
|
|
1229
|
-
handler: createTrailheadHandler(trailheadId, members),
|
|
1230
1165
|
inputSchema: buildTrailheadInputSchema(members),
|
|
1231
1166
|
memberTrailIds,
|
|
1232
1167
|
name: deriveToolName(graph.name, trailheadId),
|
|
1233
|
-
outputSchema: buildTrailheadOutputSchema(members),
|
|
1234
1168
|
trailheadId,
|
|
1235
1169
|
};
|
|
1170
|
+
return {
|
|
1171
|
+
legacy: {
|
|
1172
|
+
...common,
|
|
1173
|
+
handler: createTrailheadHandler(trailheadId, members, 'legacy'),
|
|
1174
|
+
outputSchema: buildTrailheadOutputSchema(members, 'legacy'),
|
|
1175
|
+
},
|
|
1176
|
+
modern: {
|
|
1177
|
+
...common,
|
|
1178
|
+
handler: createTrailheadHandler(trailheadId, members, 'modern'),
|
|
1179
|
+
outputSchema: buildTrailheadOutputSchema(members, 'modern'),
|
|
1180
|
+
},
|
|
1181
|
+
};
|
|
1236
1182
|
};
|
|
1237
1183
|
|
|
1238
1184
|
/** Register a trail as an MCP tool, checking for name collisions. */
|
|
1239
1185
|
const registerTool = (
|
|
1240
1186
|
graph: Topo,
|
|
1241
1187
|
trailItem: Trail<unknown, unknown, unknown>,
|
|
1242
|
-
|
|
1243
|
-
options: DeriveMcpToolsOptions,
|
|
1188
|
+
recordFor: ToolRecordFor,
|
|
1244
1189
|
nameToSourceId: Map<string, string>,
|
|
1245
|
-
tools:
|
|
1190
|
+
tools: McpToolRecord[]
|
|
1246
1191
|
): Result<void, Error> => {
|
|
1247
1192
|
const toolName = deriveToolName(graph.name, trailItem.id);
|
|
1248
1193
|
const existingId = nameToSourceId.get(toolName);
|
|
@@ -1254,7 +1199,7 @@ const registerTool = (
|
|
|
1254
1199
|
);
|
|
1255
1200
|
}
|
|
1256
1201
|
nameToSourceId.set(toolName, `trail:${trailItem.id}`);
|
|
1257
|
-
tools.push(
|
|
1202
|
+
tools.push(recordFor(trailItem));
|
|
1258
1203
|
return Result.ok();
|
|
1259
1204
|
};
|
|
1260
1205
|
|
|
@@ -1269,24 +1214,65 @@ const eligibleTrails = (
|
|
|
1269
1214
|
intent: options.intent,
|
|
1270
1215
|
});
|
|
1271
1216
|
|
|
1217
|
+
const requiresVerifiedPermit = (
|
|
1218
|
+
trailItem: Trail<unknown, unknown, unknown>
|
|
1219
|
+
): boolean => trailItem.permit !== undefined && trailItem.permit !== 'public';
|
|
1220
|
+
|
|
1221
|
+
/**
|
|
1222
|
+
* A network surface with protected trails and no verifier would fail every
|
|
1223
|
+
* protected call at runtime; refuse at build time so the gap teaches.
|
|
1224
|
+
*/
|
|
1225
|
+
const validateNetworkVerifier = (
|
|
1226
|
+
graph: Topo,
|
|
1227
|
+
options: DeriveMcpToolsOptions
|
|
1228
|
+
): Result<void, Error> => {
|
|
1229
|
+
if (
|
|
1230
|
+
options.credentialPosture !== 'network' ||
|
|
1231
|
+
options.resolvePermit !== undefined
|
|
1232
|
+
) {
|
|
1233
|
+
return Result.ok();
|
|
1234
|
+
}
|
|
1235
|
+
const protectedIds = eligibleTrails(graph, options)
|
|
1236
|
+
.filter((trailItem) => requiresVerifiedPermit(trailItem))
|
|
1237
|
+
.map((trailItem) => trailItem.id);
|
|
1238
|
+
if (protectedIds.length === 0) {
|
|
1239
|
+
return Result.ok();
|
|
1240
|
+
}
|
|
1241
|
+
return Result.err(
|
|
1242
|
+
new ValidationError(
|
|
1243
|
+
`MCP network surface exposes protected trails without a permit resolver: ${protectedIds.join(', ')}. Configure resolvePermit with a verifier, or exclude those trails from this surface. A createContext permit is not an identity source on a network surface.`,
|
|
1244
|
+
{
|
|
1245
|
+
context: {
|
|
1246
|
+
code: 'network_surface_missing_verifier',
|
|
1247
|
+
trailIds: protectedIds,
|
|
1248
|
+
},
|
|
1249
|
+
}
|
|
1250
|
+
)
|
|
1251
|
+
);
|
|
1252
|
+
};
|
|
1253
|
+
|
|
1272
1254
|
const validateToolBuild = (
|
|
1273
1255
|
graph: Topo,
|
|
1274
1256
|
options: DeriveMcpToolsOptions
|
|
1275
|
-
): Result<void, Error> =>
|
|
1257
|
+
): Result<void, Error> => {
|
|
1258
|
+
const validated = validateSurfaceTopo(graph, options);
|
|
1259
|
+
if (validated.isErr()) {
|
|
1260
|
+
return validated;
|
|
1261
|
+
}
|
|
1262
|
+
return validateNetworkVerifier(graph, options);
|
|
1263
|
+
};
|
|
1276
1264
|
|
|
1277
1265
|
const collectTrailheadMembers = (
|
|
1278
|
-
graph: Topo,
|
|
1279
1266
|
definition: McpSurfaceTrailheadDefinition,
|
|
1280
1267
|
availableTrails: readonly Trail<unknown, unknown, unknown>[],
|
|
1281
|
-
|
|
1282
|
-
options: DeriveMcpToolsOptions
|
|
1268
|
+
recordFor: ToolRecordFor
|
|
1283
1269
|
): readonly TrailheadMemberTool[] =>
|
|
1284
1270
|
availableTrails
|
|
1285
1271
|
.filter((trailItem) =>
|
|
1286
1272
|
matchesTrailheadSelector(trailItem.id, definition.trails)
|
|
1287
1273
|
)
|
|
1288
1274
|
.map((trailItem) => ({
|
|
1289
|
-
|
|
1275
|
+
record: recordFor(trailItem),
|
|
1290
1276
|
trail: trailItem,
|
|
1291
1277
|
}));
|
|
1292
1278
|
|
|
@@ -1296,7 +1282,7 @@ const registerTrailhead = (
|
|
|
1296
1282
|
definition: McpSurfaceTrailheadDefinition,
|
|
1297
1283
|
members: readonly TrailheadMemberTool[],
|
|
1298
1284
|
nameToSourceId: Map<string, string>,
|
|
1299
|
-
tools:
|
|
1285
|
+
tools: McpToolRecord[]
|
|
1300
1286
|
): Result<void, Error> => {
|
|
1301
1287
|
if (members.length === 0) {
|
|
1302
1288
|
return Result.err(
|
|
@@ -1317,9 +1303,7 @@ const registerTrailhead = (
|
|
|
1317
1303
|
}
|
|
1318
1304
|
|
|
1319
1305
|
nameToSourceId.set(toolName, `trailhead:${trailheadId}`);
|
|
1320
|
-
tools.push(
|
|
1321
|
-
buildTrailheadToolDefinition(graph, trailheadId, definition, members)
|
|
1322
|
-
);
|
|
1306
|
+
tools.push(buildTrailheadToolRecord(graph, trailheadId, definition, members));
|
|
1323
1307
|
return Result.ok();
|
|
1324
1308
|
};
|
|
1325
1309
|
|
|
@@ -1386,11 +1370,10 @@ const trailheadDefinitionsFromOverlay = (
|
|
|
1386
1370
|
const registerTrailheads = (
|
|
1387
1371
|
graph: Topo,
|
|
1388
1372
|
trailheads: McpSurfaceTrailheadMap | undefined,
|
|
1389
|
-
|
|
1390
|
-
layers: readonly Layer[],
|
|
1373
|
+
recordFor: ToolRecordFor,
|
|
1391
1374
|
availableTrails: readonly Trail<unknown, unknown, unknown>[],
|
|
1392
1375
|
nameToSourceId: Map<string, string>,
|
|
1393
|
-
tools:
|
|
1376
|
+
tools: McpToolRecord[]
|
|
1394
1377
|
): Result<ReadonlySet<string>, Error> => {
|
|
1395
1378
|
const consumedTrailIds = new Set<string>();
|
|
1396
1379
|
const ownerByTrailId = new Map<string, string>();
|
|
@@ -1403,11 +1386,9 @@ const registerTrailheads = (
|
|
|
1403
1386
|
trailheads
|
|
1404
1387
|
).toSorted()) {
|
|
1405
1388
|
const members = collectTrailheadMembers(
|
|
1406
|
-
graph,
|
|
1407
1389
|
definition,
|
|
1408
1390
|
availableTrails,
|
|
1409
|
-
|
|
1410
|
-
options
|
|
1391
|
+
recordFor
|
|
1411
1392
|
);
|
|
1412
1393
|
for (const { trail: memberTrail } of members) {
|
|
1413
1394
|
const previous = ownerByTrailId.get(memberTrail.id);
|
|
@@ -1440,17 +1421,14 @@ const registerTrailheads = (
|
|
|
1440
1421
|
|
|
1441
1422
|
/**
|
|
1442
1423
|
* Register overlay tool synonyms: additional MCP tools whose names are the
|
|
1443
|
-
* scalar binding names,
|
|
1444
|
-
* handler.
|
|
1424
|
+
* scalar binding names, with independently customizable schemas.
|
|
1445
1425
|
*/
|
|
1446
1426
|
const registerSynonymTools = (
|
|
1447
|
-
|
|
1448
|
-
options: DeriveMcpToolsOptions,
|
|
1449
|
-
layers: readonly Layer[],
|
|
1427
|
+
buildRecord: ToolRecordFor,
|
|
1450
1428
|
availableTrails: readonly Trail<unknown, unknown, unknown>[],
|
|
1451
1429
|
synonyms: Readonly<Record<string, string>>,
|
|
1452
1430
|
nameToSourceId: Map<string, string>,
|
|
1453
|
-
tools:
|
|
1431
|
+
tools: McpToolRecord[]
|
|
1454
1432
|
): Result<void, Error> => {
|
|
1455
1433
|
const trailById = new Map(
|
|
1456
1434
|
availableTrails.map((trailItem) => [trailItem.id, trailItem])
|
|
@@ -1473,9 +1451,10 @@ const registerSynonymTools = (
|
|
|
1473
1451
|
);
|
|
1474
1452
|
}
|
|
1475
1453
|
nameToSourceId.set(name, `binding:${name}`);
|
|
1454
|
+
const record = buildRecord(trailItem);
|
|
1476
1455
|
tools.push({
|
|
1477
|
-
...
|
|
1478
|
-
name,
|
|
1456
|
+
legacy: { ...record.legacy, name },
|
|
1457
|
+
modern: { ...record.modern, name },
|
|
1479
1458
|
});
|
|
1480
1459
|
}
|
|
1481
1460
|
return Result.ok();
|
|
@@ -1485,9 +1464,21 @@ const registerTools = (
|
|
|
1485
1464
|
graph: Topo,
|
|
1486
1465
|
options: DeriveMcpToolsOptions,
|
|
1487
1466
|
layers: readonly Layer[]
|
|
1488
|
-
): Result<
|
|
1489
|
-
const tools:
|
|
1467
|
+
): Result<McpToolRecord[], Error> => {
|
|
1468
|
+
const tools: McpToolRecord[] = [];
|
|
1490
1469
|
const nameToSourceId = new Map<string, string>();
|
|
1470
|
+
const buildRecord: ToolRecordFor = (trail) =>
|
|
1471
|
+
buildToolRecord(graph, trail, layers, options);
|
|
1472
|
+
const recordsByTrail = new Map<string, McpToolRecord>();
|
|
1473
|
+
const recordFor: ToolRecordFor = (trail) => {
|
|
1474
|
+
const existing = recordsByTrail.get(trail.id);
|
|
1475
|
+
if (existing !== undefined) {
|
|
1476
|
+
return existing;
|
|
1477
|
+
}
|
|
1478
|
+
const record = buildRecord(trail);
|
|
1479
|
+
recordsByTrail.set(trail.id, record);
|
|
1480
|
+
return record;
|
|
1481
|
+
};
|
|
1491
1482
|
const availableTrails = eligibleTrails(graph, options);
|
|
1492
1483
|
const expansion = resolveOverlayBindingExpansion(options, availableTrails);
|
|
1493
1484
|
if (expansion.isErr()) {
|
|
@@ -1500,8 +1491,7 @@ const registerTools = (
|
|
|
1500
1491
|
const registeredTrailheads = registerTrailheads(
|
|
1501
1492
|
graph,
|
|
1502
1493
|
trailheads,
|
|
1503
|
-
|
|
1504
|
-
layers,
|
|
1494
|
+
recordFor,
|
|
1505
1495
|
availableTrails,
|
|
1506
1496
|
nameToSourceId,
|
|
1507
1497
|
tools
|
|
@@ -1518,8 +1508,7 @@ const registerTools = (
|
|
|
1518
1508
|
const registered = registerTool(
|
|
1519
1509
|
graph,
|
|
1520
1510
|
trailItem,
|
|
1521
|
-
|
|
1522
|
-
options,
|
|
1511
|
+
recordFor,
|
|
1523
1512
|
nameToSourceId,
|
|
1524
1513
|
tools
|
|
1525
1514
|
);
|
|
@@ -1529,9 +1518,7 @@ const registerTools = (
|
|
|
1529
1518
|
}
|
|
1530
1519
|
|
|
1531
1520
|
const registeredSynonyms = registerSynonymTools(
|
|
1532
|
-
|
|
1533
|
-
options,
|
|
1534
|
-
layers,
|
|
1521
|
+
buildRecord,
|
|
1535
1522
|
availableTrails,
|
|
1536
1523
|
expansion.value?.synonyms ?? {},
|
|
1537
1524
|
nameToSourceId,
|
|
@@ -1544,6 +1531,18 @@ const registerTools = (
|
|
|
1544
1531
|
return Result.ok(tools);
|
|
1545
1532
|
};
|
|
1546
1533
|
|
|
1534
|
+
/** Derive package-private protocol views from one canonical tool build. @internal */
|
|
1535
|
+
export const deriveMcpToolRecords = (
|
|
1536
|
+
graph: Topo,
|
|
1537
|
+
options: DeriveMcpToolsOptions = {}
|
|
1538
|
+
): Result<McpToolRecord[], Error> => {
|
|
1539
|
+
const validation = validateToolBuild(graph, options);
|
|
1540
|
+
if (validation.isErr()) {
|
|
1541
|
+
return validation;
|
|
1542
|
+
}
|
|
1543
|
+
return registerTools(graph, options, options.layers ?? []);
|
|
1544
|
+
};
|
|
1545
|
+
|
|
1547
1546
|
/**
|
|
1548
1547
|
* Build MCP tool definitions from a topo without opening a transport.
|
|
1549
1548
|
*
|
|
@@ -1563,10 +1562,8 @@ export const deriveMcpTools = (
|
|
|
1563
1562
|
graph: Topo,
|
|
1564
1563
|
options: DeriveMcpToolsOptions = {}
|
|
1565
1564
|
): Result<McpToolDefinition[], Error> => {
|
|
1566
|
-
const
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
return registerTools(graph, options, options.layers ?? []);
|
|
1565
|
+
const records = deriveMcpToolRecords(graph, options);
|
|
1566
|
+
return records.isErr()
|
|
1567
|
+
? records
|
|
1568
|
+
: Result.ok(records.value.map((record) => record.legacy));
|
|
1572
1569
|
};
|