@valbuild/mcp 0.154.0 → 0.154.2

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,254 @@
1
+ function _toPrimitive(t, r) {
2
+ if ("object" != typeof t || !t) return t;
3
+ var e = t[Symbol.toPrimitive];
4
+ if (void 0 !== e) {
5
+ var i = e.call(t, r || "default");
6
+ if ("object" != typeof i) return i;
7
+ throw new TypeError("@@toPrimitive must return a primitive value.");
8
+ }
9
+ return ("string" === r ? String : Number)(t);
10
+ }
11
+
12
+ function _toPropertyKey(t) {
13
+ var i = _toPrimitive(t, "string");
14
+ return "symbol" == typeof i ? i : i + "";
15
+ }
16
+
17
+ function _defineProperty(e, r, t) {
18
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
19
+ value: t,
20
+ enumerable: !0,
21
+ configurable: !0,
22
+ writable: !0
23
+ }) : e[r] = t, e;
24
+ }
25
+
26
+ function ownKeys(e, r) {
27
+ var t = Object.keys(e);
28
+ if (Object.getOwnPropertySymbols) {
29
+ var o = Object.getOwnPropertySymbols(e);
30
+ r && (o = o.filter(function (r) {
31
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
32
+ })), t.push.apply(t, o);
33
+ }
34
+ return t;
35
+ }
36
+ function _objectSpread2(e) {
37
+ for (var r = 1; r < arguments.length; r++) {
38
+ var t = null != arguments[r] ? arguments[r] : {};
39
+ r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
40
+ _defineProperty(e, r, t[r]);
41
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
42
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
43
+ });
44
+ }
45
+ return e;
46
+ }
47
+
48
+ /**
49
+ * The public surface of Val's server-side tool registry.
50
+ *
51
+ * Types only, deliberately: this file is the contract that the MCP hosts, the
52
+ * CLI's stdio transport and the tools themselves are all written against, and
53
+ * keeping it free of implementation means those can be built in any order
54
+ * without one of them owning the shape.
55
+ *
56
+ * The design this implements is `docs/plans/mcp.md`, Part A. Two constraints
57
+ * from it are load-bearing and easy to break by accident:
58
+ *
59
+ * 1. **Nothing here may import an MCP SDK.** That is what lets hosts other
60
+ * than the template consume these tools, and it is not hypothetical
61
+ * hygiene — the TypeScript SDK reorganised itself at v2.0.0, and a registry
62
+ * coupled to it would have moved with it.
63
+ * 2. **The result type is deliberately not MCP's `CallToolResult`.** Each host
64
+ * adapts {@link ValToolResult} at its own edge, which is also where an
65
+ * error becomes an in-band `isError` result the model can recover from
66
+ * rather than a transport failure.
67
+ */
68
+
69
+ /** Why a tool call failed, in a form a host can map onto its own errors. */
70
+
71
+ /**
72
+ * The same definition with `inputSchema` as JSON Schema, for hosts that want the
73
+ * wire shape rather than a Standard Schema.
74
+ *
75
+ * Typed as whatever zod's own converter produces, so deriving it needs no cast
76
+ * and no second hand-written description of the same input.
77
+ */
78
+
79
+ /**
80
+ * How the caller was established, and there is one acceptable answer: the host
81
+ * **checked a signature**.
82
+ *
83
+ * A union of one, deliberately. It carried a second variant — a personal access
84
+ * token relayed to the backend unchecked, on the reasoning that the app cannot
85
+ * resolve one and the backend can. The reasoning held; the shape did not. A
86
+ * credential the host cannot check is one it also cannot refuse, so accepting
87
+ * one made "a deployed endpoint that authenticates nobody" a supported
88
+ * configuration, and it let a host serve these tools without ever being told
89
+ * where callers should authorize. The discriminant stays so that adding a
90
+ * second *verified* kind stays a one-line change at every call site.
91
+ */
92
+
93
+ /**
94
+ * Who is calling, established once per request by the host.
95
+ *
96
+ * `null` means local fs mode, where there is no credential to hold and patches
97
+ * are written with no author, exactly as the Studio does locally (D.1). In
98
+ * proxy mode `null` is refused rather than falling back to the app's own API
99
+ * key: that key can do more than any single user, and quietly substituting it
100
+ * would turn a missing credential into full access.
101
+ */
102
+
103
+ /**
104
+ * Brand a verified subject as an {@link AuthorId}.
105
+ *
106
+ * `AuthorId` is a branded string so that an id cannot be conjured from any
107
+ * string that happens to be lying around — which is exactly the mistake this
108
+ * type is guarding against. That makes one assertion unavoidable at the boundary
109
+ * where a real id enters the system, so it lives here, once, with a name that
110
+ * says what makes it legitimate: the caller has *verified* this subject, not
111
+ * received it.
112
+ *
113
+ * Do not reach for this to satisfy a type. If you are holding a string you did
114
+ * not verify, the honest value is `null`.
115
+ */
116
+ function authorIdFromVerifiedSubject(subject) {
117
+ return subject;
118
+ }
119
+
120
+ /** Read access. Every call needs it, the writes included. */
121
+ var VAL_SCOPE_READ = "val:read";
122
+ /** Write access. Needed *in addition* by any tool not marked `readOnlyHint`. */
123
+ var VAL_SCOPE_WRITE = "val:write";
124
+
125
+ /**
126
+ * The failure half of {@link ValToolResult}, on its own.
127
+ *
128
+ * Named because several internal steps can only fail — speculative validation
129
+ * rejecting a patch, an upload that did not land — and typing those as the
130
+ * whole union means the compiler cannot see that an early return is in fact an
131
+ * error return.
132
+ */
133
+
134
+ /**
135
+ * The one document an MCP client needs before it can authorize: RFC 9728
136
+ * Protected Resource Metadata, served by the *resource* server.
137
+ *
138
+ * This is how a client discovers where to authorize. It asks the resource — this
139
+ * app — and the resource names its authorization server. Which is why this
140
+ * belongs here and the RFC 8414 *authorization server* metadata does not: that
141
+ * document lives at the issuer, describes the issuer's own endpoints, and is
142
+ * served by the issuer. An app serving a copy would be asserting the issuer's
143
+ * configuration on its behalf, and would be wrong the moment the issuer changed
144
+ * anything.
145
+ *
146
+ * The flow, so the split reads as a whole:
147
+ *
148
+ * 1. client → `{app}/api/mcp` with no token → `401` naming this document
149
+ * 2. client → `{app}/.well-known/oauth-protected-resource` → the issuer
150
+ * 3. client → `{issuer}/.well-known/oauth-authorization-server` → endpoints
151
+ * 4. client → issuer's `/authorize`, then `/token`
152
+ * 5. client → `{app}/api/mcp` with the token
153
+ */
154
+
155
+ var CORS_HEADERS = {
156
+ // The document is public and contains no secrets — it exists to be read by
157
+ // clients whose origin we cannot know in advance, so `*` is the correct value
158
+ // rather than a lazy one. Note there is no `Access-Control-Allow-Credentials`:
159
+ // with it, `*` would be rejected by browsers, and this document is never
160
+ // fetched with credentials.
161
+ "Access-Control-Allow-Origin": "*",
162
+ "Access-Control-Allow-Methods": "GET, OPTIONS",
163
+ "Access-Control-Allow-Headers": "Content-Type, Authorization, MCP-Protocol-Version",
164
+ "Access-Control-Max-Age": "3600"
165
+ };
166
+ function createValMcpMetadata(oauth, scopesSupported) {
167
+ var document = {
168
+ // The resource identifier, which MUST be the value clients send as
169
+ // `resource` and the value that arrives back in `aud`. Same string as the
170
+ // audience this app verifies against — one value, so the two cannot drift.
171
+ resource: oauth.resource,
172
+ authorization_servers: [oauth.issuer],
173
+ scopes_supported: scopesSupported,
174
+ bearer_methods_supported: ["header"]
175
+ };
176
+ var body = JSON.stringify(document);
177
+ return {
178
+ GET: function GET() {
179
+ return new Response(body, {
180
+ status: 200,
181
+ headers: _objectSpread2({
182
+ "Content-Type": "application/json",
183
+ // Cacheable: it changes only when the app is reconfigured, and a
184
+ // client that re-reads it on every authorization costs a round trip
185
+ // for nothing.
186
+ "Cache-Control": "public, max-age=3600"
187
+ }, CORS_HEADERS)
188
+ });
189
+ },
190
+ OPTIONS: function OPTIONS() {
191
+ return new Response(null, {
192
+ status: 204,
193
+ headers: CORS_HEADERS
194
+ });
195
+ }
196
+ };
197
+ }
198
+
199
+ /** The scopes an MCP endpoint of Val's supports: read, and write. */
200
+ var VAL_SCOPES_SUPPORTED = [VAL_SCOPE_READ, VAL_SCOPE_WRITE];
201
+
202
+ /**
203
+ * The metadata handlers `initValMcp` serves as `valMcpMetadata`: `null`
204
+ * without an `oauth` config, since a project that has not been told where to
205
+ * authorize must not publish a document claiming it knows.
206
+ *
207
+ * One function, so a framework binding that builds `ValMcp` lazily (to keep
208
+ * the tools out of a site's cold start) answers this the same way.
209
+ */
210
+ function valMcpMetadataOf(oauth) {
211
+ return oauth ? createValMcpMetadata(oauth, VAL_SCOPES_SUPPORTED) : null;
212
+ }
213
+
214
+ /**
215
+ * The `WWW-Authenticate` value for a refusal (RFC 6750 section 3, RFC 9728
216
+ * section 5.1).
217
+ *
218
+ * `resource_metadata` is the load-bearing parameter: it is how a client that has
219
+ * never seen this server learns where to authorize. A `401` without it is a dead
220
+ * end — the client knows it needs a token and has no way to find out from where.
221
+ */
222
+ function wwwAuthenticate(oauth, scopesSupported, refusal) {
223
+ var metadataUrl = new URL("/.well-known/oauth-protected-resource", oauth.resource).toString();
224
+ var params = ["resource_metadata=\"".concat(metadataUrl, "\""), "scope=\"".concat(scopesSupported.join(" "), "\"")];
225
+ if (refusal) {
226
+ params.push("error=\"".concat(refusal.error, "\""));
227
+ params.push("error_description=\"".concat(headerSafe(refusal.description), "\""));
228
+ }
229
+ return "Bearer ".concat(params.join(", "));
230
+ }
231
+
232
+ /**
233
+ * Make a string safe to put inside a quoted header parameter.
234
+ *
235
+ * Three classes go, and the third is the one that matters most:
236
+ *
237
+ * - a **quote** would close the parameter early;
238
+ * - a **backslash** would start an escape the rest of the value does not
239
+ * finish;
240
+ * - a **CR or LF** would end the header line, which is response splitting — an
241
+ * attacker-influenced description could inject a header of their own, or a
242
+ * whole second response.
243
+ *
244
+ * The descriptions passed here today are all literals from this package and
245
+ * contain none of it. That is a property of today's callers rather than of the
246
+ * type, and this function exists so it stays true when a future one interpolates
247
+ * something from a request.
248
+ */
249
+ function headerSafe(value) {
250
+ // eslint-disable-next-line no-control-regex -- the point is to remove them
251
+ return value.replace(/["\\]/g, "").replace(/[\u0000-\u001f\u007f]/g, " ");
252
+ }
253
+
254
+ export { VAL_SCOPE_READ as V, _objectSpread2 as _, _defineProperty as a, VAL_SCOPE_WRITE as b, authorIdFromVerifiedSubject as c, VAL_SCOPES_SUPPORTED as d, createValMcpMetadata as e, valMcpMetadataOf as v, wwwAuthenticate as w };