scorecard-ai-mcp 2.4.1 → 2.5.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 (261) hide show
  1. package/README.md +38 -1
  2. package/code-tool-worker.d.mts.map +1 -1
  3. package/code-tool-worker.d.ts.map +1 -1
  4. package/code-tool-worker.js +141 -7
  5. package/code-tool-worker.js.map +1 -1
  6. package/code-tool-worker.mjs +141 -7
  7. package/code-tool-worker.mjs.map +1 -1
  8. package/code-tool.d.mts +1 -1
  9. package/code-tool.d.mts.map +1 -1
  10. package/code-tool.d.ts +1 -1
  11. package/code-tool.d.ts.map +1 -1
  12. package/code-tool.js +11 -13
  13. package/code-tool.js.map +1 -1
  14. package/code-tool.mjs +11 -13
  15. package/code-tool.mjs.map +1 -1
  16. package/dynamic-tools.d.mts +1 -1
  17. package/dynamic-tools.d.ts +1 -1
  18. package/dynamic-tools.js +1 -1
  19. package/dynamic-tools.mjs +1 -1
  20. package/filtering.d.mts +1 -0
  21. package/filtering.d.mts.map +1 -1
  22. package/filtering.d.ts +1 -0
  23. package/filtering.d.ts.map +1 -1
  24. package/filtering.js +4 -0
  25. package/filtering.js.map +1 -1
  26. package/filtering.mjs +3 -0
  27. package/filtering.mjs.map +1 -1
  28. package/headers.d.mts.map +1 -1
  29. package/headers.d.ts.map +1 -1
  30. package/headers.js +1 -1
  31. package/headers.js.map +1 -1
  32. package/headers.mjs +1 -1
  33. package/headers.mjs.map +1 -1
  34. package/http.js +2 -2
  35. package/http.js.map +1 -1
  36. package/http.mjs +2 -2
  37. package/http.mjs.map +1 -1
  38. package/options.d.mts.map +1 -1
  39. package/options.d.ts.map +1 -1
  40. package/options.js +6 -3
  41. package/options.js.map +1 -1
  42. package/options.mjs +6 -3
  43. package/options.mjs.map +1 -1
  44. package/package.json +5 -3
  45. package/server.js +1 -1
  46. package/server.mjs +1 -1
  47. package/src/code-tool-worker.ts +186 -7
  48. package/src/code-tool.ts +14 -13
  49. package/src/dynamic-tools.ts +1 -1
  50. package/src/filtering.ts +4 -0
  51. package/src/headers.ts +3 -1
  52. package/src/http.ts +2 -2
  53. package/src/options.ts +10 -3
  54. package/src/server.ts +1 -1
  55. package/src/tools/index.ts +2 -0
  56. package/src/tools/metrics/delete-metrics.ts +54 -0
  57. package/src/tools/projects/create-projects.ts +10 -3
  58. package/src/tools/projects/list-projects.ts +10 -3
  59. package/src/tools/records/create-records.ts +10 -3
  60. package/src/tools/records/delete-records.ts +10 -3
  61. package/src/tools/records/list-records.ts +10 -3
  62. package/src/tools/runs/create-runs.ts +10 -3
  63. package/src/tools/runs/get-runs.ts +10 -3
  64. package/src/tools/runs/list-runs.ts +10 -3
  65. package/src/tools/scores/upsert-scores.ts +12 -3
  66. package/src/tools/systems/delete-systems.ts +10 -3
  67. package/src/tools/systems/get-systems.ts +10 -3
  68. package/src/tools/systems/list-systems.ts +10 -3
  69. package/src/tools/systems/update-systems.ts +10 -3
  70. package/src/tools/systems/upsert-systems.ts +10 -3
  71. package/src/tools/systems/versions/get-systems-versions.ts +12 -5
  72. package/src/tools/systems/versions/upsert-systems-versions.ts +12 -5
  73. package/src/tools/testcases/create-testcases.ts +10 -3
  74. package/src/tools/testcases/delete-testcases.ts +10 -3
  75. package/src/tools/testcases/get-testcases.ts +10 -3
  76. package/src/tools/testcases/list-testcases.ts +10 -3
  77. package/src/tools/testcases/update-testcases.ts +10 -3
  78. package/src/tools/testsets/create-testsets.ts +10 -3
  79. package/src/tools/testsets/delete-testsets.ts +10 -3
  80. package/src/tools/testsets/get-testsets.ts +10 -3
  81. package/src/tools/testsets/list-testsets.ts +10 -3
  82. package/src/tools/testsets/update-testsets.ts +10 -3
  83. package/src/tools/types.ts +12 -0
  84. package/tools/index.d.mts.map +1 -1
  85. package/tools/index.d.ts.map +1 -1
  86. package/tools/index.js +2 -0
  87. package/tools/index.js.map +1 -1
  88. package/tools/index.mjs +2 -0
  89. package/tools/index.mjs.map +1 -1
  90. package/tools/metrics/delete-metrics.d.mts +45 -0
  91. package/tools/metrics/delete-metrics.d.mts.map +1 -0
  92. package/tools/metrics/delete-metrics.d.ts +45 -0
  93. package/tools/metrics/delete-metrics.d.ts.map +1 -0
  94. package/tools/metrics/delete-metrics.js +50 -0
  95. package/tools/metrics/delete-metrics.js.map +1 -0
  96. package/tools/metrics/delete-metrics.mjs +46 -0
  97. package/tools/metrics/delete-metrics.mjs.map +1 -0
  98. package/tools/projects/create-projects.d.mts.map +1 -1
  99. package/tools/projects/create-projects.d.ts.map +1 -1
  100. package/tools/projects/create-projects.js +9 -1
  101. package/tools/projects/create-projects.js.map +1 -1
  102. package/tools/projects/create-projects.mjs +11 -3
  103. package/tools/projects/create-projects.mjs.map +1 -1
  104. package/tools/projects/list-projects.d.mts.map +1 -1
  105. package/tools/projects/list-projects.d.ts.map +1 -1
  106. package/tools/projects/list-projects.js +9 -1
  107. package/tools/projects/list-projects.js.map +1 -1
  108. package/tools/projects/list-projects.mjs +11 -3
  109. package/tools/projects/list-projects.mjs.map +1 -1
  110. package/tools/records/create-records.d.mts.map +1 -1
  111. package/tools/records/create-records.d.ts.map +1 -1
  112. package/tools/records/create-records.js +9 -1
  113. package/tools/records/create-records.js.map +1 -1
  114. package/tools/records/create-records.mjs +11 -3
  115. package/tools/records/create-records.mjs.map +1 -1
  116. package/tools/records/delete-records.d.mts.map +1 -1
  117. package/tools/records/delete-records.d.ts.map +1 -1
  118. package/tools/records/delete-records.js +9 -1
  119. package/tools/records/delete-records.js.map +1 -1
  120. package/tools/records/delete-records.mjs +11 -3
  121. package/tools/records/delete-records.mjs.map +1 -1
  122. package/tools/records/list-records.d.mts.map +1 -1
  123. package/tools/records/list-records.d.ts.map +1 -1
  124. package/tools/records/list-records.js +9 -1
  125. package/tools/records/list-records.js.map +1 -1
  126. package/tools/records/list-records.mjs +11 -3
  127. package/tools/records/list-records.mjs.map +1 -1
  128. package/tools/runs/create-runs.d.mts.map +1 -1
  129. package/tools/runs/create-runs.d.ts.map +1 -1
  130. package/tools/runs/create-runs.js +9 -1
  131. package/tools/runs/create-runs.js.map +1 -1
  132. package/tools/runs/create-runs.mjs +11 -3
  133. package/tools/runs/create-runs.mjs.map +1 -1
  134. package/tools/runs/get-runs.d.mts.map +1 -1
  135. package/tools/runs/get-runs.d.ts.map +1 -1
  136. package/tools/runs/get-runs.js +9 -1
  137. package/tools/runs/get-runs.js.map +1 -1
  138. package/tools/runs/get-runs.mjs +11 -3
  139. package/tools/runs/get-runs.mjs.map +1 -1
  140. package/tools/runs/list-runs.d.mts.map +1 -1
  141. package/tools/runs/list-runs.d.ts.map +1 -1
  142. package/tools/runs/list-runs.js +9 -1
  143. package/tools/runs/list-runs.js.map +1 -1
  144. package/tools/runs/list-runs.mjs +11 -3
  145. package/tools/runs/list-runs.mjs.map +1 -1
  146. package/tools/scores/upsert-scores.d.mts.map +1 -1
  147. package/tools/scores/upsert-scores.d.ts.map +1 -1
  148. package/tools/scores/upsert-scores.js +9 -1
  149. package/tools/scores/upsert-scores.js.map +1 -1
  150. package/tools/scores/upsert-scores.mjs +11 -3
  151. package/tools/scores/upsert-scores.mjs.map +1 -1
  152. package/tools/systems/delete-systems.d.mts.map +1 -1
  153. package/tools/systems/delete-systems.d.ts.map +1 -1
  154. package/tools/systems/delete-systems.js +9 -1
  155. package/tools/systems/delete-systems.js.map +1 -1
  156. package/tools/systems/delete-systems.mjs +11 -3
  157. package/tools/systems/delete-systems.mjs.map +1 -1
  158. package/tools/systems/get-systems.d.mts.map +1 -1
  159. package/tools/systems/get-systems.d.ts.map +1 -1
  160. package/tools/systems/get-systems.js +9 -1
  161. package/tools/systems/get-systems.js.map +1 -1
  162. package/tools/systems/get-systems.mjs +11 -3
  163. package/tools/systems/get-systems.mjs.map +1 -1
  164. package/tools/systems/list-systems.d.mts.map +1 -1
  165. package/tools/systems/list-systems.d.ts.map +1 -1
  166. package/tools/systems/list-systems.js +9 -1
  167. package/tools/systems/list-systems.js.map +1 -1
  168. package/tools/systems/list-systems.mjs +11 -3
  169. package/tools/systems/list-systems.mjs.map +1 -1
  170. package/tools/systems/update-systems.d.mts.map +1 -1
  171. package/tools/systems/update-systems.d.ts.map +1 -1
  172. package/tools/systems/update-systems.js +9 -1
  173. package/tools/systems/update-systems.js.map +1 -1
  174. package/tools/systems/update-systems.mjs +11 -3
  175. package/tools/systems/update-systems.mjs.map +1 -1
  176. package/tools/systems/upsert-systems.d.mts.map +1 -1
  177. package/tools/systems/upsert-systems.d.ts.map +1 -1
  178. package/tools/systems/upsert-systems.js +9 -1
  179. package/tools/systems/upsert-systems.js.map +1 -1
  180. package/tools/systems/upsert-systems.mjs +11 -3
  181. package/tools/systems/upsert-systems.mjs.map +1 -1
  182. package/tools/systems/versions/get-systems-versions.d.mts.map +1 -1
  183. package/tools/systems/versions/get-systems-versions.d.ts.map +1 -1
  184. package/tools/systems/versions/get-systems-versions.js +9 -1
  185. package/tools/systems/versions/get-systems-versions.js.map +1 -1
  186. package/tools/systems/versions/get-systems-versions.mjs +11 -3
  187. package/tools/systems/versions/get-systems-versions.mjs.map +1 -1
  188. package/tools/systems/versions/upsert-systems-versions.d.mts.map +1 -1
  189. package/tools/systems/versions/upsert-systems-versions.d.ts.map +1 -1
  190. package/tools/systems/versions/upsert-systems-versions.js +9 -1
  191. package/tools/systems/versions/upsert-systems-versions.js.map +1 -1
  192. package/tools/systems/versions/upsert-systems-versions.mjs +11 -3
  193. package/tools/systems/versions/upsert-systems-versions.mjs.map +1 -1
  194. package/tools/testcases/create-testcases.d.mts.map +1 -1
  195. package/tools/testcases/create-testcases.d.ts.map +1 -1
  196. package/tools/testcases/create-testcases.js +9 -1
  197. package/tools/testcases/create-testcases.js.map +1 -1
  198. package/tools/testcases/create-testcases.mjs +11 -3
  199. package/tools/testcases/create-testcases.mjs.map +1 -1
  200. package/tools/testcases/delete-testcases.d.mts.map +1 -1
  201. package/tools/testcases/delete-testcases.d.ts.map +1 -1
  202. package/tools/testcases/delete-testcases.js +9 -1
  203. package/tools/testcases/delete-testcases.js.map +1 -1
  204. package/tools/testcases/delete-testcases.mjs +11 -3
  205. package/tools/testcases/delete-testcases.mjs.map +1 -1
  206. package/tools/testcases/get-testcases.d.mts.map +1 -1
  207. package/tools/testcases/get-testcases.d.ts.map +1 -1
  208. package/tools/testcases/get-testcases.js +9 -1
  209. package/tools/testcases/get-testcases.js.map +1 -1
  210. package/tools/testcases/get-testcases.mjs +11 -3
  211. package/tools/testcases/get-testcases.mjs.map +1 -1
  212. package/tools/testcases/list-testcases.d.mts.map +1 -1
  213. package/tools/testcases/list-testcases.d.ts.map +1 -1
  214. package/tools/testcases/list-testcases.js +9 -1
  215. package/tools/testcases/list-testcases.js.map +1 -1
  216. package/tools/testcases/list-testcases.mjs +11 -3
  217. package/tools/testcases/list-testcases.mjs.map +1 -1
  218. package/tools/testcases/update-testcases.d.mts.map +1 -1
  219. package/tools/testcases/update-testcases.d.ts.map +1 -1
  220. package/tools/testcases/update-testcases.js +9 -1
  221. package/tools/testcases/update-testcases.js.map +1 -1
  222. package/tools/testcases/update-testcases.mjs +11 -3
  223. package/tools/testcases/update-testcases.mjs.map +1 -1
  224. package/tools/testsets/create-testsets.d.mts.map +1 -1
  225. package/tools/testsets/create-testsets.d.ts.map +1 -1
  226. package/tools/testsets/create-testsets.js +9 -1
  227. package/tools/testsets/create-testsets.js.map +1 -1
  228. package/tools/testsets/create-testsets.mjs +11 -3
  229. package/tools/testsets/create-testsets.mjs.map +1 -1
  230. package/tools/testsets/delete-testsets.d.mts.map +1 -1
  231. package/tools/testsets/delete-testsets.d.ts.map +1 -1
  232. package/tools/testsets/delete-testsets.js +9 -1
  233. package/tools/testsets/delete-testsets.js.map +1 -1
  234. package/tools/testsets/delete-testsets.mjs +11 -3
  235. package/tools/testsets/delete-testsets.mjs.map +1 -1
  236. package/tools/testsets/get-testsets.d.mts.map +1 -1
  237. package/tools/testsets/get-testsets.d.ts.map +1 -1
  238. package/tools/testsets/get-testsets.js +9 -1
  239. package/tools/testsets/get-testsets.js.map +1 -1
  240. package/tools/testsets/get-testsets.mjs +11 -3
  241. package/tools/testsets/get-testsets.mjs.map +1 -1
  242. package/tools/testsets/list-testsets.d.mts.map +1 -1
  243. package/tools/testsets/list-testsets.d.ts.map +1 -1
  244. package/tools/testsets/list-testsets.js +9 -1
  245. package/tools/testsets/list-testsets.js.map +1 -1
  246. package/tools/testsets/list-testsets.mjs +11 -3
  247. package/tools/testsets/list-testsets.mjs.map +1 -1
  248. package/tools/testsets/update-testsets.d.mts.map +1 -1
  249. package/tools/testsets/update-testsets.d.ts.map +1 -1
  250. package/tools/testsets/update-testsets.js +9 -1
  251. package/tools/testsets/update-testsets.js.map +1 -1
  252. package/tools/testsets/update-testsets.mjs +11 -3
  253. package/tools/testsets/update-testsets.mjs.map +1 -1
  254. package/tools/types.d.mts +1 -0
  255. package/tools/types.d.mts.map +1 -1
  256. package/tools/types.d.ts +1 -0
  257. package/tools/types.d.ts.map +1 -1
  258. package/tools/types.js +12 -0
  259. package/tools/types.js.map +1 -1
  260. package/tools/types.mjs +11 -0
  261. package/tools/types.mjs.map +1 -1
