@ontrails/mcp 0.3.0 → 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 +10 -0
- package/README.md +1 -1
- package/package.json +2 -2
- package/src/build.ts +124 -145
- package/src/output-rendering.ts +67 -0
- package/src/resources.ts +10 -9
- package/src/server.ts +47 -28
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @ontrails/mcp
|
|
2
2
|
|
|
3
|
+
## 0.4.1
|
|
4
|
+
|
|
5
|
+
No changes in this release.
|
|
6
|
+
|
|
7
|
+
## 0.4.0
|
|
8
|
+
|
|
9
|
+
### Minor Changes
|
|
10
|
+
|
|
11
|
+
- [`b767357`](https://github.com/outfitter-dev/trails/commit/b767357eb73fa7cbf8744c23557f57f0e890204d): Preserve raw non-object output values and schemas on modern MCP connections while keeping legacy `{ data: ... }` rendering and the standalone `deriveMcpTools()` API compatible. Select tool and trailhead output schemas after protocol negotiation, and expose matching schemas through the surface-map resource.
|
|
12
|
+
|
|
3
13
|
## 0.3.0
|
|
4
14
|
|
|
5
15
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -94,7 +94,7 @@ No manual annotation definitions. The contract is the source of truth.
|
|
|
94
94
|
|
|
95
95
|
## Schemas and Examples
|
|
96
96
|
|
|
97
|
-
MCP tool definitions include the trail's input schema
|
|
97
|
+
MCP tool definitions include the trail's input schema and, when declared, its `outputSchema`. Modern MCP returns the authored output directly in `structuredContent`, including arrays, scalars, and `null`. Legacy MCP and standalone `deriveMcpTools()` keep the `{ data: ... }` envelope for schemas without an object root. See [Output Schema and Examples](../../docs/surfaces/mcp.md#output-schema-and-examples).
|
|
98
98
|
|
|
99
99
|
Trail examples are rendered as structured metadata under `_meta["ontrails/examples"]`. Each rendered example preserves its input, expected output or error, a success/error kind, and provenance pointing back to the authored `trail.examples` field.
|
|
100
100
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ontrails/mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/outfitter-dev/trails.git",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"clean": "rm -rf dist *.tsbuildinfo"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@ontrails/core": "^0.
|
|
31
|
+
"@ontrails/core": "^0.4.1"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@modelcontextprotocol/client": "^2.4.0",
|
package/src/build.ts
CHANGED
|
@@ -59,6 +59,15 @@ import {
|
|
|
59
59
|
} from './permit-resolution.js';
|
|
60
60
|
import { createMcpProgressCallback } from './progress.js';
|
|
61
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';
|
|
62
71
|
|
|
63
72
|
/**
|
|
64
73
|
* Metadata key used for structured trail examples on derived MCP tools.
|
|
@@ -512,39 +521,6 @@ const serializeOutput = async (
|
|
|
512
521
|
return [{ text: JSON.stringify(value), type: 'text' }];
|
|
513
522
|
};
|
|
514
523
|
|
|
515
|
-
// `wrapAsData` is decided at build time from the schema shape (see
|
|
516
|
-
// `buildMcpOutputSchemaRendering`). It must be threaded through to the runtime
|
|
517
|
-
// because the schema's wrap decision and the runtime value's wrap decision
|
|
518
|
-
// can diverge — e.g. for `z.union([z.object(...), z.string()])` or
|
|
519
|
-
// `z.any()`, the schema declares a `{ data: ... }` envelope but a runtime
|
|
520
|
-
// object value would otherwise be returned unwrapped, breaking the
|
|
521
|
-
// outputSchema/structuredContent contract.
|
|
522
|
-
const toStructuredContent = (
|
|
523
|
-
value: unknown,
|
|
524
|
-
wrapAsData: boolean
|
|
525
|
-
): Record<string, unknown> | undefined => {
|
|
526
|
-
const structuredValue = containsBlobRef(value)
|
|
527
|
-
? toStructuredValue(value)
|
|
528
|
-
: value;
|
|
529
|
-
if (wrapAsData) {
|
|
530
|
-
return { data: structuredValue };
|
|
531
|
-
}
|
|
532
|
-
if (
|
|
533
|
-
structuredValue !== null &&
|
|
534
|
-
typeof structuredValue === 'object' &&
|
|
535
|
-
!Array.isArray(structuredValue)
|
|
536
|
-
) {
|
|
537
|
-
return structuredValue as Record<string, unknown>;
|
|
538
|
-
}
|
|
539
|
-
// When wrapAsData is false the schema's top-level type is `'object'`, and
|
|
540
|
-
// output validation has already constrained the runtime value to that
|
|
541
|
-
// shape — a primitive or array reaching this branch indicates the
|
|
542
|
-
// validation contract was bypassed. Return `undefined` so any future
|
|
543
|
-
// bypass surfaces as a missing `structuredContent` rather than a silently
|
|
544
|
-
// wrapped envelope that contradicts the published `outputSchema`.
|
|
545
|
-
return undefined;
|
|
546
|
-
};
|
|
547
|
-
|
|
548
524
|
// ---------------------------------------------------------------------------
|
|
549
525
|
// Layer input rendering (TRL-474)
|
|
550
526
|
// ---------------------------------------------------------------------------
|
|
@@ -854,13 +830,12 @@ const createHandler =
|
|
|
854
830
|
t: Trail<unknown, unknown, unknown>,
|
|
855
831
|
layers: readonly Layer[],
|
|
856
832
|
options: DeriveMcpToolsOptions,
|
|
857
|
-
wrapAsData: boolean,
|
|
858
833
|
layerRenderings: readonly McpLayerInputRendering[]
|
|
859
834
|
): ((
|
|
860
835
|
args: Record<string, unknown>,
|
|
861
836
|
extra: McpExtra
|
|
862
|
-
) => Promise<
|
|
863
|
-
async (args, extra): Promise<
|
|
837
|
+
) => Promise<McpRuntimeResult>) =>
|
|
838
|
+
async (args, extra): Promise<McpRuntimeResult> => {
|
|
864
839
|
const progressCb = createMcpProgressCallback(extra);
|
|
865
840
|
const versionedArgs =
|
|
866
841
|
t.version === undefined
|
|
@@ -891,12 +866,15 @@ const createHandler =
|
|
|
891
866
|
: { version: versionedArgs.version }),
|
|
892
867
|
});
|
|
893
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
|
+
}
|
|
894
875
|
return {
|
|
895
876
|
content: await serializeOutput(result.value),
|
|
896
|
-
structuredContent
|
|
897
|
-
t.output === undefined
|
|
898
|
-
? undefined
|
|
899
|
-
: toStructuredContent(result.value, wrapAsData),
|
|
877
|
+
structuredContent,
|
|
900
878
|
};
|
|
901
879
|
}
|
|
902
880
|
return mcpError(result.error);
|
|
@@ -920,44 +898,12 @@ const buildDescription = (
|
|
|
920
898
|
trail: Trail<unknown, unknown, unknown>
|
|
921
899
|
): string | undefined => trail.description;
|
|
922
900
|
|
|
923
|
-
// MCP requires `outputSchema` to have literal `type: "object"` at the root
|
|
924
|
-
// (see `@modelcontextprotocol/server` Tool schema — `outputSchema: z.object({
|
|
925
|
-
// type: z.literal('object'), ... })`). Object-shaped unions like
|
|
926
|
-
// `z.discriminatedUnion(...)` emit as `{ anyOf: [...] }` from
|
|
927
|
-
// `zodToJsonSchema` with no top-level `type`, so we publish them under the
|
|
928
|
-
// data envelope. The shape of `structuredContent` then flows from the
|
|
929
|
-
// `wrapAsData` flag, keeping the runtime aligned with what the schema
|
|
930
|
-
// declares.
|
|
931
|
-
const isMcpStructuredObjectSchema = (
|
|
932
|
-
schema: Record<string, unknown>
|
|
933
|
-
): boolean => schema['type'] === 'object';
|
|
934
|
-
|
|
935
|
-
interface McpOutputSchemaRendering {
|
|
936
|
-
readonly schema: Record<string, unknown>;
|
|
937
|
-
readonly wrapAsData: boolean;
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
const renderMcpOutputSchema = (
|
|
941
|
-
schema: Parameters<typeof zodToJsonSchema>[0]
|
|
942
|
-
): McpOutputSchemaRendering => {
|
|
943
|
-
const raw = zodToJsonSchema(schema);
|
|
944
|
-
if (isMcpStructuredObjectSchema(raw)) {
|
|
945
|
-
return { schema: raw, wrapAsData: false };
|
|
946
|
-
}
|
|
947
|
-
return {
|
|
948
|
-
schema: {
|
|
949
|
-
properties: { data: raw },
|
|
950
|
-
required: ['data'],
|
|
951
|
-
type: 'object',
|
|
952
|
-
},
|
|
953
|
-
wrapAsData: true,
|
|
954
|
-
};
|
|
955
|
-
};
|
|
956
|
-
|
|
957
901
|
const buildMcpOutputSchemaRendering = (
|
|
958
902
|
trail: Trail<unknown, unknown, unknown>
|
|
959
|
-
):
|
|
960
|
-
trail.output === undefined
|
|
903
|
+
): McpOutputRendering | undefined =>
|
|
904
|
+
trail.output === undefined
|
|
905
|
+
? undefined
|
|
906
|
+
: deriveMcpOutputRendering(trail.output);
|
|
961
907
|
|
|
962
908
|
const buildMeta = (
|
|
963
909
|
trail: Trail<unknown, unknown, unknown>
|
|
@@ -979,13 +925,13 @@ const mergeMeta = (
|
|
|
979
925
|
return Object.keys(merged).length > 0 ? merged : undefined;
|
|
980
926
|
};
|
|
981
927
|
|
|
982
|
-
/**
|
|
983
|
-
const
|
|
928
|
+
/** Derive one authored trail's shared execution and protocol renderings. */
|
|
929
|
+
const buildToolRecord = (
|
|
984
930
|
graph: Topo,
|
|
985
931
|
trail: Trail<unknown, unknown, unknown>,
|
|
986
932
|
layers: readonly Layer[],
|
|
987
933
|
options: DeriveMcpToolsOptions
|
|
988
|
-
):
|
|
934
|
+
): McpToolRecord => {
|
|
989
935
|
const rawAnnotations = deriveAnnotations(trail);
|
|
990
936
|
const annotations =
|
|
991
937
|
Object.keys(rawAnnotations).length > 0 ? rawAnnotations : undefined;
|
|
@@ -998,26 +944,35 @@ const buildToolDefinition = (
|
|
|
998
944
|
const inputRendering = renderMcpInputSchema(trail, attachedLayers);
|
|
999
945
|
const inputSchema = addMcpVersionInputSchema(trail, inputRendering.schema);
|
|
1000
946
|
const versions = deriveSurfaceTrailVersionRenderings(trail);
|
|
1001
|
-
|
|
947
|
+
const common = {
|
|
1002
948
|
_meta: buildMeta(trail),
|
|
1003
949
|
annotations,
|
|
1004
950
|
description: buildDescription(trail),
|
|
1005
|
-
handler: createHandler(
|
|
1006
|
-
graph,
|
|
1007
|
-
trail,
|
|
1008
|
-
layers,
|
|
1009
|
-
options,
|
|
1010
|
-
rendering?.wrapAsData ?? false,
|
|
1011
|
-
inputRendering.renderings
|
|
1012
|
-
),
|
|
1013
951
|
inputSchema,
|
|
1014
952
|
name: deriveToolName(graph.name, trail.id),
|
|
1015
|
-
outputSchema: rendering?.schema,
|
|
1016
953
|
trailId: trail.id,
|
|
1017
954
|
...(versions === undefined ? {} : { versions }),
|
|
1018
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
|
+
};
|
|
1019
972
|
};
|
|
1020
973
|
|
|
974
|
+
type ToolRecordFor = (trail: Trail<unknown, unknown, unknown>) => McpToolRecord;
|
|
975
|
+
|
|
1021
976
|
const trailheadSelectors = (
|
|
1022
977
|
selector: McpSurfaceTrailheadTrailSelector
|
|
1023
978
|
): readonly string[] => (typeof selector === 'string' ? [selector] : selector);
|
|
@@ -1031,7 +986,7 @@ const matchesTrailheadSelector = (
|
|
|
1031
986
|
);
|
|
1032
987
|
|
|
1033
988
|
interface TrailheadMemberTool {
|
|
1034
|
-
readonly
|
|
989
|
+
readonly record: McpToolRecord;
|
|
1035
990
|
readonly trail: Trail<unknown, unknown, unknown>;
|
|
1036
991
|
}
|
|
1037
992
|
|
|
@@ -1041,9 +996,9 @@ const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
|
1041
996
|
const buildTrailheadInputSchema = (
|
|
1042
997
|
members: readonly TrailheadMemberTool[]
|
|
1043
998
|
): Record<string, unknown> => ({
|
|
1044
|
-
anyOf: members.map(({
|
|
999
|
+
anyOf: members.map(({ record, trail }) => ({
|
|
1045
1000
|
properties: {
|
|
1046
|
-
input:
|
|
1001
|
+
input: record.legacy.inputSchema,
|
|
1047
1002
|
trail: { const: trail.id },
|
|
1048
1003
|
},
|
|
1049
1004
|
required: ['trail', 'input'],
|
|
@@ -1061,9 +1016,12 @@ const buildTrailheadInputSchema = (
|
|
|
1061
1016
|
});
|
|
1062
1017
|
|
|
1063
1018
|
const buildTrailheadOutputSchema = (
|
|
1064
|
-
members: readonly TrailheadMemberTool[]
|
|
1019
|
+
members: readonly TrailheadMemberTool[],
|
|
1020
|
+
view: keyof McpToolRecord
|
|
1065
1021
|
): Record<string, unknown> => {
|
|
1066
|
-
const outputSchemas = members.map(
|
|
1022
|
+
const outputSchemas = members.map(
|
|
1023
|
+
({ record }) => record[view].outputSchema ?? {}
|
|
1024
|
+
);
|
|
1067
1025
|
return {
|
|
1068
1026
|
properties: {
|
|
1069
1027
|
output:
|
|
@@ -1096,13 +1054,15 @@ const parseJsonTextContent = (
|
|
|
1096
1054
|
|
|
1097
1055
|
const wrapTrailheadResult = (
|
|
1098
1056
|
trailId: string,
|
|
1099
|
-
result:
|
|
1057
|
+
result: McpRuntimeResult
|
|
1100
1058
|
): McpToolResult => {
|
|
1101
1059
|
if (result.isError === true) {
|
|
1102
|
-
return result;
|
|
1060
|
+
return renderLegacyMcpResult(result);
|
|
1103
1061
|
}
|
|
1104
1062
|
const output =
|
|
1105
|
-
result.structuredContent
|
|
1063
|
+
result.structuredContent === undefined
|
|
1064
|
+
? (parseJsonTextContent(result.content) ?? null)
|
|
1065
|
+
: result.structuredContent;
|
|
1106
1066
|
const envelope = { output, trail: trailId };
|
|
1107
1067
|
return {
|
|
1108
1068
|
...(result._meta === undefined ? {} : { _meta: result._meta }),
|
|
@@ -1116,10 +1076,11 @@ const wrapTrailheadResult = (
|
|
|
1116
1076
|
|
|
1117
1077
|
const createTrailheadHandler = (
|
|
1118
1078
|
trailheadId: string,
|
|
1119
|
-
members: readonly TrailheadMemberTool[]
|
|
1079
|
+
members: readonly TrailheadMemberTool[],
|
|
1080
|
+
view: keyof McpToolRecord
|
|
1120
1081
|
): McpToolDefinition['handler'] => {
|
|
1121
1082
|
const byTrailId = new Map(
|
|
1122
|
-
members.map((member) => [member.trail.id, member.
|
|
1083
|
+
members.map((member) => [member.trail.id, member.record[view]])
|
|
1123
1084
|
);
|
|
1124
1085
|
|
|
1125
1086
|
return async (args, extra): Promise<McpToolResult> => {
|
|
@@ -1190,34 +1151,43 @@ const buildTrailheadMeta = (
|
|
|
1190
1151
|
: undefined
|
|
1191
1152
|
);
|
|
1192
1153
|
|
|
1193
|
-
const
|
|
1154
|
+
const buildTrailheadToolRecord = (
|
|
1194
1155
|
graph: Topo,
|
|
1195
1156
|
trailheadId: string,
|
|
1196
1157
|
definition: McpSurfaceTrailheadDefinition,
|
|
1197
1158
|
members: readonly TrailheadMemberTool[]
|
|
1198
|
-
):
|
|
1159
|
+
): McpToolRecord => {
|
|
1199
1160
|
const memberTrailIds = members.map(({ trail }) => trail.id);
|
|
1200
|
-
|
|
1161
|
+
const common = {
|
|
1201
1162
|
_meta: buildTrailheadMeta(trailheadId, definition, memberTrailIds),
|
|
1202
1163
|
annotations: deriveTrailheadAnnotations(definition, members),
|
|
1203
1164
|
description: definition.description,
|
|
1204
|
-
handler: createTrailheadHandler(trailheadId, members),
|
|
1205
1165
|
inputSchema: buildTrailheadInputSchema(members),
|
|
1206
1166
|
memberTrailIds,
|
|
1207
1167
|
name: deriveToolName(graph.name, trailheadId),
|
|
1208
|
-
outputSchema: buildTrailheadOutputSchema(members),
|
|
1209
1168
|
trailheadId,
|
|
1210
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
|
+
};
|
|
1211
1182
|
};
|
|
1212
1183
|
|
|
1213
1184
|
/** Register a trail as an MCP tool, checking for name collisions. */
|
|
1214
1185
|
const registerTool = (
|
|
1215
1186
|
graph: Topo,
|
|
1216
1187
|
trailItem: Trail<unknown, unknown, unknown>,
|
|
1217
|
-
|
|
1218
|
-
options: DeriveMcpToolsOptions,
|
|
1188
|
+
recordFor: ToolRecordFor,
|
|
1219
1189
|
nameToSourceId: Map<string, string>,
|
|
1220
|
-
tools:
|
|
1190
|
+
tools: McpToolRecord[]
|
|
1221
1191
|
): Result<void, Error> => {
|
|
1222
1192
|
const toolName = deriveToolName(graph.name, trailItem.id);
|
|
1223
1193
|
const existingId = nameToSourceId.get(toolName);
|
|
@@ -1229,7 +1199,7 @@ const registerTool = (
|
|
|
1229
1199
|
);
|
|
1230
1200
|
}
|
|
1231
1201
|
nameToSourceId.set(toolName, `trail:${trailItem.id}`);
|
|
1232
|
-
tools.push(
|
|
1202
|
+
tools.push(recordFor(trailItem));
|
|
1233
1203
|
return Result.ok();
|
|
1234
1204
|
};
|
|
1235
1205
|
|
|
@@ -1293,18 +1263,16 @@ const validateToolBuild = (
|
|
|
1293
1263
|
};
|
|
1294
1264
|
|
|
1295
1265
|
const collectTrailheadMembers = (
|
|
1296
|
-
graph: Topo,
|
|
1297
1266
|
definition: McpSurfaceTrailheadDefinition,
|
|
1298
1267
|
availableTrails: readonly Trail<unknown, unknown, unknown>[],
|
|
1299
|
-
|
|
1300
|
-
options: DeriveMcpToolsOptions
|
|
1268
|
+
recordFor: ToolRecordFor
|
|
1301
1269
|
): readonly TrailheadMemberTool[] =>
|
|
1302
1270
|
availableTrails
|
|
1303
1271
|
.filter((trailItem) =>
|
|
1304
1272
|
matchesTrailheadSelector(trailItem.id, definition.trails)
|
|
1305
1273
|
)
|
|
1306
1274
|
.map((trailItem) => ({
|
|
1307
|
-
|
|
1275
|
+
record: recordFor(trailItem),
|
|
1308
1276
|
trail: trailItem,
|
|
1309
1277
|
}));
|
|
1310
1278
|
|
|
@@ -1314,7 +1282,7 @@ const registerTrailhead = (
|
|
|
1314
1282
|
definition: McpSurfaceTrailheadDefinition,
|
|
1315
1283
|
members: readonly TrailheadMemberTool[],
|
|
1316
1284
|
nameToSourceId: Map<string, string>,
|
|
1317
|
-
tools:
|
|
1285
|
+
tools: McpToolRecord[]
|
|
1318
1286
|
): Result<void, Error> => {
|
|
1319
1287
|
if (members.length === 0) {
|
|
1320
1288
|
return Result.err(
|
|
@@ -1335,9 +1303,7 @@ const registerTrailhead = (
|
|
|
1335
1303
|
}
|
|
1336
1304
|
|
|
1337
1305
|
nameToSourceId.set(toolName, `trailhead:${trailheadId}`);
|
|
1338
|
-
tools.push(
|
|
1339
|
-
buildTrailheadToolDefinition(graph, trailheadId, definition, members)
|
|
1340
|
-
);
|
|
1306
|
+
tools.push(buildTrailheadToolRecord(graph, trailheadId, definition, members));
|
|
1341
1307
|
return Result.ok();
|
|
1342
1308
|
};
|
|
1343
1309
|
|
|
@@ -1404,11 +1370,10 @@ const trailheadDefinitionsFromOverlay = (
|
|
|
1404
1370
|
const registerTrailheads = (
|
|
1405
1371
|
graph: Topo,
|
|
1406
1372
|
trailheads: McpSurfaceTrailheadMap | undefined,
|
|
1407
|
-
|
|
1408
|
-
layers: readonly Layer[],
|
|
1373
|
+
recordFor: ToolRecordFor,
|
|
1409
1374
|
availableTrails: readonly Trail<unknown, unknown, unknown>[],
|
|
1410
1375
|
nameToSourceId: Map<string, string>,
|
|
1411
|
-
tools:
|
|
1376
|
+
tools: McpToolRecord[]
|
|
1412
1377
|
): Result<ReadonlySet<string>, Error> => {
|
|
1413
1378
|
const consumedTrailIds = new Set<string>();
|
|
1414
1379
|
const ownerByTrailId = new Map<string, string>();
|
|
@@ -1421,11 +1386,9 @@ const registerTrailheads = (
|
|
|
1421
1386
|
trailheads
|
|
1422
1387
|
).toSorted()) {
|
|
1423
1388
|
const members = collectTrailheadMembers(
|
|
1424
|
-
graph,
|
|
1425
1389
|
definition,
|
|
1426
1390
|
availableTrails,
|
|
1427
|
-
|
|
1428
|
-
options
|
|
1391
|
+
recordFor
|
|
1429
1392
|
);
|
|
1430
1393
|
for (const { trail: memberTrail } of members) {
|
|
1431
1394
|
const previous = ownerByTrailId.get(memberTrail.id);
|
|
@@ -1458,17 +1421,14 @@ const registerTrailheads = (
|
|
|
1458
1421
|
|
|
1459
1422
|
/**
|
|
1460
1423
|
* Register overlay tool synonyms: additional MCP tools whose names are the
|
|
1461
|
-
* scalar binding names,
|
|
1462
|
-
* handler.
|
|
1424
|
+
* scalar binding names, with independently customizable schemas.
|
|
1463
1425
|
*/
|
|
1464
1426
|
const registerSynonymTools = (
|
|
1465
|
-
|
|
1466
|
-
options: DeriveMcpToolsOptions,
|
|
1467
|
-
layers: readonly Layer[],
|
|
1427
|
+
buildRecord: ToolRecordFor,
|
|
1468
1428
|
availableTrails: readonly Trail<unknown, unknown, unknown>[],
|
|
1469
1429
|
synonyms: Readonly<Record<string, string>>,
|
|
1470
1430
|
nameToSourceId: Map<string, string>,
|
|
1471
|
-
tools:
|
|
1431
|
+
tools: McpToolRecord[]
|
|
1472
1432
|
): Result<void, Error> => {
|
|
1473
1433
|
const trailById = new Map(
|
|
1474
1434
|
availableTrails.map((trailItem) => [trailItem.id, trailItem])
|
|
@@ -1491,9 +1451,10 @@ const registerSynonymTools = (
|
|
|
1491
1451
|
);
|
|
1492
1452
|
}
|
|
1493
1453
|
nameToSourceId.set(name, `binding:${name}`);
|
|
1454
|
+
const record = buildRecord(trailItem);
|
|
1494
1455
|
tools.push({
|
|
1495
|
-
...
|
|
1496
|
-
name,
|
|
1456
|
+
legacy: { ...record.legacy, name },
|
|
1457
|
+
modern: { ...record.modern, name },
|
|
1497
1458
|
});
|
|
1498
1459
|
}
|
|
1499
1460
|
return Result.ok();
|
|
@@ -1503,9 +1464,21 @@ const registerTools = (
|
|
|
1503
1464
|
graph: Topo,
|
|
1504
1465
|
options: DeriveMcpToolsOptions,
|
|
1505
1466
|
layers: readonly Layer[]
|
|
1506
|
-
): Result<
|
|
1507
|
-
const tools:
|
|
1467
|
+
): Result<McpToolRecord[], Error> => {
|
|
1468
|
+
const tools: McpToolRecord[] = [];
|
|
1508
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
|
+
};
|
|
1509
1482
|
const availableTrails = eligibleTrails(graph, options);
|
|
1510
1483
|
const expansion = resolveOverlayBindingExpansion(options, availableTrails);
|
|
1511
1484
|
if (expansion.isErr()) {
|
|
@@ -1518,8 +1491,7 @@ const registerTools = (
|
|
|
1518
1491
|
const registeredTrailheads = registerTrailheads(
|
|
1519
1492
|
graph,
|
|
1520
1493
|
trailheads,
|
|
1521
|
-
|
|
1522
|
-
layers,
|
|
1494
|
+
recordFor,
|
|
1523
1495
|
availableTrails,
|
|
1524
1496
|
nameToSourceId,
|
|
1525
1497
|
tools
|
|
@@ -1536,8 +1508,7 @@ const registerTools = (
|
|
|
1536
1508
|
const registered = registerTool(
|
|
1537
1509
|
graph,
|
|
1538
1510
|
trailItem,
|
|
1539
|
-
|
|
1540
|
-
options,
|
|
1511
|
+
recordFor,
|
|
1541
1512
|
nameToSourceId,
|
|
1542
1513
|
tools
|
|
1543
1514
|
);
|
|
@@ -1547,9 +1518,7 @@ const registerTools = (
|
|
|
1547
1518
|
}
|
|
1548
1519
|
|
|
1549
1520
|
const registeredSynonyms = registerSynonymTools(
|
|
1550
|
-
|
|
1551
|
-
options,
|
|
1552
|
-
layers,
|
|
1521
|
+
buildRecord,
|
|
1553
1522
|
availableTrails,
|
|
1554
1523
|
expansion.value?.synonyms ?? {},
|
|
1555
1524
|
nameToSourceId,
|
|
@@ -1562,6 +1531,18 @@ const registerTools = (
|
|
|
1562
1531
|
return Result.ok(tools);
|
|
1563
1532
|
};
|
|
1564
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
|
+
|
|
1565
1546
|
/**
|
|
1566
1547
|
* Build MCP tool definitions from a topo without opening a transport.
|
|
1567
1548
|
*
|
|
@@ -1581,10 +1562,8 @@ export const deriveMcpTools = (
|
|
|
1581
1562
|
graph: Topo,
|
|
1582
1563
|
options: DeriveMcpToolsOptions = {}
|
|
1583
1564
|
): Result<McpToolDefinition[], Error> => {
|
|
1584
|
-
const
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
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));
|
|
1590
1569
|
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { zodToJsonSchema } from '@ontrails/core';
|
|
2
|
+
|
|
3
|
+
import type { McpToolDefinition, McpToolResult } from './build.js';
|
|
4
|
+
|
|
5
|
+
/** Canonical output facts and the compatibility rendering, derived together. */
|
|
6
|
+
export interface McpOutputRendering {
|
|
7
|
+
readonly schema: Record<string, unknown>;
|
|
8
|
+
readonly legacySchema: Record<string, unknown>;
|
|
9
|
+
readonly legacyWrapAsData: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Internal runtime values may be any structured JSON value on the modern era. */
|
|
13
|
+
export interface McpRuntimeResult extends Omit<
|
|
14
|
+
McpToolResult,
|
|
15
|
+
'structuredContent'
|
|
16
|
+
> {
|
|
17
|
+
readonly structuredContent?: unknown;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface McpRuntimeTool extends Omit<McpToolDefinition, 'handler'> {
|
|
21
|
+
readonly handler: (
|
|
22
|
+
args: Record<string, unknown>,
|
|
23
|
+
extra: Parameters<McpToolDefinition['handler']>[1]
|
|
24
|
+
) => Promise<McpRuntimeResult>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Package-private views share one authored execution and schema derivation. */
|
|
28
|
+
export interface McpToolRecord {
|
|
29
|
+
readonly legacy: McpToolDefinition;
|
|
30
|
+
readonly modern: McpRuntimeTool;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const deriveMcpOutputRendering = (
|
|
34
|
+
output: Parameters<typeof zodToJsonSchema>[0]
|
|
35
|
+
): McpOutputRendering => {
|
|
36
|
+
const schema = zodToJsonSchema(output);
|
|
37
|
+
const legacyWrapAsData = schema['type'] !== 'object';
|
|
38
|
+
return {
|
|
39
|
+
legacySchema: legacyWrapAsData
|
|
40
|
+
? { properties: { data: schema }, required: ['data'], type: 'object' }
|
|
41
|
+
: schema,
|
|
42
|
+
legacyWrapAsData,
|
|
43
|
+
schema,
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const renderLegacyMcpResult = (
|
|
48
|
+
result: McpRuntimeResult,
|
|
49
|
+
rendering?: McpOutputRendering
|
|
50
|
+
): McpToolResult => {
|
|
51
|
+
if (result.isError === true) {
|
|
52
|
+
const { structuredContent: _omitted, ...error } = result;
|
|
53
|
+
return error;
|
|
54
|
+
}
|
|
55
|
+
const value = result.structuredContent;
|
|
56
|
+
let structuredContent: Record<string, unknown> | undefined;
|
|
57
|
+
if (rendering?.legacyWrapAsData === true) {
|
|
58
|
+
structuredContent = { data: value };
|
|
59
|
+
} else if (
|
|
60
|
+
value !== null &&
|
|
61
|
+
typeof value === 'object' &&
|
|
62
|
+
!Array.isArray(value)
|
|
63
|
+
) {
|
|
64
|
+
structuredContent = value as Record<string, unknown>;
|
|
65
|
+
}
|
|
66
|
+
return { ...result, structuredContent };
|
|
67
|
+
};
|
package/src/resources.ts
CHANGED
|
@@ -11,6 +11,9 @@ import {
|
|
|
11
11
|
} from './build.js';
|
|
12
12
|
import type { McpToolDefinition } from './build.js';
|
|
13
13
|
|
|
14
|
+
// Resource rendering consumes contract facts, never the invocation callback.
|
|
15
|
+
type McpResourceTool = Omit<McpToolDefinition, 'handler'>;
|
|
16
|
+
|
|
14
17
|
/**
|
|
15
18
|
* Resource URI used for the resolved MCP surface map.
|
|
16
19
|
*
|
|
@@ -109,7 +112,7 @@ interface McpTrailResource {
|
|
|
109
112
|
const asJson = (value: unknown): string =>
|
|
110
113
|
`${JSON.stringify(value, null, 2)}\n`;
|
|
111
114
|
|
|
112
|
-
const renderSurfaceMapTool = (tool:
|
|
115
|
+
const renderSurfaceMapTool = (tool: McpResourceTool): McpSurfaceMapTool => ({
|
|
113
116
|
annotations: tool.annotations,
|
|
114
117
|
description: tool.description,
|
|
115
118
|
inputSchema: tool.inputSchema,
|
|
@@ -128,15 +131,13 @@ const renderSurfaceMapTool = (tool: McpToolDefinition): McpSurfaceMapTool => ({
|
|
|
128
131
|
: {}),
|
|
129
132
|
});
|
|
130
133
|
|
|
131
|
-
const buildSurfaceMap = (
|
|
132
|
-
tools: readonly McpToolDefinition[]
|
|
133
|
-
): McpSurfaceMap => ({
|
|
134
|
+
const buildSurfaceMap = (tools: readonly McpResourceTool[]): McpSurfaceMap => ({
|
|
134
135
|
surface: 'mcp',
|
|
135
136
|
tools: tools.map(renderSurfaceMapTool),
|
|
136
137
|
});
|
|
137
138
|
|
|
138
139
|
const exposedTrailIds = (
|
|
139
|
-
tools: readonly
|
|
140
|
+
tools: readonly McpResourceTool[]
|
|
140
141
|
): ReadonlySet<string> =>
|
|
141
142
|
new Set(
|
|
142
143
|
tools.flatMap((tool) => [
|
|
@@ -186,7 +187,7 @@ const buildExampleResource = (
|
|
|
186
187
|
|
|
187
188
|
const buildExampleResources = (
|
|
188
189
|
graph: Topo,
|
|
189
|
-
tools: readonly
|
|
190
|
+
tools: readonly McpResourceTool[]
|
|
190
191
|
): readonly {
|
|
191
192
|
readonly content: McpResourceContent;
|
|
192
193
|
readonly listing: McpResourceDefinition;
|
|
@@ -207,7 +208,7 @@ const resourceId = (
|
|
|
207
208
|
|
|
208
209
|
const buildTrailGraphResource = (
|
|
209
210
|
trailItem: Trail<unknown, unknown, unknown>,
|
|
210
|
-
tools: readonly
|
|
211
|
+
tools: readonly McpResourceTool[]
|
|
211
212
|
): {
|
|
212
213
|
readonly content: McpResourceContent;
|
|
213
214
|
readonly listing: McpResourceDefinition;
|
|
@@ -255,7 +256,7 @@ const buildTrailGraphResource = (
|
|
|
255
256
|
|
|
256
257
|
const buildTrailGraphResources = (
|
|
257
258
|
graph: Topo,
|
|
258
|
-
tools: readonly
|
|
259
|
+
tools: readonly McpResourceTool[]
|
|
259
260
|
): readonly {
|
|
260
261
|
readonly content: McpResourceContent;
|
|
261
262
|
readonly listing: McpResourceDefinition;
|
|
@@ -285,7 +286,7 @@ const buildTrailGraphResources = (
|
|
|
285
286
|
*/
|
|
286
287
|
export const buildMcpResources = (
|
|
287
288
|
graph: Topo,
|
|
288
|
-
tools: readonly
|
|
289
|
+
tools: readonly McpResourceTool[],
|
|
289
290
|
config: McpResourcesConfig = {}
|
|
290
291
|
): BuiltMcpResources => {
|
|
291
292
|
const listings: McpResourceDefinition[] = [];
|
package/src/server.ts
CHANGED
|
@@ -17,11 +17,11 @@ import type {
|
|
|
17
17
|
import type {
|
|
18
18
|
McpSurfaceTrailheadMap,
|
|
19
19
|
McpExtra,
|
|
20
|
-
McpToolDefinition,
|
|
21
20
|
ResolveMcpPermit,
|
|
22
21
|
McpCredentialPosture,
|
|
23
22
|
} from './build.js';
|
|
24
|
-
import {
|
|
23
|
+
import { deriveMcpToolRecords } from './build.js';
|
|
24
|
+
import type { McpToolRecord } from './output-rendering.js';
|
|
25
25
|
import {
|
|
26
26
|
resolveMcpResourcePermit,
|
|
27
27
|
runMcpResourceRequest,
|
|
@@ -90,13 +90,15 @@ const requestCredentials = (
|
|
|
90
90
|
* "optional instructions describing how to use the server and its features."
|
|
91
91
|
*/
|
|
92
92
|
const createMcpServer = (
|
|
93
|
-
tools:
|
|
93
|
+
tools: readonly McpToolRecord[],
|
|
94
94
|
info: {
|
|
95
95
|
readonly name: string;
|
|
96
96
|
readonly version: string;
|
|
97
97
|
readonly description?: string | undefined;
|
|
98
98
|
},
|
|
99
|
-
mcpResources:
|
|
99
|
+
mcpResources:
|
|
100
|
+
| Readonly<Record<keyof McpToolRecord, BuiltMcpResources>>
|
|
101
|
+
| undefined,
|
|
100
102
|
options: CreateServerOptions,
|
|
101
103
|
credentials?: Pick<McpExtra, 'authorization'>
|
|
102
104
|
): Server => {
|
|
@@ -114,31 +116,35 @@ const createMcpServer = (
|
|
|
114
116
|
);
|
|
115
117
|
|
|
116
118
|
// Build a lookup map for tool dispatch
|
|
117
|
-
const toolMap = new Map
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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';
|
|
121
124
|
|
|
122
125
|
// Register tools/list handler
|
|
123
126
|
// oxlint-disable-next-line require-await -- MCP SDK requires async handler
|
|
124
|
-
server.setRequestHandler('tools/list', async () =>
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
:
|
|
135
|
-
|
|
136
|
-
|
|
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
|
+
});
|
|
137
143
|
|
|
138
144
|
// Register tools/call handler
|
|
139
145
|
server.setRequestHandler('tools/call', async (request, requestExtra) => {
|
|
140
|
-
const
|
|
141
|
-
if (
|
|
146
|
+
const record = toolMap.get(request.params.name);
|
|
147
|
+
if (record === undefined) {
|
|
142
148
|
return {
|
|
143
149
|
content: [
|
|
144
150
|
{
|
|
@@ -150,6 +156,7 @@ const createMcpServer = (
|
|
|
150
156
|
};
|
|
151
157
|
}
|
|
152
158
|
|
|
159
|
+
const tool = record[protocolView()];
|
|
153
160
|
const args = (request.params.arguments ?? {}) as Record<string, unknown>;
|
|
154
161
|
const progressToken = requestExtra.mcpReq._meta?.progressToken;
|
|
155
162
|
|
|
@@ -189,7 +196,7 @@ const createMcpServer = (
|
|
|
189
196
|
return permit;
|
|
190
197
|
}
|
|
191
198
|
const resources = [];
|
|
192
|
-
for (const resource of mcpResources.list) {
|
|
199
|
+
for (const resource of mcpResources[protocolView()].list) {
|
|
193
200
|
if (options.authorizeResourceRead !== undefined) {
|
|
194
201
|
const allowed = await options.authorizeResourceRead({
|
|
195
202
|
permit: permit.value,
|
|
@@ -222,7 +229,8 @@ const createMcpServer = (
|
|
|
222
229
|
if (permit.isErr()) {
|
|
223
230
|
return permit;
|
|
224
231
|
}
|
|
225
|
-
const
|
|
232
|
+
const rendering = mcpResources[protocolView()];
|
|
233
|
+
const resource = rendering.list.find(
|
|
226
234
|
(entry) => entry.uri === request.params.uri
|
|
227
235
|
);
|
|
228
236
|
if (resource === undefined) {
|
|
@@ -239,7 +247,7 @@ const createMcpServer = (
|
|
|
239
247
|
return allowed;
|
|
240
248
|
}
|
|
241
249
|
}
|
|
242
|
-
const content =
|
|
250
|
+
const content = rendering.read(resource.uri);
|
|
243
251
|
if (content === undefined) {
|
|
244
252
|
return Result.err(
|
|
245
253
|
new NotFoundError(`Resource ${resource.uri} not found`)
|
|
@@ -262,7 +270,7 @@ export const createServerFactory = (
|
|
|
262
270
|
graph: Topo,
|
|
263
271
|
options: CreateServerOptions = {}
|
|
264
272
|
): ((credentials?: Pick<McpExtra, 'authorization'>) => Server) => {
|
|
265
|
-
const toolsResult =
|
|
273
|
+
const toolsResult = deriveMcpToolRecords(graph, {
|
|
266
274
|
configValues: options.configValues,
|
|
267
275
|
createContext: options.createContext,
|
|
268
276
|
credentialPosture: options.credentialPosture,
|
|
@@ -284,7 +292,18 @@ export const createServerFactory = (
|
|
|
284
292
|
const mcpResources =
|
|
285
293
|
options.mcpResources === false
|
|
286
294
|
? undefined
|
|
287
|
-
:
|
|
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
|
+
};
|
|
288
307
|
|
|
289
308
|
return (credentials) =>
|
|
290
309
|
createMcpServer(
|