eval_form
Evaluate and test Clojure code snippets directly in your chosen namespace or the current one. Reload dependencies, define functions, and run operations interactively within the nREPL environment.
Instructions
Evaluate Clojure code in a specific namespace or the current one. Examples:
Define and call a function: {"code": "(defn greet [name] (str "Hello, " name "!"))(greet "World"))"}
Reload code: {"code": "(clj-reload.core/reload)"}
Evaluate in a specific namespace: {"code": "(clojure.repl.deps/sync-deps)", "ns": "user"}
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Clojure code to evaluate | |
| ns | No | Optional namespace to evaluate in. Changes persist for subsequent evaluations. |
Implementation Reference
- src/index.ts:213-235 (handler)The handler for the 'eval_form' tool. It validates the input arguments, ensures an active nREPL connection, optionally switches to the specified namespace using (in-ns), evaluates the Clojure code via NReplClient.eval, and returns the result as text content.case 'eval_form': { await this.ensureNReplClient(); const args = request.params.arguments; if (!args || typeof args.code !== 'string') { throw new McpError( ErrorCode.InvalidParams, 'code parameter must be a string' ); } let result: string; if (args.ns) { // If namespace is provided, change to it first await this.nreplClient!.eval(`(in-ns '${args.ns})`); result = await this.nreplClient!.eval(args.code); } else { result = await this.nreplClient!.eval(args.code); } return { content: [{ type: 'text', text: result }], }; }
- src/index.ts:152-166 (registration)Registration of the 'eval_form' tool in the ListTools response, including its description and input schema definition.{ name: 'eval_form', description: 'Evaluate Clojure code in a specific namespace or the current one. Examples:\n' + '- Define and call a function: {"code": "(defn greet [name] (str \\"Hello, \\" name \\"!\\"))(greet \\"World\\"))"}\n' + '- Reload code: {"code": "(clj-reload.core/reload)"}\n' + '- Evaluate in a specific namespace: {"code": "(clojure.repl.deps/sync-deps)", "ns": "user"}', inputSchema: { type: 'object', properties: { code: { type: 'string', description: 'Clojure code to evaluate' }, ns: { type: 'string', description: 'Optional namespace to evaluate in. Changes persist for subsequent evaluations.' }, }, required: ['code'], }, },
- src/nrepl-client.ts:177-211 (helper)Core helper function in NReplClient that performs the actual evaluation by sending an nREPL 'eval' operation via bencoded socket messages, processes responses, handles errors and output, strips ANSI color codes.async eval(code: string): Promise<string> { await this.ensureConnected(); if (!this.sessionId) { throw new Error('No active session'); } const responses = await this.send({ op: 'eval', code, session: this.sessionId, }); // Collect any errors const errors = responses .map((r) => r.ex || r['root-ex'] || r.err) .filter(Boolean) .map(err => this.stripAnsiColorCodes(err as string)); if (errors.length > 0) { const errorMsg = errors.join('\n'); this.lastError = errorMsg; throw new Error(errorMsg); } // Collect any output and strip ANSI color codes const output = responses .map((r) => { const text = r.value || r.out; return text ? this.stripAnsiColorCodes(text) : null; }) .filter(Boolean); return output.join('\n'); }