Loki: Get Log Context
loki_get_log_contextRetrieves the log lines surrounding a specific Loki entry, showing what a stream logged just before and after it. Use it to inspect the context around an error or event without filtering out nearby lines.
Instructions
Show the lines around one log line — what the same stream logged just before and after it, unfiltered — the way Grafana's 'show context' does. Pass the stream selector (built from the line's common_labels plus its own labels, e.g. {namespace="payments", pod="api-7d9f-x2k4"}) and the line's exact ts. Lines come back oldest first, with the anchor line marked anchor: true.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"selector": "<selector>",
"time": "<time>"
},
"name": "loki_get_log_context"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| time | Yes | The anchor line's `ts` exactly as returned (e.g. 2026-11-03T22:13:37.968458546Z), or its nanosecond timestamp. Any other time form is accepted too. | |
| after | No | Lines to show after the anchor (0-500). Defaults to 10. | |
| before | No | Lines to show before the anchor (0-500). Defaults to 10. | |
| window | No | How far to look on each side for those lines, as a duration (5m, 1h). Defaults to 1h; a quiet stream may need more. | 1h |
| selector | Yes | Stream selector for the line's stream, e.g. `{namespace="payments", pod="api-7d9f-x2k4", container="api"}`. Matchers only; the narrower, the more the context is that one process's. | |
| hide_labels | No | Stream labels to leave out of the answer. Defaults to ["filename"], which on Kubernetes is a 120-character /var/log/pods/... path repeating namespace, pod and container. Pass [] to see every label. | |
| parsed_fields | No | Include the labels extracted by | json, | logfmt, | regexp or | pattern on each line, under `fields`. Off by default because they restate the line itself and double its size; turn it on after line_format or keep has reshaped the line, or to read a nested JSON value flattened. Structured metadata (trace_id, detected_level) is always included. | |
| max_line_chars | No | Cut each line at this many characters (80-100000); a cut line ends with `… [+N chars]`. Defaults to 2000. Some services log single lines of hundreds of KB. |