MCPcopy
hub / github.com/BuilderIO/agent-native / handleToolDataList

Function handleToolDataList

packages/core/src/tools/routes.ts:237–296  ·  view source on GitHub ↗
(
  event: H3Event,
  toolId: string,
  collection: string,
  userEmail: string,
)

Source from the content-addressed store, hash-verified

235}
236
237async function handleToolDataList(
238 event: H3Event,
239 toolId: string,
240 collection: string,
241 userEmail: string,
242): Promise<unknown> {
243 await ensureToolsTables();
244 const tool = await getTool(toolId);
245 if (!tool) {
246 setResponseStatus(event, 404);
247 return { error: "Tool not found" };
248 }
249 const client = getDbExec();
250 const url = event.url;
251 const limitParam = url?.searchParams?.get("limit");
252 const limit = limitParam
253 ? Math.min(Math.max(1, Number(limitParam)), 1000)
254 : 100;
255 const scope = url?.searchParams?.get("scope") || "user";
256 const orgId = getRequestOrgId();
257
258 if (scope === "org") {
259 if (!orgId) {
260 setResponseStatus(event, 400);
261 return { error: "Org context required for scope=org" };
262 }
263 const result = await client.execute({
264 sql: `SELECT COALESCE(item_id, id) AS id, tool_id, collection, data, owner_email, scope, org_id, created_at, updated_at
265 FROM tool_data
266 WHERE tool_id = ? AND collection = ? AND scope = 'org' AND org_id = ?
267 ORDER BY created_at DESC
268 LIMIT ?`,
269 args: [toolId, collection, orgId, limit],
270 });
271 return result.rows ?? [];
272 }
273
274 if (scope === "all") {
275 const result = await client.execute({
276 sql: `SELECT COALESCE(item_id, id) AS id, tool_id, collection, data, owner_email, scope, org_id, created_at, updated_at
277 FROM tool_data
278 WHERE tool_id = ? AND collection = ?
279 AND ((scope = 'user' AND owner_email = ?) OR (scope = 'org' AND org_id = ?))
280 ORDER BY created_at DESC
281 LIMIT ?`,
282 args: [toolId, collection, userEmail, orgId ?? "", limit],
283 });
284 return result.rows ?? [];
285 }
286
287 const result = await client.execute({
288 sql: `SELECT COALESCE(item_id, id) AS id, tool_id, collection, data, owner_email, scope, org_id, created_at, updated_at
289 FROM tool_data
290 WHERE tool_id = ? AND collection = ? AND scope = 'user' AND owner_email = ?
291 ORDER BY updated_at DESC
292 LIMIT ?`,
293 args: [toolId, collection, userEmail, limit],
294 });

Callers 1

dispatchFunction · 0.85

Calls 6

ensureToolsTablesFunction · 0.85
getToolFunction · 0.85
getDbExecFunction · 0.85
getRequestOrgIdFunction · 0.85
getMethod · 0.80
executeMethod · 0.65

Tested by

no test coverage detected