@milaboratories/pl-mcp-server 53.0.0 → 54.0.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.
Files changed (103) hide show
  1. package/README.md +36 -2
  2. package/dist/instructions.cjs +3 -2
  3. package/dist/instructions.cjs.map +1 -1
  4. package/dist/instructions.js +3 -2
  5. package/dist/instructions.js.map +1 -1
  6. package/dist/package.cjs +1 -1
  7. package/dist/package.js +1 -1
  8. package/dist/project-holds.cjs +72 -0
  9. package/dist/project-holds.cjs.map +1 -0
  10. package/dist/project-holds.js +72 -0
  11. package/dist/project-holds.js.map +1 -0
  12. package/dist/server-log.cjs +373 -0
  13. package/dist/server-log.cjs.map +1 -0
  14. package/dist/server-log.js +371 -0
  15. package/dist/server-log.js.map +1 -0
  16. package/dist/server.cjs +77 -22
  17. package/dist/server.cjs.map +1 -1
  18. package/dist/server.d.ts +31 -7
  19. package/dist/server.d.ts.map +1 -1
  20. package/dist/server.js +77 -23
  21. package/dist/server.js.map +1 -1
  22. package/dist/tools/await.cjs +59 -60
  23. package/dist/tools/await.cjs.map +1 -1
  24. package/dist/tools/await.js +60 -61
  25. package/dist/tools/await.js.map +1 -1
  26. package/dist/tools/block-action-context.cjs +11 -9
  27. package/dist/tools/block-action-context.cjs.map +1 -1
  28. package/dist/tools/block-action-context.js +11 -9
  29. package/dist/tools/block-action-context.js.map +1 -1
  30. package/dist/tools/block-params.cjs +4 -3
  31. package/dist/tools/block-params.cjs.map +1 -1
  32. package/dist/tools/block-params.js +4 -3
  33. package/dist/tools/block-params.js.map +1 -1
  34. package/dist/tools/block-state.cjs +46 -41
  35. package/dist/tools/block-state.cjs.map +1 -1
  36. package/dist/tools/block-state.js +47 -42
  37. package/dist/tools/block-state.js.map +1 -1
  38. package/dist/tools/blocks.cjs +30 -20
  39. package/dist/tools/blocks.cjs.map +1 -1
  40. package/dist/tools/blocks.js +30 -20
  41. package/dist/tools/blocks.js.map +1 -1
  42. package/dist/tools/connection.cjs.map +1 -1
  43. package/dist/tools/connection.js.map +1 -1
  44. package/dist/tools/data-query.cjs +111 -80
  45. package/dist/tools/data-query.cjs.map +1 -1
  46. package/dist/tools/data-query.js +114 -83
  47. package/dist/tools/data-query.js.map +1 -1
  48. package/dist/tools/ids.cjs +16 -0
  49. package/dist/tools/ids.cjs.map +1 -1
  50. package/dist/tools/ids.js +13 -1
  51. package/dist/tools/ids.js.map +1 -1
  52. package/dist/tools/logs.cjs +28 -26
  53. package/dist/tools/logs.cjs.map +1 -1
  54. package/dist/tools/logs.js +28 -26
  55. package/dist/tools/logs.js.map +1 -1
  56. package/dist/tools/ping.cjs.map +1 -1
  57. package/dist/tools/ping.js.map +1 -1
  58. package/dist/tools/projects.cjs +10 -27
  59. package/dist/tools/projects.cjs.map +1 -1
  60. package/dist/tools/projects.js +11 -28
  61. package/dist/tools/projects.js.map +1 -1
  62. package/dist/tools/sandbox.cjs +110 -8
  63. package/dist/tools/sandbox.cjs.map +1 -1
  64. package/dist/tools/sandbox.js +107 -9
  65. package/dist/tools/sandbox.js.map +1 -1
  66. package/dist/tools/screenshot.cjs +9 -1
  67. package/dist/tools/screenshot.cjs.map +1 -1
  68. package/dist/tools/screenshot.js +9 -1
  69. package/dist/tools/screenshot.js.map +1 -1
  70. package/dist/tools/types.cjs +29 -0
  71. package/dist/tools/types.cjs.map +1 -1
  72. package/dist/tools/types.js +27 -1
  73. package/dist/tools/types.js.map +1 -1
  74. package/dist/tools/ui-interaction.cjs.map +1 -1
  75. package/dist/tools/ui-interaction.js.map +1 -1
  76. package/package.json +6 -5
  77. package/src/instructions.ts +3 -2
  78. package/src/project-holds.test.ts +183 -0
  79. package/src/project-holds.ts +105 -0
  80. package/src/server-log.test.ts +871 -0
  81. package/src/server-log.ts +504 -0
  82. package/src/server.logging.test.ts +383 -0
  83. package/src/server.request-failure.test.ts +49 -0
  84. package/src/server.test.ts +15 -5
  85. package/src/server.ts +111 -26
  86. package/src/test/recording-logger.ts +33 -0
  87. package/src/tools/await.ts +96 -98
  88. package/src/tools/block-action-context.ts +16 -16
  89. package/src/tools/block-params.ts +6 -6
  90. package/src/tools/block-state.ts +61 -60
  91. package/src/tools/blocks.ts +49 -40
  92. package/src/tools/connection.ts +2 -3
  93. package/src/tools/data-query.ts +151 -108
  94. package/src/tools/ids.ts +22 -1
  95. package/src/tools/logs.ts +43 -43
  96. package/src/tools/ping.ts +2 -3
  97. package/src/tools/projects.ts +22 -45
  98. package/src/tools/sandbox.test.ts +183 -0
  99. package/src/tools/sandbox.ts +126 -10
  100. package/src/tools/screenshot.ts +15 -4
  101. package/src/tools/types.test.ts +4 -3
  102. package/src/tools/types.ts +73 -3
  103. package/src/tools/ui-interaction.ts +2 -3
