|
| 1 | +# Pure REPL with MCP |
| 2 | + |
| 3 | +An interactive REPL (Read-Eval-Print Loop) for the [Pure language](https://legend.finos.org/docs/getting-started/glossary#pure) with built-in [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) server support. The MCP server exposes Pure evaluation capabilities as tools, making them accessible to AI coding assistants such as [Claude Code](https://docs.anthropic.com/en/docs/claude-code), [Cursor](https://www.cursor.com/), and other MCP-compatible clients. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Pure REPL allows you to: |
| 8 | + |
| 9 | +- **Evaluate Pure expressions** interactively with detailed timing and type information |
| 10 | +- **Query expression types** without execution (type inference) |
| 11 | +- **Run Pure test suites** with optional filtering and PCT adapter support |
| 12 | +- **Reload sources** incrementally or fully after editing Pure files on disk |
| 13 | +- **Inspect and configure runtime options** for the Pure compiler and execution engine |
| 14 | + |
| 15 | +The MCP server wraps these capabilities as tools over a stdio JSON-RPC 2.0 transport, enabling AI agents to programmatically evaluate Pure code, run tests, and inspect the runtime. |
| 16 | + |
| 17 | +## Module Structure |
| 18 | + |
| 19 | +``` |
| 20 | +legend-engine-pure-repl/ |
| 21 | +├── legend-engine-pure-repl-core/ # Core REPL engine, session, configuration |
| 22 | +├── legend-engine-pure-repl-cli/ # Interactive CLI (JLine-based terminal) |
| 23 | +└── legend-engine-pure-repl-mcp/ # MCP server (stdio transport) |
| 24 | +``` |
| 25 | + |
| 26 | +## Building |
| 27 | + |
| 28 | +From the legend-engine repository root: |
| 29 | + |
| 30 | +```bash |
| 31 | +# Build the entire REPL module |
| 32 | +mvn clean install -pl legend-engine-core/legend-engine-core-pure/legend-engine-pure-repl -DskipTests |
| 33 | + |
| 34 | +# Build only the MCP server |
| 35 | +mvn clean install -pl legend-engine-core/legend-engine-core-pure/legend-engine-pure-repl/legend-engine-pure-repl-mcp -DskipTests |
| 36 | +``` |
| 37 | + |
| 38 | +The MCP module produces a shaded (uber) JAR: |
| 39 | + |
| 40 | +``` |
| 41 | +legend-engine-pure-repl-mcp/target/legend-engine-pure-repl-mcp-<version>-shaded.jar |
| 42 | +``` |
| 43 | + |
| 44 | +## Running the MCP Server |
| 45 | + |
| 46 | +```bash |
| 47 | +# Basic startup (classpath-only repositories) |
| 48 | +java -jar legend-engine-pure-repl-mcp-<version>-shaded.jar |
| 49 | + |
| 50 | +# With filesystem source loading (enables hot-reload) |
| 51 | +java -jar legend-engine-pure-repl-mcp-<version>-shaded.jar \ |
| 52 | + --source-root /path/to/legend-engine \ |
| 53 | + --repositories core,relational,service \ |
| 54 | + --timeout 60000 |
| 55 | +``` |
| 56 | + |
| 57 | +### Command-Line Arguments |
| 58 | + |
| 59 | +| Argument | Description | |
| 60 | +|----------|-------------| |
| 61 | +| `--source-root <path>` | Root directory of a legend-engine checkout. Enables filesystem-backed repositories for hot-reload. | |
| 62 | +| `--repositories <csv>` | Comma-separated list of repository names to load (optional subset filter). | |
| 63 | +| `--timeout <ms>` | Expression evaluation timeout in milliseconds (default: 30000). | |
| 64 | + |
| 65 | +## MCP Tools |
| 66 | + |
| 67 | +The server exposes 8 tools via the MCP protocol: |
| 68 | + |
| 69 | +### `evaluate_pure` |
| 70 | +Evaluate a Pure expression and return the result with type and timing information. |
| 71 | + |
| 72 | +| Parameter | Type | Required | Description | |
| 73 | +|-----------|------|----------|-------------| |
| 74 | +| `expression` | string | yes | The Pure expression to evaluate | |
| 75 | + |
| 76 | +**Example response:** |
| 77 | +```json |
| 78 | +{ |
| 79 | + "status": "success", |
| 80 | + "expression": "1 + 2", |
| 81 | + "type": "Integer[1]", |
| 82 | + "result": 3, |
| 83 | + "timing": {"parseMs": 2, "compileMs": 25, "executeMs": 3} |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### `get_expression_type` |
| 88 | +Get the type of a Pure expression without evaluating it. |
| 89 | + |
| 90 | +| Parameter | Type | Required | Description | |
| 91 | +|-----------|------|----------|-------------| |
| 92 | +| `expression` | string | yes | The Pure expression to type-check | |
| 93 | + |
| 94 | +### `run_tests` |
| 95 | +Run Pure tests at a given package path. |
| 96 | + |
| 97 | +| Parameter | Type | Required | Description | |
| 98 | +|-----------|------|----------|-------------| |
| 99 | +| `path` | string | yes | Package path or test function path (e.g. `meta::test::*`, `::`) | |
| 100 | +| `pctAdapter` | string | no | PCT adapter name | |
| 101 | +| `filter` | string | no | Regex filter for test names | |
| 102 | + |
| 103 | +### `incremental_recompile` |
| 104 | +Detect changed files on disk and recompile only those. Fast but does not pick up newly created files. |
| 105 | + |
| 106 | +### `full_recompile` |
| 107 | +Fully reset the runtime and recompile all sources. Slower but picks up all changes including new files. |
| 108 | + |
| 109 | +### `get_runtime_info` |
| 110 | +Return runtime status including loaded repositories, memory usage, and current options. |
| 111 | + |
| 112 | +### `set_runtime_option` |
| 113 | +Set a Pure runtime boolean option. |
| 114 | + |
| 115 | +| Parameter | Type | Required | Description | |
| 116 | +|-----------|------|----------|-------------| |
| 117 | +| `name` | string | yes | Option name | |
| 118 | +| `value` | boolean | yes | Value to set | |
| 119 | + |
| 120 | +### `list_runtime_options` |
| 121 | +List all current Pure runtime options and their values. |
| 122 | + |
| 123 | +## Using with AI Tools |
| 124 | + |
| 125 | +The MCP server communicates over **stdio** (stdin/stdout) using JSON-RPC 2.0, with all logging directed to stderr. This makes it compatible with any MCP-capable AI tool. |
| 126 | + |
| 127 | +### Claude Code |
| 128 | + |
| 129 | +Add the following to your [Claude Code MCP configuration](https://docs.anthropic.com/en/docs/claude-code/mcp) (either in `.claude/settings.json` project-level or `~/.claude/settings.json` globally): |
| 130 | + |
| 131 | +```json |
| 132 | +{ |
| 133 | + "mcpServers": { |
| 134 | + "pure-repl": { |
| 135 | + "command": "java", |
| 136 | + "args": [ |
| 137 | + "-jar", |
| 138 | + "/absolute/path/to/legend-engine-pure-repl-mcp-<version>-shaded.jar", |
| 139 | + "--source-root", |
| 140 | + "/path/to/legend-engine", |
| 141 | + "--repositories", |
| 142 | + "core,relational,service" |
| 143 | + ] |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +Once configured, Claude Code can evaluate Pure expressions, run tests, and inspect types directly through the MCP tools. |
| 150 | + |
| 151 | +### Cursor |
| 152 | + |
| 153 | +In Cursor, go to **Settings > MCP** and add a new server: |
| 154 | + |
| 155 | +- **Name:** `pure-repl` |
| 156 | +- **Type:** `command` |
| 157 | +- **Command:** `java -jar /absolute/path/to/legend-engine-pure-repl-mcp-<version>-shaded.jar --source-root /path/to/legend-engine` |
| 158 | + |
| 159 | +### VS Code with Continue |
| 160 | + |
| 161 | +Add to your `.continue/config.json`: |
| 162 | + |
| 163 | +```json |
| 164 | +{ |
| 165 | + "mcpServers": [ |
| 166 | + { |
| 167 | + "name": "pure-repl", |
| 168 | + "command": "java", |
| 169 | + "args": [ |
| 170 | + "-jar", |
| 171 | + "/absolute/path/to/legend-engine-pure-repl-mcp-<version>-shaded.jar", |
| 172 | + "--source-root", |
| 173 | + "/path/to/legend-engine" |
| 174 | + ] |
| 175 | + } |
| 176 | + ] |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +### Generic MCP Client |
| 181 | + |
| 182 | +Any MCP client that supports stdio transport can connect. The server expects one JSON-RPC 2.0 message per line on stdin and writes responses to stdout. Example request: |
| 183 | + |
| 184 | +```json |
| 185 | +{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "evaluate_pure", "arguments": {"expression": "1 + 2"}}} |
| 186 | +``` |
| 187 | + |
| 188 | +## Configuration |
| 189 | + |
| 190 | +The REPL can be configured via a JSON file and/or environment variables. |
| 191 | + |
| 192 | +### Configuration File |
| 193 | + |
| 194 | +Default location: `~/.pure-repl.json` (override with `PURE_REPL_CONFIG` environment variable). |
| 195 | + |
| 196 | +```json |
| 197 | +{ |
| 198 | + "timeout": 30000, |
| 199 | + "outputFormat": "text", |
| 200 | + "debug": false, |
| 201 | + "quiet": false, |
| 202 | + "sourceRoot": "/path/to/legend-engine", |
| 203 | + "requiredRepositories": ["core", "relational", "service"] |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +### Environment Variables |
| 208 | + |
| 209 | +| Variable | Description | |
| 210 | +|----------|-------------| |
| 211 | +| `PURE_REPL_CONFIG` | Path to configuration JSON file | |
| 212 | +| `PURE_REPL_TIMEOUT` | Evaluation timeout in milliseconds | |
| 213 | +| `PURE_REPL_OUTPUT_FORMAT` | Output format: `text` or `json` | |
| 214 | +| `PURE_REPL_DEBUG` | Enable debug logging (`true`/`false`) | |
| 215 | +| `PURE_REPL_HISTORY` | Path to command history file | |
| 216 | + |
| 217 | +Environment variables take precedence over the configuration file. |
| 218 | + |
| 219 | +## Interactive CLI Commands |
| 220 | + |
| 221 | +When using the CLI (not the MCP server), these commands are available: |
| 222 | + |
| 223 | +| Command | Aliases | Description | |
| 224 | +|---------|---------|-------------| |
| 225 | +| `:help` | `:h`, `:?` | Display help | |
| 226 | +| `:info` | `:i` | Show runtime info (repositories, memory) | |
| 227 | +| `:type <expr>` | `:t` | Get type of expression without evaluating | |
| 228 | +| `:reload` | `:r` | Recompile all Pure sources | |
| 229 | +| `:test <path>` | | Run tests at package path | |
| 230 | +| `:option <name> <true\|false>` | `:opt` | Set a runtime option | |
| 231 | +| `:option <name>` | `:opt` | Get a runtime option value | |
| 232 | +| `:options` | | List all runtime options | |
| 233 | +| `:quit` | `:exit`, `:q` | Exit the REPL | |
0 commit comments