Logging MCP-Use

MCP-Use provides built-in logging functionality that helps diagnose issues in your agent implementation.

Enabling Debug Mode

Choose the debug method that best fits your workflow - environment variables for one-off debugging or programmatic control for conditional debugging.

There are two primary ways to enable debug mode:

Run your script with the DEBUG environment variable:

# Level 1: Show INFO level messages
DEBUG=1 python3.11 examples/browser_use.py

# Level 2: Show DEBUG level messages (full verbose output)
DEBUG=2 python3.11 examples/browser_use.py

This sets the debug level only for that specific Python process - perfect for quick troubleshooting.

2. Setting the Debug Flag Programmatically

Programmatic control is useful for debugging specific parts of your application or conditionally enabling debug mode based on your application state.

import mcp_use

# Different debug levels
mcp_use.set_debug(1)  # INFO level
mcp_use.set_debug(2)  # DEBUG level (full verbose output)
mcp_use.set_debug(0)  # Turn off debug (WARNING level)

You can conditionally enable debugging based on environment or configuration:

import os
import mcp_use

if os.getenv("ENVIRONMENT") == "development":
    mcp_use.set_debug(2)

Debug Levels

Level 0 - Normal

Minimal Output

Only WARNING and above messages are shown

set_debug(0)

Level 1 - Info

Default Operation

Shows INFO level messages and tool calls - useful for basic operational information

DEBUG=1 or set_debug(1) (default)

Level 2 - Full Debug

Maximum Verbosity

Shows all detailed debugging information including internal operations

DEBUG=2 or set_debug(2)

Agent-Specific Verbosity

If you only want to increase verbosity for the agent component without enabling full debug mode for the entire package, you can use the verbose parameter when creating an MCPAgent:

from mcp_use import MCPAgent

# Create agent with increased verbosity
agent = MCPAgent(
    llm=your_llm,
    client=your_client,
    verbose=True  # Only shows debug messages from the agent
)

This option is useful when you want to see the agent’s steps and decision-making process without all the low-level debug information from other components.

Debug Information

When debug mode is enabled, you’ll see more detailed information about:

  • Server initialization and connection details
  • Tool registration and resolution
  • Agent steps and decision-making
  • Request and response formats
  • Communication with MCP servers
  • Error details and stack traces

This can be extremely helpful when diagnosing issues with custom MCP servers or understanding why an agent might be behaving unexpectedly.

Troubleshooting Common Issues

Server Connection Problems

If you’re having issues connecting to MCP servers, enabling debug mode will show detailed information about the connection attempts, server initialization, and any errors encountered.

Agent Not Using Expected Tools

When debug mode is enabled, you’ll see each tool registration and the exact prompts being sent to the LLM, which can help diagnose why certain tools might not be used as expected.

Performance Issues

Debug logs can help identify potential bottlenecks in your implementation by showing timing information for various operations.