Evaluate JavaScript in Obsidian
obsidian_evalExecute JavaScript in Obsidian with full access to the app object. Use only when other tools cannot meet the need.
Instructions
Runs arbitrary JavaScript inside the running Obsidian instance with access to the app object. DANGEROUS: can read/modify any vault data and execute side effects. Use only when no narrower tool fits.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| vault | No | Vault name to target. Optional — defaults to the most recently focused vault. | |
| code | Yes | JavaScript code to evaluate. | |
| confirm | No | Set to true to skip the interactive confirmation prompt. Use only when the caller has already confirmed with the user. |
Implementation Reference
- src/tools.ts:272-272 (registration)The 'tools' array is exported, and obsidian_eval is registered as part of this array (line 755). The tool definitions array is iterated over in src/index.ts (line 40-51) where each tool is registered with the MCP server.
export const tools: ToolDef[] = [ - src/tools.ts:760-764 (schema)Input schema for obsidian_eval: accepts 'vault' (optional, string), 'code' (required string of JavaScript to evaluate), and 'confirm' (optional boolean from ConfirmArg).
inputSchema: { ...VaultArg, code: z.string().min(1).describe("JavaScript code to evaluate."), ...ConfirmArg, }, - src/tools.ts:773-787 (handler)Handler function for obsidian_eval. Checks if the code exceeds the platform's argv limit using shouldChunk/getLimit. If too large, returns an error suggesting the user write the script to a note first. Otherwise, runs the code via runText with command 'eval'.
handler: async ({ vault, code }) => { if (shouldChunk(code)) { return errorResult( new Error( `Eval code is ${Buffer.byteLength(code, "utf8")} bytes, over the ` + `${getLimit()}-byte argv limit on this platform. JavaScript cannot ` + `be chunked — write the script to a note via append (which auto-chunks) ` + `then load it inside a smaller eval, e.g. ` + `\`new Function(await app.vault.adapter.read('script.md'))()\`.`, ), ); } return runText("eval", { vault, params: { code } }); }, }, - src/index.ts:40-51 (registration)MCP server registration loop that registers each tool (including obsidian_eval) from the tools array using server.registerTool().
for (const tool of tools) { server.registerTool( tool.name, { title: tool.title, description: tool.description, inputSchema: tool.inputSchema, annotations: tool.annotations, }, buildHandler(server, tool), ); } - src/tools.ts:99-110 (helper)runText helper function used by the obsidian_eval handler. Calls runObsidian with the given command and options, then formats the output as a text result.
async function runText( command: string, opts: Parameters<typeof runObsidian>[1] = {}, ): Promise<McpToolResult> { try { const result = await runObsidian(command, opts); const text = result.stdout.trim() || result.stderr.trim() || "(no output)"; return textResult(text); } catch (err) { return errorResult(err); } }