@anchrd/intel-contract 0.12.0 → 0.14.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,172 @@
1
+ import { z } from "zod";
2
+ function normalizedHostname(url) {
3
+ return url.hostname.toLowerCase().replace(/^\[|\]$/g, "");
4
+ }
5
+ function isPrivateIpv4(hostname) {
6
+ const parts = hostname.split(".").map(Number);
7
+ if (parts.length !== 4 ||
8
+ parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) {
9
+ return false;
10
+ }
11
+ const [first = 0, second = 0] = parts;
12
+ return (first === 0 ||
13
+ first === 10 ||
14
+ first === 127 ||
15
+ (first === 100 && second >= 64 && second <= 127) ||
16
+ (first === 169 && second === 254) ||
17
+ (first === 172 && second >= 16 && second <= 31) ||
18
+ (first === 192 && second === 168) ||
19
+ (first === 198 && (second === 18 || second === 19)) ||
20
+ first >= 224);
21
+ }
22
+ function isPublicToolHost(url) {
23
+ const hostname = normalizedHostname(url);
24
+ if (hostname === "localhost" || hostname === "127.0.0.1" || hostname === "::1") {
25
+ return url.protocol === "http:";
26
+ }
27
+ if (!hostname.includes(".") ||
28
+ hostname.endsWith(".local") ||
29
+ hostname.endsWith(".localhost") ||
30
+ hostname.endsWith(".internal") ||
31
+ isPrivateIpv4(hostname) ||
32
+ hostname.includes(":")) {
33
+ return false;
34
+ }
35
+ return url.protocol === "https:";
36
+ }
37
+ /**
38
+ * One MCP server as the portal names it. The handle is what the portal puts in front of every tool
39
+ * that server offers (`notion_notion-search` belongs to `notion`), and it is the only identifier
40
+ * Intel can both store and recognise again in a live `tools/list`.
41
+ *
42
+ * ⚠️ A handle is never invented from a tool name. Which servers exist is the portal's answer
43
+ * (`portal_list_servers`), and the prefix is only used to attribute a tool to a server that answer
44
+ * already named — see `packages/api/src/tools/tool-servers` for why splitting on the underscore
45
+ * alone would be ambiguous.
46
+ */
47
+ export const ToolServerHandle = z
48
+ .string()
49
+ .trim()
50
+ .min(1)
51
+ .max(120)
52
+ .regex(/^[A-Za-z0-9][A-Za-z0-9._-]*$/, "A server handle is the portal's own identifier");
53
+ export const ToolSourceUrl = z.url().refine((value) => {
54
+ try {
55
+ const url = new URL(value);
56
+ return !url.username && !url.password && isPublicToolHost(url);
57
+ }
58
+ catch {
59
+ return false;
60
+ }
61
+ }, "The portal must use an approved public HTTPS host without embedded credentials");
62
+ // The portal namespaces every upstream tool, so the name alone identifies the target server. The
63
+ // portal is still the one that resolves it and attaches the credentials — Intel never holds an
64
+ // upstream credential. Since D30 Intel does read the namespace for one purpose: attributing a tool
65
+ // to a server the portal's own `portal_list_servers` already named, so a delegation can be cut to
66
+ // whole servers. That is attribution, not routing.
67
+ export const ToolName = z.string().min(1).max(240);
68
+ export const ToolAnnotations = z.strictObject({
69
+ title: z.string().max(240).optional(),
70
+ readOnlyHint: z.boolean().optional(),
71
+ destructiveHint: z.boolean().optional(),
72
+ idempotentHint: z.boolean().optional(),
73
+ openWorldHint: z.boolean().optional(),
74
+ });
75
+ export const ToolCapability = z.strictObject({
76
+ name: ToolName,
77
+ title: z.string().max(240).nullable(),
78
+ description: z.string().max(10_000).nullable(),
79
+ inputSchema: z.record(z.string(), z.unknown()),
80
+ outputSchema: z.record(z.string(), z.unknown()).nullable(),
81
+ annotations: ToolAnnotations,
82
+ fingerprint: z.string().regex(/^[a-f0-9]{64}$/),
83
+ });
84
+ // The catalog reflects one live tools/list for the requesting user. It is never stored as a
85
+ // permission mirror, so there is no per-source state and no Intel-owned connection status.
86
+ export const ToolCatalog = z.strictObject({
87
+ portalConnected: z.boolean(),
88
+ items: z.array(ToolCapability),
89
+ /**
90
+ * Which delegated servers actually contributed a tool to this catalog (#289).
91
+ *
92
+ * ⚠️ Present only where the attribution was actually made — a delegated caller whose catalog was
93
+ * read. It is absent for an ordinary user, and absent as well when the answer comes from one of
94
+ * the short paths that never reach the portal (nothing delegated, no portal sign-in, connection
95
+ * dropped). Absent therefore means "not stated", never "nothing arrived"; `[]` is the second one.
96
+ *
97
+ * That it is missing rather than empty on those paths is deliberate rather than half-finished:
98
+ * the attribution already happens for a delegation — `capabilities` has to make it to cut the
99
+ * list — so naming it costs nothing there, while computing the same thing for an ordinary user
100
+ * would mean a second portal request per call, for a question their screen does not ask.
101
+ *
102
+ * ⚠️ It is the answer to "what arrived", never to "what was granted". A server missing here has
103
+ * been switched off, revoked, or is failing right now; the delegation in the definition is
104
+ * unchanged. Reading it the other way round would turn an outage into a permission change.
105
+ */
106
+ reached: z.array(ToolServerHandle).optional(),
107
+ });
108
+ /**
109
+ * One MCP server the asking user reaches right now, as the portal itself names it (D30).
110
+ *
111
+ * ⚠️ `toolCount` is a fact about this moment and this user, not a size. It exists so a picker can
112
+ * say "9 tools" instead of showing a handle alone, and it must never be read as what an agent will
113
+ * get: the delegated run asks the portal again, with the delegator's token.
114
+ */
115
+ export const ToolServer = z.strictObject({
116
+ handle: ToolServerHandle,
117
+ name: z.string().min(1).max(240),
118
+ toolCount: z.number().int().min(0),
119
+ });
120
+ // The same live-query rule as the tool catalog, one level up. `portalConnected: false` is the state
121
+ // of somebody who has not signed into the portal yet, and it is not an error.
122
+ export const ToolServerCatalog = z.strictObject({
123
+ portalConnected: z.boolean(),
124
+ items: z.array(ToolServer),
125
+ });
126
+ /**
127
+ * Which of the named servers a tool belongs to, or `null` for none of them.
128
+ *
129
+ * ⚠️ THE TRAP: a tool name does not say where its server name ends.
130
+ *
131
+ * The portal writes `<server>_<tool>`, and both halves may contain underscores — `intel_flow_get`
132
+ * reads equally well as server `intel` with tool `flow_get` and as a server called `intel_flow`
133
+ * with tool `get`. Splitting on the first underscore is therefore a guess that is wrong the day
134
+ * somebody adds a server whose name contains one, and on the API side being wrong means an agent
135
+ * delegated server A quietly reaching server B.
136
+ *
137
+ * So the prefix is never split. It is only ever MATCHED against handles the portal itself named,
138
+ * and the longest match wins: with `intel` and `intel_flow` both declared, `intel_flow_get` belongs
139
+ * to `intel_flow`, which is the only reading in which both declarations stay true.
140
+ *
141
+ * ⚠️ This lives in the contract because HOW A NAME IS READ is a property of the wire, and both
142
+ * surfaces read the same wire: `packages/api` cuts a delegation with it, `packages/ui` groups the
143
+ * tools screen with it (#212). A second implementation in the browser would be the third answer to
144
+ * one question — the underscore rule has already been answered differently in two places once
145
+ * (#106, #107), and the copies disagreed. What deliberately stays OUT of here is everything about
146
+ * reach: which handles are declared, which are enabled, which may be delegated and which one owns
147
+ * the portal's own management tools are decisions with consequences, and they belong to
148
+ * `packages/api/src/tools/tool-servers`. This function only reads a name.
149
+ */
150
+ export function serverOf(toolName, handles) {
151
+ let best = null;
152
+ for (const handle of handles) {
153
+ if (!toolName.startsWith(`${handle}_`))
154
+ continue;
155
+ if (best === null || handle.length > best.length)
156
+ best = handle;
157
+ }
158
+ return best;
159
+ }
160
+ export const TestToolInput = z.strictObject({
161
+ name: ToolName.describe("The tool's full name as tool_list reported it, including the server handle it is prefixed with. The portal routes on that prefix, so a bare name reaches nothing."),
162
+ arguments: z
163
+ .record(z.string(), z.unknown())
164
+ .default({})
165
+ .describe("The arguments, shaped by that tool's own inputSchema from tool_list. Validated against it before anything is sent, so a wrong shape is refused here rather than by the far side."),
166
+ });
167
+ export const ExecuteToolInput = TestToolInput;
168
+ export const ToolTestResult = z.strictObject({
169
+ isError: z.boolean(),
170
+ content: z.array(z.unknown()),
171
+ structuredContent: z.unknown().optional(),
172
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anchrd/intel-contract",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "type": "module",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -15,6 +15,34 @@
15
15
  ".": {
16
16
  "types": "./dist/contract/contract.d.ts",
17
17
  "default": "./dist/contract/contract.js"
18
+ },
19
+ "./bundle": {
20
+ "types": "./dist/contract/bundle.d.ts",
21
+ "default": "./dist/contract/bundle.js"
22
+ },
23
+ "./flow": {
24
+ "types": "./dist/contract/flow.d.ts",
25
+ "default": "./dist/contract/flow.js"
26
+ },
27
+ "./flow-run": {
28
+ "types": "./dist/contract/flow-run.d.ts",
29
+ "default": "./dist/contract/flow-run.js"
30
+ },
31
+ "./node": {
32
+ "types": "./dist/contract/node.d.ts",
33
+ "default": "./dist/contract/node.js"
34
+ },
35
+ "./share": {
36
+ "types": "./dist/contract/share.d.ts",
37
+ "default": "./dist/contract/share.js"
38
+ },
39
+ "./table": {
40
+ "types": "./dist/contract/table.d.ts",
41
+ "default": "./dist/contract/table.js"
42
+ },
43
+ "./tool": {
44
+ "types": "./dist/contract/tool.d.ts",
45
+ "default": "./dist/contract/tool.js"
18
46
  }
19
47
  },
20
48
  "files": [