search_log_last_n_records
Retrieve the most recent log entries with optional filtering and surrounding context for analysis.
Instructions
Search for the last N (newest) records, optionally filtering, with context.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| count | Yes | ||
| scope | No | default | |
| context_before | No | ||
| context_after | No | ||
| log_dirs_override | No | ||
| log_content_patterns_override | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- The MCP tool handler for search_log_last_n_records. Builds filter criteria with last_n parameter and delegates to analysis_engine.search_logs(filter_criteria) to perform the search.
@mcp.tool() async def search_log_last_n_records( count: int, scope: str = "default", context_before: int = 2, context_after: int = 2, log_dirs_override: str = "", log_content_patterns_override: str = "", ) -> list[dict[str, Any]]: """Search for the last N (newest) records, optionally filtering, with context.""" logger.info( "MCP search_log_last_n_records called with count=%s, scope='%s', " "context=%sB/%sA, log_dirs_override='%s', " "log_content_patterns_override='%s'", count, scope, context_before, context_after, log_dirs_override, log_content_patterns_override, ) if count <= 0: logger.error("Invalid count for search_log_last_n_records: %s. Must be > 0.", count) raise McpError(ErrorData(code=-32602, message="Count must be a positive integer.")) log_dirs_list = log_dirs_override.split(",") if log_dirs_override else None log_content_patterns_list = log_content_patterns_override.split(",") if log_content_patterns_override else None filter_criteria = build_filter_criteria( last_n=count, scope=scope, context_before=context_before, context_after=context_after, log_dirs_override=log_dirs_list, log_content_patterns_override=log_content_patterns_list, ) try: results = await asyncio.to_thread(analysis_engine.search_logs, filter_criteria) logger.info("search_log_last_n_records returning %s records.", len(results)) return results except Exception as e: # pylint: disable=broad-exception-caught logger.error("Error in search_log_last_n_records: %s", e, exc_info=True) custom_message = f"Failed to search last N logs: {e!s}" raise McpError(ErrorData(code=-32603, message=custom_message)) from e - Pydantic BaseModel defining the input schema for the search_log_last_n_records tool, inheriting common fields from BaseSearchInput and adding the required 'count' parameter.
class SearchLogLastNInput(BaseSearchInput): """Input for search_log_last_n_records.""" count: int = Field(description="Number of last (newest) matching records to return.", gt=0) - src/log_analyzer_mcp/log_analyzer_mcp_server.py:756-756 (registration)The @mcp.tool() decorator registers the search_log_last_n_records function as an MCP tool in the FastMCP server.
@mcp.tool()