Looking for agent configuration? If you’re building AI agents that interact with MCP servers, see the Agent Configuration guide instead.
Quick Start
Connection Types
MCP servers communicate with clients using different transport protocols. The mcp-use client supports multiple connection types, each optimized for different use cases.Supported Connection Types
The client supports two primary connection types:- STDIO: Local process communication via standard input/output
- HTTP/SSE: Remote servers via HTTP with Server-Sent Events
When to Use Each Type
| Connection Type | Best For | Use Cases |
|---|---|---|
| STDIO | Local development | Testing servers locally, file system access, no network setup needed |
| HTTP/SSE | Production deployment | Remote servers, load balancing, authentication, multiple clients |
Transport Migration Notice (MCP Spec 2025-11-25)The old SSE transport (separate POST and SSE endpoints) is deprecated in favor of Streamable HTTP (unified endpoint).Your existing code still works! The No action required for existing code. Migrate at your own pace.Learn more: MCP Spec - Streamable HTTP
sse transport option is maintained for backward compatibility.What changed:- Old way: POST
/messagesfor requests, GET/ssefor notifications (two endpoints) - New way: POST
/mcpand GET/mcp(one unified endpoint) - SSE still works for notifications - just through the unified endpoint
Automatic Session Management
mcp-use clients automatically handle session lifecycle and recovery per the MCP specification.Automatic 404 Recovery
When using HTTP/SSE transports, the client automatically handles server restarts and expired sessions: How it works:- Client makes request with session ID
- Server returns 404 (session expired or server restarted)
- Client automatically:
- Detects 404 error with active session
- Clears stale session ID
- Sends new
initializerequest - Retries original request with new session
- Original request succeeds - completely transparent to your app
- ✅ No manual reconnection required
- ✅ Seamless recovery from server restarts
- ✅ Works for both HTTP and SSE transports
- ✅ Complies with MCP specification
- ✅ Automatic in inspector, React hooks, and Node.js clients
Session Persistence
HTTP/SSE connections maintain session state across requests:- Session ID: Automatically managed by the client
- Client capabilities: Preserved across requests
- Server capabilities: Cached after initialization
- Subscriptions: Maintained for resource updates
Server Configuration
The MCPClient uses a flexible configuration system that supports any MCP server, regardless of connection type.Configuration Structure
Server configuration is defined as a JavaScript object:Configuration Parameters
Each server entry requires a unique name and connection-specific parameters. The client automatically detects the connection type based on the provided configuration. Common Parameters:server_name: Unique identifier for the server (used as the object key)
command: Executable command to start the server (e.g.,"npx","python")args: Array of command-line argumentsenv: Environment variables for the server process
url: Full URL to the MCP server endpointheaders: Custom HTTP headers (useful for authentication)
Example Configuration
Here’s a basic example of how to configure an MCP server:Multiple Server Configuration
You can configure multiple MCP servers in a single configuration file, allowing you to use different servers for different tasks or combine their capabilities (e.g.):Client Creation Methods
There are several ways to create an MCPClient:From Dictionary/Object
Create configuration programmatically:TypeScript
From Configuration File
Load configuration from a JSON file:With Sandbox Options
Enable sandboxed execution:Best Practices
- API Keys: Always use environment variables for sensitive information
- Configuration Files: Keep configuration files in version control (without sensitive data)
- Server Naming: Use descriptive names for your MCP servers
- Environment Variables: Set appropriate environment variables for each server
- Testing: Test server connections independently before using with agents
- Monitoring: Enable logging to monitor server connection health
Error Handling
Common client configuration errors and solutions:- Server Not Found: Check if the server command is installed and accessible
- Connection Timeout: Verify server is running and network connectivity
- Permission Denied: Ensure proper file permissions and environment setup
- Invalid Configuration: Validate JSON syntax and required fields