Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Full documentation lives at **[valkey-admin.valkey.io](https://valkey-admin.valk
- **[Cluster Topology](https://valkey-admin.valkey.io/features/cluster-topology/)** — Visual map of shards, primaries, and replicas with per-node metrics.
- **[Activity](https://valkey-admin.valkey.io/features/activity/)** — Hot Keys monitoring plus Command Logs (slow commands, large requests, large replies) aggregated across the cluster.
- **[OpenTelemetry Export](https://valkey-admin.valkey.io/features/otel-export/)** — Push memory, keyspace and CPU metrics over OTLP to Prometheus and other backends. Turn it on with `METRICS_OTEL_ENABLED=true`.
- **[AI Assistants (MCP)](https://valkey-admin.valkey.io/features/mcp/)** — Read-only MCP server that lets assistants such as Claude Code and Cursor read server info, big keys, and hot keys.
- **[AI Assistants (MCP)](https://valkey-admin.valkey.io/features/mcp/)** — Read-only MCP server that lets assistants such as Claude Code and Cursor read server info, big keys, hot keys, and command logs.

## Compatibility

Expand Down
33 changes: 3 additions & 30 deletions apps/frontend/src/components/activity-view/command-log-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,14 @@ import { Typography } from "../ui/typography"
import { CustomTooltip } from "../ui/tooltip"
import { NodeErrorsBanner } from "./hotkeys/hot-keys-banners"
import { NodeFilterDropdown } from "./hotkeys/node-filter-dropdown"
import type { CommandLogRow, CommandLogType } from "@common/src/commandlogs"

type SortOrder = typeof SORT_ORDER.ASC | typeof SORT_ORDER.DESC
type SortField = typeof SORT_FIELD.TIMESTAMP | typeof SORT_FIELD.METRIC

interface SlowLogEntry {
id: string
ts: number
duration_us: number
argv: string[]
addr: string
client: string
}

interface LargeLogEntry {
id: string
ts: number
size: number
argv: string[]
addr: string
client: string
}

interface LogGroup {
ts: number
metric: string
values: (SlowLogEntry | LargeLogEntry)[]
nodeId: string,
}

type LogType = "slow" | "large-request" | "large-reply"

interface CommandLogTableProps {
data: LogGroup[] | null
logType: LogType
data: CommandLogRow[] | null
logType: CommandLogType
nodeErrors?: { nodeId: string; error: string }[]
isCluster?: boolean
}
Expand Down Expand Up @@ -99,7 +73,6 @@ export function CommandLogTable({ data, logType, nodeErrors, isCluster }: Comman
logGroup.values.map((entry) => ({
...entry,
groupTs: logGroup.ts,
nodeId: entry.nodeId ?? (logGroup).nodeId,
})),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("commandLogsSlice", () => {
it("keys the standalone pending entry by the db-less nodeId", () => {
const state = commandLogsReducer(
initialState,
commandLogsRequested({ connectionId: "host-6379-db0" }),
commandLogsRequested({ connectionId: "host-6379-db0", commandLogType: COMMANDLOG_TYPE.SLOW }),
)

expect(state["host-6379"]).toBeDefined()
Expand All @@ -24,11 +24,11 @@ describe("commandLogsSlice", () => {
it("collapses two connections to the same node on different dbs into one entry", () => {
const afterDb0 = commandLogsReducer(
initialState,
commandLogsRequested({ connectionId: "host-6379-db0" }),
commandLogsRequested({ connectionId: "host-6379-db0", commandLogType: COMMANDLOG_TYPE.SLOW }),
)
const afterDb1 = commandLogsReducer(
afterDb0,
commandLogsRequested({ connectionId: "host-6379-db1" }),
commandLogsRequested({ connectionId: "host-6379-db1", commandLogType: COMMANDLOG_TYPE.SLOW }),
)

expect(Object.keys(afterDb1)).toEqual(["host-6379"])
Expand All @@ -37,7 +37,7 @@ describe("commandLogsSlice", () => {
it("keys the cluster pending entry by clusterId", () => {
const state = commandLogsReducer(
initialState,
commandLogsRequested({ connectionId: "node-1-db0", clusterId: "cluster-1" }),
commandLogsRequested({ connectionId: "node-1-db0", clusterId: "cluster-1", commandLogType: COMMANDLOG_TYPE.SLOW }),
)

expect(state["cluster-1"]).toBeDefined()
Expand Down Expand Up @@ -83,28 +83,28 @@ describe("commandLogsSlice", () => {
it("keys a standalone error by nodeId", () => {
const seeded = commandLogsReducer(
initialState,
commandLogsRequested({ connectionId: "host-6379-db0" }),
commandLogsRequested({ connectionId: "host-6379-db0", commandLogType: COMMANDLOG_TYPE.SLOW }),
)
const state = commandLogsReducer(
seeded,
commandLogsError({ nodeId: "host-6379", error: { message: "boom" } }),
commandLogsError({ nodeId: "host-6379", error: "boom" }),
)

expect(state["host-6379"].error).toEqual({ message: "boom" })
expect(state["host-6379"].error).toBe("boom")
expect(state["host-6379"].loading).toBe(false)
})

it("keys a cluster error by clusterId", () => {
const seeded = commandLogsReducer(
initialState,
commandLogsRequested({ connectionId: "node-1-db0", clusterId: "cluster-1" }),
commandLogsRequested({ connectionId: "node-1-db0", clusterId: "cluster-1", commandLogType: COMMANDLOG_TYPE.SLOW }),
)
const state = commandLogsReducer(
seeded,
commandLogsError({ clusterId: "cluster-1", error: { message: "boom" } }),
commandLogsError({ nodeId: "node-1", clusterId: "cluster-1", error: "boom" }),
)

expect(state["cluster-1"].error).toEqual({ message: "boom" })
expect(state["cluster-1"].error).toBe("boom")
})
})
})
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { createSlice } from "@reduxjs/toolkit"
import { type JSONObject } from "@common/src/json-utils"
import { createSlice, type PayloadAction } from "@reduxjs/toolkit"
import { VALKEY } from "@common/src/constants.ts"
import { toNodeId } from "@common/src/connection-id.ts"
import * as R from "ramda"
import { COMMANDLOG_TYPE } from "@common/src/constants.ts"
import type {
CommandLogRow,
CommandLogsErrorPayload,
CommandLogsFulfilledPayload,
CommandLogsRequestPayload,
CommandLogType
} from "@common/src/commandlogs.ts"
import type { NodeError } from "@common/src/node-results.ts"
import type { RootState } from "@/store.ts"

type CommandLogType = typeof COMMANDLOG_TYPE.SLOW | typeof COMMANDLOG_TYPE.LARGE_REQUEST | typeof COMMANDLOG_TYPE.LARGE_REPLY

// `targetId` is the state key: `clusterId` for a cluster aggregate or the
// db-less `nodeId` for a standalone node.
export const selectCommandLogs =
Expand All @@ -21,44 +25,12 @@ export const selectCommandLogsNodeErrors =
(state: RootState) =>
R.path([VALKEY.COMMANDLOGS.name, targetId, "nodeErrors"], state) ?? []

interface CommandLogSlowEntry {
id: string
ts: number
duration_us: number
argv: string[]
addr: string
client: string
}

interface CommandLogLargeEntry {
id: string
ts: number
size: number
argv: string[]
addr: string
client: string
}

interface CommandLogEntry {
ts: number,
metric: string,
values: CommandLogLargeEntry[]
}

interface CommandLogState {
// Keyed by `targetId`: `clusterId` (cluster) or `nodeId` (standalone).
[targetId: string]: {
logs: {
slow: Array<{
ts: number
metric: string
values: CommandLogSlowEntry[]
}>
[COMMANDLOG_TYPE.LARGE_REPLY]: CommandLogEntry[],
[COMMANDLOG_TYPE.LARGE_REQUEST]: CommandLogEntry[],
}
logs: Record<CommandLogType, CommandLogRow[]>
count: number
error?: JSONObject | null
error?: string | null
loading?: boolean
nodeErrors?: NodeError[]
}
Expand All @@ -70,7 +42,7 @@ const commandLogsSlice = createSlice({
name: "commandLogs",
initialState: initialCommandLogsState,
reducers: {
commandLogsRequested: (state, action) => {
commandLogsRequested: (state, action: PayloadAction<CommandLogsRequestPayload>) => {
const { connectionId, clusterId } = action.payload
// Standalone command logs state is node-level.
const targetId = clusterId ?? toNodeId(connectionId)
Expand All @@ -87,10 +59,9 @@ const commandLogsSlice = createSlice({
}
state[targetId].loading = true
},
commandLogsFulfilled: (state, action) => {
const { parsedResponse, nodeErrors } = action.payload
const targetId = action.payload.clusterId ?? action.payload.nodeId
const commandLogType: CommandLogType = action.payload.commandLogType
commandLogsFulfilled: (state, action: PayloadAction<CommandLogsFulfilledPayload>) => {
const { parsedResponse, nodeErrors, commandLogType } = action.payload
const targetId = "clusterId" in action.payload ? action.payload.clusterId : action.payload.nodeId
const { rows, count } = parsedResponse
if (!state[targetId]) {
state[targetId] = {
Expand All @@ -108,7 +79,7 @@ const commandLogsSlice = createSlice({
state[targetId].loading = false
state[targetId].nodeErrors = nodeErrors ?? []
},
commandLogsError: (state, action) => {
commandLogsError: (state, action: PayloadAction<CommandLogsErrorPayload>) => {
const { error } = action.payload
const targetId = action.payload.clusterId ?? action.payload.nodeId
if (state[targetId]) {
Expand Down
6 changes: 5 additions & 1 deletion apps/mcp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ The `VALKEY_*` connection variables apply only in header mode, to requests that
| `get_server_info` | INFO and memory data | None | Yes | Connected node only |
| `get_big_keys` | Largest keys by memory | `topN` (default 20), `scanLimit` per node (default 10000) | Yes | All nodes |
| `get_hot_keys` | Most frequently accessed keys | `count` (default 20) | No | All nodes |
| `get_command_logs` | Recent slow commands, large requests or large replies, largest first | `type` (`slow`, `large-request` or `large-reply`, default `slow`), `count` (default 20, max 100) | Yes | All nodes |

`get_hot_keys` requires cluster mode, `cluster-slot-stats-enabled yes` and an LFU `maxmemory-policy` such as `allkeys-lfu`.

`get_command_logs` reads `COMMANDLOG`, which needs Valkey 8.1 or later. Each node keeps its latest entries per log, so results show recent activity, not a time range. Results include the command name and argument count only, never the arguments, so key names and values stay out.

`get_big_keys` adds load to each node it scans. `scanLimit` applies per node, so a three-node cluster with the default limit can scan up to about 30,000 keys.

## Add it to your AI assistant
Expand Down Expand Up @@ -178,7 +181,7 @@ The rule starts with `reset`, so it always produces the same read-only user, eve
| `@read`, `@connection` | Big keys (`SCAN`, `TYPE`, `TTL`, `MEMORY USAGE`, `OBJECT FREQ`), connecting |
| `INFO`, `MEMORY STATS` | Server info and the dashboard data |
| `CLUSTER SLOTS`, `CLUSTER SLOT-STATS` | Finding cluster nodes, hot keys |
| `COMMANDLOG GET`, `COMMAND INFO` | The metrics collector |
| `COMMANDLOG GET`, `COMMAND INFO` | The metrics collector, command logs |

Writes, `FLUSHALL`, `CONFIG GET`, `CONFIG SET`, `KEYS`, `MONITOR`, `CLIENT KILL` and the other admin commands stay blocked.

Expand Down Expand Up @@ -209,6 +212,7 @@ If a cluster result is correct, `nodeId` shows keys from more than one node and
| `Cannot reach Valkey Admin at ...` | Start the Valkey Admin server and check `VALKEY_ADMIN_URL`. If it is running, add the MCP origin to its `VALKEY_ADMIN_ALLOWED_WS_ORIGINS`, because a rejected origin shows this same error |
| `No reply from Valkey Admin for ...` | The request passed the 10-second limit. Lower `scanLimit` and try again |
| `Hot keys use cluster slot stats, which need ...` | Apply the settings listed in the error |
| `Command logs need Valkey 8.1 or later ...` | `get_command_logs` needs Valkey 8.1 or later. Upgrade the nodes, or use the other tools |
| `Metrics server not started` | Metrics are still starting. Tools retry for a few seconds, so try again shortly |
| `NOPERM ...` | The Valkey user lacks a command the tools use. See [Read-only Valkey user](#read-only-valkey-user) |

Expand Down
1 change: 1 addition & 0 deletions apps/mcp/src/admin-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const READ_ONLY_ACTIONS = new Set<string>([
VALKEY.STATS.setData,
VALKEY.BIGKEYS.bigKeysRequested,
VALKEY.HOTKEYS.hotKeysRequested,
VALKEY.COMMANDLOGS.commandLogsRequested,
])

const CONNECT_REPLIES = [
Expand Down
2 changes: 2 additions & 0 deletions apps/mcp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
loadConfig
} from "./config"
import { registerBigKeys } from "./tools/big-keys"
import { registerCommandLogs } from "./tools/command-logs"
import { registerHotKeys } from "./tools/hot-keys"
import { registerServerInfo } from "./tools/server-info"

Expand All @@ -33,6 +34,7 @@ const main = () => {
registerServerInfo(server, admin)
registerBigKeys(server, admin)
registerHotKeys(server, admin)
registerCommandLogs(server, admin)
return server
}, { onerror })
const handle = toNodeHandler(mcp, { onerror })
Expand Down
Loading
Loading