package/README.md CHANGED
@@ -36,12 +36,36 @@ For clients with a configuration JSON, it might look something like this:
36
36
  }
37
37
  ```
38
38
 
39
+ ### Cursor
40
+
41
+ If you use Cursor, you can install the MCP server by using the button below. You will need to set your environment variables
42
+ in Cursor's `mcp.json`, which can be found in Cursor Settings > Tools & MCP > New MCP Server.
43
+
44
+ [![Add to Cursor](https://cursor.com/deeplink/mcp-install-dark.svg)](https://cursor.com/en-US/install-mcp?name=scorecard-ai-mcp&config=eyJjb21tYW5kIjoibnB4IiwiYXJncyI6WyIteSIsInNjb3JlY2FyZC1haS1tY3AiXSwiZW52Ijp7IlNDT1JFQ0FSRF9BUElfS0VZIjoiU2V0IHlvdXIgU0NPUkVDQVJEX0FQSV9LRVkgaGVyZS4ifX0)
45
+
46
+ ### VS Code
47
+
48
+ If you use MCP, you can install the MCP server by clicking the link below. You will need to set your environment variables
49
+ in VS Code's `mcp.json`, which can be found via Command Palette > MCP: Open User Configuration.
50
+
51
+ [Open VS Code](https://vscode.stainless.com/mcp/%7B%22name%22%3A%22scorecard-ai-mcp%22%2C%22command%22%3A%22npx%22%2C%22args%22%3A%5B%22-y%22%2C%22scorecard-ai-mcp%22%5D%2C%22env%22%3A%7B%22SCORECARD_API_KEY%22%3A%22Set%20your%20SCORECARD_API_KEY%20here.%22%7D%7D)
52
+
53
+ ### Claude Code
54
+
55
+ If you use Claude Code, you can install the MCP server by running the command below in your terminal. You will need to set your
56
+ environment variables in Claude Code's `.claude.json`, which can be found in your home directory.
57
+
58
+ ```
59
+ claude mcp add --transport stdio scorecard_ai_api --env SCORECARD_API_KEY="Your SCORECARD_API_KEY here." -- npx -y scorecard-ai-mcp
60
+ ```
61
+
39
62
  ## Exposing endpoints to your MCP Client
40
63
 
41
- There are two ways to expose endpoints as tools in the MCP server:
64
+ There are three ways to expose endpoints as tools in the MCP server:
42
65
 
43
66
  1. Exposing one tool per endpoint, and filtering as necessary
44
67
  2. Exposing a set of tools to dynamically discover and invoke endpoints from the API
68
+ 3. Exposing a docs search tool and a code execution tool, allowing the client to write code to be executed against the TypeScript client
45
69
 
46
70
  ### Filtering endpoints and tools
47
71
 
@@ -76,6 +100,18 @@ All of these command-line options can be repeated, combined together, and have c
76
100
 
77
101
  Use `--list` to see the list of available tools, or see below.
78
102
 
103
+ ### Code execution
104
+
105
+ If you specify `--tools=code` to the MCP server, it will expose just two tools:
106
+
107
+ - `search_docs` - Searches the API documentation and returns a list of markdown results
108
+ - `execute` - Runs code against the TypeScript client
109
+
110
+ This allows the LLM to implement more complex logic by chaining together many API calls without loading
111
+ intermediary results into its context window.
112
+
113
+ The code execution itself happens in a Deno sandbox that has network access only to the base URL for the API.
114
+
79
115
  ### Specifying the MCP Client
80
116
 
81
117
  Different clients have varying abilities to handle arbitrary tools and schemas.
@@ -247,6 +283,7 @@ The following tools are available in this MCP server.
247
283
  - `create_metrics` (`write`): Create a new Metric for evaluating system outputs. The structure of a metric depends on the evalType and outputType of the metric.
248
284
  - `update_metrics` (`write`): Update an existing Metric. You must specify the evalType and outputType of the metric. The structure of a metric depends on the evalType and outputType of the metric.
249
285
  - `list_metrics` (`read`): List Metrics configured for the specified Project. Metrics are returned in reverse chronological order.
286
+ - `delete_metrics` (`write`): Delete a specific Metric by ID. The metric will be removed from metric groups and monitors.
250
287
  - `get_metrics` (`read`): Retrieve a specific Metric by ID.
251
288
 
252
289
  ### Resource `records`:
@@ -1 +1 @@
1
- {"version":3,"file":"code-tool-worker.d.mts","sourceRoot":"","sources":["src/code-tool-worker.ts"],"names":[],"mappings":";iBAM0B,OAAO,KAAG,OAAO,CAAC,QAAQ,CAAC;;AAuCrD,wBAAyB"}
1
+ {"version":3,"file":"code-tool-worker.d.mts","sourceRoot":"","sources":["src/code-tool-worker.ts"],"names":[],"mappings":";iBAwK0B,OAAO,KAAG,OAAO,CAAC,QAAQ,CAAC;;AAwDrD,wBAAyB"}
@@ -1 +1 @@
1
- {"version":3,"file":"code-tool-worker.d.ts","sourceRoot":"","sources":["src/code-tool-worker.ts"],"names":[],"mappings":";iBAM0B,OAAO,KAAG,OAAO,CAAC,QAAQ,CAAC;;AAuCrD,wBAAyB"}
1
+ {"version":3,"file":"code-tool-worker.d.ts","sourceRoot":"","sources":["src/code-tool-worker.ts"],"names":[],"mappings":";iBAwK0B,OAAO,KAAG,OAAO,CAAC,QAAQ,CAAC;;AAwDrD,wBAAyB"}
@@ -5,9 +5,147 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  };
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  const node_util_1 = __importDefault(require("node:util"));
8
+ const fuse_js_1 = __importDefault(require("fuse.js"));
9
+ const typescript_1 = __importDefault(require("typescript"));
8
10
  const scorecard_ai_1 = require("scorecard-ai");
11
+ function getRunFunctionNode(code) {
12
+ const sourceFile = typescript_1.default.createSourceFile('code.ts', code, typescript_1.default.ScriptTarget.Latest, true);
13
+ for (const statement of sourceFile.statements) {
14
+ // Check for top-level function declarations
15
+ if (typescript_1.default.isFunctionDeclaration(statement)) {
16
+ if (statement.name?.text === 'run') {
17
+ return statement;
18
+ }
19
+ }
20
+ // Check for variable declarations: const run = () => {} or const run = function() {}
21
+ if (typescript_1.default.isVariableStatement(statement)) {
22
+ for (const declaration of statement.declarationList.declarations) {
23
+ if (typescript_1.default.isIdentifier(declaration.name) && declaration.name.text === 'run') {
24
+ // Check if it's initialized with a function
25
+ if (declaration.initializer &&
26
+ (typescript_1.default.isFunctionExpression(declaration.initializer) || typescript_1.default.isArrowFunction(declaration.initializer))) {
27
+ return declaration.initializer;
28
+ }
29
+ }
30
+ }
31
+ }
32
+ }
33
+ return null;
34
+ }
35
+ const fuse = new fuse_js_1.default([
36
+ 'client.projects.create',
37
+ 'client.projects.list',
38
+ 'client.testsets.create',
39
+ 'client.testsets.delete',
40
+ 'client.testsets.get',
41
+ 'client.testsets.list',
42
+ 'client.testsets.update',
43
+ 'client.testcases.create',
44
+ 'client.testcases.delete',
45
+ 'client.testcases.get',
46
+ 'client.testcases.list',
47
+ 'client.testcases.update',
48
+ 'client.runs.create',
49
+ 'client.runs.get',
50
+ 'client.runs.list',
51
+ 'client.metrics.create',
52
+ 'client.metrics.delete',
53
+ 'client.metrics.get',
54
+ 'client.metrics.list',
55
+ 'client.metrics.update',
56
+ 'client.records.create',
57
+ 'client.records.delete',
58
+ 'client.records.list',
59
+ 'client.scores.upsert',
60
+ 'client.systems.delete',
61
+ 'client.systems.get',
62
+ 'client.systems.list',
63
+ 'client.systems.update',
64
+ 'client.systems.upsert',
65
+ 'client.systems.versions.get',
66
+ 'client.systems.versions.upsert',
67
+ ], { threshold: 1, shouldSort: true });
68
+ function getMethodSuggestions(fullyQualifiedMethodName) {
69
+ return fuse
70
+ .search(fullyQualifiedMethodName)
71
+ .map(({ item }) => item)
72
+ .slice(0, 5);
73
+ }
74
+ const proxyToObj = new WeakMap();
75
+ const objToProxy = new WeakMap();
76
+ function makeSdkProxy(obj, { path, isBelievedBad = false }) {
77
+ let proxy = objToProxy.get(obj);
78
+ if (!proxy) {
79
+ proxy = new Proxy(obj, {
80
+ get(target, prop, receiver) {
81
+ const propPath = [...path, String(prop)];
82
+ const value = Reflect.get(target, prop, receiver);
83
+ if (isBelievedBad || (!(prop in target) && value === undefined)) {
84
+ // If we're accessing a path that doesn't exist, it will probably eventually error.
85
+ // Let's proxy it and mark it bad so that we can control the error message.
86
+ // We proxy an empty class so that an invocation or construction attempt is possible.
87
+ return makeSdkProxy(class {
88
+ }, { path: propPath, isBelievedBad: true });
89
+ }
90
+ if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
91
+ return makeSdkProxy(value, { path: propPath, isBelievedBad });
92
+ }
93
+ return value;
94
+ },
95
+ apply(target, thisArg, args) {
96
+ if (isBelievedBad || typeof target !== 'function') {
97
+ const fullyQualifiedMethodName = path.join('.');
98
+ const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
99
+ throw new Error(`${fullyQualifiedMethodName} is not a function. Did you mean: ${suggestions.join(', ')}`);
100
+ }
101
+ return Reflect.apply(target, proxyToObj.get(thisArg) ?? thisArg, args);
102
+ },
103
+ construct(target, args, newTarget) {
104
+ if (isBelievedBad || typeof target !== 'function') {
105
+ const fullyQualifiedMethodName = path.join('.');
106
+ const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
107
+ throw new Error(`${fullyQualifiedMethodName} is not a constructor. Did you mean: ${suggestions.join(', ')}`);
108
+ }
109
+ return Reflect.construct(target, args, newTarget);
110
+ },
111
+ });
112
+ objToProxy.set(obj, proxy);
113
+ proxyToObj.set(proxy, obj);
114
+ }
115
+ return proxy;
116
+ }
117
+ function parseError(code, error) {
118
+ if (!(error instanceof Error))
119
+ return;
120
+ const message = error.name ? `${error.name}: ${error.message}` : error.message;
121
+ try {
122
+ // Deno uses V8; the first "<anonymous>:LINE:COLUMN" is the top of stack.
123
+ const lineNumber = error.stack?.match(/<anonymous>:([0-9]+):[0-9]+/)?.[1];
124
+ // -1 for the zero-based indexing
125
+ const line = lineNumber &&
126
+ code
127
+ .split('\n')
128
+ .at(parseInt(lineNumber, 10) - 1)
129
+ ?.trim();
130
+ return line ? `${message}\n at line ${lineNumber}\n ${line}` : message;
131
+ }
132
+ catch {
133
+ return message;
134
+ }
135
+ }
9
136
  const fetch = async (req) => {
10
137
  const { opts, code } = (await req.json());
138
+ if (code == null) {
139
+ return Response.json({
140
+ message: 'The code param is missing. Provide one containing a top-level `run` function. Write code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```',
141
+ }, { status: 400, statusText: 'Code execution error' });
142
+ }
143
+ const runFunctionNode = getRunFunctionNode(code);
144
+ if (!runFunctionNode) {
145
+ return Response.json({
146
+ message: 'The code is missing a top-level `run` function. Write code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```',
147
+ }, { status: 400, statusText: 'Code execution error' });
148
+ }
11
149
  const client = new scorecard_ai_1.Scorecard({
12
150
  ...opts,
13
151
  });