package/README.md CHANGED
@@ -47,7 +47,7 @@ Claude Code ──MCP──▶ pl-mcp-server (worker) ──IPC──▶ Des
47
47
 
48
48
  4. **Connect to the backend** — use `list_connections` to see already saved connections. You could easily reuse them. Or call `connect_to_server` directly with URL, username and password to create new connection.
49
49
 
50
- 5. **Open a project** — use `open_project` to start working with a specific project.
50
+ 5. **Pick a project** — use `list_projects` or `create_project`. Every tool that works on a project takes its `projectId`.
51
51
 
52
52
  ## Connecting From Claude Desktop App (Chat)
53
53
 
@@ -70,7 +70,7 @@ Claude Desktop App uses stdio transport, so it cannot connect to the HTTP MCP en
70
70
 
71
71
  Replace the URL with the actual MCP Server URL from the Platforma Desktop App settings.
72
72
 
73
- 3. **Restart Claude Desktop App** — the Platforma tools will appear in your chat sessions. Follow the same workflow: `list_connections` -> `connect_to_server` -> `open_project`.
73
+ 3. **Restart Claude Desktop App** — the Platforma tools will appear in your chat sessions. Follow the same workflow: `list_connections` -> `connect_to_server` -> `list_projects`.
74
74
 
75
75
  ## Skill
76
76
 
@@ -82,6 +82,40 @@ The desktop app uses Electron with potentially multiple WebContents layers. Scre
82
82
 
83
83
  To click on an element visible in a screenshot: divide the screenshot pixel coordinates by the device pixel ratio (typically 2 on macOS Retina).
84
84
 
85
+ ## Log
86
+
87
+ Give the server a logger (`PlMcpServerOptions.logger`, the `MiLogger` of the middle layer) and it writes what it does; without one it writes nothing. Every entry is one string that starts with its kind and continues with `key=value` fields; a value that is not a plain token is written as a JSON string.
88
+
89
+ | Kind | Level | When |
90
+ | --------------------- | ----- | ---------------------------------------------------------------------- |
91
+ | `mcp-call` | info | A tool call ended. One line per call that reached its handler. |
92
+ | `mcp-call-failed` | error | The call threw or returned an error; follows its `mcp-call` line. |
93
+ | `mcp-call-timeout` | warn | A wait (`await_block_done`) answered that it ran out of time. |
94
+ | `mcp-call-slow` | warn | The call took over 5 s. Not written for `await_block_done`. |
95
+ | `mcp-sdk-error` | error | The MCP handler refused a request or failed outside a tool handler. |
96
+ | `mcp-request-failed` | error | An HTTP request failed outside the MCP handler (answered 500). |
97
+ | `mcp-server-start` | info | The server listens. The secret in the URL is replaced with `<secret>`. |
98
+ | `mcp-server-stop` | info | The server stopped; `calls` is how many tool calls it served. |
99
+ | `mcp-project-release` | info | The server gave back the hold it kept on a project between calls. |
100
+
101
+ ```
102
+ mcp-call call=mcp-1a2b3c4d#57 tool=set_block_data project=NG:0x35B1EB block=0b6d2a3e-5f0c-4c1d-9e7a-2f4b8c6d1e90 argsBytes=111 ms=107 outcome=ok resultBytes=52 inflight=0 txConflictsDuring=0
103
+ ```
104
+
105
+ - `call`: the id of the server instance (also the author id of the writes it makes) and the number of the call. The other lines about the same call carry the same id.
106
+ - `project`, `block`: the `projectId` and `blockId` arguments, when the call has them.
107
+ - `argsBytes`: the size of the arguments as JSON, in bytes.
108
+ - `resultBytes`: the size of the content of the result: the UTF-8 bytes of its texts plus the length of its base64 data (images, binary resources). The JSON around the content is not counted. 0 when the handler threw.
109
+ - `outcome`: `ok`, `error` (the handler returned `isError: true`) or `thrown`.
110
+ - `inflight`: other calls that were being served when this one started.
111
+ - `txConflictsDuring`: commit conflicts of the backend client between the start and the end of the call. The client counts them for all its work, so calls in flight at the same time report the same conflicts. Absent without a backend.
112
+
113
+ `mcp-project-release` carries `id` (the server instance), `project` and `reason`: `idle` (no call on the project for `projectIdleReleaseMs`), `release-all` (the server stopped or was given another middle layer) or `dropped` (the hold was kept on a middle layer the server no longer uses). A hold the middle layer ends itself, because the project was deleted or the middle layer closed, writes no line.
114
+
115
+ `mcp-call-failed` adds `argKeys` (the names of the top-level arguments) and `error` (the first line of the error); the lines after the first hold the whole error as it is: for a thrown one what Node writes of it (`util.inspect`: the stack, other properties and every error of the `cause` chain with its stack, six levels deep), for an error result its text, the error of a failed `transform` included.
116
+
117
+ The log is written like the rest of the application's log: the line of a call holds of its arguments only the `projectId` and `blockId`, their size and their top-level names, but the text of an error is not cut, and it can quote argument values, paths and block data. Secrets are kept out of every line and replaced with `<secret>`: the secret of the URL, and the values of the secret arguments, found by name (`password`, `token`, `secret`, `apiKey`). They are replaced before anything is cut, so no part of one is left. A call refused before its handler (arguments that do not fit the schema, an unknown tool) writes no line.
118
+
85
119
  ## Development
