|
| 1 | +# Sysdig MCP Server – Agent Handbook |
| 2 | + |
| 3 | +This document is optimized for MCP coding agents. It highlights what matters most when you need to explore the repository, extend a tool, or run validation quickly. |
| 4 | + |
| 5 | +## Quick Facts |
| 6 | + |
| 7 | +| Topic | Details | |
| 8 | +| --- | --- | |
| 9 | +| Purpose | Expose vetted Sysdig Secure workflows to LLMs through MCP tools | |
| 10 | +| Entry point | `cmd/server/main.go` (Cobra CLI that wires config, Sysdig client, handler, transports) | |
| 11 | +| Runtime | Go 1.25+, uses `mcp-go`, `cobra`, `ginkgo/gomega`, `golangci-lint` | |
| 12 | +| Dev shell | `nix develop` (check `IN_NIX_SHELL=1` before hacking or running commands) | |
| 13 | +| Key commands | `just fmt`, `just lint`, `just test`, `just check`, `just test-coverage` | |
| 14 | + |
| 15 | +## Repository Layout |
| 16 | + |
| 17 | +| Path | Ownership Notes | |
| 18 | +| --- | --- | |
| 19 | +| `cmd/server` | Cobra CLI + transport bootstrap; `setupHandler` registers every MCP tool. | |
| 20 | +| `internal/config` | Loads environment variables (`SYSDIG_MCP_*`) and enforces validation (stdio requires host/token). | |
| 21 | +| `internal/infra/mcp` | Generic MCP handler, HTTP/SSE middlewares, permission filtering logic. | |
| 22 | +| `internal/infra/mcp/tools` | One file per tool + `_test.go`. Helpers live in `utils.go`. | |
| 23 | +| `internal/infra/sysdig` | Typed Sysdig Secure client plus auth helpers (`WrapContextWithHost/Token`). | |
| 24 | +| `docs/` | Assets referenced from the README (diagrams, screenshots). | |
| 25 | +| `justfile` | Canonical dev tasks (format, lint, generate, test, dependency bump). | |
| 26 | + |
| 27 | +## Day-to-Day Workflow |
| 28 | + |
| 29 | +1. Assume you are in a Nix shell and you have all the available tools. Otherwise edit `flake.nix` to add any required tool you don't have in the PATH. |
| 30 | +2. Make focused changes (new MCP tool, bugfix, docs, etc.). |
| 31 | +3. Run the default quality gates: |
| 32 | + ```bash |
| 33 | + just fmt # gofumpt -w . |
| 34 | + just lint # golangci-lint run |
| 35 | + just test # ginkgo -r -p (auto-runs `go generate ./...`) |
| 36 | + ``` |
| 37 | +4. Use `just check` to chain fmt+lint+test, and `just test-coverage` when you need coverage artifacts. |
| 38 | +5. Follow Conventional Commits when preparing PRs. |
| 39 | +6. In case you need to update or add more dependencies run `just bump`. |
| 40 | + |
| 41 | +## MCP Tools & Permissions |
| 42 | + |
| 43 | +The handler filters tools dynamically based on `GetMyPermissions` from Sysdig Secure. Each tool declares mandatory permissions via `WithRequiredPermissions`. Current tools (`internal/infra/mcp/tools`): |
| 44 | + |
| 45 | +| Tool | File | Capability | Required Permissions | Useful Prompts | |
| 46 | +| --- | --- | --- | --- | --- | |
| 47 | +| `list_runtime_events` | `tool_list_runtime_events.go` | Query runtime events with filters, cursor, scope. | `policy-events.read` | “Show high severity runtime events from last 2h.” | |
| 48 | +| `get_event_info` | `tool_get_event_info.go` | Pull full payload for a single policy event. | `policy-events.read` | “Fetch event `abc123` details.” | |
| 49 | +| `get_event_process_tree` | `tool_get_event_process_tree.go` | Retrieve the process tree for an event when available. | `policy-events.read` | “Show the process tree behind event `abc123`.” | |
| 50 | +| `run_sysql` | `tool_run_sysql.go` | Execute caller-supplied Sysdig SysQL queries safely. | `sage.exec`, `risks.read` | “Run the following SysQL…”. | |
| 51 | +| `generate_sysql` | `tool_generate_sysql.go` | Convert natural language to SysQL via Sysdig Sage. | `sage.exec` (does not work with Service Accounts) | “Create a SysQL to list S3 buckets.” | |
| 52 | + |
| 53 | +Every tool has a companion `_test.go` file that exercises request validation, permission metadata, and Sysdig client calls through mocks. |
| 54 | +Note that if you add more tools you need to also update this file to reflect that. |
| 55 | + |
| 56 | +## Adding or Updating Tools |
| 57 | + |
| 58 | +1. Create a new file in `internal/infra/mcp/tools/tool_<name>.go` plus `_test.go`. |
| 59 | +2. Implement a struct with a `handle` method and `RegisterInServer`; reuse helpers from `utils.go` (`Examples`, `WithRequiredPermissions`, `toPtr`, etc.). |
| 60 | +3. Cover all branches with Ginkgo/Gomega tests. Use the `tools_suite_test.go` suite for shared setup. |
| 61 | +4. Register the tool in `cmd/server/main.go` inside `setupHandler`. |
| 62 | +5. Document required permissions and sample prompts in both the README and MCP metadata. |
| 63 | + |
| 64 | +## Testing & Quality Gates |
| 65 | + |
| 66 | +- `just test` runs `go generate ./...` first, then executes the whole suite via Ginkgo (`-r -p` to parallelize). Avoid leaving focused specs (`FDescribe`, `FIt`) in committed code. |
| 67 | +- `just lint` runs `golangci-lint run` using the repo’s configuration (see `.golangci.yml` if adjustments are necessary). |
| 68 | +- `just test-coverage` emits `coverage.out`; open it with `go tool cover -func=coverage.out`. |
| 69 | +- For manual checks, `go test ./...` and `ginkgo ./path/to/package` work inside the Nix shell. |
| 70 | + |
| 71 | +## Troubleshooting & Tips |
| 72 | + |
| 73 | +- **Missing config:** `SYSDIG_MCP_API_HOST` and `SYSDIG_MCP_API_SECURE_TOKEN` are mandatory in `stdio`. Validation fails early in `internal/config/config.go`. |
| 74 | +- **Token scope:** If a tool does not appear, verify the token’s permissions under **Settings > Users & Teams > Roles**. `generate_sysql` currently requires a regular user token, not a Service Account. |
| 75 | +- **Remote auth:** When using `streamable-http` or `sse`, pass `Authorization: Bearer <token>` and optionally `X-Sysdig-Host`. These values override env vars via the request context middleware. |
| 76 | +- **Environment drift:** Always run inside `nix develop`; lint/test expect binaries like `gofumpt`, `golangci-lint`, and `ginkgo` provided by the flake. |
| 77 | +- **Dependency refresh:** Use `just bump` (updates flake inputs, runs `go get -u`, `go mod tidy`, and rebuilds `package.nix`) when you truly need to refresh dependencies. |
| 78 | + |
| 79 | +## Reference Links |
| 80 | + |
| 81 | +- `README.md` – comprehensive product docs, quickstart, and client configuration samples. |
| 82 | +- `pkg.go.dev/github.com/sysdiglabs/sysdig-mcp-server` – use when checking published module versions. |
| 83 | +- [Model Context Protocol](https://modelcontextprotocol.io/) – protocol reference for tool/transport behavior. |
0 commit comments