@segmentstream/ontology 0.96.0 → 0.97.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,154 @@
1
+ import { Schema } from "effect";
2
+ export const ToolsetTypeId = Symbol.for("@segmentstream/ontology/Toolset");
3
+ /** The lock format `segmentstream connection mcp lock` writes. */
4
+ export const TOOLSET_LOCK_VERSION = 1;
5
+ /**
6
+ * A tool name an MCP API may declare: lowercase snake_case of at most 64 characters, so that
7
+ * `<toolset>:<tool>` is an ontology permission and `<toolset>_<tool>` an MCP tool name.
8
+ */
9
+ export const MCP_TOOL_NAME = /^(?=.{1,64}$)[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/;
10
+ /** The most tools one MCP API may declare, as `connections/connections.py` enforces. */
11
+ export const MAXIMUM_TOOLSET_TOOLS = 100;
12
+ /** A value the workspace fixes for one argument of a tool. */
13
+ export const FixedValue = Schema.Union([Schema.String, Schema.Number, Schema.Boolean]);
14
+ /** One locked tool: the declared access and fixed values, and what the server said about it. */
15
+ export const ToolsetLockTool = Schema.Struct({
16
+ name: Schema.String,
17
+ access: Schema.Literals(["read", "write"]),
18
+ fixed: Schema.Record(Schema.String, FixedValue),
19
+ description: Schema.String,
20
+ inputSchema: Schema.Record(Schema.String, Schema.Json),
21
+ outputSchema: Schema.optionalKey(Schema.Record(Schema.String, Schema.Json)),
22
+ annotations: Schema.optionalKey(Schema.Record(Schema.String, Schema.Json)),
23
+ });
24
+ /**
25
+ * The pinned tools of one MCP API of a connection, written by `segmentstream connection mcp lock`
26
+ * and committed with the workspace. The ontology's catalog is built from it, so a provider's
27
+ * change reaches members only through a reviewed commit and a deploy.
28
+ */
29
+ export const ToolsetLock = Schema.Struct({
30
+ version: Schema.Literal(TOOLSET_LOCK_VERSION),
31
+ connection: Schema.String,
32
+ api: Schema.String,
33
+ url: Schema.String,
34
+ protocolVersion: Schema.String,
35
+ server: Schema.optionalKey(Schema.Struct({ name: Schema.String, version: Schema.optionalKey(Schema.String) })),
36
+ tools: Schema.Array(ToolsetLockTool),
37
+ });
38
+ /**
39
+ * Serves the tools a connection's MCP API declares, as `connections/connections.py` lists them and
40
+ * the lock pins them, to every client of the ontology's MCP server. Each tool becomes the MCP tool
41
+ * `<toolset>_<tool>`, which a member may see and call with the permission `<toolset>:<tool>`. A
42
+ * toolset has no records: no table, `find`, `get` or `history`.
43
+ *
44
+ * `lock` is the JSON of `ontology/mcp/<key>.<api>.lock.json`, imported with `{ type: "json" }`:
45
+ *
46
+ * ```ts
47
+ * export const GitHub = toolset("github", {
48
+ * connection: github,
49
+ * api: "mcp",
50
+ * lock: githubLock,
51
+ * description: "The platform's source repository: code, commits, pull requests and issues.",
52
+ * });
53
+ * ```
54
+ */
55
+ export function toolset(name, definition) {
56
+ return { [ToolsetTypeId]: ToolsetTypeId, name, definition };
57
+ }
58
+ export function isToolset(value) {
59
+ return (typeof value === "object" &&
60
+ value !== null &&
61
+ value[ToolsetTypeId] === ToolsetTypeId);
62
+ }
63
+ /**
64
+ * Root keywords that constrain arguments through subschemas. Removing a fixed argument cannot be
65
+ * reflected in them, so a tool whose input uses one cannot have fixed values.
66
+ */
67
+ const COMPOSING_KEYWORDS = [
68
+ "allOf",
69
+ "anyOf",
70
+ "oneOf",
71
+ "not",
72
+ "if",
73
+ "then",
74
+ "else",
75
+ "dependentSchemas",
76
+ "dependencies",
77
+ "patternProperties",
78
+ "propertyNames",
79
+ "unevaluatedProperties",
80
+ ];
81
+ /** The root keyword that composes subschemas in a tool's input, if any. */
82
+ export function composingKeyword(schema) {
83
+ return COMPOSING_KEYWORDS.find((keyword) => Object.hasOwn(schema, keyword));
84
+ }
85
+ /**
86
+ * The root keyword that keeps the service from changing a tool's input, if any: fixed values are
87
+ * removed from it, and a tool that is not a read gains the confirmation argument.
88
+ */
89
+ export function fixedArgumentsConflict(schema, fixed, access = "read") {
90
+ return Object.keys(fixed).length === 0 && access === "read"
91
+ ? undefined
92
+ : composingKeyword(schema);
93
+ }
94
+ function isSchemaObject(value) {
95
+ return typeof value === "object" && value !== null && !Array.isArray(value);
96
+ }
97
+ /**
98
+ * A tool's input schema without its fixed arguments: they are removed from `properties`,
99
+ * `required` and `dependentRequired`, and `minProperties` and `maxProperties` no longer count the
100
+ * ones the schema declares, so a caller cannot send or even see them and the remaining input is
101
+ * satisfiable; the service adds their values to each call. `check` refuses a schema whose other
102
+ * root keywords a fixed value could break ({@link fixedArgumentsConflict}).
103
+ */
104
+ export function withoutFixedArguments(schema, fixed) {
105
+ const names = new Set(Object.keys(fixed));
106
+ if (names.size === 0) {
107
+ return { ...schema, type: "object" };
108
+ }
109
+ const declared = isSchemaObject(schema.properties) ? schema.properties : undefined;
110
+ const properties = declared === undefined
111
+ ? undefined
112
+ : Object.fromEntries(Object.entries(declared).filter(([property]) => !names.has(property)));
113
+ const required = Array.isArray(schema.required)
114
+ ? schema.required.filter((property) => typeof property !== "string" || !names.has(property))
115
+ : undefined;
116
+ const dependentRequired = isSchemaObject(schema.dependentRequired)
117
+ ? Object.fromEntries(Object.entries(schema.dependentRequired)
118
+ .filter(([property]) => !names.has(property))
119
+ .map(([property, needs]) => [
120
+ property,
121
+ Array.isArray(needs)
122
+ ? needs.filter((need) => typeof need !== "string" || !names.has(need))
123
+ : needs,
124
+ ]))
125
+ : undefined;
126
+ // Fixed arguments the schema declares count towards the properties of every call.
127
+ const counted = declared === undefined ? names.size : [...names].filter((name) => name in declared).length;
128
+ const minimum = typeof schema.minProperties === "number"
129
+ ? { minProperties: Math.max(0, schema.minProperties - counted) }
130
+ : {};
131
+ const maximum = typeof schema.maxProperties === "number"
132
+ ? { maxProperties: Math.max(0, schema.maxProperties - counted) }
133
+ : {};
134
+ return {
135
+ ...schema,
136
+ type: "object",
137
+ ...(properties === undefined ? {} : { properties }),
138
+ ...(required === undefined ? {} : { required }),
139
+ ...(dependentRequired === undefined ? {} : { dependentRequired }),
140
+ ...minimum,
141
+ ...maximum,
142
+ };
143
+ }
144
+ /** The workspace's access and the provider's annotations, as the service's tool hints. */
145
+ export function toolHints(access, annotations) {
146
+ return access === "read"
147
+ ? { readOnly: true, destructive: false, idempotent: true }
148
+ : {
149
+ readOnly: false,
150
+ // The provider's own hints can only add caution: an unknown write is not called safe.
151
+ destructive: annotations?.destructiveHint !== false,
152
+ idempotent: annotations?.idempotentHint === true,
153
+ };
154
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.96.0",
3
+ "version": "0.97.0",
4
4
  "description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",