---
title: 'Debugging'
description: 'Learn how to debug and log in mcp-use'
---

# Debugging MCP-Use

MCP-Use provides built-in debugging functionality that increases log verbosity and helps diagnose issues in your agent implementation.

## Enabling Debug Mode

There are two primary ways to enable debug mode:

### 1. Environment Variable (Recommended for One-off Runs)

Run your script with the `DEBUG` environment variable set to the desired level:

```bash
# 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 the duration of that specific Python process. This is particularly useful for quickly troubleshooting issues without modifying your code.
Alternatively you can set the environment variable MCP_USE_DEBUG as such:
```bash
export MCP_USE_DEBUG=1 # or 2
```

### 2. Setting the Debug Flag Programmatically

You can set the global debug flag directly in your code, which is useful for debugging specific parts of your application or conditionally enabling debug mode based on your application state:

```python
import mcp_use

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

## Debug Levels

MCP-Use supports different levels of debugging:

| Level | Environment Variable | Program Setting | Description |
|-------|---------------------|-----------------|-------------|
| 0     | (not set)           | `set_debug(0)`  | Normal operation, only WARNING and above messages are shown |
| 1     | `DEBUG=1`           | `set_debug(1)`  | INFO level messages are shown - useful for basic operational information. Shows tool calls.|
| 2     | `DEBUG=2`           | `set_debug(2)`  | Full DEBUG level - all detailed debugging information is shown |

## 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:

```python
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.

## Langsmith

Langchain offers a very nice tool to debug agent behaviour which integrates seamlessly with mcp-use. You can visit https://smith.langchain.com/ and login, they will give you a set of variables to copy in an .env file
you will be then able to visualize the agent behaviour on their platform.

## 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.