@@ -23,11 +161,8 @@ const fetch = async (req) => {
23
161
  };
24
162
  try {
25
163
  let run_ = async (client) => { };
26
- eval(`
27
- ${code}
28
- run_ = run;
29
- `);
30
- const result = await run_(client);
164
+ eval(`${code}\nrun_ = run;`);
165
+ const result = await run_(makeSdkProxy(client, { path: ['client'] }));
31
166
  return Response.json({
32
167
  result,
33
168
  logLines,
@@ -35,9 +170,8 @@ const fetch = async (req) => {
35
170
  });
36
171
  }
37
172
  catch (e) {
38
- const message = e instanceof Error ? e.message : undefined;
39
173
  return Response.json({
40
- message,
174
+ message: parseError(code, e),
41
175
  }, { status: 400, statusText: 'Code execution error' });
42
176
  }
43
177
  };
@@ -1 +1 @@
1
- {"version":3,"file":"code-tool-worker.js","sourceRoot":"","sources":["src/code-tool-worker.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;;;AAEtF,0DAA6B;AAE7B,+CAAyC;AAEzC,MAAM,KAAK,GAAG,KAAK,EAAE,GAAY,EAAqB,EAAE;IACtD,MAAM,EAAE,IAAI,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CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;QACtE,OAAO,QAAQ,CAAC,IAAI,CAAC;YACnB,MAAM;YACN,QAAQ;YACR,QAAQ;SACe,CAAC,CAAC;IAC7B,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,QAAQ,CAAC,IAAI,CAClB;YACE,OAAO,EAAE,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC;SACP,EACvB,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,sBAAsB,EAAE,CACpD,CAAC;IACJ,CAAC;AACH,CAAC,CAAC;AAEF,kBAAe,EAAE,KAAK,EAAE,CAAC"}
@@ -1,8 +1,146 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
  import util from 'node:util';
3
+ import Fuse from 'fuse.js';
4
+ import ts from 'typescript';
3
5
  import { Scorecard } from 'scorecard-ai';
6
+ function getRunFunctionNode(code) {
7
+ const sourceFile = ts.createSourceFile('code.ts', code, ts.ScriptTarget.Latest, true);
8
+ for (const statement of sourceFile.statements) {
9
+ // Check for top-level function declarations
10
+ if (ts.isFunctionDeclaration(statement)) {
11
+ if (statement.name?.text === 'run') {
12
+ return statement;
13
+ }
14
+ }
15
+ // Check for variable declarations: const run = () => {} or const run = function() {}
16
+ if (ts.isVariableStatement(statement)) {
17
+ for (const declaration of statement.declarationList.declarations) {
18
+ if (ts.isIdentifier(declaration.name) && declaration.name.text === 'run') {
19
+ // Check if it's initialized with a function
20
+ if (declaration.initializer &&
21
+ (ts.isFunctionExpression(declaration.initializer) || ts.isArrowFunction(declaration.initializer))) {
22
+ return declaration.initializer;
23
+ }
24
+ }
25
+ }
26
+ }
27
+ }
28
+ return null;
29
+ }
30
+ const fuse = new Fuse([
31
+ 'client.projects.create',
32
+ 'client.projects.list',
33
+ 'client.testsets.create',
34
+ 'client.testsets.delete',
35
+ 'client.testsets.get',
36
+ 'client.testsets.list',
37
+ 'client.testsets.update',
38
+ 'client.testcases.create',
39
+ 'client.testcases.delete',
40
+ 'client.testcases.get',
41
+ 'client.testcases.list',
42
+ 'client.testcases.update',
43
+ 'client.runs.create',
44
+ 'client.runs.get',
45
+ 'client.runs.list',
46
+ 'client.metrics.create',
47
+ 'client.metrics.delete',
48
+ 'client.metrics.get',
49
+ 'client.metrics.list',
50
+ 'client.metrics.update',
51
+ 'client.records.create',
52
+ 'client.records.delete',
53
+ 'client.records.list',
54
+ 'client.scores.upsert',
55
+ 'client.systems.delete',
56
+ 'client.systems.get',
57
+ 'client.systems.list',
58
+ 'client.systems.update',
59
+ 'client.systems.upsert',
60
+ 'client.systems.versions.get',
61
+ 'client.systems.versions.upsert',
62
+ ], { threshold: 1, shouldSort: true });
63
+ function getMethodSuggestions(fullyQualifiedMethodName) {
64
+ return fuse
65
+ .search(fullyQualifiedMethodName)
66
+ .map(({ item }) => item)
67
+ .slice(0, 5);
68
+ }
69
+ const proxyToObj = new WeakMap();
70
+ const objToProxy = new WeakMap();
71
+ function makeSdkProxy(obj, { path, isBelievedBad = false }) {
72
+ let proxy = objToProxy.get(obj);
73
+ if (!proxy) {
74
+ proxy = new Proxy(obj, {
75
+ get(target, prop, receiver) {
76
+ const propPath = [...path, String(prop)];
77
+ const value = Reflect.get(target, prop, receiver);
78
+ if (isBelievedBad || (!(prop in target) && value === undefined)) {
79
+ // If we're accessing a path that doesn't exist, it will probably eventually error.
80
+ // Let's proxy it and mark it bad so that we can control the error message.
81
+ // We proxy an empty class so that an invocation or construction attempt is possible.
82
+ return makeSdkProxy(class {
83
+ }, { path: propPath, isBelievedBad: true });
84
+ }
85
+ if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
86
+ return makeSdkProxy(value, { path: propPath, isBelievedBad });
87
+ }
88
+ return value;
89
+ },
90
+ apply(target, thisArg, args) {
91
+ if (isBelievedBad || typeof target !== 'function') {
92
+ const fullyQualifiedMethodName = path.join('.');
93
+ const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
94
+ throw new Error(`${fullyQualifiedMethodName} is not a function. Did you mean: ${suggestions.join(', ')}`);
95
+ }
96
+ return Reflect.apply(target, proxyToObj.get(thisArg) ?? thisArg, args);
97
+ },
98
+ construct(target, args, newTarget) {
99
+ if (isBelievedBad || typeof target !== 'function') {
100
+ const fullyQualifiedMethodName = path.join('.');
101
+ const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
102
+ throw new Error(`${fullyQualifiedMethodName} is not a constructor. Did you mean: ${suggestions.join(', ')}`);
103
+ }
104
+ return Reflect.construct(target, args, newTarget);
105
+ },
106
+ });
107
+ objToProxy.set(obj, proxy);
108
+ proxyToObj.set(proxy, obj);
109
+ }
110
+ return proxy;
111
+ }
112
+ function parseError(code, error) {
113
+ if (!(error instanceof Error))
114
+ return;
115
+ const message = error.name ? `${error.name}: ${error.message}` : error.message;
116
+ try {
117
+ // Deno uses V8; the first "<anonymous>:LINE:COLUMN" is the top of stack.
118
+ const lineNumber = error.stack?.match(/<anonymous>:([0-9]+):[0-9]+/)?.[1];
119
+ // -1 for the zero-based indexing
120
+ const line = lineNumber &&
121
+ code
122
+ .split('\n')
123
+ .at(parseInt(lineNumber, 10) - 1)
124
+ ?.trim();
125
+ return line ? `${message}\n at line ${lineNumber}\n ${line}` : message;
126
+ }
127
+ catch {
128
+ return message;
129
+ }
130
+ }
4
131
  const fetch = async (req) => {
5
132
  const { opts, code } = (await req.json());
133
+ if (code == null) {
134
+ return Response.json({
135
+ message: 'The code param is missing. Provide one containing a top-level `run` function. Write code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```',
136
+ }, { status: 400, statusText: 'Code execution error' });
137
+ }
138
+ const runFunctionNode = getRunFunctionNode(code);
139
+ if (!runFunctionNode) {
140
+ return Response.json({
141
+ message: 'The code is missing a top-level `run` function. Write code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```',
142
+ }, { status: 400, statusText: 'Code execution error' });
143
+ }
6
144
  const client = new Scorecard({
7
145
  ...opts,
8
146
  });
@@ -18,11 +156,8 @@ const fetch = async (req) => {
18
156
  };
19
157
  try {
20
158
  let run_ = async (client) => { };
21
- eval(`
22
- ${code}
23
- run_ = run;
24
- `);
25
- const result = await run_(client);
159
+ eval(`${code}\nrun_ = run;`);
160
+ const result = await run_(makeSdkProxy(client, { path: ['client'] }));
26
161
  return Response.json({
27
162
  result,
28
163
  logLines,
@@ -30,9 +165,8 @@ const fetch = async (req) => {
30
165
  });
31
166
  }
32
167
  catch (e) {
33
- const message = e instanceof Error ? e.message : undefined;
34
168
  return Response.json({
35
- message,
169
+ message: parseError(code, e),
36
170
  }, { status: 400, statusText: 'Code execution error' });
37
171
  }
38
172
  };
@@ -1 +1 @@
1
- {"version":3,"file":"code-tool-worker.mjs","sourceRoot":"","sources":["src/code-tool-worker.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,IAAI,MAAM,WAAW;OAErB,EAAE,SAAS,EAAE,MAAM,cAAc;AAExC,MAAM,KAAK,GAAG,KAAK,EAAE,GAAY,EAAqB,EAAE;IACtD,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAgB,CAAC;IACzD,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC;QAC3B,GAAG,IAAI;KACR,CAAC,CAAC;IAEH,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,MAAM,OAAO,GAAG;QACd,GAAG,EAAE,CAAC,GAAG,IAAe,EAAE,EAAE;YAC1B,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;QACtC,CAAC;QACD,KAAK,EAAE,CAAC,GAAG,IAAe,EAAE,EAAE;YAC5B,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;QACtC,CAAC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2
  /**
3
3
  * A tool that runs code against a copy of the SDK.
4
4
  *
5
- * Instead of exposing every endpoint as it's own tool, which uses up too many tokens for LLMs to use at once,
5
+ * Instead of exposing every endpoint as its own tool, which uses up too many tokens for LLMs to use at once,
6
6
  * we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
7
7
  * a generic endpoint that can be used to invoke any endpoint with the provided arguments.
8
8
  *
@@ -1 +1 @@
1
- {"version":3,"file":"code-tool.d.mts","sourceRoot":"","sources":["src/code-tool.ts"],"names":[],"mappings":"OAKO,EAAE,QAAQ,EAA0B;AAM3C;;;;;;;;GAQG;AACH,wBAAsB,QAAQ,IAAI,OAAO,CAAC,QAAQ,CAAC,CA6HlD"}
1
+ {"version":3,"file":"code-tool.d.mts","sourceRoot":"","sources":["src/code-tool.ts"],"names":[],"mappings":"OAKO,EAAgB,QAAQ,EAA4B;AAM3D;;;;;;;;GAQG;AACH,wBAAsB,QAAQ,IAAI,OAAO,CAAC,QAAQ,CAAC,CA8HlD"}
package/code-tool.d.ts CHANGED
@@ -2,7 +2,7 @@ import { Endpoint } from "./tools/types.js";
2
2
  /**
3
3
  * A tool that runs code against a copy of the SDK.
4
4
  *
5
- * Instead of exposing every endpoint as it's own tool, which uses up too many tokens for LLMs to use at once,
5
+ * Instead of exposing every endpoint as its own tool, which uses up too many tokens for LLMs to use at once,
6
6
  * we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
7
7
  * a generic endpoint that can be used to invoke any endpoint with the provided arguments.
8
8
  *
@@ -1 +1 @@
1
- {"version":3,"file":"code-tool.d.ts","sourceRoot":"","sources":["src/code-tool.ts"],"names":[],"mappings":"OAKO,EAAE,QAAQ,EAA0B;AAM3C;;;;;;;;GAQG;AACH,wBAAsB,QAAQ,IAAI,OAAO,CAAC,QAAQ,CAAC,CA6HlD"}
1
+ {"version":3,"file":"code-tool.d.ts","sourceRoot":"","sources":["src/code-tool.ts"],"names":[],"mappings":"OAKO,EAAgB,QAAQ,EAA4B;AAM3D;;;;;;;;GAQG;AACH,wBAAsB,QAAQ,IAAI,OAAO,CAAC,QAAQ,CAAC,CA8HlD"}
package/code-tool.js CHANGED
@@ -40,7 +40,7 @@ const node_url_1 = require("node:url");
40
40
  /**
41
41
  * A tool that runs code against a copy of the SDK.
42
42
  *
43
- * Instead of exposing every endpoint as it's own tool, which uses up too many tokens for LLMs to use at once,
43
+ * Instead of exposing every endpoint as its own tool, which uses up too many tokens for LLMs to use at once,
44
44
  * we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
45
45
  * a generic endpoint that can be used to invoke any endpoint with the provided arguments.
46
46
  *
@@ -50,7 +50,7 @@ async function codeTool() {
50
50
  const metadata = { resource: 'all', operation: 'write', tags: [] };
51
51
  const tool = {
52
52
  name: 'execute',
53
- description: 'Runs Typescript code to interact with the API.\nYou are a skilled programmer writing code to interface with the service.\nDefine an async function named "run" that takes a single parameter of an initialized client, and it will be run.\nDo not initialize a client, but instead use the client that you are given as a parameter.\nYou will be returned anything that your function returns, plus the results of any console.log statements.\nIf any code triggers an error, the tool will return an error response, so you do not need to add error handling unless you want to output something more helpful than the raw error.\nIt is not necessary to add comments to code, unless by adding those comments you believe that you can generate better code.\nThis code will run in a container, and you will not be able to use fetch or otherwise interact with the network calls other than through the client you are given.\nAny variables you define won\'t live between successive uses of this call, so make sure to return or log any data you might need later.',
53
+ description: 'Runs JavaScript code to interact with the API.\n\nYou are a skilled programmer writing code to interface with the service.\nDefine an async function named "run" that takes a single parameter of an initialized client named "client", and it will be run.\nWrite code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```\n\nYou will be returned anything that your function returns, plus the results of any console.log statements.\nIf any code triggers an error, the tool will return an error response, so you do not need to add error handling unless you want to output something more helpful than the raw error.\nIt is not necessary to add comments to code, unless by adding those comments you believe that you can generate better code.\nThis code will run in a container, and you will not be able to use fetch or otherwise interact with the network calls other than through the client you are given.\nAny variables you define won\'t live between successive uses of this call, so make sure to return or log any data you might need later.',
54
54
  inputSchema: { type: 'object', properties: { code: { type: 'string' } } },
55
55
  };
56
56
  // Import dynamically to avoid failing at import time in cases where the environment is not well-supported.
@@ -110,7 +110,7 @@ async function codeTool() {
110
110
  code,
111
111
  });
112
112
  req.write(body, (err) => {
113
- if (err !== null && err !== undefined) {
113
+ if (err != null) {
114
114
  reject(err);
115
115
  }
116
116
  });
@@ -118,12 +118,10 @@ async function codeTool() {
118
118
  });
119
119
  if (resp.status === 200) {
120
120
  const { result, logLines, errLines } = (await resp.json());
121
- const returnOutput = result === null ? null
122
- : result === undefined ? null
123
- : {
124
- type: 'text',
125
- text: typeof result === 'string' ? result : JSON.stringify(result),
126
- };
121
+ const returnOutput = result == null ? null : ({
122
+ type: 'text',
123
+ text: typeof result === 'string' ? result : JSON.stringify(result),
124
+ });
127
125
  const logOutput = logLines.length === 0 ?
128
126
  null
129
127
  : {
@@ -142,12 +140,12 @@ async function codeTool() {
142
140
  }
143
141
  else {
144
142
  const { message } = (await resp.json());
145
- throw new Error(message);
143
+ return {
144
+ content: message == null ? [] : [{ type: 'text', text: message }],
145
+ isError: true,
146
+ };
146
147
  }
147
148
  }
148
- catch (e) {
149
- throw e;
150
- }
151
149
  finally {
152
150
  worker.terminate();
153
151
  }
package/code-tool.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"code-tool.js","sourceRoot":"","sources":["src/code-tool.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoBtF,4BA6HC;AA/ID,yCAAoC;AACpC,uCAAyC;AAQzC;;;;;;;;GAQG;AACI,KAAK,UAAU,QAAQ;IAC5B,MAAM,QAAQ,GAAa,EAAE,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;IAC7E,MAAM,IAAI,GAAS;QACjB,IAAI,EAAE,SAAS;QACf,WAAW,EACT,khCAAkhC;QACphC,WAAW,EAAE,EAAE,IAAI,EAAE,Q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AM,KAAK,CAAC,CAAC,CAAC;oBACrB,IAAI;oBACN,CAAC,CAAC;wBACE,IAAI,EAAE,MAAM;wBACZ,IAAI,EAAE,iBAAiB,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC;qBAC9C,CAAC;gBACN,OAAO;oBACL,OAAO,EAAE,CAAC,YAAY,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,IAAI,CAAC;iBAChF,CAAC;YACJ,CAAC;iBAAM,CAAC;gBACN,MAAM,EAAE,OAAO,EAAE,GAAG,CAAC,MAAM,IAAI,CAAC,IAAI,EAAE,CAAgB,CAAC;gBACvD,OAAO;oBACL,OAAO,EAAE,OAAO,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;oBACjE,OAAO,EAAE,IAAI;iBACd,CAAC;YACJ,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,SAAS,EAAE,CAAC;QACrB,CAAC;IACH,CAAC,CAAC;IAEF,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AACrC,CAAC"}
package/code-tool.mjs CHANGED
@@ -4,7 +4,7 @@ import { pathToFileURL } from 'node:url';
4
4
  /**
5
5
  * A tool that runs code against a copy of the SDK.
6
6
  *
7
- * Instead of exposing every endpoint as it's own tool, which uses up too many tokens for LLMs to use at once,
7
+ * Instead of exposing every endpoint as its own tool, which uses up too many tokens for LLMs to use at once,
8
8
  * we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
9
9
  * a generic endpoint that can be used to invoke any endpoint with the provided arguments.
10
10
  *
@@ -14,7 +14,7 @@ export async function codeTool() {
14
14
  const metadata = { resource: 'all', operation: 'write', tags: [] };
15
15
  const tool = {
16
16
  name: 'execute',
17
- description: 'Runs Typescript code to interact with the API.\nYou are a skilled programmer writing code to interface with the service.\nDefine an async function named "run" that takes a single parameter of an initialized client, and it will be run.\nDo not initialize a client, but instead use the client that you are given as a parameter.\nYou will be returned anything that your function returns, plus the results of any console.log statements.\nIf any code triggers an error, the tool will return an error response, so you do not need to add error handling unless you want to output something more helpful than the raw error.\nIt is not necessary to add comments to code, unless by adding those comments you believe that you can generate better code.\nThis code will run in a container, and you will not be able to use fetch or otherwise interact with the network calls other than through the client you are given.\nAny variables you define won\'t live between successive uses of this call, so make sure to return or log any data you might need later.',
17
+ description: 'Runs JavaScript code to interact with the API.\n\nYou are a skilled programmer writing code to interface with the service.\nDefine an async function named "run" that takes a single parameter of an initialized client named "client", and it will be run.\nWrite code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```\n\nYou will be returned anything that your function returns, plus the results of any console.log statements.\nIf any code triggers an error, the tool will return an error response, so you do not need to add error handling unless you want to output something more helpful than the raw error.\nIt is not necessary to add comments to code, unless by adding those comments you believe that you can generate better code.\nThis code will run in a container, and you will not be able to use fetch or otherwise interact with the network calls other than through the client you are given.\nAny variables you define won\'t live between successive uses of this call, so make sure to return or log any data you might need later.',
18
18
  inputSchema: { type: 'object', properties: { code: { type: 'string' } } },
19
19
  };
20
20
  // Import dynamically to avoid failing at import time in cases where the environment is not well-supported.
@@ -74,7 +74,7 @@ export async function codeTool() {
74
74
  code,
75
75
  });
76
76
  req.write(body, (err) => {
77
- if (err !== null && err !== undefined) {
77
+ if (err != null) {
78
78
  reject(err);
79
79
  }
80
80
  });
@@ -82,12 +82,10 @@ export async function codeTool() {
82
82
  });
83
83
  if (resp.status === 200) {
84
84
  const { result, logLines, errLines } = (await resp.json());
85
- const returnOutput = result === null ? null
86
- : result === undefined ? null
87
- : {
88
- type: 'text',
89
- text: typeof result === 'string' ? result : JSON.stringify(result),
90
- };
85
+ const returnOutput = result == null ? null : ({
86
+ type: 'text',
87
+ text: typeof result === 'string' ? result : JSON.stringify(result),
88
+ });
91
89
  const logOutput = logLines.length === 0 ?
92
90
  null
93
91
  : {
@@ -106,12 +104,12 @@ export async function codeTool() {
106
104
  }
107
105
  else {
108
106
  const { message } = (await resp.json());
109
- throw new Error(message);
107
+ return {
108
+ content: message == null ? [] : [{ type: 'text', text: message }],
109
+ isError: true,
110
+ };
110
111
  }
111
112
  }
112
- catch (e) {
113
- throw e;
114
- }
115
113
  finally {
116
114
  worker.terminate();
117
115
  }