@valbuild/mcp 0.150.23 → 0.152.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.
@@ -1,6 +1,6 @@
1
1
  import { _ as _asyncToGenerator, a as _regenerator } from './asyncToGenerator-500f022f.esm.js';
2
2
  import { z } from 'zod';
3
- import { createValOps, getSettings, resolveRemoteFileAuth, getFileExt, initHandlerOptions } from '@valbuild/server';
3
+ import { valRuntime } from '@valbuild/server/internal';
4
4
  import { Internal, getSourcePathFromRoute, DEFAULT_VAL_REMOTE_HOST } from '@valbuild/core';
5
5
  import { performSearch, createSearchIndex, indexModule, filterBlockingValidationErrors, describeContainerAtPath, chainHeadOf, safeParsePatch, buildDuplicatePatch, buildEmptyAtPathPatch, declaredLocales, buildRemoveImageGalleryEntryPatch, resolveEncodeSettings, resolveSerializedSchemaAtPath, withExtension, fitWithin, isSkippedSource, ENCODE_MIME_TYPE_OF, isMimeTypeAccepted, chooseEncoded, buildImageGalleryPatch } from '@valbuild/shared/internal';
6
6
  import { minimatch } from 'minimatch';
@@ -9,6 +9,7 @@ import { JSONOps, applyPatch, deepClone } from '@valbuild/core/patch';
9
9
  import { result } from '@valbuild/core/fp';
10
10
  import fs from 'node:fs/promises';
11
11
  import path from 'node:path';
12
+ import { getSettings, resolveRemoteFileAuth, getFileExt, initHandlerOptions } from '@valbuild/server';
12
13
 
