scorecard-ai-mcp 2.4.0 → 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
@@ -1,11 +1,194 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
3
  import util from 'node:util';
4
+
5
+ import Fuse from 'fuse.js';
6
+ import ts from 'typescript';
7
+
4
8
  import { WorkerInput, WorkerSuccess, WorkerError } from './code-tool-types';
5
9
  import { Scorecard } from 'scorecard-ai';
6
10
 
11
+ function getRunFunctionNode(
12
+ code: string,
13
+ ): ts.FunctionDeclaration | ts.FunctionExpression | ts.ArrowFunction | null {
14
+ const sourceFile = ts.createSourceFile('code.ts', code, ts.ScriptTarget.Latest, true);
15
+
16
+ for (const statement of sourceFile.statements) {
17
+ // Check for top-level function declarations
18
+ if (ts.isFunctionDeclaration(statement)) {
19
+ if (statement.name?.text === 'run') {
20
+ return statement;
21
+ }
22
+ }
23
+
24
+ // Check for variable declarations: const run = () => {} or const run = function() {}
25
+ if (ts.isVariableStatement(statement)) {
26
+ for (const declaration of statement.declarationList.declarations) {
27
+ if (ts.isIdentifier(declaration.name) && declaration.name.text === 'run') {
28
+ // Check if it's initialized with a function
29
+ if (
30
+ declaration.initializer &&
31
+ (ts.isFunctionExpression(declaration.initializer) || ts.isArrowFunction(declaration.initializer))
32
+ ) {
33
+ return declaration.initializer;
34
+ }
35
+ }
36
+ }
37
+ }
38
+ }
39
+
40
+ return null;
41
+ }
42
+
43
+ const fuse = new Fuse(
44
+ [
45
+ 'client.projects.create',
46
+ 'client.projects.list',
47
+ 'client.testsets.create',
48
+ 'client.testsets.delete',
49
+ 'client.testsets.get',
50
+ 'client.testsets.list',
51
+ 'client.testsets.update',
52
+ 'client.testcases.create',
53
+ 'client.testcases.delete',
54
+ 'client.testcases.get',
55
+ 'client.testcases.list',
56
+ 'client.testcases.update',
57
+ 'client.runs.create',
58
+ 'client.runs.get',
59
+ 'client.runs.list',
60
+ 'client.metrics.create',
61
+ 'client.metrics.delete',
62
+ 'client.metrics.get',
63
+ 'client.metrics.list',
64
+ 'client.metrics.update',
65
+ 'client.records.create',
66
+ 'client.records.delete',
67
+ 'client.records.list',
68
+ 'client.scores.upsert',
69
+ 'client.systems.delete',
70
+ 'client.systems.get',
71
+ 'client.systems.list',
72
+ 'client.systems.update',
73
+ 'client.systems.upsert',
74
+ 'client.systems.versions.get',
75
+ 'client.systems.versions.upsert',
76
+ ],
77
+ { threshold: 1, shouldSort: true },
78
+ );
79
+
80
+ function getMethodSuggestions(fullyQualifiedMethodName: string): string[] {
81
+ return fuse
82
+ .search(fullyQualifiedMethodName)
83
+ .map(({ item }) => item)
84
+ .slice(0, 5);
85
+ }
86
+
87
+ const proxyToObj = new WeakMap<any, any>();
88
+ const objToProxy = new WeakMap<any, any>();
89
+
90
+ type ClientProxyConfig = {
91
+ path: string[];
92
+ isBelievedBad?: boolean;
93
+ };
94
+
95
+ function makeSdkProxy<T extends object>(obj: T, { path, isBelievedBad = false }: ClientProxyConfig): T {
96
+ let proxy: T = objToProxy.get(obj);
97
+
98
+ if (!proxy) {
99
+ proxy = new Proxy(obj, {
100
+ get(target, prop, receiver) {
101
+ const propPath = [...path, String(prop)];
102
+ const value = Reflect.get(target, prop, receiver);
103
+
104
+ if (isBelievedBad || (!(prop in target) && value === undefined)) {
105
+ // If we're accessing a path that doesn't exist, it will probably eventually error.
106
+ // Let's proxy it and mark it bad so that we can control the error message.
107
+ // We proxy an empty class so that an invocation or construction attempt is possible.
108
+ return makeSdkProxy(class {}, { path: propPath, isBelievedBad: true });
109
+ }
110
+
111
+ if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
112
+ return makeSdkProxy(value, { path: propPath, isBelievedBad });
113
+ }
114
+
115
+ return value;
116
+ },
117
+
118
+ apply(target, thisArg, args) {
119
+ if (isBelievedBad || typeof target !== 'function') {
120
+ const fullyQualifiedMethodName = path.join('.');
121
+ const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
122
+ throw new Error(
123
+ `${fullyQualifiedMethodName} is not a function. Did you mean: ${suggestions.join(', ')}`,
124
+ );
125
+ }
126
+
127
+ return Reflect.apply(target, proxyToObj.get(thisArg) ?? thisArg, args);
128
+ },
129
+
130
+ construct(target, args, newTarget) {
131
+ if (isBelievedBad || typeof target !== 'function') {
132
+ const fullyQualifiedMethodName = path.join('.');
133
+ const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
134
+ throw new Error(
135
+ `${fullyQualifiedMethodName} is not a constructor. Did you mean: ${suggestions.join(', ')}`,
136
+ );
137
+ }
138
+
139
+ return Reflect.construct(target, args, newTarget);
140
+ },
141
+ });
142
+
143
+ objToProxy.set(obj, proxy);
144
+ proxyToObj.set(proxy, obj);
145
+ }
146
+
147
+ return proxy;
148
+ }
149
+
150
+ function parseError(code: string, error: unknown): string | undefined {
151
+ if (!(error instanceof Error)) return;
152
+ const message = error.name ? `${error.name}: ${error.message}` : error.message;
153
+ try {
154
+ // Deno uses V8; the first "<anonymous>:LINE:COLUMN" is the top of stack.
155
+ const lineNumber = error.stack?.match(/<anonymous>:([0-9]+):[0-9]+/)?.[1];
156
+ // -1 for the zero-based indexing
157
+ const line =
158
+ lineNumber &&
159
+ code
160
+ .split('\n')
161
+ .at(parseInt(lineNumber, 10) - 1)
162
+ ?.trim();
163
+ return line ? `${message}\n at line ${lineNumber}\n ${line}` : message;
164
+ } catch {
165
+ return message;
166
+ }
167
+ }
168
+
7
169
  const fetch = async (req: Request): Promise<Response> => {
8
170
  const { opts, code } = (await req.json()) as WorkerInput;
171
+ if (code == null) {
172
+ return Response.json(
173
+ {
174
+ message:
175
+ '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```',
176
+ } satisfies WorkerError,
177
+ { status: 400, statusText: 'Code execution error' },
178
+ );
179
+ }
180
+
181
+ const runFunctionNode = getRunFunctionNode(code);
182
+ if (!runFunctionNode) {
183
+ return Response.json(
184
+ {
185
+ message:
186
+ '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```',
187
+ } satisfies WorkerError,
188
+ { status: 400, statusText: 'Code execution error' },
189
+ );
190
+ }
191
+
9
192
  const client = new Scorecard({
10
193
  ...opts,
11
194
  });
@@ -22,21 +205,17 @@ const fetch = async (req: Request): Promise<Response> => {
22
205
  };
23
206
  try {
24
207
  let run_ = async (client: any) => {};
25
- eval(`
26
- ${code}
27
- run_ = run;
28
- `);
29
- const result = await run_(client);
208
+ eval(`${code}\nrun_ = run;`);
209
+ const result = await run_(makeSdkProxy(client, { path: ['client'] }));
30
210
  return Response.json({
31
211
  result,
32
212
  logLines,
33
213
  errLines,
34
214
  } satisfies WorkerSuccess);
35
215
  } catch (e) {
36
- const message = e instanceof Error ? e.message : undefined;
37
216
  return Response.json(
38
217
  {
39
- message,
218
+ message: parseError(code, e),
40
219
  } satisfies WorkerError,
41
220
  { status: 400, statusText: 'Code execution error' },
42
221
  );
package/src/code-tool.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  import { dirname } from 'node:path';
4
4
  import { pathToFileURL } from 'node:url';
5
5
  import Scorecard, { ClientOptions } from 'scorecard-ai';
6
- import { Endpoint, ContentBlock, Metadata } from './tools/types';
6
+ import { ContentBlock, Endpoint, Metadata, ToolCallResult } from './tools/types';
7
7
 
8
8
  import { Tool } from '@modelcontextprotocol/sdk/types.js';
9
9
 
@@ -12,7 +12,7 @@ import { WorkerInput, WorkerError, WorkerSuccess } from './code-tool-types';
12
12
  /**
13
13
  * A tool that runs code against a copy of the SDK.
14
14
  *
15
- * Instead of exposing every endpoint as it's own tool, which uses up too many tokens for LLMs to use at once,
15
+ * Instead of exposing every endpoint as its own tool, which uses up too many tokens for LLMs to use at once,
16
16
  * we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
17
17
  * a generic endpoint that can be used to invoke any endpoint with the provided arguments.
18
18
  *
@@ -23,7 +23,7 @@ export async function codeTool(): Promise<Endpoint> {
23
23
  const tool: Tool = {
24
24
  name: 'execute',
25
25
  description:
26
- '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.',
26
+ '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.',
27
27
  inputSchema: { type: 'object', properties: { code: { type: 'string' } } },
28
28
  };
29
29
 
@@ -31,7 +31,7 @@ export async function codeTool(): Promise<Endpoint> {
31
31
  const { newDenoHTTPWorker } = await import('@valtown/deno-http-worker');
32
32
  const { workerPath } = await import('./code-tool-paths.cjs');
33
33
 
34
- const handler = async (client: Scorecard, args: unknown) => {
34
+ const handler = async (client: Scorecard, args: unknown): Promise<ToolCallResult> => {
35
35
  const baseURLHostname = new URL(client.baseURL).hostname;
36
36
  const { code } = args as { code: string };
37
37
 
@@ -97,7 +97,7 @@ export async function codeTool(): Promise<Endpoint> {
97
97
  } satisfies WorkerInput);
98
98
 
99
99
  req.write(body, (err) => {
100
- if (err !== null && err !== undefined) {
100
+ if (err != null) {
101
101
  reject(err);
102
102
  }
103
103
  });
@@ -108,12 +108,12 @@ export async function codeTool(): Promise<Endpoint> {
108
108
  if (resp.status === 200) {
109
109
  const { result, logLines, errLines } = (await resp.json()) as WorkerSuccess;
110
110
  const returnOutput: ContentBlock | null =
111
- result === null ? null
112
- : result === undefined ? null
113
- : {
111
+ result == null ? null : (
112
+ {
114
113
  type: 'text',
115
- text: typeof result === 'string' ? (result as string) : JSON.stringify(result),
116
- };
114
+ text: typeof result === 'string' ? result : JSON.stringify(result),
115
+ }
116
+ );
117
117
  const logOutput: ContentBlock | null =
118
118
  logLines.length === 0 ?
119
119
  null
@@ -133,10 +133,11 @@ export async function codeTool(): Promise<Endpoint> {
133
133
  };
134
134
  } else {
135
135
  const { message } = (await resp.json()) as WorkerError;
136
- throw new Error(message);
136
+ return {
137
+ content: message == null ? [] : [{ type: 'text', text: message }],
138
+ isError: true,
139
+ };
137
140
  }
138
- } catch (e) {
139
- throw e;
140
141
  } finally {
141
142
  worker.terminate();
142
143
  }
@@ -14,7 +14,7 @@ function zodToInputSchema(schema: z.ZodSchema) {
14
14
  /**
15
15
  * A list of tools that expose all the endpoints in the API dynamically.
16
16
  *
17
- * Instead of exposing every endpoint as it's own tool, which uses up too many tokens for LLMs to use at once,
17
+ * Instead of exposing every endpoint as its own tool, which uses up too many tokens for LLMs to use at once,
18
18
  * we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
19
19
  * a generic endpoint that can be used to invoke any endpoint with the provided arguments.
20
20
  *
package/src/filtering.ts CHANGED
@@ -12,3 +12,7 @@ export async function maybeFilter(jqFilter: unknown | undefined, response: any):
12
12
  async function jq(json: any, jqFilter: string) {
13
13
  return (await initJq).json(json, jqFilter);
14
14
  }
15
+
16
+ export function isJqError(error: any): error is Error {
17
+ return error instanceof Error && 'stderr' in error;
18
+ }
package/src/headers.ts CHANGED
@@ -11,7 +11,9 @@ export const parseAuthHeaders = (req: IncomingMessage): Partial<ClientOptions> =
11
11
  case 'Bearer':
12
12
  return { apiKey: req.headers.authorization.slice('Bearer '.length) };
13
13
  default:
14
- throw new Error(`Unsupported authorization scheme`);
14
+ throw new Error(
15
+ 'Unsupported authorization scheme. Expected the "Authorization" header to be a supported scheme (Bearer).',
16
+ );
15
17
  }
16
18
  }
17
19
 
package/src/http.ts CHANGED
@@ -46,12 +46,12 @@ const newServer = ({
46
46
  },
47
47
  mcpOptions,
48
48
  });
49
- } catch {
49
+ } catch (error) {
50
50
  res.status(401).json({
51
51
  jsonrpc: '2.0',
52
52
  error: {
53
53
  code: -32000,
54
- message: 'Unauthorized',
54
+ message: `Unauthorized: ${error instanceof Error ? error.message : error}`,
55
55
  },
56
56
  });
57
57
  return null;
package/src/options.ts CHANGED
@@ -284,8 +284,10 @@ const coerceArray = <T extends z.ZodTypeAny>(zodType: T) =>
284
284
  );
285
285
 
286
286
  const QueryOptions = z.object({
287
- tools: coerceArray(z.enum(['dynamic', 'all', 'docs'])).describe('Use dynamic tools or all tools'),
288
- no_tools: coerceArray(z.enum(['dynamic', 'all', 'docs'])).describe('Do not use dynamic tools or all tools'),
287
+ tools: coerceArray(z.enum(['dynamic', 'all', 'code', 'docs'])).describe('Specify which MCP tools to use'),
288
+ no_tools: coerceArray(z.enum(['dynamic', 'all', 'code', 'docs'])).describe(
289
+ 'Specify which MCP tools to not use.',
290
+ ),
289
291
  tool: coerceArray(z.string()).describe('Include tools matching the specified names'),
290
292
  resource: coerceArray(z.string()).describe('Include tools matching the specified resources'),
291
293
  operation: coerceArray(z.enum(['read', 'write'])).describe(
@@ -385,11 +387,16 @@ export function parseQueryOptions(defaultOptions: McpOptions, query: unknown): M
385
387
  : queryOptions.tools?.includes('docs') ? true
386
388
  : defaultOptions.includeDocsTools;
387
389
 
390
+ let codeTools: boolean | undefined =
391
+ queryOptions.no_tools && queryOptions.no_tools?.includes('code') ? false
392
+ : queryOptions.tools?.includes('code') && defaultOptions.includeCodeTools ? true
393
+ : defaultOptions.includeCodeTools;
394
+
388
395
  return {
389
396
  client: queryOptions.client ?? defaultOptions.client,
390
397
  includeDynamicTools: dynamicTools,
391
398
  includeAllTools: allTools,
392
- includeCodeTools: undefined,
399
+ includeCodeTools: codeTools,
393
400
  includeDocsTools: docsTools,
394
401
  filters,
395
402
  capabilities: clientCapabilities,
package/src/server.ts CHANGED
@@ -34,7 +34,7 @@ export const newMcpServer = () =>
34
34
  new McpServer(
35
35
  {
36
36
  name: 'scorecard_ai_api',
37
- version: '2.4.0',
37
+ version: '2.5.0',
38
38
  },
39
39
  { capabilities: { tools: {}, logging: {} } },
40
40
  );
@@ -22,6 +22,7 @@ import get_runs from './runs/get-runs';
22
22
  import create_metrics from './metrics/create-metrics';
23
23
  import update_metrics from './metrics/update-metrics';
24
24
  import list_metrics from './metrics/list-metrics';
25
+ import delete_metrics from './metrics/delete-metrics';
25
26
  import get_metrics from './metrics/get-metrics';
26
27
  import create_records from './records/create-records';
27
28
  import list_records from './records/list-records';
@@ -59,6 +60,7 @@ addEndpoint(get_runs);
59
60
  addEndpoint(create_metrics);
60
61
  addEndpoint(update_metrics);
61
62
  addEndpoint(list_metrics);
63
+ addEndpoint(delete_metrics);
62
64
  addEndpoint(get_metrics);
63
65
  addEndpoint(create_records);
64
66
  addEndpoint(list_records);
@@ -0,0 +1,54 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import { isJqError, maybeFilter } from 'scorecard-ai-mcp/filtering';
4
+ import { Metadata, asErrorResult, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
5
+
6
+ import { Tool } from '@modelcontextprotocol/sdk/types.js';
7
+ import Scorecard from 'scorecard-ai';
8
+
9
+ export const metadata: Metadata = {
10
+ resource: 'metrics',
11
+ operation: 'write',
12
+ tags: [],
13
+ httpMethod: 'delete',
14
+ httpPath: '/metrics/{metricId}',
15
+ operationId: 'deleteMetric',
16
+ };
17
+
18
+ export const tool: Tool = {
19
+ name: 'delete_metrics',
20
+ description:
21
+ "When using this tool, always use the `jq_filter` parameter to reduce the response size and improve performance.\n\nOnly omit if you're sure you don't need the data.\n\nDelete a specific Metric by ID. The metric will be removed from metric groups and monitors.\n\n# Response Schema\n```json\n{\n $ref: '#/$defs/metric_delete_response',\n $defs: {\n metric_delete_response: {\n type: 'object',\n properties: {\n success: {\n type: 'boolean',\n description: 'Whether the deletion was successful.'\n }\n },\n required: [ 'success'\n ]\n }\n }\n}\n```",
22
+ inputSchema: {
23
+ type: 'object',
24
+ properties: {
25
+ metricId: {
26
+ type: 'string',
27
+ },
28
+ jq_filter: {
29
+ type: 'string',
30
+ title: 'jq Filter',
31
+ description:
32
+ 'A jq filter to apply to the response to include certain fields. Consult the output schema in the tool description to see the fields that are available.\n\nFor example: to include only the `name` field in every object of a results array, you can provide ".results[].name".\n\nFor more information, see the [jq documentation](https://jqlang.org/manual/).',
33
+ },
34
+ },
35
+ required: ['metricId'],
36
+ },
37
+ annotations: {
38
+ idempotentHint: true,
39
+ },
40
+ };
41
+
42
+ export const handler = async (client: Scorecard, args: Record<string, unknown> | undefined) => {
43
+ const { metricId, jq_filter, ...body } = args as any;
44
+ try {
45
+ return asTextContentResult(await maybeFilter(jq_filter, await client.metrics.delete(metricId)));
46
+ } catch (error) {
47
+ if (isJqError(error)) {
48
+ return asErrorResult(error.message);
49
+ }
50
+ throw error;
51
+ }
52
+ };
53
+
54
+ export default { metadata, tool, handler };
@@ -1,7 +1,7 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
- import { maybeFilter } from 'scorecard-ai-mcp/filtering';
4
- import { Metadata, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
3
+ import { isJqError, maybeFilter } from 'scorecard-ai-mcp/filtering';
4
+ import { Metadata, asErrorResult, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
5
5
 
6
6
  import { Tool } from '@modelcontextprotocol/sdk/types.js';
7
7
  import Scorecard from 'scorecard-ai';
@@ -44,7 +44,14 @@ export const tool: Tool = {
44
44
 
45
45
  export const handler = async (client: Scorecard, args: Record<string, unknown> | undefined) => {
46
46
  const { jq_filter, ...body } = args as any;
47
- return asTextContentResult(await maybeFilter(jq_filter, await client.projects.create(body)));
47
+ try {
48
+ return asTextContentResult(await maybeFilter(jq_filter, await client.projects.create(body)));
49
+ } catch (error) {
50
+ if (isJqError(error)) {
51
+ return asErrorResult(error.message);
52
+ }
53
+ throw error;
54
+ }
48
55
  };
49
56
 
50
57
  export default { metadata, tool, handler };
@@ -1,7 +1,7 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
- import { maybeFilter } from 'scorecard-ai-mcp/filtering';
4
- import { Metadata, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
3
+ import { isJqError, maybeFilter } from 'scorecard-ai-mcp/filtering';
4
+ import { Metadata, asErrorResult, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
5
5
 
6
6
  import { Tool } from '@modelcontextprotocol/sdk/types.js';
7
7
  import Scorecard from 'scorecard-ai';
@@ -49,7 +49,14 @@ export const tool: Tool = {
49
49
  export const handler = async (client: Scorecard, args: Record<string, unknown> | undefined) => {
50
50
  const { jq_filter, ...body } = args as any;
51
51
  const response = await client.projects.list(body).asResponse();
52
- return asTextContentResult(await maybeFilter(jq_filter, await response.json()));
52
+ try {
53
+ return asTextContentResult(await maybeFilter(jq_filter, await response.json()));
54
+ } catch (error) {
55
+ if (isJqError(error)) {
56
+ return asErrorResult(error.message);
57
+ }
58
+ throw error;
59
+ }
53
60
  };
54
61
 
55
62
  export default { metadata, tool, handler };
@@ -1,7 +1,7 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
- import { maybeFilter } from 'scorecard-ai-mcp/filtering';
4
- import { Metadata, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
3
+ import { isJqError, maybeFilter } from 'scorecard-ai-mcp/filtering';
4
+ import { Metadata, asErrorResult, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
5
5
 
6
6
  import { Tool } from '@modelcontextprotocol/sdk/types.js';
7
7
  import Scorecard from 'scorecard-ai';
@@ -58,7 +58,14 @@ export const tool: Tool = {
58
58
 
59
59
  export const handler = async (client: Scorecard, args: Record<string, unknown> | undefined) => {
60
60
  const { runId, jq_filter, ...body } = args as any;
61
- return asTextContentResult(await maybeFilter(jq_filter, await client.records.create(runId, body)));
61
+ try {
62
+ return asTextContentResult(await maybeFilter(jq_filter, await client.records.create(runId, body)));
63
+ } catch (error) {
64
+ if (isJqError(error)) {
65
+ return asErrorResult(error.message);
66
+ }
67
+ throw error;
68
+ }
62
69
  };
63
70
 
64
71
  export default { metadata, tool, handler };
@@ -1,7 +1,7 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
- import { maybeFilter } from 'scorecard-ai-mcp/filtering';
4
- import { Metadata, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
3
+ import { isJqError, maybeFilter } from 'scorecard-ai-mcp/filtering';
4
+ import { Metadata, asErrorResult, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
5
5
 
6
6
  import { Tool } from '@modelcontextprotocol/sdk/types.js';
7
7
  import Scorecard from 'scorecard-ai';
@@ -41,7 +41,14 @@ export const tool: Tool = {
41
41
 
42
42
  export const handler = async (client: Scorecard, args: Record<string, unknown> | undefined) => {
43
43
  const { recordId, jq_filter, ...body } = args as any;
44
- return asTextContentResult(await maybeFilter(jq_filter, await client.records.delete(recordId)));
44
+ try {
45
+ return asTextContentResult(await maybeFilter(jq_filter, await client.records.delete(recordId)));
46
+ } catch (error) {
47
+ if (isJqError(error)) {
48
+ return asErrorResult(error.message);
49
+ }
50
+ throw error;
51
+ }
45
52
  };
46
53
 
47
54
  export default { metadata, tool, handler };
@@ -1,7 +1,7 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
- import { maybeFilter } from 'scorecard-ai-mcp/filtering';
4
- import { Metadata, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
3
+ import { isJqError, maybeFilter } from 'scorecard-ai-mcp/filtering';
4
+ import { Metadata, asErrorResult, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
5
5
 
6
6
  import { Tool } from '@modelcontextprotocol/sdk/types.js';
7
7
  import Scorecard from 'scorecard-ai';
@@ -52,7 +52,14 @@ export const tool: Tool = {
52
52
  export const handler = async (client: Scorecard, args: Record<string, unknown> | undefined) => {
53
53
  const { runId, jq_filter, ...body } = args as any;
54
54
  const response = await client.records.list(runId, body).asResponse();
55
- return asTextContentResult(await maybeFilter(jq_filter, await response.json()));
55
+ try {
56
+ return asTextContentResult(await maybeFilter(jq_filter, await response.json()));
57
+ } catch (error) {
58
+ if (isJqError(error)) {
59
+ return asErrorResult(error.message);
60
+ }
61
+ throw error;
62
+ }
56
63
  };
57
64
 
58
65
  export default { metadata, tool, handler };
@@ -1,7 +1,7 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
- import { maybeFilter } from 'scorecard-ai-mcp/filtering';
4
- import { Metadata, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
3
+ import { isJqError, maybeFilter } from 'scorecard-ai-mcp/filtering';
4
+ import { Metadata, asErrorResult, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
5
5
 
6
6
  import { Tool } from '@modelcontextprotocol/sdk/types.js';
7
7
  import Scorecard from 'scorecard-ai';
@@ -54,7 +54,14 @@ export const tool: Tool = {
54
54
 
55
55
  export const handler = async (client: Scorecard, args: Record<string, unknown> | undefined) => {
56
56
  const { projectId, jq_filter, ...body } = args as any;
57
- return asTextContentResult(await maybeFilter(jq_filter, await client.runs.create(projectId, body)));
57
+ try {
58
+ return asTextContentResult(await maybeFilter(jq_filter, await client.runs.create(projectId, body)));
59
+ } catch (error) {
60
+ if (isJqError(error)) {
61
+ return asErrorResult(error.message);
62
+ }
63
+ throw error;
64
+ }
58
65
  };
59
66
 
60
67
  export default { metadata, tool, handler };
@@ -1,7 +1,7 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
- import { maybeFilter } from 'scorecard-ai-mcp/filtering';
4
- import { Metadata, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
3
+ import { isJqError, maybeFilter } from 'scorecard-ai-mcp/filtering';
4
+ import { Metadata, asErrorResult, asTextContentResult } from 'scorecard-ai-mcp/tools/types';
5
5
 
6
6
  import { Tool } from '@modelcontextprotocol/sdk/types.js';
7
7
  import Scorecard from 'scorecard-ai';
@@ -41,7 +41,14 @@ export const tool: Tool = {
41
41
 
42
42
  export const handler = async (client: Scorecard, args: Record<string, unknown> | undefined) => {
43
43
  const { runId, jq_filter, ...body } = args as any;
44
- return asTextContentResult(await maybeFilter(jq_filter, await client.runs.get(runId)));
44
+ try {
45
+ return asTextContentResult(await maybeFilter(jq_filter, await client.runs.get(runId)));
46
+ } catch (error) {
47
+ if (isJqError(error)) {
48
+ return asErrorResult(error.message);
49
+ }
50
+ throw error;
51
+ }
45
52
  };
46
53
 
47
54
  export default { metadata, tool, handler };