spanlog

v0.1.0
io.github.chaugiakhang193/spanlog-mcp Checked 3 days ago Published 20 days ago

Find the lines of one request, the newest failures and the slowest calls in spanlog JSON logs

@spanlog/mcp serves spanlog log files to an AI assistant over the Model Context Protocol (MCP). The assistant asks for every line of one request, the newest failures or the slowest lines, and gets those lines back instead of the whole file. This is what the failures tool returns for a file with two orders, one slow and one failed. The server reads the files named at startup and never writes to them. No tool takes a file path. The server talks MCP over stdin and stdout. Started by hand, it waits for a client.

Summary extracted from the README.

spanlog MCP server

@spanlog/mcp serves spanlog log files to an AI assistant over the Model Context Protocol (MCP). The assistant asks for every line of one request, the newest failures or the slowest lines, and gets those lines back instead of the whole file.

2026-10-24T14:15:04.304Z ERROR [shop] req=b81e44 OrderService.handleOrder - call InventoryService.reserve | FAILED 3ms Error: stock not available
    at InventoryService.reserve (file:///app/src/inventory.service.js:12:11)
    at async OrderService.handleOrder (file:///app/src/order.service.js:20:5)
2026-10-24T14:15:04.305Z WARN  [shop] req=b81e44 POST /orders | FAILED 500 5ms
(1 failure, 0 malformed lines skipped)

This is what the failures tool returns for a file with two orders, one slow and one failed. The server reads the files named at startup and never writes to them. No tool takes a file path.

Requirements

  • Node.js 22.13.0 or later, with npx.
  • Log files written by the built-in JSON writer of spanlog: jsonSink in Node.js, JSONSink in Go, json_sink in Python. The shape of a line is spec/log-line.schema.json.

Run

npx -y @spanlog/mcp --file /var/log/shop/app.log
  • --file can be given more than once.
  • Use absolute paths. The client picks the working directory of the server.
  • Every tool call reads the files again, so lines written after startup are found.
  • A missing file makes the tool call return an error that names the path. The server keeps running.
  • npx -y @spanlog/mcp --version prints the version and exits.

The server talks MCP over stdin and stdout. Started by hand, it waits for a client.

Client setup

Every client below starts the server with the same command. Replace /var/log/shop/app.log with the absolute path of a log file. On Windows, write it as C:/logs/shop/app.log, or as C:\\logs\\shop\\app.log inside JSON and TOML strings.

Codex

codex mcp add spanlog -- npx -y @spanlog/mcp --file /var/log/shop/app.log

The command writes this table to ~/.codex/config.toml. A trusted project can hold it in .codex/config.toml.

[mcp_servers.spanlog]
command = "npx"
args = ["-y", "@spanlog/mcp", "--file", "/var/log/shop/app.log"]

The first start downloads the package. Codex waits 10 seconds for a server by default. Add startup_timeout_sec = 30 to the table when the download takes longer.

Claude Code

claude mcp add spanlog -- npx -y @spanlog/mcp --file /var/log/shop/app.log

Without --scope, the server is added for the current project only, in ~/.claude.json. --scope user adds it for every project. --scope project writes it to .mcp.json at the project root, the file to share through version control. Servers in ~/.claude.json and .mcp.json also run in the Code tab of the desktop app, and the VS Code extension uses the same configuration as the CLI.

Claude Desktop

Open Settings, then Developer, then Edit Config. The file is claude_desktop_config.json, in %APPDATA%\Claude on Windows and in ~/Library/Application Support/Claude on macOS.

{
  "mcpServers": {
    "spanlog": {
      "command": "npx",
      "args": ["-y", "@spanlog/mcp", "--file", "C:\\logs\\shop\\app.log"]
    }
  }
}

Quit the app completely and open it again after saving. The app also loads this server into local sessions of the Code tab. The output of the server on stderr goes to mcp-server-spanlog.log, in %APPDATA%\Claude\logs on Windows and in ~/Library/Logs/Claude on macOS.

VS Code

Put .mcp.json at the root of the workspace:

{
  "mcpServers": {
    "spanlog": {
      "type": "stdio",
      "command": "npx",
      "args": ["-y", "@spanlog/mcp", "--file", "/var/log/shop/app.log"]
    }
  }
}

Claude Code reads the same file as its project scope.

OpenCode

opencode mcp add spanlog -- npx -y @spanlog/mcp --file /var/log/shop/app.log

The command writes to the project configuration. --global writes to the global one instead. Written by hand in opencode.jsonc:

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "servers": {
      "spanlog": {
        "type": "local",
        "command": ["npx", "-y", "@spanlog/mcp", "--file", "/var/log/shop/app.log"]
      }
    }
  }
}

