Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Agent ignores using tools from MCP Server? #208

@mikael-andersson91

Description

@mikael-andersson91

I am currently working on an agent that is supposed to use tools from a coded MCP server hosted in Orchestrator. The agent is supposed to take user input (in our case support tickets) and use it to cross-reference with the data it retrieved with its tools.

However, it seems that the agent always ignores all of its tools at its disposal, except for the structured output I want it to put its final results into.

As far as I can see, I am following the official LangChain guidelines for combining tool usage with structured output. See documentation here.

Have I made a mistake in how tools are loaded from the MCP Server, or is this a bug in how with_structured_output works with UiPathChat chat models?

Code

import logging
import os

from dotenv import load_dotenv
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_mcp_adapters.tools import load_mcp_tools
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
from uipath_langchain.chat.models import UiPathChat

from src.models.analysis_result import AnalysisResult
from src.models.state import State
from src.prompts import sys_message

load_dotenv()

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


async def investigation_node(state: State) -> State:
    """Extract user requirements using an LLM and generate SQL.

    Args:
        state (State): The current state of the agent.

    Returns:
        State: The updated state after running the function.

    """
    logger.info("Starting investigation_node")
    # Get environment variables at runtime, not import time
    bq_mcp_server_url = os.getenv("BQ_MCP_SERVER_URL")
    logger.info("bq_mcp_server_url: %s", bq_mcp_server_url)
    async with streamablehttp_client(
        url=bq_mcp_server_url,
        headers={
            "Authorization": f"Bearer {os.getenv('UIPATH_ACCESS_TOKEN')}",
        },
        timeout=60,
    ) as transport:
        read_stream, write_stream, _get_session_id = transport
        async with ClientSession(read_stream, write_stream) as session:
            await session.initialize()
            tools = await load_mcp_tools(session)
            logger.info(
                "Loaded tools %s from MCP",
                ", ".join(tool.name for tool in tools),
            )
            llm = UiPathChat(
                model="gpt-4o-2024-11-20",
                temperature=0.3,
                max_tokens=1024,
            )

            llm_with_tools = llm.bind_tools(tools)
            structured_llm = llm_with_tools.with_structured_output(
                AnalysisResult,
            )

            incident_prompt = f"""
Investigate this incident by using the available tools to gather information:
{state.incident.model_dump_json()}

Use the tools to:
1. Find all processes that have been executed
2. Search for relevant error logs
3. Look for queue items related to this incident
4. Check for any processes that might be involved
5. Execute relevant BigQuery queries to get more details

Provide a comprehensive analysis of what you found.
"""

            messages = [
                SystemMessage(content=sys_message),
                HumanMessage(content=incident_prompt),
            ]

            response = await structured_llm.ainvoke(
                messages,
            )

            state.analysis_result = response

            return state

Logs

In the logs I can see that the tools have been successfully loaded from the MCP Server:

[2025-10-08T13:18:20.870][Info ] bq_mcp_server_url: https://cloud.uipath.com/ingka/GlobalTest/agenthub_/mcp/32630e3d-722e-4a62-8aa1-b2b611ef835b/opsdata
[2025-10-08T13:18:22.775][Info ] HTTP Request: POST https://cloud.uipath.com/a59380c6-8519-4e7f-8014-7d4773a448dc/bf216af0-8130-4df8-8d31-181510e3f6ea/llmopstenant_/api/Traces/spans?traceId=d0896290-3c17-4478-8db9-095aa175e0bd&source=Robots "HTTP/1.1 200 OK"
[2025-10-08T13:18:50.447][Info ] HTTP Request: POST https://cloud.uipath.com/ingka/GlobalTest/agenthub_/mcp/32630e3d-722e-4a62-8aa1-b2b611ef835b/opsdata "HTTP/1.1 200 OK"
[2025-10-08T13:18:50.447][Info ] Received session ID: 256f5f79-597b-4bc1-ad8a-3b7b6cfa8b31
[2025-10-08T13:18:50.447][Info ] Negotiated protocol version: 2025-06-18
[2025-10-08T13:18:50.447][Info ] HTTP Request: GET https://cloud.uipath.com/ingka/GlobalTest/agenthub_/mcp/***/opsdata "HTTP/1.1 405 Method Not Allowed"
[2025-10-08T13:18:50.746][Info ] HTTP Request: POST https://cloud.uipath.com/ingka/GlobalTest/agenthub_/mcp/****/opsdata "HTTP/1.1 202 Accepted"
[2025-10-08T13:18:51.153][Info ] HTTP Request: POST https://cloud.uipath.com/ingka/GlobalTest/agenthub_/mcp/***/opsdata "HTTP/1.1 200 OK"
[2025-10-08T13:18:51.153][Info ] Loaded tools get_processes, get_table_schemas, execute_bigquery, get_error_logs, search_logs_by_reference, get_queue_items, search_queue_items_by_reference from MCP
[2025-10-08T13:18:52.158][Info ] HTTP Request: GET https://cloud.uipath.com/****/orchestrator_/llm/api/capabilities "HTTP/1.1 500 Internal Server Error"
[2025-10-08T13:18:52.263][Info ] HTTP Request: GET https://cloud.uipath.com/a59380c6-8519-4e7f-8014-7d4773a448dc/bf216af0-8130-4df8-8d31-181510e3f6ea/agenthub_/llm/api/capabilities "HTTP/1.1 200 OK"
[2025-10-08T13:18:54.168][Info ] HTTP Request: POST https://cloud.uipath.com/**/agenthub_/llm/api/chat/completions "HTTP/1.1 200 OK"
[2025-10-08T13:18:54.369][Info ] HTTP Request: DELETE https://cloud.uipath.com/ingka/GlobalTest/agenthub_/mcp/*/opsdata "HTTP/1.1 204 No Content"
[2025-10-08T13:18:54.369][Error] /tmp/home/.job-data/c959082f-e998-4100-cc80-0b48cb6f73ef/.venv/lib/python3.13/site-packages/pydantic/main.py:464: UserWarning: Pydantic serializer warnings:
[2025-10-08T13:18:54.369][Error]   PydanticSerializationUnexpectedValue(Expected `AnalysisResult` - serialized value may not be as expected [field_name='analysis_result', input_value={'work_notes': 'Starting ...executed_queries': None}, input_type=dict])
[2025-10-08T13:18:54.369][Error]   return self.__pydantic_serializer__.to_python(
[2025-10-08T13:18:54.793][Info ] ✓  Successful execution.
[2025-10-08T13:18:54.866][Info ] HTTP Request: POST https://cloud.uipath.com/*/llmopstenant_/api/Traces/spans?traceId=*&source=Robots "HTTP/1.1 200 OK"

Trace in Orchestrator

On traces I see no indication of tool usage, except for transforming into structured output:

Image

Tools available from MCP server

The MCP server was available at the time, with the tools seen in the screenshot below. And as can be seen in logs, all tools were retrieved from the server.
**[2025-10-08T13:18:51.153][Info ] Loaded tools get_processes, get_table_schemas, execute_bigquery, get_error_logs, search_logs_by_reference, get_queue_items, search_queue_items_by_reference from MCP
**

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions