Skip to main content
Code mode exposes execute_code and search_tools so agents run JavaScript with access to connected MCP tools instead of making individual tool calls. Node.js only.

Enable

The filesystem server’s list_directory tool returns a formatted string, so result.result contains the directory listing. A string’s .length counts characters, not directory entries. The example creates ./data if needed and uses its absolute path for both the server’s allowed directory and the tool call.

Executors

A custom function receives (code, timeout?) and returns an ExecutionResult. It owns cleanup for whatever runtime it wraps — client.close() does not release anything on its behalf. A non-arrow function is invoked with the MCPClient as its this, so it can reach the client without closing over it:

Agent integration

Use with @mcp-use/agent and PROMPTS.CODE_MODE:

Reference

Client API reference — config types, searchTools, and ExecutionResult. Anthropic: Code execution with MCP