query
Query specific data from a UniCloud database collection using JQL conditions, filter results by fields, apply sorting, and limit or skip records for precise retrieval.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | 集合名称 | |
| field | No | 返回字段 (可选) | |
| limit | No | 限制返回数量 (可选) | |
| orderBy | No | 排序条件 (可选) | |
| skip | No | 跳过记录数 (可选) | |
| where | Yes | 查询条件 (JQL格式) |
Implementation Reference
- database.js:232-253 (handler)The main handler function for the 'query' MCP tool. Extracts parameters, calls queryDatabase helper, and returns formatted MCP response or error.async function handleQueryTool(params, dbUrl) { const { collection, where, field, orderBy, limit, skip } = params; try { const options = { field: field || undefined, orderBy: orderBy || undefined, limit: limit || undefined, skip: skip || undefined, }; const data = await queryDatabase(collection, where, options, dbUrl); return { content: [{ type: 'text', text: JSON.stringify(data, null, 2) }], }; } catch (error) { return { content: [{ type: 'text', text: error.message }], isError: true, }; } }
- database.js:373-390 (schema)Tool definition including name 'query', Zod input schema (params), and reference to handler function.queryTool: { name: 'query', params: { collection: z.string().describe('集合名称'), where: z.record(z.any()).describe('查询条件 (JQL格式)'), field: z.record(z.any()).optional().describe('返回字段 (可选)'), orderBy: z .object({ field: z.string(), order: z.enum(['asc', 'desc']), }) .optional() .describe('排序条件 (可选)'), limit: z.number().optional().describe('限制返回数量 (可选)'), skip: z.number().optional().describe('跳过记录数 (可选)'), }, handler: (params) => handleQueryTool(params, dbServiceUrl), },
- index.js:38-42 (registration)Registration of the 'query' tool on the MCP server using the definition from getToolDefinitions().server.tool( tools.queryTool.name, tools.queryTool.params, tools.queryTool.handler );
- database.js:30-79 (helper)Helper function that performs HTTP request to the uniCloud DB service (mcp_service) with 'query' action, handling timeout and response.async function queryDatabase( collection, where, options = {}, dbUrl = DEFAULT_DB_URL ) { try { // 创建请求控制器,用于超时处理 const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), REQUEST_TIMEOUT); // 使用传入的dbUrl或默认URL const targetUrl = dbUrl || DEFAULT_DB_URL; // console.log('查询操作使用URL:', targetUrl); // 发送请求 const response = await fetch(targetUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ operation: { collection, action: 'query', where, field: options.field, orderBy: options.orderBy, limit: options.limit, skip: options.skip, }, }), signal: controller.signal, }); clearTimeout(timeoutId); const result = await response.json(); // 处理响应结果 if (result.code !== 0) { throw new Error(result.msg || '查询失败'); } return result.data; } catch (error) { // 超时错误特殊处理 if (error.name === 'AbortError') { throw new Error(`查询超时: 请求超过${REQUEST_TIMEOUT}毫秒`); } throw new Error(`查询错误: ${error.message}`); } }
- mcp_service/index.js:61-67 (helper)Backend database query execution in uniCloud service (called by queryDatabase helper). Builds and executes the JQL query.case 'query': if (where) query = query.where(where); if (field) query = query.field(field); if (orderBy) query = query.orderBy(orderBy.field, orderBy.order); if (limit) query = query.limit(limit); if (skip) query = query.skip(skip); return await query.get();