View the client source on GitHub: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/client.ts
View the executor base types: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/client/executors/base.ts
View the Code Mode connector: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/client/connectors/codeMode.ts
View the executor base types: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/client/executors/base.ts
View the Code Mode connector: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/client/connectors/codeMode.ts
MCPClient. When enabled, the client exposes executeCode() and searchTools(), and the internal Code Mode connector provides execute_code and search_tools tools for agents.
Import the client and public executor types from mcp-use:
Enable Code Mode
SetcodeMode: true to use the default VM executor. Pass CodeModeConfig when you need E2B, a custom executor function, a custom executor class, or executor options.
Signature
CodeModeConfig
Advanced configuration for code execution mode. FieldsSignatureWhether code execution mode is enabled.Executor implementation. Defaults to"vm".Executor-specific options.
Executor options
VMExecutorOptions
Options for the Node.js VM executor. FieldsSignatureMaximum execution time in milliseconds. Defaults to30000.Optional memory limit in megabytes. When omitted, no memory limit is configured.
E2BExecutorOptions
Options for the E2B cloud sandbox executor. FieldsSignatureE2B API key. Required.Maximum execution time in milliseconds. Defaults to300000.
MCPClient methods
executeCode
Executes JavaScript or TypeScript code with access to connected MCP tools. Code Mode must be enabled. ParametersReturnsCode to execute.Per-execution timeout in milliseconds.
Throws Throws
Error when Code Mode is not enabled.
Signature
searchTools
Searches tools across active MCP sessions. Code Mode must be enabled. ParametersReturnsSearch query. Empty string returns all tools.Amount of detail to return for each matching tool.
Throws Throws
Error when Code Mode is not enabled.
Signature
close
Closes the client. When Code Mode has created an executor,close() cleans up executor resources before closing sessions.
Returns
Signature
ExecutionResult
Result returned byexecuteCode().
Fields
SignatureReturn value from the executed code.Console output captured during execution.Error message when execution failed, otherwisenull.Execution duration in seconds.
Tool search types
ToolSearchResponse
Response returned bysearchTools() and by the runtime search_tools() helper available inside executed code.
Fields
SignatureSearch metadata, including total tool count and namespaces.Matching tools.
ToolSearchMeta
FieldsSignatureTotal number of tools across active sessions before filtering.Sorted server namespaces that have tools.Number of matching tools returned inresults.
ToolSearchResult
FieldsSignatureTool name.Server namespace.Tool description. Included whendetailLevelis"descriptions"or"full".Tool input schema. Included whendetailLevelis"full".
Custom executors
CodeExecutorFunction
Function form for a custom executor. SignatureBaseCodeExecutor
ExtendBaseCodeExecutor when a custom executor needs access to MCP sessions, namespaces, and the shared tool search function.
Constructor parameters
MethodsClient whose configured servers should be exposed to executed code.
SignatureRuns code. Subclasses must implement this method.Releases executor resources. Subclasses must implement this method.Returns the runtime tool search helper.Protected helper that connects missing configured servers before execution.Protected helper that returns active server namespaces and tools, excluding the internalcode_modeserver.
Related
- Code Mode: guide to enabling Code Mode and choosing an executor.
MCPClient: complete client constructor, lifecycle, configuration, and session APIs.- Building agents: agent setup with MCP tools.