mcp-use 1.2.6__tar.gz → 1.2.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of mcp-use might be problematic. Click here for more details.

Files changed (82) hide show
  1. {mcp_use-1.2.6 → mcp_use-1.2.8}/.github/workflows/tests.yml +1 -1
  2. {mcp_use-1.2.6 → mcp_use-1.2.8}/.pre-commit-config.yaml +2 -2
  3. mcp_use-1.2.8/CONTRIBUTING.md +146 -0
  4. {mcp_use-1.2.6 → mcp_use-1.2.8}/PKG-INFO +12 -10
  5. {mcp_use-1.2.6 → mcp_use-1.2.8}/README.md +8 -8
  6. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/docs.json +1 -0
  7. mcp_use-1.2.8/docs/essentials/server-manager.mdx +342 -0
  8. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/adapters/langchain_adapter.py +3 -0
  9. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/agents/__init__.py +0 -2
  10. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/agents/mcpagent.py +4 -7
  11. mcp_use-1.2.8/mcp_use/managers/__init__.py +21 -0
  12. mcp_use-1.2.8/mcp_use/managers/server_manager.py +101 -0
  13. mcp_use-1.2.8/mcp_use/managers/tools/__init__.py +17 -0
  14. mcp_use-1.2.8/mcp_use/managers/tools/base_tool.py +19 -0
  15. mcp_use-1.2.8/mcp_use/managers/tools/connect_server.py +69 -0
  16. mcp_use-1.2.8/mcp_use/managers/tools/disconnect_server.py +45 -0
  17. mcp_use-1.2.8/mcp_use/managers/tools/get_active_server.py +32 -0
  18. mcp_use-1.2.8/mcp_use/managers/tools/list_servers_tool.py +52 -0
  19. mcp_use-1.2.8/mcp_use/managers/tools/search_tools.py +303 -0
  20. mcp_use-1.2.8/mcp_use/managers/tools/use_tool.py +167 -0
  21. {mcp_use-1.2.6 → mcp_use-1.2.8}/pyproject.toml +5 -29
  22. mcp_use-1.2.8/ruff.toml +25 -0
  23. mcp_use-1.2.6/mcp_use/agents/server_manager.py +0 -282
  24. {mcp_use-1.2.6 → mcp_use-1.2.8}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
  25. {mcp_use-1.2.6 → mcp_use-1.2.8}/.github/pull_request_template.md +0 -0
  26. {mcp_use-1.2.6 → mcp_use-1.2.8}/.github/workflows/publish.yml +0 -0
  27. {mcp_use-1.2.6 → mcp_use-1.2.8}/.gitignore +0 -0
  28. {mcp_use-1.2.6 → mcp_use-1.2.8}/LICENSE +0 -0
  29. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/README.md +0 -0
  30. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/api-reference/introduction.mdx +0 -0
  31. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/building-custom-agents.mdx +0 -0
  32. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/development.mdx +0 -0
  33. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/essentials/configuration.mdx +0 -0
  34. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/essentials/connection-types.mdx +0 -0
  35. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/essentials/debugging.mdx +0 -0
  36. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/essentials/llm-integration.mdx +0 -0
  37. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/favicon.svg +0 -0
  38. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/images/hero-dark.png +0 -0
  39. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/images/hero-light.png +0 -0
  40. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/introduction.mdx +0 -0
  41. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/logo/dark.svg +0 -0
  42. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/logo/light.svg +0 -0
  43. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/quickstart.mdx +0 -0
  44. {mcp_use-1.2.6 → mcp_use-1.2.8}/docs/snippets/snippet-intro.mdx +0 -0
  45. {mcp_use-1.2.6 → mcp_use-1.2.8}/examples/airbnb_mcp.json +0 -0
  46. {mcp_use-1.2.6 → mcp_use-1.2.8}/examples/airbnb_use.py +0 -0
  47. {mcp_use-1.2.6 → mcp_use-1.2.8}/examples/blender_use.py +0 -0
  48. {mcp_use-1.2.6 → mcp_use-1.2.8}/examples/browser_mcp.json +0 -0
  49. {mcp_use-1.2.6 → mcp_use-1.2.8}/examples/browser_use.py +0 -0
  50. {mcp_use-1.2.6 → mcp_use-1.2.8}/examples/chat_example.py +0 -0
  51. {mcp_use-1.2.6 → mcp_use-1.2.8}/examples/filesystem_use.py +0 -0
  52. {mcp_use-1.2.6 → mcp_use-1.2.8}/examples/http_example.py +0 -0
  53. {mcp_use-1.2.6 → mcp_use-1.2.8}/examples/multi_server_example.py +0 -0
  54. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/__init__.py +0 -0
  55. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/adapters/__init__.py +0 -0
  56. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/adapters/base.py +0 -0
  57. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/agents/base.py +0 -0
  58. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/agents/prompts/system_prompt_builder.py +0 -0
  59. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/agents/prompts/templates.py +0 -0
  60. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/client.py +0 -0
  61. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/config.py +0 -0
  62. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/connectors/__init__.py +0 -0
  63. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/connectors/base.py +0 -0
  64. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/connectors/http.py +0 -0
  65. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/connectors/stdio.py +0 -0
  66. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/connectors/websocket.py +0 -0
  67. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/logging.py +0 -0
  68. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/session.py +0 -0
  69. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/task_managers/__init__.py +0 -0
  70. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/task_managers/base.py +0 -0
  71. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/task_managers/sse.py +0 -0
  72. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/task_managers/stdio.py +0 -0
  73. {mcp_use-1.2.6 → mcp_use-1.2.8}/mcp_use/task_managers/websocket.py +0 -0
  74. {mcp_use-1.2.6 → mcp_use-1.2.8}/pytest.ini +0 -0
  75. {mcp_use-1.2.6 → mcp_use-1.2.8}/static/image.jpg +0 -0
  76. {mcp_use-1.2.6 → mcp_use-1.2.8}/tests/conftest.py +0 -0
  77. {mcp_use-1.2.6 → mcp_use-1.2.8}/tests/unit/test_client.py +0 -0
  78. {mcp_use-1.2.6 → mcp_use-1.2.8}/tests/unit/test_config.py +0 -0
  79. {mcp_use-1.2.6 → mcp_use-1.2.8}/tests/unit/test_http_connector.py +0 -0
  80. {mcp_use-1.2.6 → mcp_use-1.2.8}/tests/unit/test_logging.py +0 -0
  81. {mcp_use-1.2.6 → mcp_use-1.2.8}/tests/unit/test_session.py +0 -0
  82. {mcp_use-1.2.6 → mcp_use-1.2.8}/tests/unit/test_stdio_connector.py +0 -0