86
120
 
87
121
  ```bash
@@ -11,7 +11,7 @@ const SERVER_INSTRUCTIONS = `Platforma Desktop: build and run data analysis proj
11
11
 
12
12
  Order of work:
13
13
  1. Connection: get_connection_status. If not connected: list_connections, then connect_to_server.
14
- 2. Project: list_projects or create_project, then open_project. Every block tool needs an opened project.
14
+ 2. Project: list_projects finds a project, create_project makes a new one. Every tool that works on a project takes its projectId.
15
15
  3. Add blocks: list_available_blocks finds a block; add_block adds it, with params to configure it at creation. Add blocks in the order the data flows.
16
16
  4. Configure a block: get_block_state returns the block's data and outputs. Change the data and write the whole object back with set_block_data. The choices a block offers for its inputs (datasets, columns of blocks above) are in its outputs: get_block_outputs.
17
17
  5. Check and run: get_project_overview shows for each block whether its inputs are valid and whether it can run. run_block starts a block together with the stale blocks above it; await_block_done waits for it.
@@ -22,7 +22,8 @@ Rules:
22
22
  - set_block_data replaces the block's data: send the whole object, not the changed part.
23
23
  - Changing a block's data can make its results, and the results of blocks that read them, stale until they run again.
24
24
  - Only blocks on the current block API can be added.
25
- - capture_screenshot, click, type_text, press_key, scroll, execute_js and select_block drive the application window. They are for testing the application; configuring and running blocks does not need them.`;
25
+ - select_block shows a block to the user in the application; use it only when the user asks to be shown something.
26
+ - capture_screenshot, click, type_text, press_key, scroll and execute_js drive the application window. They are for testing the application; configuring and running blocks does not need them.`;
26
27
  exports.SERVER_INSTRUCTIONS = SERVER_INSTRUCTIONS;
27
28
 
28
29
  //# sourceMappingURL=instructions.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"instructions.cjs","names":[],"sources":["../src/instructions.ts"],"sourcesContent":["/**\n * What a client is told about the server before it has seen any tool: what the server is for and\n * the order the tools are used in. Clients that load tools on demand show the model this text and\n * the tool names only, so the order of work has to be readable from here.\n *\n * Kept under 4096 characters, the length some clients cut instructions at, with the order of work\n * first. It repeats nothing a tool description says about that tool's own arguments.\n */\nexport const SERVER_INSTRUCTIONS = `Platforma Desktop: build and run data analysis projects. A project is an ordered list of blocks; each block is one analysis step with its own configuration, and reads the results of blocks above it.\n\nOrder of work:\n1. Connection: get_connection_status. If not connected: list_connections, then connect_to_server.\n2. Project: list_projects or create_project, then open_project. Every block tool needs an opened project.\n3. Add blocks: list_available_blocks finds a block; add_block adds it, with params to configure it at creation. Add blocks in the order the data flows.\n4. Configure a block: get_block_state returns the block's data and outputs. Change the data and write the whole object back with set_block_data. The choices a block offers for its inputs (datasets, columns of blocks above) are in its outputs: get_block_outputs.\n5. Check and run: get_project_overview shows for each block whether its inputs are valid and whether it can run. run_block starts a block together with the stale blocks above it; await_block_done waits for it.\n6. Results: get_block_outputs, then list_columns and query_table for tables, get_block_logs for logs.\n\nRules:\n- Project ids, block ids, references to blocks and columns, and table handles come from tool results. Copy them; do not compose them.\n- set_block_data replaces the block's data: send the whole object, not the changed part.\n- Changing a block's data can make its results, and the results of blocks that read them, stale until they run again.\n- Only blocks on the current block API can be added.\n- capture_screenshot, click, type_text, press_key, scroll, execute_js and select_block drive the application window. They are for testing the application; configuring and running blocks does not need them.`;\n"],"mappings":";;;;;;;;;AAQA,MAAa,sBAAsB"}
