|
1 | | -# splunk-cli |
2 | | -Splunk CLI |
| 1 | +# Splunk CLI & MCP Server |
| 2 | + |
| 3 | +A Splunk CLI and MCP server that allows you and your coding agents to interact with Splunk. Inspired by the GitHub CLI and following the same concept as jira-cli, it aims to provide a simple and efficient way for humans and agents to interact with Splunk from the command line. |
| 4 | + |
| 5 | +Being both a CLI and an MCP server means you get the best of both worlds. Agents can be directed to perform specific commands (e.g., `Run a search for errors in the last hour by running splunk search 'error' '-1h' 'now'`), or they can use the MCP server to interact with Splunk directly. |
| 6 | + |
| 7 | +Like `jq`, it is a single tiny binary, without the overhead of installing a Node runtime, and without the need to put your Splunk token in plain text file (it uses the system key-ring). |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +### Supported Platforms |
| 12 | + |
| 13 | +Binaries are available for: |
| 14 | +- **Linux**: amd64, arm64 |
| 15 | +- **macOS**: amd64 (Intel), arm64 (Apple Silicon) |
| 16 | +- **Windows**: amd64 |
| 17 | + |
| 18 | +### Download and Install |
| 19 | + |
| 20 | +Download the binary for your platform from the [release page](https://github.com/kitproj/splunk-cli/releases). |
| 21 | + |
| 22 | +#### Linux |
| 23 | + |
| 24 | +**For Linux (amd64):** |
| 25 | +```bash |
| 26 | +sudo curl -fsL -o /usr/local/bin/splunk https://github.com/kitproj/splunk-cli/releases/download/v0.0.1/splunk_v0.0.1_linux_amd64 |
| 27 | +sudo chmod +x /usr/local/bin/splunk |
| 28 | +``` |
| 29 | + |
| 30 | +**For Linux (arm64):** |
| 31 | +```bash |
| 32 | +sudo curl -fsL -o /usr/local/bin/splunk https://github.com/kitproj/splunk-cli/releases/download/v0.0.1/splunk_v0.0.1_linux_arm64 |
| 33 | +sudo chmod +x /usr/local/bin/splunk |
| 34 | +``` |
| 35 | + |
| 36 | +#### macOS |
| 37 | + |
| 38 | +**For macOS (Apple Silicon/arm64):** |
| 39 | +```bash |
| 40 | +sudo curl -fsL -o /usr/local/bin/splunk https://github.com/kitproj/splunk-cli/releases/download/v0.0.1/splunk_v0.0.1_darwin_arm64 |
| 41 | +sudo chmod +x /usr/local/bin/splunk |
| 42 | +``` |
| 43 | + |
| 44 | +**For macOS (Intel/amd64):** |
| 45 | +```bash |
| 46 | +sudo curl -fsL -o /usr/local/bin/splunk https://github.com/kitproj/splunk-cli/releases/download/v0.0.1/splunk_v0.0.1_darwin_amd64 |
| 47 | +sudo chmod +x /usr/local/bin/splunk |
| 48 | +``` |
| 49 | + |
| 50 | +#### Verify Installation |
| 51 | + |
| 52 | +After installing, verify the installation works: |
| 53 | +```bash |
| 54 | +splunk -h |
| 55 | +``` |
| 56 | + |
| 57 | +## Usage |
| 58 | + |
| 59 | +### Configuration |
| 60 | + |
| 61 | +#### Getting a Splunk API Token |
| 62 | + |
| 63 | +Before configuring, you'll need to create a Splunk authentication token: |
| 64 | + |
| 65 | +1. Log in to your Splunk instance: `https://your-splunk-host:8000` |
| 66 | +2. Go to Settings > Tokens |
| 67 | +3. Click "New Token" or "Enable Token Authentication" if not already enabled |
| 68 | +4. Generate and copy the token (you won't be able to see it again) |
| 69 | + |
| 70 | +#### Configure the CLI |
| 71 | + |
| 72 | +The `splunk` CLI can be configured in two ways: |
| 73 | + |
| 74 | +1. **Using the configure command (recommended, secure)**: |
| 75 | + ```bash |
| 76 | + echo "your-api-token" | splunk configure your-splunk-host |
| 77 | + ``` |
| 78 | + This stores the host in `~/.config/splunk-cli/config.json` and the token securely in your system's keyring. |
| 79 | + |
| 80 | +2. **Using environment variables**: |
| 81 | + ```bash |
| 82 | + export SPLUNK_HOST=your-splunk-host |
| 83 | + export SPLUNK_TOKEN=your-api-token |
| 84 | + ``` |
| 85 | + Note: The SPLUNK_TOKEN environment variable is still supported for backward compatibility, but using the keyring (via `splunk configure`) is more secure on multi-user systems. |
| 86 | + |
| 87 | +## Usage |
| 88 | + |
| 89 | +### Direct CLI Usage |
| 90 | + |
| 91 | +```bash |
| 92 | +Usage: |
| 93 | + splunk configure <host> - Configure Splunk host and token (reads token from stdin) |
| 94 | + splunk search <query> [earliest-time] [latest-time] - Run a Splunk search query |
| 95 | + splunk mcp-server - Start MCP server (stdio transport) |
| 96 | +``` |
| 97 | + |
| 98 | +#### Examples |
| 99 | + |
| 100 | +**Run a search:** |
| 101 | +```bash |
| 102 | +splunk search "error" "-1h" "now" |
| 103 | +# Search for "error" in the last hour |
| 104 | + |
| 105 | +splunk search "index=main sourcetype=access_combined | stats count by status" |
| 106 | +# Search with SPL query |
| 107 | +``` |
| 108 | + |
| 109 | +### MCP Server Mode |
| 110 | + |
| 111 | +The MCP (Model Context Protocol) server allows AI assistants and other tools to interact with Splunk through a standardized JSON-RPC protocol over stdio. This enables seamless integration with AI coding assistants and other automation tools. |
| 112 | + |
| 113 | +Learn more about MCP: https://modelcontextprotocol.io |
| 114 | + |
| 115 | +**Setup:** |
| 116 | + |
| 117 | +1. First, configure your Splunk host and token (stored securely in the system keyring): |
| 118 | + ```bash |
| 119 | + echo "your-api-token" | splunk configure your-splunk-host |
| 120 | + ``` |
| 121 | + |
| 122 | +2. Add the MCP server configuration to your MCP client (e.g., Claude Desktop, Cline): |
| 123 | + ```json |
| 124 | + { |
| 125 | + "mcpServers": { |
| 126 | + "splunk": { |
| 127 | + "command": "splunk", |
| 128 | + "args": ["mcp-server"] |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + ``` |
| 133 | + |
| 134 | + For **Claude Desktop**, add this to: |
| 135 | + - macOS: `~/Library/Application Support/Claude/claude_desktop_config.json` |
| 136 | + - Windows: `%APPDATA%\Claude\claude_desktop_config.json` |
| 137 | + |
| 138 | +The server exposes the following tool: |
| 139 | +- `search` - Run a Splunk search query and return results |
| 140 | + |
| 141 | +**Example usage from an AI assistant:** |
| 142 | +> "Search Splunk for errors in the main index in the last hour and show me the top 10 results." |
| 143 | +
|
| 144 | +## Development |
| 145 | + |
| 146 | +### Built With |
| 147 | + |
| 148 | +This CLI uses the following Go libraries: |
| 149 | +- **[github.com/mark3labs/mcp-go](https://github.com/mark3labs/mcp-go)** - Model Context Protocol server library |
| 150 | +- **[github.com/zalando/go-keyring](https://github.com/zalando/go-keyring)** - Cross-platform keyring library for secure token storage |
| 151 | + |
| 152 | +The Splunk API client is a custom implementation using the Splunk REST API, as there is no official Go SDK for Splunk Enterprise. |
| 153 | + |
| 154 | +### Building from Source |
| 155 | + |
| 156 | +```bash |
| 157 | +# Clone the repository |
| 158 | +git clone https://github.com/kitproj/splunk-cli.git |
| 159 | +cd splunk-cli |
| 160 | + |
| 161 | +# Build the binary |
| 162 | +go build -o splunk |
| 163 | + |
| 164 | +# Run tests |
| 165 | +go test ./... |
| 166 | +``` |
| 167 | + |
| 168 | +### Project Structure |
| 169 | + |
| 170 | +``` |
| 171 | +splunk-cli/ |
| 172 | +├── internal/ |
| 173 | +│ ├── config/ # Configuration management (host, token storage) |
| 174 | +│ └── splunk/ # Splunk REST API client |
| 175 | +├── main.go # CLI entry point and command handlers |
| 176 | +├── mcp.go # MCP server implementation |
| 177 | +├── mcp_test.go # MCP server tests |
| 178 | +└── README.md # This file |
| 179 | +``` |
| 180 | + |
| 181 | +## Troubleshooting |
| 182 | + |
| 183 | +### Common Issues |
| 184 | + |
| 185 | +**"Splunk host must be configured" error** |
| 186 | +- Make sure you've run `splunk configure <host>` or set the `SPLUNK_HOST` environment variable |
| 187 | +- Check that the config file exists: `cat ~/.config/splunk-cli/config.json` |
| 188 | + |
| 189 | +**"Failed to execute request" or authentication errors** |
| 190 | +- Verify your API token is still valid (tokens can expire) |
| 191 | +- Re-run the configure command to update the token: `echo "new-token" | splunk configure your-splunk-host` |
| 192 | +- Make sure your Splunk user has permission to access the requested resources |
| 193 | + |
| 194 | +**Keyring issues on Linux** |
| 195 | +- Some Linux systems may not have a keyring service installed |
| 196 | +- Install `gnome-keyring` or `kwallet` for your desktop environment |
| 197 | +- Alternatively, use environment variables: `export SPLUNK_TOKEN=your-token` |
| 198 | + |
| 199 | +**MCP server not appearing in Claude Desktop** |
| 200 | +- Restart Claude Desktop after editing the config file |
| 201 | +- Check the config file syntax is valid JSON |
| 202 | +- Verify the `splunk` binary is in your PATH: `which splunk` |
| 203 | + |
| 204 | +### Getting Help |
| 205 | + |
| 206 | +- Report issues: https://github.com/kitproj/splunk-cli/issues |
| 207 | +- Check existing issues for solutions and workarounds |
| 208 | + |
| 209 | +## License |
| 210 | + |
| 211 | +This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details. |
0 commit comments