# CLAUDE.md - Project Context This file provides context for Claude when working on this project. ## Project Overview **LabMCP** is a collection of Model Context Protocol (MCP) servers written in Go, designed to extend Claude's capabilities with custom tools. The repository is structured to be generic and extensible, allowing multiple MCP servers to be added over time. ## MCP Servers ### Nixpkgs Search (`nixpkgs-search`) - **Primary** Combined search for NixOS options and Nix packages from nixpkgs. Provides two separate MCP servers: - **Options server**: Search NixOS configuration options (`nixpkgs-search options serve`) - **Packages server**: Search Nix packages (`nixpkgs-search packages serve`) ### NixOS Options (`nixos-options`) - Legacy Search and query NixOS configuration options. Uses nixpkgs as source. *Note: Prefer using `nixpkgs-search options` instead.* ### Home Manager Options (`hm-options`) Search and query Home Manager configuration options. Uses home-manager repository as source. ### Lab Monitoring (`lab-monitoring`) Query Prometheus metrics, Alertmanager alerts, and Loki logs. Unlike other servers, this queries live HTTP APIs — no database or indexing needed. - 8 core tools: list/get alerts, search metrics, get metadata, PromQL query, list targets, list/create silences - 3 optional Loki tools (when `LOKI_URL` is set): query_logs, list_labels, list_label_values - Configurable Prometheus, Alertmanager, and Loki URLs via flags or environment variables - Optional basic auth for Loki (`LOKI_USERNAME`/`LOKI_PASSWORD`) ### Git Explorer (`git-explorer`) Read-only access to git repository information. Designed for deployment verification. - 9 tools: resolve_ref, get_log, get_commit_info, get_diff_files, get_file_at_commit, is_ancestor, commits_between, list_branches, search_commits - Uses go-git library for pure Go implementation - All operations are read-only (never modifies repository) The nixpkgs/options/hm servers share a database-backed architecture: - Full-text search across option/package names and descriptions - Query specific options/packages with full metadata - Index multiple revisions (by git hash or channel name) - Fetch module source files - PostgreSQL and SQLite backends ## Technology Stack - **Language**: Go 1.24+ - **Build System**: Nix flakes - **Databases**: PostgreSQL and SQLite (both fully supported) - **Protocol**: MCP (Model Context Protocol) - JSON-RPC over STDIO or HTTP/SSE - **Module Path**: `git.t-juice.club/torjus/labmcp` ## Project Status **Complete and maintained** - All core features implemented: - Full MCP servers (6 tools each for nixpkgs/options, 8-11 tools for monitoring) - PostgreSQL and SQLite backends with FTS (for nixpkgs/options servers) - Live API queries for Prometheus/Alertmanager/Loki (monitoring server) - NixOS modules for deployment - CLI for manual operations - Comprehensive test suite ## Repository Structure ``` labmcp/ ├── cmd/ │ ├── nixpkgs-search/ │ │ └── main.go # Combined options+packages CLI (primary) │ ├── nixos-options/ │ │ └── main.go # NixOS options CLI (legacy) │ ├── hm-options/ │ │ └── main.go # Home Manager options CLI │ ├── lab-monitoring/ │ │ └── main.go # Prometheus/Alertmanager CLI │ └── git-explorer/ │ └── main.go # Git repository explorer CLI ├── internal/ │ ├── database/ │ │ ├── interface.go # Store interface (options + packages) │ │ ├── schema.go # Schema versioning │ │ ├── postgres.go # PostgreSQL implementation │ │ ├── sqlite.go # SQLite implementation │ │ └── *_test.go # Database tests │ ├── mcp/ │ │ ├── server.go # MCP server core + ServerConfig + modes │ │ ├── handlers.go # Tool implementations (options + packages) │ │ ├── types.go # Protocol types │ │ ├── transport.go # Transport interface │ │ ├── transport_stdio.go # STDIO transport │ │ ├── transport_http.go # HTTP/SSE transport │ │ ├── session.go # HTTP session management │ │ └── *_test.go # MCP tests │ ├── options/ │ │ └── indexer.go # Shared Indexer interface │ ├── nixos/ │ │ ├── indexer.go # NixOS options indexing │ │ ├── parser.go # options.json parsing │ │ ├── types.go # Channel aliases, extensions │ │ └── *_test.go # Indexer tests │ ├── homemanager/ │ │ ├── indexer.go # Home Manager indexing │ │ ├── types.go # Channel aliases, extensions │ │ └── *_test.go # Indexer tests │ ├── packages/ │ │ ├── indexer.go # Nix packages indexing │ │ ├── parser.go # nix-env JSON parsing │ │ ├── types.go # Package types, channel aliases │ │ └── *_test.go # Parser tests │ ├── monitoring/ │ │ ├── types.go # Prometheus/Alertmanager/Loki API types │ │ ├── prometheus.go # Prometheus HTTP client │ │ ├── alertmanager.go # Alertmanager HTTP client │ │ ├── loki.go # Loki HTTP client │ │ ├── handlers.go # MCP tool definitions + handlers │ │ ├── format.go # Markdown formatting utilities │ │ └── *_test.go # Tests (httptest-based) │ └── gitexplorer/ │ ├── client.go # go-git repository wrapper │ ├── types.go # Type definitions │ ├── handlers.go # MCP tool definitions + handlers │ ├── format.go # Markdown formatters │ ├── validation.go # Path validation │ └── *_test.go # Tests ├── nix/ │ ├── module.nix # NixOS module for nixos-options │ ├── hm-options-module.nix # NixOS module for hm-options │ ├── lab-monitoring-module.nix # NixOS module for lab-monitoring │ ├── git-explorer-module.nix # NixOS module for git-explorer │ └── package.nix # Parameterized Nix package ├── testdata/ │ └── options-sample.json # Test fixture ├── flake.nix ├── go.mod ├── .mcp.json # MCP client configuration ├── CLAUDE.md # This file ├── README.md └── TODO.md # Future improvements ``` ## MCP Tools ### Options Servers (nixpkgs-search options, nixos-options, hm-options) | Tool | Description | |------|-------------| | `search_options` | Full-text search across option names and descriptions | | `get_option` | Get full details for a specific option with children | | `get_file` | Fetch source file contents from indexed repository | | `index_revision` | Index a revision (options, files, and packages for nixpkgs) | | `list_revisions` | List all indexed revisions | | `delete_revision` | Delete an indexed revision | ### Packages Server (nixpkgs-search packages) | Tool | Description | |------|-------------| | `search_packages` | Full-text search across package names and descriptions | | `get_package` | Get full details for a specific package by attr path | | `get_file` | Fetch source file contents from nixpkgs | | `index_revision` | Index a revision to make its packages searchable | | `list_revisions` | List all indexed revisions | | `delete_revision` | Delete an indexed revision | ### Monitoring Server (lab-monitoring) | Tool | Description | |------|-------------| | `list_alerts` | List alerts with optional filters (state, severity, receiver) | | `get_alert` | Get full details for a specific alert by fingerprint | | `search_metrics` | Search metric names with substring filter, enriched with metadata | | `get_metric_metadata` | Get type, help text, and unit for a specific metric | | `query` | Execute instant PromQL query | | `list_targets` | List scrape targets with health status | | `list_silences` | List active/pending silences | | `create_silence` | Create a silence (confirms with user first) | | `query_logs` | Execute a LogQL range query against Loki (requires `LOKI_URL`) | | `list_labels` | List available label names from Loki (requires `LOKI_URL`) | | `list_label_values` | List values for a specific label from Loki (requires `LOKI_URL`) | ### Git Explorer Server (git-explorer) | Tool | Description | |------|-------------| | `resolve_ref` | Resolve a git ref (branch, tag, commit) to its full commit hash | | `get_log` | Get commit log with optional filters (author, path, limit) | | `get_commit_info` | Get full details for a specific commit | | `get_diff_files` | Get list of files changed between two commits | | `get_file_at_commit` | Get file contents at a specific commit | | `is_ancestor` | Check if one commit is an ancestor of another | | `commits_between` | Get all commits between two refs | | `list_branches` | List all branches in the repository | | `search_commits` | Search commit messages for a pattern | ## Key Implementation Details ### Database - Schema versioning with automatic recreation on version mismatch - Full-text search: SQLite FTS5, PostgreSQL tsvector/GIN - Path-based queries use LIKE for exact prefix matching - Batch operations for efficient indexing ### Indexing - Uses `nix-build` to evaluate options from any revision - File indexing downloads tarball and stores allowed extensions (.nix, .json, .md, etc.) - File indexing enabled by default (use `--no-files` to skip) - Skips already-indexed revisions (use `--force` to re-index) ### Transports - **STDIO**: Default transport, line-delimited JSON-RPC (for CLI/desktop MCP clients) - **HTTP**: Streamable HTTP transport with SSE (for web-based MCP clients) - Session management with cryptographically secure IDs - Configurable CORS (localhost-only by default) - Optional TLS support - SSE keepalive messages (15s default) ### Security - Revision parameter validated against strict regex to prevent Nix injection - Path traversal protection using `filepath.Clean()` and `filepath.IsAbs()` - NixOS module supports `connectionStringFile` for PostgreSQL secrets - Systemd service runs with extensive hardening options - HTTP transport hardening: - Request body size limit (1MB default) - Server timeouts (read: 30s, write: 30s, idle: 120s, header: 10s) - Maximum session limit (10,000 default) - Origin validation for CORS ## CLI Commands ### nixpkgs-search (Primary) ```bash # Options MCP Server nixpkgs-search options serve # Run options MCP server on STDIO nixpkgs-search options search # Search options nixpkgs-search options get