get_recent_logs
Retrieve recent log events from AWS CloudWatch Log Groups to monitor application performance and troubleshoot issues.
Instructions
Retrieves recent log events from a CloudWatch Log Group.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| log_group_name | Yes | Name of the Log Group. | |
| limit | No | Number of log events to return (default: 20). |
Implementation Reference
- src/index.ts:1737-1777 (handler)Executes the get_recent_logs tool: fetches the latest log stream from the specified CloudWatch Log Group and retrieves the most recent log events using AWS CloudWatchLogsClient.if (name === "get_recent_logs") { const groupName = (args as any).log_group_name; const limit = (args as any)?.limit || 20; // Get latest stream const streamCmd = new DescribeLogStreamsCommand({ logGroupName: groupName, orderBy: "LastEventTime", descending: true, limit: 1 }); try { const streamResp = await cloudWatchLogsClient.send(streamCmd); const streamName = streamResp.logStreams?.[0]?.logStreamName; if (!streamName) { return { content: [{ type: "text", text: "No log streams found." }] }; } const eventsCmd = new GetLogEventsCommand({ logGroupName: groupName, logStreamName: streamName, limit: limit, startFromHead: false }); const eventsResp = await cloudWatchLogsClient.send(eventsCmd); const logs = eventsResp.events?.map(e => ({ Timestamp: new Date(e.timestamp || 0).toISOString(), Message: e.message })) || []; return { content: [{ type: "text", text: JSON.stringify(logs, null, 2) }] }; } catch (err: any) { return { content: [{ type: "text", text: `Error fetching logs: ${err.message}` }], isError: true }; } }
- src/index.ts:464-481 (registration)Registers the 'get_recent_logs' tool in the ListTools response, defining its name, description, and input schema.{ name: "get_recent_logs", description: "Retrieves recent log events from a CloudWatch Log Group.", inputSchema: { type: "object", properties: { log_group_name: { type: "string", description: "Name of the Log Group." }, limit: { type: "number", description: "Number of log events to return (default: 20)." } }, required: ["log_group_name"] } },
- src/index.ts:467-480 (schema)Defines the input schema for the get_recent_logs tool, specifying required log_group_name and optional limit.inputSchema: { type: "object", properties: { log_group_name: { type: "string", description: "Name of the Log Group." }, limit: { type: "number", description: "Number of log events to return (default: 20)." } }, required: ["log_group_name"] }