Files
Luigi Pederzani f08e870e09 feat: Upgrade to Langchain 1.0.0 (#393)
* chore: update imports

* refactor(mcpagent): update agent creation logic and imports

- Replaced `create_tool_calling_agent` with `create_agent` for improved agent instantiation.
- Simplified the `_create_agent` method by removing the `AgentExecutor` and directly returning the created agent.
- Adjusted type annotations for `_agent_executor` to initialize as `None` instead of using a union type.

* feat(mcpagent): migrate to LangChain 1.0.0 and simplify agent methods

- Updated MCPAgent to utilize LangChain 1.0.0's `create_agent()` for improved agent instantiation.
- Introduced new methods `astream_simplified()` and `run_v2()` that leverage the built-in `astream()` for internal agent loop handling.
- Retained legacy methods `stream()` and `run()` for backward compatibility with manual execution.
- Enhanced documentation to reflect changes and provide clearer usage examples.

* feat(mcpagent): implement dynamic tool updates during agent execution

- Enhanced the MCPAgent to support dynamic tool updates when using server_manager mode.
- Implemented logic to check for tool changes before and during execution, allowing for immediate updates and safe restarts.
- Introduced a maximum restart limit to prevent infinite loops during tool updates.
- Updated documentation to reflect the new behavior and usage of dynamic tool management.

* refactor(mcpagent): clean up imports and streamline logging

- Removed unused imports and organized existing ones for better clarity in `mcpagent.py`.
- Simplified logging statements for consistency and improved readability.
- Enhanced the overall structure of the code by eliminating unnecessary whitespace.
- Updated unit tests to reflect changes in imports and ensure compatibility with the refactored code.

* feat(mcpagent): enhance agent execution with step tracking and middleware

- Introduced `ModelCallLimitMiddleware` to enforce a maximum number of model calls during agent execution.
- Updated the `_consume_and_return` method to return a tuple of the final result and the number of steps taken.
- Enhanced telemetry tracking to include steps taken and execution time for better performance insights.
- Simplified agent creation logic by integrating middleware directly into the `create_agent` function.
- Improved logging for agent execution details, including steps and tool usage.

* fix(mcpagent): add null check for node output messages extraction

- Updated the MCPAgent to include a null check for `node_output` before attempting to access the "messages" key, preventing potential errors during message extraction.

* minor

* feat(mcpagent): filter out ToolMessage from history

- Updated the MCPAgent to exclude ToolMessage instances from being added to the message history, ensuring that only relevant messages are recorded.
- This change helps maintain a cleaner history by avoiding intermediate tool execution details.

* fix(mcpagent): set max_steps assignment

* ruff

* docs

* e2e tests for agent

* key in tests

* fix manager test and split ci tests

* telemetry off for tests

* fix server manager test

* feat(mcpagent): delegate query streaming to remote agent in remote mode

- recreated functionality to the MCPAgent to yield results from a remote agent when operating in remote mode, enhancing the agent's capability to handle queries efficiently.

* fix: agent unit tests

* Print inputs

* rollback, wrong repo

* fix streaming output

* stricter streaming test

* pass observability callbacks

* fix tests mock

---------

Co-authored-by: pietrozullo <pietro.zullo@gmail.com>
2025-10-27 14:26:01 +01:00

48 lines
1.3 KiB
Python

"""
End-to-end integration test for agent.run().
Tests the agent.run() method performing calculations using MCP tools.
"""
import sys
from pathlib import Path
import pytest
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient
from mcp_use.logging import logger
@pytest.mark.asyncio
@pytest.mark.integration
async def test_agent_run():
"""Test agent.run() performing calculations using MCP tools."""
server_path = Path(__file__).parent.parent / "servers_for_testing" / "simple_server.py"
config = {"mcpServers": {"simple": {"command": sys.executable, "args": [str(server_path), "--transport", "stdio"]}}}
client = MCPClient.from_dict(config)
llm = ChatOpenAI(model="gpt-4o")
agent = MCPAgent(llm=llm, client=client, max_steps=10)
try:
query = "Use the add tool to calculate 42 + 58. Just give me the answer."
logger.info("\n" + "=" * 80)
logger.info("TEST: test_agent_run")
logger.info("=" * 80)
logger.info(f"Query: {query}")
result = await agent.run(query)
logger.info(f"Result: {result}")
logger.info(f"Tools used: {agent.tools_used_names}")
logger.info("=" * 80 + "\n")
assert "100" in result
assert len(agent.tools_used_names) > 0
assert "add" in agent.tools_used_names
finally:
await agent.close()