npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

grafana-loki-mcp

v1.0.1

Published

MCP service for querying Loki logs across multiple environments

Readme

MCP 日志查询服务

一个基于 MCP 的日志查询服务,用于通过统一工具接口管理环境配置、执行日志检索以及获取统计结果。

功能概览

  • 支持读取多个环境配置
  • 支持列出当前可用环境
  • 支持按时间范围执行日志查询
  • 支持获取查询统计信息
  • 支持通过 MCP 客户端接入

安装

npm install
npm run build

配置

服务启动时会自动查找配置文件,支持的文件名包括:

  • loki-mcp.config.json
  • .loki-mcp.config.json
  • loki-mcp.json

也可以通过启动参数显式指定配置文件:

node dist/index.js --config=./path/to/config.json

还可以通过环境变量显式指定配置文件路径:

LOKI_MCP_CONFIG_PATH=./path/to/config.json node dist/index.js

优先级从高到低为:

  1. LOKI_MCP_CONFIG_PATH
  2. --config=
  3. 自动查找默认文件名

如果 LOKI_MCP_CONFIG_PATH 指向的文件不存在,服务会直接报错并退出,不会回退到其他配置来源。

配置文件示例:

{
  "default": "default",
  "envs": {
    "default": {
      "url": "https://your-log-service.example.com",
      "lokiDatasourceUid": "your-datasource-uid",
      "lokiDatasourceId": 1,
      "user": "optional-username",
      "password": "optional-password"
    }
  }
}

配置项说明

| 配置项 | 必填 | 说明 | |--------|------|------| | default | 否 | 默认使用的环境名称 | | envs | 是 | 环境配置集合,键为环境名 | | url | 是 | 日志服务访问地址 | | lokiDatasourceUid | 是 | 数据源唯一标识 | | lokiDatasourceId | 否 | 数据源 ID | | user | 否 | 认证用户名 | | password | 否 | 认证密码 |

除配置文件外,也可以通过环境变量提供环境信息。命名格式如下:

  • LOKI_<ENV_NAME>_URL
  • LOKI_<ENV_NAME>_USER
  • LOKI_<ENV_NAME>_PASSWORD
  • LOKI_DEFAULT

运行

# 开发模式
npm run dev

# 构建后运行
npm start

可用工具

list_envs

列出当前已配置的环境。

调用示例:

{
  "name": "list_envs",
  "arguments": {}
}

query_logs

按环境、查询语句和时间范围检索日志。

调用示例:

{
  "name": "query_logs",
  "arguments": {
    "env": "default",
    "query": "{app=\"your-app\"} |= \"error\"",
    "start": "2024-01-01T00:00:00Z",
    "end": "2024-01-01T23:59:59Z",
    "limit": 100
  }
}

query_stats

获取指定查询条件在时间范围内的统计信息。

调用示例:

{
  "name": "query_stats",
  "arguments": {
    "env": "default",
    "query": "{app=\"your-app\"}",
    "start": "2024-01-01T00:00:00Z",
    "end": "2024-01-01T23:59:59Z"
  }
}

get_current_time

返回当前时间信息。

调用示例:

{
  "name": "get_current_time",
  "arguments": {}
}

查询示例

{app="your-app"}
{service="your-service"} |= "error"
{filename="/path/to/log.file"}

技术说明

  • 服务通过 MCP 标准输入输出传输协议对外提供工具能力
  • 支持从配置文件和环境变量加载环境定义
  • 查询结果以结构化数据返回,便于客户端进一步处理

故障排查

未找到可用环境

确认配置文件存在且格式正确,或检查环境变量是否已按约定命名。

认证失败

确认访问地址、用户名和密码配置正确,并且目标服务允许当前凭据访问。

查询失败

确认查询语句、时间范围和目标环境名称有效,并检查目标日志服务是否可访问。