MCP Logging NL Query Server

MCP Logging NL Query Server

Allows developers and AI Agents to query Google Cloud Logging using natural language, translating queries into Google Cloud Logging Query Language (LQL) with Vertex AI Gemini 2.5.

Category
访问服务器

README

MCP Logging NL Query Server

This project provides a Model Context Protocol (MCP) server that allows developers and AI Agents to query Google Cloud Logging using natural language. The server uses Vertex AI Gemini 2.5 to translate natural language queries into Google Cloud Logging Query Language (LQL), then queries Cloud Logging and returns the results.

Features

  • Natural language to LQL translation using Vertex AI Gemini 2.5
  • Flexible log querying: filter on monitored resource, log name, severity, time, and more
  • REST API for easy integration
  • Ready for deployment on Google Cloud Run or GKE

API Usage

Endpoints

1. Natural Language Query

POST /logs/nl_query

Request:

{
  "query": "Show me all error logs from yesterday for my Cloud Run service 'my-service'",
  "max_results": 20
}

Response:

{
  "lql": "resource.type = \"cloud_run_revision\" AND resource.labels.service_name = \"my-service\" AND severity = ERROR AND timestamp >= \"2025-04-17T00:00:00Z\" AND timestamp < \"2025-04-18T00:00:00Z\"",
  "entries": [ ... log entries ... ]
}

2. LQL Filter Query

POST /logs/query

Request:

{
  "filter": "resource.type=\"cloud_run_revision\" AND severity=ERROR",
  "max_results": 20
}

Response:

{
  "lql": "resource.type=\"cloud_run_revision\" AND severity=ERROR",
  "entries": [ ... log entries ... ]
}

OpenAPI & Tooling

  • OpenAPI/Swagger docs available at /docs and /openapi.json when running.
  • Both endpoints are also discoverable as MCP tools for agent frameworks (Smithery, Claude Desktop, etc).

Example curl commands

curl -X POST $MCP_BASE_URL/logs/nl_query -H 'Content-Type: application/json' -d '{"query": "Show error logs for my Cloud Run service", "max_results": 2}'

curl -X POST $MCP_BASE_URL/logs/query -H 'Content-Type: application/json' -d '{"filter": "resource.type=\"cloud_run_revision\" AND severity=ERROR", "max_results": 2}'

Tests

  • Example test script: test_main.py (see repo)

.gitignore

  • Standard Python ignores included (see repo)

Deployment

Running on Google Cloud Run

You can deploy this server to Google Cloud Run for a fully managed, scalable solution.

Steps:

  1. Build the Docker image:

    gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/mcp-logging-server
    
  2. Deploy to Cloud Run:

    gcloud run deploy mcp-logging-server \
      --image gcr.io/YOUR_PROJECT_ID/mcp-logging-server \
      --platform managed \
      --region YOUR_REGION \
      --allow-unauthenticated \
      --port 8080
    

    Replace YOUR_PROJECT_ID and YOUR_REGION with your actual GCP project ID and region (e.g., us-central1).

  3. Set Environment Variables:

    • In the Cloud Run deployment UI or with the --set-env-vars flag, provide:
      • VERTEX_PROJECT=your-gcp-project-id
      • VERTEX_LOCATION=us-central1 (or your region)
    • Credentials:
      • Prefer using the Cloud Run service account with the right IAM roles (Logging Viewer, Vertex AI User).
      • You usually do NOT need to set GOOGLE_APPLICATION_CREDENTIALS on Cloud Run unless using a non-default service account key.
  4. IAM Permissions:

    • Ensure the Cloud Run service account has:
      • roles/logging.viewer
      • roles/aiplatform.user
  5. Accessing the Service:

    • After deployment, Cloud Run will provide a service URL (e.g., https://mcp-logging-server-xxxxxx.a.run.app).
    • Use this as your $MCP_BASE_URL in API requests.

Google Cloud Authentication Setup

This project requires Google Cloud Application Default Credentials (ADC) to access Logging and Vertex AI APIs.

Steps to Set Up Credentials:

  1. Create a Service Account:
  2. Create and Download a Key:
    • In the Service Account, click "Manage keys" → "Add key" → "Create new key" (choose JSON).
    • Download the JSON key file to your computer.
  3. Set the Environment Variable:
    • In your terminal, set the environment variable to the path of your downloaded key:
      export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-key.json"
      
    • Replace /path/to/your/service-account-key.json with the actual path.
  4. (Optional) Set Project and Location:
    • You may also need:
      export VERTEX_PROJECT=your-gcp-project-id
      export VERTEX_LOCATION=us-central1
      
  5. Verify Authentication:
    • Run a simple gcloud or Python client call to ensure authentication is working.
    • If you see DefaultCredentialsError, check your environment variable and file path.

Prerequisites

  • Python 3.9+
  • Google Cloud project with Logging and Vertex AI APIs enabled
  • Service account with permissions for Logging Viewer and Vertex AI User
  • Set environment variables:
    • VERTEX_PROJECT: Your GCP project ID
    • VERTEX_LOCATION: Vertex AI region (default: us-central1)
    • GOOGLE_APPLICATION_CREDENTIALS: Path to your service account JSON key file

Local Development

pip install -r requirements.txt
export VERTEX_PROJECT=your-project-id
export VERTEX_LOCATION=us-central1
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json
python main.py

Deploy to Cloud Run

gcloud builds submit --tag gcr.io/$VERTEX_PROJECT/mcp-logging-server
 gcloud run deploy mcp-logging-server \
    --image gcr.io/$VERTEX_PROJECT/mcp-logging-server \
    --platform managed \
    --region $VERTEX_LOCATION \
    --allow-unauthenticated

Example Natural Language Queries

  • Show all logs from Kubernetes clusters
  • Show error logs from Compute Engine and AWS EC2 instances
  • Find Admin Activity audit logs for project my-project
  • Find logs containing the word unicorn
  • Find logs with both unicorn and phoenix
  • Find logs where textPayload contains both unicorn and phoenix
  • Find logs where textPayload contains the phrase 'unicorn phoenix'
  • Show logs from yesterday for Cloud Run service 'my-service'
  • Show logs from the last 30 minutes
  • Show logs for logName containing request_log in GKE
  • Show logs where pod_name matches foo or bar using regex
  • Show logs for Compute Engine where severity is WARNING or higher
  • Show logs for Cloud SQL instances in us-central1
  • Show logs for Pub/Sub topics containing 'payments'
  • Show logs for log entries between two timestamps
  • Show logs where jsonPayload.message matches regex 'foo.*bar'
  • Show logs where labels.env is not prod

For more LQL examples, see the official documentation.

License

Apache 2.0

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选