这是一个简单的模型上下文协议(MCP)服务器,用于通过 NRQL 查询 New Relic 日志。此服务器使大型语言模型(如 Claude)能够与您的 New Relic 数据进行交互。
uv 包管理器:# 在 macOS/Linux 上
curl -LsSf https://astral.sh/uv/install.sh | sh
# 在 Windows (PowerShell) 上
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# 创建目录
mkdir newrelic-mcp
cd newrelic-mcp
# 创建虚拟环境
uv venv
# 激活虚拟环境
source .venv/bin/activate # 在 Unix/macOS 上
.venv\Scripts\activate # 在 Windows 上
# 安装依赖项
uv pip install "mcp[cli]" httpx
创建服务器文件 newrelic_logs_server.py 并使用提供的代码。
配置您的环境变量:
# 在 Unix/macOS 上
export NEW_RELIC_API_KEY="your-api-key-here"
export NEW_RELIC_ACCOUNT_ID="your-account-id-here"
# 在 Windows (CMD) 上
set NEW_RELIC_API_KEY=your-api-key-here
set NEW_RELIC_ACCOUNT_ID=your-account-id-here
# 在 Windows (PowerShell) 上
$env:NEW_RELIC_API_KEY = "your-api-key-here"
$env:NEW_RELIC_ACCOUNT_ID = "your-account-id-here"
通过编辑配置文件来配置 Claude Desktop:
~/Library/Application Support/Claude/claude_desktop_config.json%APPDATA%\Claude\claude_desktop_config.json添加以下配置:
{
"mcpServers": {
"newrelic": {
"command": "uv",
"args": [
"--directory",
"/absolute/path/to/newrelic-mcp",
"run",
"newrelic_logs_server.py"
],
"env": {
"NEW_RELIC_API_KEY": "your-api-key-here",
"NEW_RELIC_ACCOUNT_ID": "your-account-id-here"
}
}
}
}
SELECT * FROM Transaction SINCE 1 小时前
SELECT * FROM Transaction WHERE error IS TRUE SINCE 1 小时前 LIMIT 10
SELECT average(duration) FROM Transaction FACET name ORDER BY average(duration) DESC LIMIT 5
您可以向 Claude 提问如下问题:
# 在 macOS/Linux 上
tail -f ~/Library/Logs/Claude/mcp-server-newrelic.log
# 在 Windows 上
type %APPDATA%\Claude\logs\mcp-server-newrelic.log
使用以下命令测试您的服务器功能:
npx @modelcontextprotocol/inspector uv run newrelic_logs_server.py
export NEW_RELIC_API_KEY="your-api-key-here"
export NEW_RELIC_ACCOUNT_ID="your-account-id-here"
uv run newrelic_logs_server.py
服务器实现:
该项目采用 MIT 许可证。
如果您遇到问题: