query
Query data from UniCloud database collections using JQL conditions, with options to filter fields, sort results, and limit returns.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | 集合名称 | |
| where | Yes | 查询条件 (JQL格式) | |
| field | No | 返回字段 (可选) | |
| orderBy | No | 排序条件 (可选) | |
| limit | No | 限制返回数量 (可选) | |
| skip | No | 跳过记录数 (可选) |
Implementation Reference
- database.js:232-253 (handler)Handler function for the 'query' tool. Destructures input params, prepares query options, executes queryDatabase helper, and returns MCP-formatted content or error response.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:376-389 (schema)Input schema for the 'query' tool defined using Zod, validating parameters: collection (string), where (record), field (optional record), orderBy (optional object), limit/skip (optional numbers).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 instance via server.tool(name, paramsSchema, handler).server.tool( tools.queryTool.name, tools.queryTool.params, tools.queryTool.handler );
- database.js:30-79 (helper)Helper function that sends HTTP POST request to the database service endpoint with 'query' action and parameters, handling timeout and response parsing.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}`); } }