1
+ {"version":3,"file":"instructions.cjs","names":[],"sources":["../src/instructions.ts"],"sourcesContent":["/**\n * What a client is told about the server before it has seen any tool: what the server is for and\n * the order the tools are used in. Clients that load tools on demand show the model this text and\n * the tool names only, so the order of work has to be readable from here.\n *\n * Kept under 4096 characters, the length some clients cut instructions at, with the order of work\n * first. It repeats nothing a tool description says about that tool's own arguments.\n */\nexport const SERVER_INSTRUCTIONS = `Platforma Desktop: build and run data analysis projects. A project is an ordered list of blocks; each block is one analysis step with its own configuration, and reads the results of blocks above it.\n\nOrder of work:\n1. Connection: get_connection_status. If not connected: list_connections, then connect_to_server.\n2. Project: list_projects finds a project, create_project makes a new one. Every tool that works on a project takes its projectId.\n3. Add blocks: list_available_blocks finds a block; add_block adds it, with params to configure it at creation. Add blocks in the order the data flows.\n4. Configure a block: get_block_state returns the block's data and outputs. Change the data and write the whole object back with set_block_data. The choices a block offers for its inputs (datasets, columns of blocks above) are in its outputs: get_block_outputs.\n5. Check and run: get_project_overview shows for each block whether its inputs are valid and whether it can run. run_block starts a block together with the stale blocks above it; await_block_done waits for it.\n6. Results: get_block_outputs, then list_columns and query_table for tables, get_block_logs for logs.\n\nRules:\n- Project ids, block ids, references to blocks and columns, and table handles come from tool results. Copy them; do not compose them.\n- set_block_data replaces the block's data: send the whole object, not the changed part.\n- Changing a block's data can make its results, and the results of blocks that read them, stale until they run again.\n- Only blocks on the current block API can be added.\n- select_block shows a block to the user in the application; use it only when the user asks to be shown something.\n- capture_screenshot, click, type_text, press_key, scroll and execute_js drive the application window. They are for testing the application; configuring and running blocks does not need them.`;\n"],"mappings":";;;;;;;;;AAQA,MAAa,sBAAsB"}
@@ -11,7 +11,7 @@ const SERVER_INSTRUCTIONS = `Platforma Desktop: build and run data analysis proj
11
11
 
12
12
  Order of work:
13
13
  1. Connection: get_connection_status. If not connected: list_connections, then connect_to_server.
14
- 2. Project: list_projects or create_project, then open_project. Every block tool needs an opened project.
14
+ 2. Project: list_projects finds a project, create_project makes a new one. Every tool that works on a project takes its projectId.
15
15
  3. Add blocks: list_available_blocks finds a block; add_block adds it, with params to configure it at creation. Add blocks in the order the data flows.
16
16
  4. Configure a block: get_block_state returns the block's data and outputs. Change the data and write the whole object back with set_block_data. The choices a block offers for its inputs (datasets, columns of blocks above) are in its outputs: get_block_outputs.
17
17
  5. Check and run: get_project_overview shows for each block whether its inputs are valid and whether it can run. run_block starts a block together with the stale blocks above it; await_block_done waits for it.
@@ -22,7 +22,8 @@ Rules:
22
22
  - set_block_data replaces the block's data: send the whole object, not the changed part.
23
23
  - Changing a block's data can make its results, and the results of blocks that read them, stale until they run again.
24
24
  - Only blocks on the current block API can be added.
25
- - capture_screenshot, click, type_text, press_key, scroll, execute_js and select_block drive the application window. They are for testing the application; configuring and running blocks does not need them.`;
25
+ - select_block shows a block to the user in the application; use it only when the user asks to be shown something.
26
+ - capture_screenshot, click, type_text, press_key, scroll and execute_js drive the application window. They are for testing the application; configuring and running blocks does not need them.`;
26
27
  export { SERVER_INSTRUCTIONS };
27
28
 
28
29
  //# sourceMappingURL=instructions.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"instructions.js","names":[],"sources":["../src/instructions.ts"],"sourcesContent":["/**\n * What a client is told about the server before it has seen any tool: what the server is for and\n * the order the tools are used in. Clients that load tools on demand show the model this text and\n * the tool names only, so the order of work has to be readable from here.\n *\n * Kept under 4096 characters, the length some clients cut instructions at, with the order of work\n * first. It repeats nothing a tool description says about that tool's own arguments.\n */\nexport const SERVER_INSTRUCTIONS = `Platforma Desktop: build and run data analysis projects. A project is an ordered list of blocks; each block is one analysis step with its own configuration, and reads the results of blocks above it.\n\nOrder of work:\n1. Connection: get_connection_status. If not connected: list_connections, then connect_to_server.\n2. Project: list_projects or create_project, then open_project. Every block tool needs an opened project.\n3. Add blocks: list_available_blocks finds a block; add_block adds it, with params to configure it at creation. Add blocks in the order the data flows.\n4. Configure a block: get_block_state returns the block's data and outputs. Change the data and write the whole object back with set_block_data. The choices a block offers for its inputs (datasets, columns of blocks above) are in its outputs: get_block_outputs.\n5. Check and run: get_project_overview shows for each block whether its inputs are valid and whether it can run. run_block starts a block together with the stale blocks above it; await_block_done waits for it.\n6. Results: get_block_outputs, then list_columns and query_table for tables, get_block_logs for logs.\n\nRules:\n- Project ids, block ids, references to blocks and columns, and table handles come from tool results. Copy them; do not compose them.\n- set_block_data replaces the block's data: send the whole object, not the changed part.\n- Changing a block's data can make its results, and the results of blocks that read them, stale until they run again.\n- Only blocks on the current block API can be added.\n- capture_screenshot, click, type_text, press_key, scroll, execute_js and select_block drive the application window. They are for testing the application; configuring and running blocks does not need them.`;\n"],"mappings":";;;;;;;;;AAQA,MAAa,sBAAsB"}
