import { MCPClient } from "mcp-use/client";const client = MCPClient.fromDict(config);// Create sessionsawait client.createAllSessions();// List available toolsconst tools = await client.getTools();console.log("Tools:", tools);// Get a specific sessionconst session = client.getSession("filesystem");// Call a toolconst result = await session.callTool("read_file", { path: "/tmp/test.txt",});console.log("File content:", result.content[0].text);// List resourcesconst resources = await session.listResources();console.log("Resources:", resources);// Read a resourceconst data = await session.readResource("app://config");console.log("Config:", data);// Close sessionsawait client.closeAllSessions();
from mcp_use import MCPClientclient = MCPClient.from_dict(config)# Create sessionsawait client.create_all_sessions()# List available toolstools = await client.get_tools()print(f"Tools: {tools}")# Get a specific sessionsession = client.get_session("filesystem")# Call a toolresult = await session.call_tool( name="read_file", arguments={"path": "/tmp/test.txt"},)print(f"File content: {result.content[0].text}")# List resourcesresources = await session.list_resources()print(f"Resources: {resources}")# Read a resourcedata = await session.read_resource("app://config")print(f"Config: {data}")# Close sessionsawait client.close_all_sessions()
// Create specific sessionsawait client.createSession("filesystem");// Check if session existsif (client.hasSession("filesystem")) { const session = client.getSession("filesystem");}// Get all sessionsconst sessions = client.getSessions();// Close specific sessionawait client.closeSession("filesystem");// Close all sessionsawait client.closeAllSessions();
# Create specific sessionawait client.create_session("filesystem")# Check if session existsif client.has_session("filesystem"): session = client.get_session("filesystem")# Get all sessionssessions = client.get_sessions()# Close specific sessionawait client.close_session("filesystem")# Close all sessionsawait client.close_all_sessions()
const config = { mcpServers: { // Local process filesystem: { command: "npx", args: ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"], }, // HTTP server weather: { url: "http://localhost:3000/mcp", }, // Another local process airbnb: { command: "npx", args: ["-y", "@openbnb/mcp-server-airbnb"], }, },};const client = MCPClient.fromDict(config);await client.createAllSessions();// Use tools from any serverconst allTools = await client.getTools();// Tools from all three servers are available
try { await client.createSession("remote-server");} catch (error) { console.error("Failed to connect:", error); // Fall back to alternative server or cached data}
Use Configuration Files
Store configs in version control
Use environment variables for secrets
Different configs for dev/staging/prod
Document available servers
Validate Tool Inputs
const result = await session.callTool("process_data", { input: validateInput(userInput),});