@@ -22,7 +22,7 @@ jobs:
22
22
  - name: Install dependencies
23
23
  run: |
24
24
  python -m pip install --upgrade pip
25
- pip install .[dev,anthropic,openai]
25
+ pip install .[dev,anthropic,openai,search]
26
26
  - name: Lint with ruff
27
27
  run: |
28
28
  ruff check .
@@ -4,10 +4,10 @@ repos:
4
4
  rev: v0.3.2
5
5
  hooks:
6
6
  - id: ruff
7
- args: [--fix, --exit-non-zero-on-fix, --config=pyproject.toml]
7
+ args: [--fix, --exit-non-zero-on-fix, --config=ruff.toml]
8
8
  types: [python]
9
9
  - id: ruff-format
10
- args: [--config=pyproject.toml]
10
+ args: [--config=ruff.toml]
11
11
  types: [python]
12
12
 
13
13
  - repo: https://github.com/pre-commit/pre-commit-hooks
@@ -0,0 +1,146 @@
1
+ # Contributing to MCP-Use
2
+
3
+ Thank you for your interest in contributing to MCP-Use! This document provides guidelines and instructions for contributing to this project.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Getting Started](#getting-started)
8
+ - [Development Environment](#development-environment)
9
+ - [Installation from Source](#installation-from-source)
10
+ - [Development Workflow](#development-workflow)
11
+ - [Branching Strategy](#branching-strategy)
12
+ - [Commit Messages](#commit-messages)
13
+ - [Code Style](#code-style)
14
+ - [Pre-commit Hooks](#pre-commit-hooks)
15
+ - [Testing](#testing)
16
+ - [Running Tests](#running-tests)
17
+ - [Adding Tests](#adding-tests)
18
+ - [Pull Requests](#pull-requests)
19
+ - [Creating a Pull Request](#creating-a-pull-request)
20
+ - [Pull Request Template](#pull-request-template)
21
+ - [Documentation](#documentation)
22
+ - [Release Process](#release-process)
23
+ - [Getting Help](#getting-help)
24
+
25
+ ## Getting Started
26
+
27
+ ### Development Environment
28
+
29
+ MCP-Use requires:
30
+ - Python 3.11 or later
31
+
32
+ ### Installation from Source
33
+
34
+ 1. Fork the repository on GitHub.
35
+ 2. Clone your fork locally:
36
+ ```bash
37
+ git clone https://github.com/YOUR_USERNAME/mcp-use.git
38
+ cd mcp-use
39
+ ```
40
+ 3. Install the package in development mode:
41
+ ```bash
42
+ pip install -e ".[dev,search]"
43
+ ```
44
+ 4. Set up pre-commit hooks:
45
+ ```bash
46
+ pip install pre-commit
47
+ pre-commit install
48
+ ```
49
+
50
+ ## Development Workflow
51
+
52
+ ### Branching Strategy
53
+
54
+ - `main` branch contains the latest stable code
55
+ - Create feature branches from `main` named according to the feature you're implementing: `feature/your-feature-name`
56
+ - For bug fixes, use: `fix/bug-description`
57
+
58
+ ### Commit Messages
59
+
60
+ For now no commit style is enforced, try to keep your commit messages informational.
61
+ ### Code Style
62
+
63
+ We use [Ruff](https://github.com/astral-sh/ruff) for code formatting and linting. The configuration is in `ruff.toml`.
64
+
65
+ Key style guidelines:
66
+ - Line length: 100 characters
67
+ - Use double quotes for strings
68
+ - Follow PEP 8 naming conventions
69
+ - Add type hints to function signatures
70
+
71
+ ### Pre-commit Hooks
72
+
73
+ We use pre-commit hooks to ensure code quality before committing. The configuration is in `.pre-commit-config.yaml`.
74
+
75
+ The hooks will:
76
+ - Format code using Ruff
77
+ - Run linting checks
78
+ - Check for trailing whitespace and fix it
79
+ - Ensure files end with a newline
80
+ - Validate YAML files
81
+ - Check for large files
82
+ - Remove debug statements
83
+
84
+ ## Testing
85
+
86
+ ### Running Tests
87
+
88
+ Run the test suite with pytest:
89
+
90
+ ```bash
91
+ pytest
92
+ ```
93
+
94
+ To run specific test categories:
95
+
96
+ ```bash
97
+ pytest tests/
98
+ ```
99
+
100
+ ### Adding Tests
101
+
102
+ - Add unit tests for new functionality in `tests/unit/`
103
+ - For slow or network-dependent tests, mark them with `@pytest.mark.slow` or `@pytest.mark.integration`
104
+ - Aim for high test coverage of new code
105
+
106
+ ## Pull Requests
107
+
108
+ ### Creating a Pull Request
109
+
110
+ 1. Ensure your code passes all tests and pre-commit hooks
111
+ 2. Push your changes to your fork
112
+ 3. Submit a pull request to the main repository
113
+ 4. Follow the pull request template
114
+
115
+ ## Documentation
116
+
117
+ - Update docstrings for new or modified functions, classes, and methods
118
+ - Use Google-style docstrings:
119
+ ```python
120
+ def function_name(param1: type, param2: type) -> return_type:
121
+ """Short description.
122
+
123
+ Longer description if needed.
124
+
125
+ Args:
126
+ param1: Description of param1
127
+ param2: Description of param2
128
+
129
+ Returns:
130
+ Description of return value
131
+
132
+ Raises:
133
+ ExceptionType: When and why this exception is raised
134
+ """
135
+ ```
136
+ - Update README.md for user-facing changes
137
+
138
+ ## Getting Help
139
+
140
+ If you need help with your contribution:
141
+
142
+ - Open an issue for discussion
143
+ - Reach out to the maintainers
144
+ - Check existing code for examples
145
+
146
+ Thank you for contributing to MCP-Use!
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mcp-use
3
- Version: 1.2.6
3
+ Version: 1.2.8
4
4
  Summary: MCP Library for LLMs
5
5
  Author-email: Pietro Zullo <pietro.zullo@gmail.com>
6
6
  License: MIT
@@ -18,7 +18,7 @@ Requires-Dist: aiohttp>=3.9.0
18
18
  Requires-Dist: jsonschema-pydantic>=0.1.0
19
19
  Requires-Dist: langchain-community>=0.0.10
20
20
  Requires-Dist: langchain>=0.1.0
21
- Requires-Dist: mcp
21
+ Requires-Dist: mcp>=1.5.0
22
22
  Requires-Dist: pydantic>=2.0.0
23
23
  Requires-Dist: python-dotenv>=1.0.0
24
24
  Requires-Dist: typing-extensions>=4.8.0
@@ -35,6 +35,8 @@ Requires-Dist: pytest>=7.4.0; extra == 'dev'
35
35
  Requires-Dist: ruff>=0.1.0; extra == 'dev'
36
36
  Provides-Extra: openai
37
37
  Requires-Dist: openai>=1.10.0; extra == 'openai'
38
+ Provides-Extra: search
39
+ Requires-Dist: fastembed>=0.0.1; extra == 'search'
38
40
  Description-Content-Type: text/markdown
39
41
 
40
42
  <picture>
@@ -63,13 +65,13 @@ Description-Content-Type: text/markdown
63
65
 
64
66
  | Feature | Description |
65
67
  |---------|-------------|
66
- | 🔄 **Ease of use** | Create your first MCP capable agent you need only 6 lines of code |
67
- | 🤖 **LLM Flexibility** | Works with any langchain supported LLM that supports tool calling (OpenAI, Anthropic, Groq, LLama etc.) |
68
- | 🌐 **HTTP Support** | Direct connection to MCP servers running on specific HTTP ports |
69
- | ⚙️ **Dynamic Server Selection** | Agents can dynamically choose the most appropriate MCP server for a given task from the available pool |
70
- | 🧩 **Multi-Server Support** | Use multiple MCP servers simultaneously in a single agent |
71
- | 🛡️ **Tool Restrictions** | Restrict potentially dangerous tools like file system or network access |
72
- | 🔧 **Custom Agents** | Build your own agents with any framework using the LangChain adapter or create new adapters |
68
+ | 🔄 [**Ease of use**](#quick-start) | Create your first MCP capable agent you need only 6 lines of code |
69
+ | 🤖 [**LLM Flexibility**](#installing-langchain-providers) | Works with any langchain supported LLM that supports tool calling (OpenAI, Anthropic, Groq, LLama etc.) |
70
+ | 🌐 [**HTTP Support**](#http-connection-example) | Direct connection to MCP servers running on specific HTTP ports |
71
+ | ⚙️ [**Dynamic Server Selection**](#dynamic-server-selection-server-manager) | Agents can dynamically choose the most appropriate MCP server for a given task from the available pool |
72
+ | 🧩 [**Multi-Server Support**](#multi-server-support) | Use multiple MCP servers simultaneously in a single agent |
73
+ | 🛡️ [**Tool Restrictions**](#tool-access-control) | Restrict potentially dangerous tools like file system or network access |
74
+ | 🔧 [**Custom Agents**](#build-a-custom-agent) | Build your own agents with any framework using the LangChain adapter or create new adapters |
73
75
 
74
76
 
75
77
  # Quick start
@@ -610,7 +612,7 @@ This is useful when you only need to see the agent's steps and decision-making p
610
612
 
611
613
  # Contributing
612
614
 
613
- We love contributions! Feel free to open issues for bugs or feature requests.
615
+ We love contributions! Feel free to open issues for bugs or feature requests. Look at [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
614
616
 
615
617
  # Requirements
616
618
 
@@ -24,13 +24,13 @@
24
24
 
25
25
  | Feature | Description |
26
26
  |---------|-------------|
27
- | 🔄 **Ease of use** | Create your first MCP capable agent you need only 6 lines of code |
28
- | 🤖 **LLM Flexibility** | Works with any langchain supported LLM that supports tool calling (OpenAI, Anthropic, Groq, LLama etc.) |
29
- | 🌐 **HTTP Support** | Direct connection to MCP servers running on specific HTTP ports |
30
- | ⚙️ **Dynamic Server Selection** | Agents can dynamically choose the most appropriate MCP server for a given task from the available pool |
31
- | 🧩 **Multi-Server Support** | Use multiple MCP servers simultaneously in a single agent |
32
- | 🛡️ **Tool Restrictions** | Restrict potentially dangerous tools like file system or network access |
33
- | 🔧 **Custom Agents** | Build your own agents with any framework using the LangChain adapter or create new adapters |
27
+ | 🔄 [**Ease of use**](#quick-start) | Create your first MCP capable agent you need only 6 lines of code |
28
+ | 🤖 [**LLM Flexibility**](#installing-langchain-providers) | Works with any langchain supported LLM that supports tool calling (OpenAI, Anthropic, Groq, LLama etc.) |
29
+ | 🌐 [**HTTP Support**](#http-connection-example) | Direct connection to MCP servers running on specific HTTP ports |
30
+ | ⚙️ [**Dynamic Server Selection**](#dynamic-server-selection-server-manager) | Agents can dynamically choose the most appropriate MCP server for a given task from the available pool |
31
+ | 🧩 [**Multi-Server Support**](#multi-server-support) | Use multiple MCP servers simultaneously in a single agent |
32
+ | 🛡️ [**Tool Restrictions**](#tool-access-control) | Restrict potentially dangerous tools like file system or network access |
33
+ | 🔧 [**Custom Agents**](#build-a-custom-agent) | Build your own agents with any framework using the LangChain adapter or create new adapters |
34
34
 
35
35
 
36
36
  # Quick start
@@ -571,7 +571,7 @@ This is useful when you only need to see the agent's steps and decision-making p
571
571
 
572
572
  # Contributing
573
573
 
574
- We love contributions! Feel free to open issues for bugs or feature requests.
574
+ We love contributions! Feel free to open issues for bugs or feature requests. Look at [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
575
575
 
576
576
  # Requirements
577
577
 
@@ -27,6 +27,7 @@
27
27
  "essentials/llm-integration",
28
28
  "essentials/debugging",
29
29
  "essentials/connection-types",
30
+ "essentials/server-manager",
30
31
  "building-custom-agents"
31
32
  ]
32
33
  },
@@ -0,0 +1,342 @@
1
+ ---
2
+ title: 'Server Manager'
3
+ description: 'Intelligent management of multiple MCP servers and tool discovery'
4
+ ---
5
+
6
+ # Server Manager
7
+
8
+ The Server Manager is a powerful feature that helps manage connections to multiple MCP servers efficiently and enables tool discovery across servers. When your agent has access to tools from different servers, the Server Manager intelligently selects the appropriate server and helps discover the most relevant tools for each task.
9
+
10
+ ## Overview
11
+
12
+ When building agents that need to interact with multiple MCP servers (such as combining web browsing with file operations or 3D modeling), the Server Manager:
13
+
14
+ - Dynamically connects to the appropriate server based on the task at hand
15
+ - Provides tool discovery and search capabilities across all configured servers
16
+ - Minimizes unnecessary connections by only connecting to servers when needed
17
+ - Reduces agent confusion by organizing tool sets appropriately
18
+ - Improves overall performance and efficiency of multi-server operations
19
+
20
+ ## Enabling the Server Manager
21
+
22
+ The Server Manager can be enabled by setting `use_server_manager=True` during the initialization of an `MCPAgent`:
23
+
24
+ ```python
25
+ from mcp_use import MCPClient, MCPAgent
26
+ from langchain_openai import ChatOpenAI
27
+
28
+ # Create client with multiple servers
29
+ client = MCPClient.from_config_file("multi_server_config.json")
30
+
31
+ # Create agent with the Server Manager enabled
32
+ agent = MCPAgent(
33
+ llm=ChatOpenAI(model="gpt-4o"),
34
+ client=client,
35
+ use_server_manager=True # Enable the Server Manager
36
+ )
37
+
38
+ # The agent will now intelligently manage server connections
39
+ ```
40
+
41
+ ## Server Management Tools
42
+
43
+ When the Server Manager is enabled, it provides the following management tools to the agent:
44
+
45
+ 1. **list_mcp_servers** - Lists all available MCP servers and their tools
46
+ 2. **connect_to_mcp_server** - Connects to a specific MCP server
47
+ 3. **get_active_mcp_server** - Shows which server is currently active
48
+ 4. **disconnect_from_mcp_server** - Disconnects from the current server
49
+ 5. **search_mcp_tools** - Searches for tools across all MCP servers
50
+ 6. **use_tool_from_server** - Uses a specific tool from a specific server without switching the active connection
51
+
52
+ These tools allow the agent to discover available servers, connect to the appropriate server for a given task, search for specific tools, and manage server connections throughout its operation.
53
+
54
+ ## Tool Discovery and Search
55
+
56
+ ### Listing Available Servers and Their Tools
57
+
58
+ The `list_mcp_servers` tool provides a comprehensive overview of all available servers and their tools:
59
+
60
+ ```python
61
+ # Example of using the Server Manager for listing servers and tools
62
+ result = await agent.run(
63
+ "What tools do I have available? List all servers and their tools."
64
+ )
65
+ ```
66
+
67
+ This will return output similar to:
68
+
69
+ ```
70
+ Available MCP servers:
71
+ 1. playwright
72
+ Tools: navigate_browser, click_element, get_page_content, ...
73
+ 2. file_system
74
+ Tools: read_file, write_file, list_directory, ...
75
+ ```
76
+
77
+ This allows the agent to understand which tools are available on each server before deciding which one to connect to.
78
+
79
+ ### Searching for Tools Across Servers
80
+
81
+ The `search_mcp_tools` tool is especially powerful, allowing agents to search for specific tools across all servers based on functionality needed:
82
+
83
+ ```python
84
+ # Example of searching for tools across servers
85
+ result = await agent.run(
86
+ "I need to find tools that can help me navigate a website. Can you search for relevant tools?"
87
+ )
88
+ ```
89
+
90
+ This will search across all servers and return a ranked list of tools that match the query:
91
+
92
+ ```
93
+ Search results for: 'navigate website'
94
+
95
+ [1] Tool: navigate_browser (95.2% match)
96
+ Server: playwright
97
+ Description: Navigates to a specified URL in a web browser
98
+
99
+ [2] Tool: click_element (87.4% match)
100
+ Server: playwright
101
+ Description: Clicks on an element on the page identified by a selector
102
+
103
+ [3] Tool: get_page_content (82.1% match)
104
+ Server: playwright
105
+ Description: Retrieves the HTML content of the current page
106
+
107
+ ...
108
+
109
+ To use a tool, connect to the appropriate server first, then invoke the tool.
110
+ ```
111
+
112
+ This search functionality helps agents quickly find the most relevant tools for their tasks without needing to manually inspect all available tools across all servers.
113
+
114
+ ### Direct Tool Execution Across Servers
115
+
116
+ The `use_tool_from_server` tool enables direct execution of a tool from a specific server without explicitly connecting to it first:
117
+
118
+ ```python
119
+ # Example of using a tool from a specific server without switching context
120
+ result = await agent.run(
121
+ "I need to navigate to https://example.com but I don't want to disrupt my current server connection. Can you use the navigate_browser tool from the playwright server directly?"
122
+ )
123
+ ```
124
+
125
+ This is especially useful for one-off operations where you don't want to disconnect from the current server just to perform a single action on another server.
126
+
127
+ ### Detailed Tool Information
128
+
129
+ When an agent connects to a server, the Server Manager provides detailed descriptions of all available tools:
130
+
131
+ ```
132
+ Connected to MCP server 'playwright'. 25 tools are now available.
133
+
134
+ Available tools for this server:
135
+ 1. navigate_browser: Navigates to a specified URL in a web browser
136
+ 2. click_element: Clicks on an element on the page identified by a selector
137
+ 3. get_page_content: Retrieves the HTML content of the current page
138
+ ...
139
+ ```
140
+
141
+ This detailed information helps the agent understand the purpose and capabilities of each tool, enabling it to make better decisions about which tool to use for a specific task.
142
+
143
+ ## Benefits of Using the Server Manager
144
+
145
+ ### 1. Reduced Connection Overhead
146
+
147
+ Without the Server Manager, the agent might connect to all configured servers at once, even if only one server is needed for a particular task. The Server Manager ensures that only the necessary connections are established.
148
+
149
+ ### 2. Improved Tool Selection
150
+
151
+ When an agent has access to dozens or even hundreds of tools from different servers, it can be challenging for the LLM to select the right tool for a specific task. The Server Manager helps organize tools by server and provides search capabilities, making it easier for the LLM to make appropriate selections.
152
+
153
+ ### 3. Efficient Resource Management
154
+
155
+ The Server Manager monitors server usage and can close inactive connections, freeing up resources when they're no longer needed.
156
+
157
+ ### 4. Reduced Cognitive Load
158
+
159
+ Agents don't need to memorize all available tools across all servers; they can discover and search for them at runtime.
160
+
161
+ ### 5. Dynamic Tool Selection
162
+
163
+ Agents can adapt to available tools at runtime, connecting to the appropriate server for each task.
164
+
165
+ ### 6. Cross-Server Tool Usage
166
+
167
+ The `use_tool_from_server` capability allows agents to use tools from different servers without disrupting their current workflow or connection state.
168
+
169
+ ## Example: Multi-Server Agent with Server Manager
170
+
171
+ ```python
172
+ import asyncio
173
+ from mcp_use import MCPClient, MCPAgent
174
+ from langchain_anthropic import ChatAnthropic
175
+
176
+ async def main():
177
+ # Create client with multiple servers
178
+ client = MCPClient.from_dict({
179
+ "mcpServers": {
180
+ "playwright": {
181
+ "command": "npx",
182
+ "args": ["@playwright/mcp@latest"],
183
+ "env": {"DISPLAY": ":1"}
184
+ },
185
+ "database": {
186
+ "command": "npx",
187
+ "args": ["@database/mcp-server"]
188
+ },
189
+ "file_system": {
190
+ "command": "npx",
191
+ "args": ["@file/mcp-server"]
192
+ }
193
+ }
194
+ })
195
+
196
+ # Create agent with the Server Manager
197
+ agent = MCPAgent(
198
+ llm=ChatAnthropic(model="claude-3-5-sonnet-20240620"),
199
+ client=client,
200
+ use_server_manager=True # Enable the Server Manager
201
+ )
202
+
203
+ try:
204
+ # Run a complex task requiring tools from multiple servers
205
+ result = await agent.run(
206
+ """Task: Extract data from a website, save it to a file, and then load it into a database.
207
+
208
+ Start by figuring out which tools you need and which servers provide them,
209
+ then complete the task step by step."""
210
+ )
211
+ print(result)
212
+ finally:
213
+ # Clean up all sessions
214
+ await client.close_all_sessions()
215
+
216
+ if __name__ == "__main__":
217
+ asyncio.run(main())
218
+ ```
219
+
220
+ In this example, the agent will:
221
+ 1. Use `list_mcp_servers` or `search_mcp_tools` to discover available tools across all servers
222
+ 2. Connect to the appropriate server for each step of the task
223
+ 3. Use the most suitable tools to complete each part of the workflow
224
+ 4. Optionally use `use_tool_from_server` for one-off operations on other servers
225
+
226
+ ## How the Server Manager Works
227
+
228
+ Internally, the Server Manager operates through these key mechanisms:
229
+
230
+ 1. **Tool Organization**: Server Manager maintains a registry of which tools belong to which servers
231
+ 2. **Server Management**: It manages connections to servers, only connecting when necessary
232
+ 3. **Tool Search**: It provides search capabilities across all available tools from all servers
233
+ 4. **Tool Updates**: It updates the available tools based on the currently active server
234
+ 5. **Connection Lifecycle**: It handles connecting and disconnecting from servers as needed
235
+ 6. **Cross-Server Operations**: It enables using tools from non-active servers when needed
236
+
237
+ ## Advanced Usage Examples
238
+
239
+ ### Example 1: Tool Search Workflow
240
+
241
+ ```python
242
+ # The agent searches for tools that can help with a specific task
243
+ result = await agent.run(
244
+ """I need to perform the following tasks:
245
+ 1. Navigate to a website
246
+ 2. Take a screenshot of the page
247
+ 3. Save the screenshot to a file
248
+
249
+ First, search for tools that could help me with each of these tasks."""
250
+ )
251
+ ```
252
+
253
+ The agent will use the `search_mcp_tools` tool to find relevant tools for each part of the task, which might return results like:
254
+
255
+ ```
256
+ [1] Tool: navigate_browser (96.5% match)
257
+ Server: playwright
258
+ Description: Navigates to a specified URL in a web browser
259
+
260
+ [2] Tool: take_screenshot (95.1% match)
261
+ Server: playwright
262
+ Description: Captures a screenshot of the current page
263
+
264
+ [3] Tool: write_file (92.8% match)
265
+ Server: file_system
266
+ Description: Writes data to a file at the specified path
267
+ ```
268
+
269
+ ### Example 2: Cross-Server Tool Usage
270
+
271
+ ```python
272
+ # The agent is connected to the database server, but needs to use a web tool
273
+ result = await agent.run(
274
+ """I'm currently analyzing database data, but I need to quickly check a website
275
+ for reference information. Can you help me navigate to https://example.com without
276
+ disrupting my database connection?"""
277
+ )
278
+ ```
279
+
280
+ The agent will use the `use_tool_from_server` tool to invoke the web navigation tool directly:
281
+
282
+ ```python
283
+ # The agent invokes use_tool_from_server
284
+ "I'll help you check the website without disrupting your database connection. I'll use the navigate_browser tool from the playwright server directly."
285
+
286
+ # Tool execution
287
+ tool_result = await use_tool_from_server(
288
+ server_name="playwright",
289
+ tool_name="navigate_browser",
290
+ tool_input={"url": "https://example.com"}
291
+ )
292
+
293
+ # After the tool execution, the agent remains connected to the database server
294
+ ```
295
+
296
+ ## Using Server Manager in Custom Implementations
297
+
298
+ If you're building a custom agent, you can also leverage the Server Manager directly:
299
+
300
+ ```python
301
+ from mcp_use import MCPClient, ServerManager
302
+ from mcp_use.adapters.langchain_adapter import LangChainAdapter
303
+
304
+ # Create client and adapter
305
+ client = MCPClient.from_config_file("config.json")
306
+ adapter = LangChainAdapter()
307
+
308
+ # Create server manager
309
+ server_manager = ServerManager(client=client, adapter=adapter)
310
+ await server_manager.initialize()
311
+
312
+ # Get server management tools
313
+ management_tools = await server_manager.get_server_management_tools()
314
+
315
+ # List available servers
316
+ servers_info = await server_manager.list_servers()
317
+ print(servers_info)
318
+
319
+ # Connect to a specific server
320
+ connection_result = await server_manager.connect_to_server("playwright")
321
+ print(connection_result)
322
+
323
+ # Get tools from the active server
324
+ active_server_tools = await server_manager.get_active_server_tools()
325
+
326
+ # Get all tools (management + active server)
327
+ all_tools = await server_manager.get_all_tools()
328
+
329
+ # Search for tools across all servers
330
+ search_results = await server_manager.search_tools("navigate web page")
331
+ print(search_results)
332
+
333
+ # Use a tool from a specific server
334
+ result = await server_manager.use_tool_from_server(
335
+ server_name="playwright",
336
+ tool_name="navigate_browser",
337
+ tool_input={"url": "https://example.com"}
338
+ )
339
+ ```
340
+ ## Conclusion
341
+
342
+ The Server Manager is a powerful feature that enhances the capabilities of multi-server MCP agents. By intelligently managing server connections, providing comprehensive tool discovery and search functionality, and enabling cross-server tool usage, it enables more complex and efficient agent workflows that combine capabilities from different MCP servers. Agents can dynamically discover, select, and use the most appropriate tools for each task, resulting in more effective task completion and better resource utilization.
@@ -118,6 +118,9 @@ class LangChainAdapter(BaseAdapter):
118
118
  tool_connector: BaseConnector = connector # Renamed variable to avoid name conflict
119
119
  handle_tool_error: bool = True
120
120
 
121
+ def __repr__(self) -> str:
122
+ return f"MCP tool: {self.name}: {self.description}"
123
+
121
124
  def _run(self, **kwargs: Any) -> NoReturn:
122
125
  """Synchronous run method that always raises an error.
123
126
 
@@ -7,10 +7,8 @@ that are pre-configured for using MCP tools.
7
7
 
8
8
  from .base import BaseAgent
9
9
  from .mcpagent import MCPAgent
10
- from .server_manager import ServerManager
11
10
 
12
11
  __all__ = [
13
12
  "BaseAgent",
14
13
  "MCPAgent",
15
- "ServerManager",
16
14
  ]
@@ -23,9 +23,9 @@ from mcp_use.session import MCPSession
23
23
 
24
24
  from ..adapters.langchain_adapter import LangChainAdapter
25
25
  from ..logging import logger
26
+ from ..managers.server_manager import ServerManager
26
27
  from .prompts.system_prompt_builder import create_system_message
27
28
  from .prompts.templates import DEFAULT_SYSTEM_PROMPT_TEMPLATE, SERVER_MANAGER_SYSTEM_PROMPT_TEMPLATE
28
- from .server_manager import ServerManager
29
29
 
30
30
  set_debug(logger.level == logging.DEBUG)
31
31
 
@@ -42,7 +42,6 @@ class MCPAgent:
42
42
  llm: BaseLanguageModel,
43
43
  client: MCPClient | None = None,
44
44
  connectors: list[BaseConnector] | None = None,
45
- server_name: str | None = None,
46
45
  max_steps: int = 5,
47
46
  auto_initialize: bool = False,
48
47
  memory_enabled: bool = True,
@@ -59,7 +58,6 @@ class MCPAgent:
59
58
  llm: The LangChain LLM to use.
60
59
  client: The MCPClient to use. If provided, connector is ignored.
61
60
  connectors: A list of MCP connectors to use if client is not provided.
62
- server_name: The name of the server to use if client is provided.
63
61
  max_steps: The maximum number of steps to take.
64
62
  auto_initialize: Whether to automatically initialize the agent when run is called.
65
63
  memory_enabled: Whether to maintain conversation history for context.
@@ -72,7 +70,6 @@ class MCPAgent:
72
70
  self.llm = llm
73
71
  self.client = client
74
72
  self.connectors = connectors or []
75
- self.server_name = server_name
76
73
  self.max_steps = max_steps
77
74
  self.auto_initialize = auto_initialize
78
75
  self.memory_enabled = memory_enabled
@@ -114,7 +111,7 @@ class MCPAgent:
114
111
  if self.use_server_manager and self.server_manager:
115
112
  await self.server_manager.initialize()
116
113
  # Get server management tools
117
- management_tools = await self.server_manager.get_server_management_tools()
114
+ management_tools = self.server_manager.tools
118
115
  self._tools = management_tools
119
116
  logger.info(
120
117
  f"🔧 Server manager mode active with {len(management_tools)} management tools"
@@ -391,7 +388,7 @@ class MCPAgent:
391
388
  for step_num in range(steps):
392
389
  # --- Check for tool updates if using server manager ---
393
390
  if self.use_server_manager and self.server_manager:
394
- current_tools = await self.server_manager.get_all_tools()
391
+ current_tools = self.server_manager.tools
395
392
  current_tool_names = {tool.name for tool in current_tools}
396
393
  existing_tool_names = {tool.name for tool in self._tools}
397
394
 
@@ -412,7 +409,7 @@ class MCPAgent:
412
409
  [tool.name for tool in self._tools], excluded_colors=["green", "red"]
413
410
  )
414
411
 
415
- logger.info(f"🔍 Step {step_num + 1}/{steps}")
412
+ logger.info(f"👣 Step {step_num + 1}/{steps}")
416
413
 
417
414
  # --- Plan and execute the next step ---
418
415
  try: