Files
mcp-use__mcp-use/docs/python/development/logging.mdx
Pietro Zullo 1cae240ca2 feat/docs (#340)
* Check out feat/server/python branch with latest docs

* feat: structure v1

* feat: product navigation

* stubs

* emtpy pages

* feat: home page

* autogenerate docs updated

* side bar on home page with redirects

* theme adaptation of welcome cards

* cleanup logos

* cleanup

* WIP disclosure in Python Server

* feat: add initial documentation and images for MCP server framework

* feat: add TypeScript documentation for mcp agent and mcp client + remove typescript reference from python files

* fix: update sandbox documentation for Python and TypeScript clients

- Adjusted Python sandbox documentation for clarity in API key usage.
- Added a warning in TypeScript documentation indicating that sandbox execution is not yet supported, with a reference to the Python version for users needing this functionality.

* feat: enhance TypeScript SDK documentation with new icons and structured navigation

* fix

* feat: update getting started guide

* refactor: remove sandboxed execution section from TypeScript connection types documentation

- Deleted the Sandboxed Execution section, including installation instructions and characteristics.
- Updated the Choosing the Right Connection Type section to reflect the removal of sandboxed execution details.

* feat: add observability documentation for MCP agents

* feat: add comprehensive documentation for MCP server framework

* feat: expand TypeScript server documentation with new templates, prompts, and UI widgets sections

* fix

* color hover

* remove inspector

* feat: remove claude code review

* fix: broken links

---------

Co-authored-by: Luigi Pederzani <luigipederzani@gmail.com>
2025-10-20 14:35:55 +02:00

159 lines
4.1 KiB
Plaintext

---
title: "Logging"
description: "Learn how to debug and log in mcp-use"
icon: "bug"
---
# Logging MCP-Use
<Info>
MCP-Use provides built-in logging functionality that helps diagnose issues in your agent implementation.
</Info>
## Enabling Debug Mode
<Note>
Choose the debug method that best fits your workflow - environment variables for one-off debugging or programmatic control for conditional debugging.
</Note>
There are two primary ways to enable debug mode:
### 1. Environment Variable (Recommended for One-off Runs)
<Tabs>
<Tab title="Inline (Temporary)">
Run your script with the `DEBUG` environment variable:
```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
```
<Tip>
This sets the debug level only for that specific Python process - perfect for quick troubleshooting.
</Tip>
</Tab>
<Tab title="Persistent">
Set the environment variable in your shell:
```bash
export MCP_USE_DEBUG=1 # or 2
```
Or add it to your `.env` file:
```bash .env
MCP_USE_DEBUG=2
```
</Tab>
</Tabs>
### 2. Setting the Debug Flag Programmatically
<Note>
Programmatic control is useful for debugging specific parts of your application or conditionally enabling debug mode based on your application state.
</Note>
<CodeGroup>
```python Python
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)
```
</CodeGroup>
<Tip>
You can conditionally enable debugging based on environment or configuration:
<CodeGroup>
```python Python
import os
import mcp_use
if os.getenv("ENVIRONMENT") == "development":
mcp_use.set_debug(2)
```
</CodeGroup>
</Tip>
## Debug Levels
<CardGroup cols={3}>
<Card title="Level 0 - Normal" icon="volume-off">
**Minimal Output**
Only WARNING and above messages are shown
`set_debug(0)`
</Card>
<Card title="Level 1 - Info" icon="volume-low">
**Default Operation**
Shows INFO level messages and tool calls - useful for basic operational information
`DEBUG=1` or `set_debug(1)` (default)
</Card>
<Card title="Level 2 - Full Debug" icon="volume-high">
**Maximum Verbosity**
Shows all detailed debugging information including internal operations
`DEBUG=2` or `set_debug(2)`
</Card>
</CardGroup>
## 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:
<CodeGroup>
```python 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
)
```
</CodeGroup>
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.