Tools

The three tools only read. A result has one line per log line, {time} {LEVEL} [{service}] req={reqId} {msg}. The time stays in UTC as the JSON writer wrote it, and a line outside any request shows req=-. The last line counts the entries and the malformed lines skipped. A line that is not a JSON writer line, such as a startup message, counts as malformed.

The examples read a file with six lines of two orders of the shop service. Order 7f3a9c ends SLOW after 1192 ms and order b81e44 fails with status 500.

find_request

Every line of one request in file order, with the frames of each error stack.

Parameter Type Default Meaning
reqId string, required The req= value of a line
service string every service Only lines of this service

{ "reqId": "b81e44" } returns:

2026-10-24T14:15:04.300Z INFO  [shop] req=b81e44 POST /orders | START
2026-10-24T14:15:04.304Z ERROR [shop] req=b81e44 OrderService.handleOrder - call InventoryService.reserve | FAILED 3ms Error: stock not available
    at InventoryService.reserve (file:///app/src/inventory.service.js:12:11)
    at async OrderService.handleOrder (file:///app/src/order.service.js:20:5)
2026-10-24T14:15:04.304Z INFO  [shop] req=b81e44 OrderService.handleOrder - run | FAILED 4ms Error: stock not available
2026-10-24T14:15:04.305Z WARN  [shop] req=b81e44 POST /orders | FAILED 500 5ms
(4 lines, 0 malformed lines skipped)

The count is of log lines, without the stack frames.

failures

The newest failures, newest first, separated by an empty line. A failed request shows up to three of its failed lines that carry an error stack, with the frames, then its request line. When none of its failed lines carries a stack, its first failed line takes their place. A failed line with a stack outside any request, such as a line of a job, shows alone.

Parameter Type Default Meaning
sinceMinutes integer, 1 or more the whole files Only the last minutes before the newest line in the files
service string every service Only failures of this service
limit integer, 1 to 100 20 Most failures to return

{} returns the result at the top of this page. The outer run | FAILED line of b81e44 carries no stack and stays out. A request with more than three failed lines that carry a stack gets a line such as (2 more failed lines with a stack. find_request with reqId e7c2a0 shows them.) under its third one.

With sinceMinutes, the result opens with its window: Window: {since} to {newest}, the {sinceMinutes} minutes up to the newest line. The window ends at the newest line of all files, not at the current time, so a log file written days ago can still be searched.

slowest

The lines with the longest duration, longest first, without stacks. Every line except START has a duration, failed and aborted lines included.

Parameter Type Default Meaning
limit integer, 1 to 100 10 Most lines to return
sinceMinutes integer, 1 or more the whole files Only the last minutes before the newest line in the files
service string every service Only lines of this service
action request, run, call or query every action Only lines of this action

{ "limit": 1 } returns:

2026-10-24T14:15:04.310Z WARN  [shop] req=7f3a9c POST /orders | SLOW 201 1192ms
(1 of 4 lines, 0 malformed lines skipped)

1 of 4 means the limit left out three lines.

Limits

  • Only lines of the built-in JSON writer are read. Lines written through pino, slog or logging encode the time, the level and the message in their own way and count as malformed.
  • find_request lists lines in file order, file by file in the order of --file. Lines of several files are not merged by time.
  • A failures block holds the lines of one service. A request that crosses services gets one block per service that failed. find_request without service shows all of its lines.
  • Two requests with the same service and reqId that run at the same time mix their failed lines in failures.
  • A request that never ends, for example when its process was killed, keeps up to four lines in memory until the end of the read.
  • Every call reads the files from the start, twice with sinceMinutes. A large file makes every call slower.
  • An error message is written as it is and can repeat a value, such as a SQL parameter. The assistant sees what the log holds.

Changes

Each release is listed in CHANGELOG.md. The version of @spanlog/mcp is separate from the version of the spanlog packages.

License

MIT