Skip to main content

API Reference

This section provides comprehensive documentation for the easy-mcp-use API, including all components, methods, their arguments, and when to use different options.

MCPClient

The MCPClient is the core class for interacting with MCP servers. It handles connection management, session creation, and communication with MCP servers.

Initialization Methods

From Config File

From Dictionary

Core Methods

createSession

Creates a new session with an MCP server.
When to use:
  • Use a longer timeout for servers that take more time to initialize
  • Increase retryCount in unstable network environments
  • Use specific serverName when working with multiple servers in the same config

closeSession

Closes a specific session.

closeAllSessions

Closes all active sessions.
When to use:
  • Always call this at the end of your application to clean up resources
  • Use when switching between different tasks that require different servers

getServer

Gets a server instance by name.

MCPAgent

The MCPAgent class combines an LLM with an MCPClient to create an intelligent agent capable of using MCP tools.

Initialization

When to use different parameters:
  • llm:
    • easy-mcp-use supports ANY LLM that is compatible with LangChain
    • You can use models from OpenAI, Anthropic, Google, Mistral, Groq, Cohere, or any other provider with a LangChain integration
    • You can even use open source models via LlamaCpp, HuggingFace, or other interfaces
    • Custom or self-hosted models are also supported as long as they implement LangChain’s interface
  • maxSteps:
    • Increase for complex tasks that require many interactions
    • Decrease for simpler tasks to improve efficiency
    • Use higher values (50+) for web browsing or multi-stage tasks
    • Use lower values (10-20) for targeted, specific tasks
  • systemPrompt / systemPromptTemplate:
    • Use to customize the initial instructions given to the LLM
    • Helps shape the agent’s behavior and capabilities
    • Use for specialized tasks or custom interaction patterns
  • memoryEnabled:
    • Enable to maintain conversation history
    • Disable for stateless operation or to save on token usage
  • sessionOptions:
    • Customize timeout for long-running server operations
    • Set retry parameters for unstable connections
  • useServerManager:
    • Set to true when using an MCPClient configured with multiple servers to enable efficient, automatic server selection per tool call. This can reduce agent confusion and minimize unnecessary server connections.
    • Keep as false (default) if using a single server or if you prefer to manually specify the target server using the serverName parameter in agent.run() or rely on the agent to handle tool availability across all connected servers.
  • disallowedTools:
    • Use to restrict which tools the agent can access
    • Helpful for security or to limit agent capabilities
    • Useful when certain tools might be dangerous or unnecessary for a specific task
    • Can be updated after initialization using setDisallowedTools()

Core Methods

run

Runs the agent with a given query.
When to use different parameters:
  • maxSteps: Override the instance default for specific queries
  • stopOnFirstResult: Use true for simple lookups, false for thorough exploration
  • serverName: Specify when using multiple servers for different tasks
  • callbacks: Add for monitoring or logging specific runs

reset

Resets the agent state.
When to use:
  • Between different tasks to clear context
  • When starting a new conversation thread
  • When agent gets stuck in a particular strategy

getHistory

Gets the agent’s interaction history.
When to use:
  • For debugging agent behavior
  • When implementing custom logging
  • To provide context for follow-up queries

setDisallowedTools

Sets the list of tools that should not be available to the agent.
When to use:
  • To restrict access to specific tools for security reasons
  • To limit agent capabilities for specific tasks
  • To prevent the agent from using potentially dangerous tools
  • Note: Changes take effect on next initialization

getDisallowedTools

Gets the list of tools that are not available to the agent.
When to use:
  • To check which tools are currently restricted
  • For debugging or auditing purposes
  • To verify tool restrictions before running the agent

Configuration Details

MCP Server Configuration Schema

When to use different options:
  • command & args: Vary based on the specific MCP server implementation
  • env:
    • Set environment-specific variables needed by the server
    • Override default server settings (ports, directories)
    • Set display settings for GUI-based servers
  • timeout:
    • Increase for servers with longer startup times
    • Lower for simpler servers to fail fast
  • retry configuration:
    • Adjust for different network conditions
    • Increase maxAttempts in unstable environments
    • Adjust backoffFactor based on server behavior

Error Handling

easy-mcp-use provides several exception types to handle different error scenarios: Handling Strategies:

Advanced Usage

Multi-Server Configuration

Configure and use multiple MCP servers in a single application:

Custom Output Parsing

Implement custom output parsers for specialized MCP servers:
This approach is useful when:
  • The MCP server returns structured data that needs special handling
  • You need to extract specific information from responses
  • You’re integrating with custom or specialized MCP servers

Restricting Tool Access

Control which tools are available to the agent:
This approach is useful when:
  • You need to restrict access to sensitive operations
  • You want to limit the agent’s capabilities for specific tasks
  • You’re concerned about security implications of certain tools
  • You want to focus the agent on specific functionality