MCP Connection: Loki API
What MCP Is
MCP (Model Context Protocol) is a standard protocol that lets AI agents connect to external tools and services. When you connect Claude Code to the Loki API via MCP, AI can query your logs directly instead of you copying LogQL results into the conversation. Same protocol works regardless of which AI coding agent you use.
Configuration Steps
Add the Loki MCP server to your Claude Code configuration. Open your Claude Code settings and add this MCP server entry:
{
"mcpServers": {
"loki": {
"command": "npx",
"args": ["-y", "@anthropic/mcp-loki-server"],
"env": {
"LOKI_URL": "http://localhost:3100",
"LOKI_TIMEOUT": "30000"
}
}
}
}
This connects Claude Code to the Loki instance running in your Docker environment. The timeout is set to 30 seconds to prevent expensive queries from hanging indefinitely.
Verification
After configuring the MCP connection, verify it works:
- Start a new Claude Code session
- Ask Claude to query Loki for recent logs: "Show me the last 10 log entries from the tourism-portal service"
- Claude should execute a LogQL query via MCP and return actual log data
If the connection works, you should see log entries with timestamps, service labels, and request content -- data Claude read directly from Loki, not from your description.
Scoping Notes
- Read-only access: The MCP connection to Loki is read-only. AI can query logs but cannot modify them, create alerts, or change the Loki configuration.
- Query timeout: The 30-second timeout prevents AI from running expensive queries that scan weeks of log data when you only need the last hour. If AI's query times out, the first question is whether the query was too broad.
- When to check AI's queries: Watch for queries that scan all time ranges (AI may default to scanning everything). Watch for queries that return "no results" -- this may mean AI constructed the wrong query, not that the data doesn't exist. Check whether Alloy is still collecting from the target container before trusting a "no results" answer.