13
14
  function _arrayWithHoles(r) {
14
15
  if (Array.isArray(r)) return r;
@@ -1430,7 +1431,7 @@ function _savePatch() {
1430
1431
  attempt = 0;
1431
1432
  case 10:
1432
1433
  if (!(attempt < 2)) {
1433
- _context4.n = 19;
1434
+ _context4.n = 20;
1434
1435
  break;
1435
1436
  }
1436
1437
  if (!(attempt > 0)) {
@@ -1471,7 +1472,7 @@ function _savePatch() {
1471
1472
  _context4.n = 16;
1472
1473
  break;
1473
1474
  }
1474
- return _context4.a(3, 18);
1475
+ return _context4.a(3, 19);
1475
1476
  case 16:
1476
1477
  if (!(saved.error.errorType === "plan-refused")) {
1477
1478
  _context4.n = 17;
@@ -1483,6 +1484,16 @@ function _savePatch() {
1483
1484
  message: saved.error.error.message
1484
1485
  });
1485
1486
  case 17:
1487
+ if (!(saved.error.errorType === "service-error")) {
1488
+ _context4.n = 18;
1489
+ break;
1490
+ }
1491
+ return _context4.a(2, {
1492
+ status: "error",
1493
+ code: "internal",
1494
+ message: "".concat(saved.error.message, " (").concat(saved.error.details, ")")
1495
+ });
1496
+ case 18:
1486
1497
  return _context4.a(2, {
1487
1498
  status: "error",
1488
1499
  code: "internal",
@@ -1490,11 +1501,11 @@ function _savePatch() {
1490
1501
  // `{ errorType: "other", error: <that> }`.
1491
1502
  message: saved.error.error.message
1492
1503
  });
1493
- case 18:
1504
+ case 19:
1494
1505
  attempt++;
1495
1506
  _context4.n = 10;
1496
1507
  break;
1497
- case 19:
1508
+ case 20:
1498
1509
  return _context4.a(2, {
1499
1510
  status: "error",
1500
1511
  code: "conflict",
@@ -1856,7 +1867,13 @@ var _excluded = ["handler"],
1856
1867
  */
1857
1868
  function createValTools(valModules, options) {
1858
1869
  var extraTools = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
1859
- var resolveOps = createOpsResolver(valModules, options);
1870
+ /*
1871
+ * The runtime every reader in this process shares (`valRuntime`): the API's,
1872
+ * the draft readers' and this registry's, so a write through any of them
1873
+ * is read by all, and the content is the head's -- made once per version,
1874
+ * not rebuilt per call.
1875
+ */
1876
+ var ops = valRuntime(valModules, options).ops;
1860
1877
  var tools = [].concat(_toConsumableArray(readTools()), _toConsumableArray(writeTools()), _toConsumableArray(extraTools));
1861
1878
  var byName = new Map();
1862
1879
  var _iterator = _createForOfIteratorHelper(tools),
@@ -1903,7 +1920,7 @@ function createValTools(valModules, options) {
1903
1920
  },
1904
1921
  call: function call(name, args, ctx) {
1905
1922
  return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
1906
- var tool, parsed, insufficient, resolved, ops, state, deps, _t;
1923
+ var tool, parsed, insufficient, refused, state, deps, _t;
1907
1924
  return _regenerator().w(function (_context) {
1908
1925
  while (1) switch (_context.p = _context.n) {
1909
1926
  case 0:
@@ -1938,37 +1955,36 @@ function createValTools(valModules, options) {
1938
1955
  }
1939
1956
  return _context.a(2, insufficient);
1940
1957
  case 3:
1941
- resolved = resolveOps(ctx);
1942
- if (!(resolved.status === "error")) {
1958
+ refused = refuseCaller(ctx, options);
1959
+ if (!(refused !== null)) {
1943
1960
  _context.n = 4;
1944
1961
  break;
1945
1962
  }
1946
- return _context.a(2, resolved.result);
1963
+ return _context.a(2, refused);
1947
1964
  case 4:
1948
- ops = resolved.ops;
1949
- _context.p = 5;
1950
- _context.n = 6;
1965
+ _context.p = 4;
1966
+ _context.n = 5;
1951
1967
  return loadState(ops);
1952
- case 6:
1968
+ case 5:
1953
1969
  state = _context.v;
1954
1970
  if (!(state.status === "error")) {
1955
- _context.n = 7;
1971
+ _context.n = 6;
1956
1972
  break;
1957
1973
  }
1958
1974
  return _context.a(2, state.result);
1959
- case 7:
1975
+ case 6:
1960
1976
  deps = {
1961
1977
  ops: ops,
1962
1978
  config: options,
1963
1979
  ctx: ctx,
1964
1980
  state: state.state
1965
1981
  };
1966
- _context.n = 8;
1982
+ _context.n = 7;
1967
1983
  return tool.handler(parsed.data, deps);
1968
- case 8:
1984
+ case 7:
1969
1985
  return _context.a(2, _context.v);
1970
- case 9:
1971
- _context.p = 9;
1986
+ case 8:
1987
+ _context.p = 8;
1972
1988
  _t = _context.v;
1973
1989
  return _context.a(2, {
1974
1990
  status: "error",
@@ -1976,7 +1992,7 @@ function createValTools(valModules, options) {
1976
1992
  message: _t instanceof Error ? _t.message : String(_t)
1977
1993
  });
1978
1994
  }
1979
- }, _callee, null, [[5, 9]]);
1995
+ }, _callee, null, [[4, 8]]);
1980
1996
  }))();
1981
1997
  },
1982
1998
  dispose: function dispose() {
@@ -1996,8 +2012,9 @@ function createValTools(valModules, options) {
1996
2012
  }
1997
2013
 
1998
2014
  /**
1999
- * Pick the data layer for a call, which in proxy mode means picking whose
2000
- * credential the backend will see.
2015
+ * Whether this caller may reach the data layer at all, which in proxy mode
2016
+ * means whether there is a credential the backend will act on. `null`: it
2017
+ * may.
2001
2018
  *
2002
2019
  * This is the one place authorization is decided, and in proxy mode there is
2003
2020
  * exactly one credential it will act on: an access token whose signature,
@@ -2018,97 +2035,64 @@ function createValTools(valModules, options) {
2018
2035
  * profile has to be one the host checked cryptographically, never one it was
2019
2036
  * handed. An `authenticate()` that decided a credential's rights inside the app
2020
2037
  * would make every bug in it full access to every project that key can reach.
2038
+ *
2039
+ * Every allowed caller reads the same data layer, and that is correct rather
2040
+ * than a shortcut: in fs mode it is a developer's own working tree, with no
2041
+ * credential to vary by, and in proxy mode it authenticates with the app's own
2042
+ * API key, so there is nothing per-caller in it to keep apart.
2021
2043
  */
2022
- function createOpsResolver(valModules, options) {
2044
+ function refuseCaller(ctx, options) {
2045
+ var _ctx$auth;
2023
2046
  if (options.mode === "fs") {
2024
- // One instance, built once: fs mode is a developer's own working tree, so
2025
- // there is no credential to vary by and no reason to re-evaluate modules.
2026
- var ops = createValOps(valModules, options);
2027
- return function (ctx) {
2028
- if (ctx.auth) {
2029
- // Refused rather than ignored. A host that thinks it is passing a
2030
- // credential should not silently get local filesystem access instead —
2031
- // and the difference matters, because fs mode writes straight to disk
2032
- // with no backend permission check at all.
2033
- //
2034
- // A verified access token is not something the caller chose to send: it
2035
- // only exists because this app advertised an authorization server, so
2036
- // the developer seeing this did not do anything wrong — a config file
2037
- // did, and naming it is the difference between a two-minute fix and an
2038
- // afternoon.
2039
- return {
2040
- status: "error",
2041
- result: {
2042
- status: "error",
2043
- code: "unsupported",
2044
- message: "This Val project is running in local filesystem mode, so there is nothing to authenticate against, but it is configured with an `oauth` issuer and is therefore asking clients for an access token it cannot use. Remove the `oauth` config (or `VAL_OAUTH_ISSUER` from your local `.env`) for local development."
2045
- }
2046
- };
2047
- }
2048
- return {
2049
- status: "ok",
2050
- ops: ops
2051
- };
2052
- };
2053
- }
2054
-
2055
- /**
2056
- * One instance for every verified caller, and that is correct rather than a
2057
- * shortcut: this instance authenticates with the app's own API key, so there
2058
- * is nothing per-caller in it to keep apart. Who did what travels as the
2059
- * patch's `authorId` instead — see `writePath`.
2060
- *
2061
- * One instance is also all proxy mode keeps now. It could not share while a
2062
- * personal access token reached this function: each token needed its own
2063
- * `ValOpsHttp` to hold it, each of those cached the project's evaluated
2064
- * modules, and the bounded cache that kept that memory in check turned an
2065
- * eviction into a re-evaluation of every module on the next call.
2066
- */
2067
- var sharedOps = null;
2068
- return function (ctx) {
2069
- var _ctx$auth;
2070
- if (((_ctx$auth = ctx.auth) === null || _ctx$auth === void 0 ? void 0 : _ctx$auth.type) !== "verified-profile") {
2047
+ if (ctx.auth) {
2048
+ // Refused rather than ignored. A host that thinks it is passing a
2049
+ // credential should not silently get local filesystem access instead —
2050
+ // and the difference matters, because fs mode writes straight to disk
2051
+ // with no backend permission check at all.
2052
+ //
2053
+ // A verified access token is not something the caller chose to send: it
2054
+ // only exists because this app advertised an authorization server, so
2055
+ // the developer seeing this did not do anything wrong — a config file
2056
+ // did, and naming it is the difference between a two-minute fix and an
2057
+ // afternoon.
2071
2058
  return {
2072
2059
  status: "error",
2073
- result: {
2074
- status: "error",
2075
- code: "forbidden",
2076
- message: "This Val project talks to the Val content backend, so every call needs an access token from the Val authorization server. If this endpoint is not asking clients to authorize, it has no `oauth` config — give `initValMcp` one, or run the project in local filesystem mode for development."
2077
- }
2060
+ code: "unsupported",
2061
+ message: "This Val project is running in local filesystem mode, so there is nothing to authenticate against, but it is configured with an `oauth` issuer and is therefore asking clients for an access token it cannot use. Remove the `oauth` config (or `VAL_OAUTH_ISSUER` from your local `.env`) for local development."
2078
2062
  };
2079
2063
  }
2080
- if (!options.apiKey) {
2081
- // Proxy mode is inferred from the api key being present, so this is
2082
- // unreachable through `initHandlerOptions`. It stays because the
2083
- // alternative to refusing is building ops with no credential at all.
2084
- return {
2085
- status: "error",
2086
- result: {
2087
- status: "error",
2088
- code: "forbidden",
2089
- message: "This Val project has no API key configured, so a verified access token cannot be exchanged for backend access."
2090
- }
2091
- };
2092
- }
2093
- if (!sharedOps) {
2094
- sharedOps = createValOps(valModules, options);
2095
- }
2064
+ return null;
2065
+ }
2066
+ if (((_ctx$auth = ctx.auth) === null || _ctx$auth === void 0 ? void 0 : _ctx$auth.type) !== "verified-profile") {
2096
2067
  return {
2097
- status: "ok",
2098
- ops: sharedOps
2068
+ status: "error",
2069
+ code: "forbidden",
2070
+ message: "This Val project talks to the Val content backend, so every call needs an access token from the Val authorization server. If this endpoint is not asking clients to authorize, it has no `oauth` config — give `initValMcp` one, or run the project in local filesystem mode for development."
2099
2071
  };
2100
- };
2072
+ }
2073
+ if (!options.apiKey) {
2074
+ // Proxy mode is inferred from the api key being present, so this is
2075
+ // unreachable through `initHandlerOptions`. It stays because the
2076
+ // alternative to refusing is acting with no credential at all.
2077
+ return {
2078
+ status: "error",
2079
+ code: "forbidden",
2080
+ message: "This Val project has no API key configured, so a verified access token cannot be exchanged for backend access."
2081
+ };
2082
+ }
2083
+ return null;
2101
2084
  }
2085
+
2102
2086
  /**
2103
- * The content as the caller should see it, loaded once per call.
2087
+ * The content as the caller should see it: the head's (`ValHead.tree`).
2104
2088
  *
2105
2089
  * Pending patches are applied, because an agent looking at a project mid-edit
2106
2090
  * should see what the Studio would show rather than the last published state.
2107
- *
2108
- * Deliberately not cached across calls. In fs mode a save recomputes the base
2109
- * sha within the same process, so a cached view would go stale silently — and
2110
- * the cost of being wrong here is an agent writing a patch against content that
2111
- * has already moved.
2091
+ * The head re-reads the chain only when it moved (`seq`), and makes each
2092
+ * patched module once per version, so a call after nothing changed costs one
2093
+ * "has it moved?" -- and a call after a write (a save in fs mode moves the
2094
+ * base too) sees it, so an agent never writes against content that has
2095
+ * already moved.
2112
2096
  *
2113
2097
  * Exported for `toolsFixture`, not for consumers: `tools/index.ts` re-exports by
2114
2098
  * name, so this stays inside the package. A test that assembled its own state
@@ -2119,17 +2103,19 @@ function loadState(_x) {
2119
2103
  }
2120
2104
  function _loadState() {
2121
2105
  _loadState = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(ops) {
2122
- var patches, analysis, sourcesRes, _yield$Promise$all, _yield$Promise$all2, schemas, serializedSchemas;
2106
+ var tree, _yield$Promise$all, _yield$Promise$all2, schemas, serialized, serializedSchemas, isModuleFilePath, _i, _Object$entries, _Object$entries$_i, path, schema;
2123
2107
  return _regenerator().w(function (_context3) {
2124
2108
  while (1) switch (_context3.n) {
2125
2109
  case 0:
2126
2110
  _context3.n = 1;
2127
- return ops.fetchPatches({
2128
- excludePatchOps: false
2129
- });
2111
+ return ops.head.tree();
2130
2112
  case 1:
2131
- patches = _context3.v;
2132
- if (!patches.unauthorized) {
2113
+ tree = _context3.v;
2114
+ if (!("error" in tree)) {
2115
+ _context3.n = 4;
2116
+ break;
2117
+ }
2118
+ if (!tree.error.unauthorized) {
2133
2119
  _context3.n = 2;
2134
2120
  break;
2135
2121
  }
@@ -2142,7 +2128,7 @@ function _loadState() {
2142
2128
  }
2143
2129
  });
2144
2130
  case 2:
2145
- if (!patches.networkError) {
2131
+ if (!tree.error.networkError) {
2146
2132
  _context3.n = 3;
2147
2133
  break;
2148
2134
  }
@@ -2155,48 +2141,46 @@ function _loadState() {
2155
2141
  }
2156
2142
  });
2157
2143
  case 3:
2158
- if (!patches.error) {
2159
- _context3.n = 4;
2160
- break;
2161
- }
2162
2144
  return _context3.a(2, {
2163
2145
  status: "error",
2164
2146
  result: {
2165
2147
  status: "error",
2166
2148
  code: "internal",
2167
- message: patches.error.message
2149
+ message: tree.error.message
2168
2150
  }
2169
2151
  });
2170
2152
  case 4:
2171
- analysis = ops.analyzePatches(patches.patches); // getSourcesWithPatchesApplied, not getSources(analysis): the latter returns
2172
- // only the modules that had patches, and validating that subset reports
2173
- // spurious errors for anything that looks across modules, like keyOf or a
2174
- // router.
2175
2153
  _context3.n = 5;
2176
- return ops.getSourcesWithPatchesApplied(_objectSpread2(_objectSpread2({}, analysis), patches));
2154
+ return Promise.all([ops.getSchemas(), ops.head.serializedSchemas()]);
2177
2155
  case 5:
2178
- sourcesRes = _context3.v;
2179
- _context3.n = 6;
2180
- return Promise.all([ops.getSchemas(), ops.getSerializedSchemas()]);
2181
- case 6:
2182
2156
  _yield$Promise$all = _context3.v;
2183
2157
  _yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 2);
2184
2158
  schemas = _yield$Promise$all2[0];
2185
- serializedSchemas = _yield$Promise$all2[1];
2159
+ serialized = _yield$Promise$all2[1];
2160
+ serializedSchemas = {}; // Keys of `serialized` are the module paths it was written under.
2161
+ isModuleFilePath = function isModuleFilePath(key) {
2162
+ return Object.prototype.hasOwnProperty.call(serialized, key);
2163
+ };
2164
+ for (_i = 0, _Object$entries = Object.entries(serialized); _i < _Object$entries.length; _i++) {
2165
+ _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), path = _Object$entries$_i[0], schema = _Object$entries$_i[1];
2166
+ if (schema !== undefined && isModuleFilePath(path)) {
2167
+ serializedSchemas[path] = schema;
2168
+ }
2169
+ }
2186
2170
  return _context3.a(2, {
2187
2171
  status: "ok",
2188
2172
  state: {
2189
2173
  schemas: schemas,
2190
2174
  serializedSchemas: serializedSchemas,
2191
- sources: sourcesRes.sources,
2192
- patches: patches,
2193
- analysis: analysis,
2175
+ sources: tree.ok.sources,
2176
+ patches: tree.ok.chain.fetched,
2177
+ analysis: tree.ok.chain.analysis,
2194
2178
  // Which modules hold a pending patch that would not apply. Carried rather
2195
2179
  // than discarded because their `sources` silently lack that change: the
2196
2180
  // content here is not what publishing would produce, so a write against
2197
2181
  // it would be based on a state that does not exist. See
2198
2182
  // `unappliedPatchesFor`.
2199
- unappliedPatches: sourcesRes.errors
2183
+ unappliedPatches: tree.ok.errors
2200
2184
  }
2201
2185
  });
2202
2186
  }
@@ -3365,7 +3349,7 @@ var packageJson = {
3365
3349
  "./package.json": "./package.json"
3366
3350
  },
3367
3351
  types: "dist/valbuild-mcp.cjs.d.ts",
3368
- version: "0.150.23",
3352
+ version: "0.152.0",
3369
3353
  scripts: {
3370
3354
  typecheck: "tsc --noEmit",
3371
3355
  test: "jest",
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./package.json": "./package.json"
21
21
  },
22
22
  "types": "dist/valbuild-mcp.cjs.d.ts",
23
- "version": "0.150.23",
23
+ "version": "0.152.0",
24
24
  "preconstruct": {
25
25
  "entrypoints": [
26
26
  "./index.ts",
@@ -31,9 +31,9 @@
31
31
  "dependencies": {
32
32
  "minimatch": "^10.2.6",
33
33
  "zod": "^4.4.3",
34
- "@valbuild/server": "0.150.23",
35
- "@valbuild/shared": "0.150.20",
36
- "@valbuild/core": "0.150.3"
34
+ "@valbuild/core": "0.151.0",
35
+ "@valbuild/shared": "0.152.0",
36
+ "@valbuild/server": "0.152.0"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@prettier/sync": "^0.6.1",