View the source code for the Node.js client on GitHub: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/client.ts
View the shared base client implementation: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/client/base.ts
View the browser client implementation: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/client/browser.ts
View the shared base client implementation: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/client/base.ts
View the browser client implementation: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/client/browser.ts
MCPClient stores MCP server configuration and creates MCPSession instances for live connections. Use the client to add or remove servers, open sessions, manage lifecycle, handle client-side callbacks, and enable code mode.
mcp-use/browser. The browser entry exports BrowserMCPClient as MCPClient; it supports HTTP servers only and inherits the same session-management methods.
Constructor
Creates a Node.js MCP client. The constructor accepts an inline config object, a path to a JSON config file, or no config. Servers can also be added later withaddServer().
Parameters
SignatureInline client configuration, path to a JSON config file, or omitted for an empty client.Client behavior options such as code mode, sampling, elicitation, and notification callbacks.
Static methods
fromDict
Creates anMCPClient from an inline configuration object.
Parameters
ReturnsClient configuration with an optionalmcpServersmap.Client behavior options.
Example
fromConfigFile
Loads a JSON config file and creates anMCPClient.
Parameters
ReturnsPath to the JSON configuration file.Client behavior options.
Signature
getPackageVersion
Returns the installedmcp-use package version.
Returns
Signature
Session methods
createSession
Creates a newMCPSession for a configured server. If a session already exists for the same server name, the new session replaces it.
Parameters
ReturnsName of the server inconfig.mcpServers.Whentrue, connects and runs the MCP initialize handshake before resolving.
Throws Throws
Error when serverName is not found in the config.
Example
createAllSessions
Creates sessions for every configured server. Sessions are opened sequentially. ParametersReturnsWhentrue, connects and initializes each session before resolving.
Signature
getSession
Returns an existing session ornull when the session has not been created.
Parameters
ReturnsName of the server session to retrieve.
Signature
requireSession
Returns an existing session and throws when the session is missing. ParametersReturnsName of the server session to retrieve.
Throws Throws
Error with the available session names when no session exists for serverName.
Signature
getAllActiveSessions
Returns the active sessions as a map keyed by server name. ReturnsSignature
closeSession
Disconnects one active session and removes it from the active session list. Calling this method for a missing session logs a warning and resolves. ParametersReturnsName of the session to close.
Signature
closeAllSessions
Closes every active session. Errors are logged and remaining sessions continue closing. ReturnsSignature
close
Closes the client. This cleans up any code executor first, then closes all active sessions. Prefer this for application shutdown. ReturnsExample
Configuration methods
addServer
Adds or replaces a server configuration. The server can be used bycreateSession() after it is added.
Parameters
ReturnsUnique server name.Server transport configuration.
Example
removeServer
Removes a server configuration and removes the server name fromactiveSessions. Close the session first when you need to disconnect the transport.
Parameters
ReturnsServer name to remove.
Signature
getServerNames
Returns configured server names. In Node.js code mode, the internalcode_mode server is excluded.
Returns
Signature
getServerConfig
Returns the configuration for a single server. ParametersReturnsServer name.
Signature
getConfig
Returns the complete client configuration. ReturnsSignature
saveConfig
Writes the current client configuration to a JSON file. This method is only available on the Node.jsMCPClient.
Parameters
ReturnsDestination file path. Missing parent directories are created.
Signature
Code mode methods
executeCode
Executes JavaScript or TypeScript code with access to connected MCP tools. This method is only available whenoptions.codeMode is enabled.
Parameters
ReturnsCode to execute.Execution timeout in milliseconds.
Throws Throws
Error when code mode is not enabled.
Signature
searchTools
Searches tools across active MCP sessions. This method is only available whenoptions.codeMode is enabled.
Parameters
ReturnsSearch query. Empty string returns all tools.Amount of tool detail to return.
Throws Throws
Error when code mode is not enabled.
Signature
Types
MCPClientConfigShape
Top-level client configuration. ThemcpServers map holds named server configs. Root callback fields and clientInfo are used as defaults when a server config omits them.
Fields
SignatureOptional map of server names to transport configs.Optional default client metadata sent during initialization.Optional default callback for server sampling requests.Optional default callback for server elicitation requests.Optional default callback for server notifications.
ServerConfig
Transport configuration for one MCP server. Node.js clients support HTTP and Standard I/O configs. Browser clients support HTTP configs only. FieldsSignatureHTTP or SSE MCP endpoint URL.Optional request headers for HTTP servers.Bearer token for HTTP servers.Optional SDK-compatible auth provider.Optional transport preference. Defaults to streamable HTTP with SSE fallback.Prefer SSE transport when connecting.Disable automatic SSE fallback for streamable HTTP.Stdio command for Node.js clients.Stdio arguments for Node.js clients.Optional environment variables for Standard I/O servers.
MCPClientOptions
Options passed as the second constructor argument. These values are global defaults. Per-server callback fields override them. FieldsSignatureEnable code execution mode.Handlesampling/createMessagerequests from servers.Handleelicitation/createrequests from servers.Handle server notifications when a server config does not provide its own handler.Deprecated alias foronSampling.Deprecated alias foronElicitation.
CodeModeConfig
Advanced code execution configuration. FieldsSignatureEnable or disable code mode.Executor implementation. Defaults to"vm".Executor-specific options.
ExecutionResult
Result returned byexecuteCode().
Fields
Return value from the executed code.Console output captured during execution.Error message when execution failed, otherwisenull.Execution duration in seconds.
Usage
Use an initialized session for MCP protocol calls:Related
MCPSession: live MCP protocol calls for tools, resources, prompts, roots, and notifications.- MCP Client overview: choose the right client entry point and make a first tool call.
- Code mode: guide to code execution mode and executors.
- Sampling: guide to handling server sampling requests.
- Elicitation: guide to handling user input requests.