querySLSLogs
Extract and analyze logs from Aliyun Simple Log Service (SLS) by specifying project, logstore, query, and time range. Supports pagination, result ordering, and customizable limits for efficient log retrieval.
Instructions
Query Aliyun SLS (Simple Log Service) logs
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| from | No | Start time in milliseconds (defaults to 1 hour ago) | |
| limit | No | Maximum number of logs to return (default: 100, max: 1000) | |
| logstore | Yes | SLS logstore name | |
| offset | No | Offset for pagination (default: 0) | |
| project | Yes | SLS project name | |
| query | Yes | SLS query statement | |
| reverse | No | Whether to return results in reverse order (default: false) | |
| to | No | End time in milliseconds (defaults to now) |
Implementation Reference
- src/services/sls.ts:94-140 (handler)Core implementation of the querySLSLogs tool logic using Aliyun SLS SDK.public static async queryLogs(params: SLSQueryParams): Promise<any> { console.error('[SLS] Querying logs with params:', JSON.stringify({ project: params.project, logstore: params.logstore, query: params.query, from: params.from, to: params.to, limit: params.limit })); try { const slsClient = this.getSLSClient(); // Prepare query parameters const now = Math.floor(Date.now() / 1000); const queryParams = { projectName: params.project, logStoreName: params.logstore, from: params.from ? Math.floor(params.from / 1000) : now - 3600, // Default to last hour to: params.to ? Math.floor(params.to / 1000) : now, query: params.query, line: params.limit || 100, offset: params.offset || 0, reverse: params.reverse || false }; // Execute the query return new Promise((resolve, reject) => { slsClient.getLogs(queryParams, (error: any, data: any) => { if (error) { console.error('[SLS] Error querying logs:', error); reject(error); return; } console.error('[SLS] Successfully retrieved logs'); resolve(data); }); }); } catch (error: any) { console.error('[SLS] Error in queryLogs:', error); throw new McpError( ErrorCode.InternalError, `SLS query failed: ${error.message || 'Unknown error'}` ); } }
- src/services/sls.ts:11-20 (schema)TypeScript interface defining the input parameters for the SLS log query.export interface SLSQueryParams { project: string; logstore: string; query: string; from?: number; to?: number; limit?: number; offset?: number; reverse?: boolean; }
- src/index.ts:70-111 (registration)Tool registration in MCP ListTools response, defining name, description, and input schema.{ name: "querySLSLogs", description: "Query Aliyun SLS (Simple Log Service) logs", inputSchema: { type: "object", properties: { project: { type: "string", description: "SLS project name" }, logstore: { type: "string", description: "SLS logstore name" }, query: { type: "string", description: "SLS query statement" }, from: { type: "number", description: "Start time in milliseconds (defaults to 1 hour ago)" }, to: { type: "number", description: "End time in milliseconds (defaults to now)" }, limit: { type: "number", description: "Maximum number of logs to return (default: 100, max: 1000)" }, offset: { type: "number", description: "Offset for pagination (default: 0)" }, reverse: { type: "boolean", description: "Whether to return results in reverse order (default: false)" } }, required: ["project", "logstore", "query"] } }
- src/index.ts:123-165 (handler)MCP CallToolRequestSchema handler for querySLSLogs, validates input and delegates to SLSService.case "querySLSLogs": { try { // Cast arguments to SLSQueryParams with proper type checking const args = request.params.arguments || {}; const params: SLSQueryParams = { project: String(args.project || ''), logstore: String(args.logstore || ''), query: String(args.query || ''), from: typeof args.from === 'number' ? args.from : undefined, to: typeof args.to === 'number' ? args.to : undefined, limit: typeof args.limit === 'number' ? args.limit : undefined, offset: typeof args.offset === 'number' ? args.offset : undefined, reverse: typeof args.reverse === 'boolean' ? args.reverse : undefined }; if (!params.project || !params.logstore || !params.query) { throw new McpError( ErrorCode.InvalidParams, "Missing required parameters: project, logstore, and query are required" ); } const result = await SLSService.queryLogs(params); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] }; } catch (error: any) { console.error('[Tool] Error executing querySLSLogs:', error); if (error instanceof McpError) { throw error; } throw new McpError( ErrorCode.InternalError, `Error querying SLS logs: ${error.message || 'Unknown error'}` ); } }