1
+ {"version":3,"file":"instructions.js","names":[],"sources":["../src/instructions.ts"],"sourcesContent":["/**\n * What a client is told about the server before it has seen any tool: what the server is for and\n * the order the tools are used in. Clients that load tools on demand show the model this text and\n * the tool names only, so the order of work has to be readable from here.\n *\n * Kept under 4096 characters, the length some clients cut instructions at, with the order of work\n * first. It repeats nothing a tool description says about that tool's own arguments.\n */\nexport const SERVER_INSTRUCTIONS = `Platforma Desktop: build and run data analysis projects. A project is an ordered list of blocks; each block is one analysis step with its own configuration, and reads the results of blocks above it.\n\nOrder of work:\n1. Connection: get_connection_status. If not connected: list_connections, then connect_to_server.\n2. Project: list_projects finds a project, create_project makes a new one. Every tool that works on a project takes its projectId.\n3. Add blocks: list_available_blocks finds a block; add_block adds it, with params to configure it at creation. Add blocks in the order the data flows.\n4. Configure a block: get_block_state returns the block's data and outputs. Change the data and write the whole object back with set_block_data. The choices a block offers for its inputs (datasets, columns of blocks above) are in its outputs: get_block_outputs.\n5. Check and run: get_project_overview shows for each block whether its inputs are valid and whether it can run. run_block starts a block together with the stale blocks above it; await_block_done waits for it.\n6. Results: get_block_outputs, then list_columns and query_table for tables, get_block_logs for logs.\n\nRules:\n- Project ids, block ids, references to blocks and columns, and table handles come from tool results. Copy them; do not compose them.\n- set_block_data replaces the block's data: send the whole object, not the changed part.\n- Changing a block's data can make its results, and the results of blocks that read them, stale until they run again.\n- Only blocks on the current block API can be added.\n- select_block shows a block to the user in the application; use it only when the user asks to be shown something.\n- capture_screenshot, click, type_text, press_key, scroll and execute_js drive the application window. They are for testing the application; configuring and running blocks does not need them.`;\n"],"mappings":";;;;;;;;;AAQA,MAAa,sBAAsB"}
package/dist/package.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // package.json
2
- var version = "53.0.0";
2
+ var version = "54.0.0";
3
3
  Object.defineProperty(exports, "version", {
4
4
  enumerable: true,
5
5
  get: function() {
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // package.json
2
- var version = "53.0.0";
2
+ var version = "54.0.0";
3
3
  export { version };
4
4
 
5
5
  //# sourceMappingURL=package.js.map
@@ -0,0 +1,72 @@
1
+ // src/project-holds.ts
2
+ /**
3
+ * The MCP server's holds on projects.
4
+ *
5
+ * Every call takes a hold of its own for as long as it runs, so nobody else's release can destroy
6
+ * the project under it. When the call ends, one hold per project is kept for `idleMs` more, so a
7
+ * run of calls on a project does not load it again for each call; the idle time restarts with
8
+ * every call. A kept hold the middle layer ends (the project was deleted, the middle layer closed)
9
+ * is forgotten.
10
+ */
11
+ var ProjectHolds = class {
12
+ options;
13
+ kept = /* @__PURE__ */ new Map();
14
+ /** Counts {@link releaseAll}: a call that started before the last one keeps nothing. */
15
+ generation = 0;
16
+ constructor(options) {
17
+ this.options = options;
18
+ }
19
+ /** Runs `fn` on the project while holding it. */
20
+ async use(source, id, fn) {
21
+ const generation = this.generation;
22
+ const hold = await source.acquireProject(id, this.options.holder);
23
+ try {
24
+ return await fn(hold.project);
25
+ } finally {
26
+ if (generation === this.generation) this.keep(id, hold);
27
+ else hold.release();
28
+ }
29
+ }
30
+ /**
31
+ * Gives back every kept hold. Holds of calls still running are given back when those calls end,
32
+ * not kept.
33
+ */
34
+ releaseAll() {
35
+ this.generation++;
36
+ for (const id of this.kept.keys()) this.release(id, "release-all");
37
+ }
38
+ /** Makes the hold of a finished call the kept one, or gives it back when one is kept already. */
39
+ keep(id, hold) {
40
+ if (hold.ended) return;
41
+ const kept = this.kept.get(id);
42
+ if (kept !== void 0) {
43
+ hold.release();
44
+ kept.timer.refresh();
45
+ return;
46
+ }
47
+ const entry = {
48
+ hold,
49
+ timer: setTimeout(() => {
50
+ if (this.kept.get(id) === entry) this.release(id, "idle");
51
+ }, this.options.idleMs)
52
+ };
53
+ entry.timer.unref();
54
+ hold.signal.addEventListener("abort", () => {
55
+ if (this.kept.get(id) !== entry) return;
56
+ clearTimeout(entry.timer);
57
+ this.kept.delete(id);
58
+ }, { once: true });
59
+ this.kept.set(id, entry);
60
+ }
61
+ release(id, reason) {
62
+ const entry = this.kept.get(id);
63
+ if (entry === void 0) return;
64
+ clearTimeout(entry.timer);
65
+ this.kept.delete(id);
66
+ entry.hold.release();
67
+ this.options.onRelease?.(id, reason);
68
+ }
69
+ };
70
+ exports.ProjectHolds = ProjectHolds;
71
+
72
+ //# sourceMappingURL=project-holds.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"project-holds.cjs","names":[],"sources":["../src/project-holds.ts"],"sourcesContent":["import type { ProjectHolder, ProjectId } from \"@milaboratories/pl-middle-layer\";\n\n/** A hold on a loaded project, as {@link ProjectHolds} uses it: the middle layer's `ProjectHandle`. */\nexport interface Hold<P> {\n readonly project: P;\n readonly signal: AbortSignal;\n readonly ended: boolean;\n release(): Promise<void>;\n}\n\n/** Where holds come from: the middle layer, or a fake of it in tests. */\nexport interface HoldSource<P> {\n acquireProject(id: ProjectId, holder: ProjectHolder): Promise<Hold<P>>;\n}\n\n/** Why {@link ProjectHolds} gave a kept hold back. */\nexport type KeptHoldRelease = \"idle\" | \"release-all\";\n\nexport interface ProjectHoldsOptions {\n /** The holder every hold is taken under. */\n holder: ProjectHolder;\n /** How long a project stays held after the last call on it ended. */\n idleMs: number;\n /** Told about every kept hold given back. */\n onRelease?: (id: ProjectId, reason: KeptHoldRelease) => void;\n}\n\ntype Kept<P> = { hold: Hold<P>; timer: NodeJS.Timeout };\n\n/**\n * The MCP server's holds on projects.\n *\n * Every call takes a hold of its own for as long as it runs, so nobody else's release can destroy\n * the project under it. When the call ends, one hold per project is kept for `idleMs` more, so a\n * run of calls on a project does not load it again for each call; the idle time restarts with\n * every call. A kept hold the middle layer ends (the project was deleted, the middle layer closed)\n * is forgotten.\n */\nexport class ProjectHolds<P> {\n private readonly kept = new Map<ProjectId, Kept<P>>();\n /** Counts {@link releaseAll}: a call that started before the last one keeps nothing. */\n private generation = 0;\n\n constructor(private readonly options: ProjectHoldsOptions) {}\n\n /** Runs `fn` on the project while holding it. */\n async use<T>(source: HoldSource<P>, id: ProjectId, fn: (project: P) => Promise<T>): Promise<T> {\n const generation = this.generation;\n const hold = await source.acquireProject(id, this.options.holder);\n try {\n return await fn(hold.project);\n } finally {\n if (generation === this.generation) this.keep(id, hold);\n else void hold.release();\n }\n }\n\n /**\n * Gives back every kept hold. Holds of calls still running are given back when those calls end,\n * not kept.\n */\n releaseAll(): void {\n this.generation++;\n for (const id of this.kept.keys()) this.release(id, \"release-all\");\n }\n\n /** Makes the hold of a finished call the kept one, or gives it back when one is kept already. */\n private keep(id: ProjectId, hold: Hold<P>): void {\n if (hold.ended) return;\n\n const kept = this.kept.get(id);\n if (kept !== undefined) {\n void hold.release();\n kept.timer.refresh();\n return;\n }\n\n const entry: Kept<P> = {\n hold,\n timer: setTimeout(() => {\n if (this.kept.get(id) === entry) this.release(id, \"idle\");\n }, this.options.idleMs),\n };\n entry.timer.unref();\n hold.signal.addEventListener(\n \"abort\",\n () => {\n if (this.kept.get(id) !== entry) return;\n clearTimeout(entry.timer);\n this.kept.delete(id);\n },\n { once: true },\n );\n this.kept.set(id, entry);\n }\n\n private release(id: ProjectId, reason: KeptHoldRelease): void {\n const entry = this.kept.get(id);\n if (entry === undefined) return;\n clearTimeout(entry.timer);\n this.kept.delete(id);\n void entry.hold.release();\n this.options.onRelease?.(id, reason);\n }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K,KAAK,OAAO,EAAE;EACnB,MAAW,KAAK,QAAQ;EACxB,KAAK,QAAQ,YAAY,IAAI,MAAM;CACrC;AACF"}
@@ -0,0 +1,72 @@
1
+ // src/project-holds.ts
2
+ /**
3
+ * The MCP server's holds on projects.
4
+ *
5
+ * Every call takes a hold of its own for as long as it runs, so nobody else's release can destroy
6
+ * the project under it. When the call ends, one hold per project is kept for `idleMs` more, so a
7
+ * run of calls on a project does not load it again for each call; the idle time restarts with
8
+ * every call. A kept hold the middle layer ends (the project was deleted, the middle layer closed)
9
+ * is forgotten.
10
+ */
11
+ var ProjectHolds = class {
12
+ options;
13
+ kept = /* @__PURE__ */ new Map();
14
+ /** Counts {@link releaseAll}: a call that started before the last one keeps nothing. */
15
+ generation = 0;
16
+ constructor(options) {
17
+ this.options = options;
18
+ }
19
+ /** Runs `fn` on the project while holding it. */
20
+ async use(source, id, fn) {
21
+ const generation = this.generation;
22
+ const hold = await source.acquireProject(id, this.options.holder);
23
+ try {
24
+ return await fn(hold.project);
25
+ } finally {
26
+ if (generation === this.generation) this.keep(id, hold);
27
+ else hold.release();
28
+ }
29
+ }
30
+ /**
31
+ * Gives back every kept hold. Holds of calls still running are given back when those calls end,
32
+ * not kept.
33
+ */
34
+ releaseAll() {
35
+ this.generation++;
36
+ for (const id of this.kept.keys()) this.release(id, "release-all");
37
+ }
38
+ /** Makes the hold of a finished call the kept one, or gives it back when one is kept already. */
39
+ keep(id, hold) {
40
+ if (hold.ended) return;
41
+ const kept = this.kept.get(id);
42
+ if (kept !== void 0) {
43
+ hold.release();
44
+ kept.timer.refresh();
45
+ return;
46
+ }
47
+ const entry = {
48
+ hold,
49
+ timer: setTimeout(() => {
50
+ if (this.kept.get(id) === entry) this.release(id, "idle");
51
+ }, this.options.idleMs)
52
+ };
53
+ entry.timer.unref();
54
+ hold.signal.addEventListener("abort", () => {
55
+ if (this.kept.get(id) !== entry) return;
56
+ clearTimeout(entry.timer);
57
+ this.kept.delete(id);
58
+ }, { once: true });
59
+ this.kept.set(id, entry);
60
+ }
61
+ release(id, reason) {
62
+ const entry = this.kept.get(id);
63
+ if (entry === void 0) return;
64
+ clearTimeout(entry.timer);
65
+ this.kept.delete(id);
66
+ entry.hold.release();
67
+ this.options.onRelease?.(id, reason);
68
+ }
69
+ };
70
+ export { ProjectHolds };
71
+
72
+ //# sourceMappingURL=project-holds.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"project-holds.js","names":[],"sources":["../src/project-holds.ts"],"sourcesContent":["import type { ProjectHolder, ProjectId } from \"@milaboratories/pl-middle-layer\";\n\n/** A hold on a loaded project, as {@link ProjectHolds} uses it: the middle layer's `ProjectHandle`. */\nexport interface Hold<P> {\n readonly project: P;\n readonly signal: AbortSignal;\n readonly ended: boolean;\n release(): Promise<void>;\n}\n\n/** Where holds come from: the middle layer, or a fake of it in tests. */\nexport interface HoldSource<P> {\n acquireProject(id: ProjectId, holder: ProjectHolder): Promise<Hold<P>>;\n}\n\n/** Why {@link ProjectHolds} gave a kept hold back. */\nexport type KeptHoldRelease = \"idle\" | \"release-all\";\n\nexport interface ProjectHoldsOptions {\n /** The holder every hold is taken under. */\n holder: ProjectHolder;\n /** How long a project stays held after the last call on it ended. */\n idleMs: number;\n /** Told about every kept hold given back. */\n onRelease?: (id: ProjectId, reason: KeptHoldRelease) => void;\n}\n\ntype Kept<P> = { hold: Hold<P>; timer: NodeJS.Timeout };\n\n/**\n * The MCP server's holds on projects.\n *\n * Every call takes a hold of its own for as long as it runs, so nobody else's release can destroy\n * the project under it. When the call ends, one hold per project is kept for `idleMs` more, so a\n * run of calls on a project does not load it again for each call; the idle time restarts with\n * every call. A kept hold the middle layer ends (the project was deleted, the middle layer closed)\n * is forgotten.\n */\nexport class ProjectHolds<P> {\n private readonly kept = new Map<ProjectId, Kept<P>>();\n /** Counts {@link releaseAll}: a call that started before the last one keeps nothing. */\n private generation = 0;\n\n constructor(private readonly options: ProjectHoldsOptions) {}\n\n /** Runs `fn` on the project while holding it. */\n async use<T>(source: HoldSource<P>, id: ProjectId, fn: (project: P) => Promise<T>): Promise<T> {\n const generation = this.generation;\n const hold = await source.acquireProject(id, this.options.holder);\n try {\n return await fn(hold.project);\n } finally {\n if (generation === this.generation) this.keep(id, hold);\n else void hold.release();\n }\n }\n\n /**\n * Gives back every kept hold. Holds of calls still running are given back when those calls end,\n * not kept.\n */\n releaseAll(): void {\n this.generation++;\n for (const id of this.kept.keys()) this.release(id, \"release-all\");\n }\n\n /** Makes the hold of a finished call the kept one, or gives it back when one is kept already. */\n private keep(id: ProjectId, hold: Hold<P>): void {\n if (hold.ended) return;\n\n const kept = this.kept.get(id);\n if (kept !== undefined) {\n void hold.release();\n kept.timer.refresh();\n return;\n }\n\n const entry: Kept<P> = {\n hold,\n timer: setTimeout(() => {\n if (this.kept.get(id) === entry) this.release(id, \"idle\");\n }, this.options.idleMs),\n };\n entry.timer.unref();\n hold.signal.addEventListener(\n \"abort\",\n () => {\n if (this.kept.get(id) !== entry) return;\n clearTimeout(entry.timer);\n this.kept.delete(id);\n },\n { once: true },\n );\n this.kept.set(id, entry);\n }\n\n private release(id: ProjectId, reason: KeptHoldRelease): void {\n const entry = this.kept.get(id);\n if (entry === undefined) return;\n clearTimeout(entry.timer);\n this.kept.delete(id);\n void entry.hold.release();\n this.options.onRelease?.(id, reason);\n }\n}\n"],"mappings":";;;;;;;;;;AAsCA,IAAa,eAAb,MAA6B;CAKE;CAJ7B,uBAAwB,IAAI,IAAwB;;CAEpD,aAAqB;CAErB,YAAY,SAA+C;EAA9B,KAAA,UAAA;CAA+B;;CAG5D,MAAM,IAAO,QAAuB,IAAe,IAA4C;EAC7F,MAAM,aAAa,KAAK;EACxB,MAAM,OAAO,MAAM,OAAO,eAAe,IAAI,KAAK,QAAQ,MAAM;EAChE,IAAI;GACF,OAAO,MAAM,GAAG,KAAK,OAAO;EAC9B,UAAU;GACR,IAAI,eAAe,KAAK,YAAY,KAAK,KAAK,IAAI,IAAI;QACjD,KAAU,QAAQ;EACzB;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