-
Notifications
You must be signed in to change notification settings - Fork 0
Add integration test reproducing MCP tool description omission in function logs #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
7
commits into
main
Choose a base branch
from
copilot/update-agent-instructions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a26b03b
Initial plan
Copilot 7688c03
Add integration test reproducing MCP tool descriptions issue
Copilot 0b9a440
Fix type hints in MCP tool test functions
Copilot 593f0ce
Address code review feedback on MCP tool descriptions test
Copilot 84f2a77
Move pytest.skip to beginning of test for proper skipping
Copilot c4f7e0f
Update test to reproduce exact bug: MCP tool descriptions missing fro…
Copilot 202e897
Merge branch 'main' into copilot/update-agent-instructions
g3force File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| """Integration test to verify MCP tool descriptions are included in agent instructions. | ||
|
|
||
| This test reproduces the issue described in the GitHub issue about MCP tool descriptions | ||
| not being contained in agent instructions. | ||
|
|
||
| The problem: When MCP tools are added to an agent, their descriptions should be | ||
| included in the agent's instructions so the LLM knows what each tool does. However, | ||
| currently the tool descriptions are not being added to the instructions, unlike | ||
| sub-agents which do have their descriptions added. | ||
|
|
||
| Expected behavior: | ||
| - Agent instructions should contain tool names and descriptions | ||
| - Format should be similar to sub-agent descriptions | ||
|
|
||
| Actual behavior: | ||
| - Agent instructions do not contain MCP tool descriptions | ||
| - Tools are available but LLM doesn't know what they do without explicit prompts | ||
| """ | ||
|
|
||
| from typing import Any | ||
|
|
||
| import pytest | ||
| import respx | ||
| from agenticlayer.config import McpTool | ||
| from asgi_lifespan import LifespanManager | ||
| from fastmcp import FastMCP | ||
| from pydantic import AnyHttpUrl | ||
|
|
||
| from tests.utils.helpers import create_asgi_request_handler | ||
|
|
||
|
|
||
| class TestMcpToolDescriptions: | ||
| """Tests for MCP tool descriptions in agent instructions.""" | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_mcp_tool_descriptions_missing_from_agent_instructions( | ||
| self, | ||
| app_factory: Any, | ||
| agent_factory: Any, | ||
| respx_mock: respx.MockRouter, | ||
| ) -> None: | ||
| """Test that reproduces the issue: MCP tool descriptions are not in agent instructions. | ||
|
|
||
| This test demonstrates that when MCP tools are added to an agent, their | ||
| descriptions are NOT included in the agent's instructions, making it harder | ||
| for the LLM to know what tools are available and how to use them. | ||
|
|
||
| This is the ACTUAL behavior (the bug we're documenting). | ||
| """ | ||
|
|
||
| # Given: MCP server with tools that have detailed descriptions | ||
| mcp = FastMCP("CustomerService") | ||
|
|
||
| @mcp.tool() | ||
| def get_customer_crm_data(customer_id: str) -> dict[str, Any]: | ||
| """Retrieve customer CRM data including contact info and purchase history.""" | ||
| return {"customer_id": customer_id, "name": "John Doe"} | ||
|
|
||
| @mcp.tool() | ||
| def get_all_customer_data() -> list[dict[str, str]]: | ||
| """Get a list of all customers in the system.""" | ||
| return [{"id": "1", "name": "Customer 1"}] | ||
|
|
||
| @mcp.tool() | ||
| def send_message(customer_id: str, subject: str, body: str) -> str: | ||
| """Send a personalized message to a customer via email.""" | ||
| return f"Message sent to {customer_id}" | ||
|
|
||
| @mcp.tool() | ||
| def get_insurance_products() -> list[dict[str, str]]: | ||
| """Get all available insurance products.""" | ||
| return [{"id": "prod1", "name": "Health Insurance"}] | ||
|
|
||
| @mcp.tool() | ||
| def get_product_details(product_id: str) -> dict[str, str]: | ||
| """Get detailed information about a specific insurance product.""" | ||
| return {"id": product_id, "name": "Product Details"} | ||
|
|
||
| @mcp.tool() | ||
| def get_products_by_segment(segment: str) -> list[dict[str, str]]: | ||
| """Get insurance products filtered by customer segment.""" | ||
| return [{"id": "prod1", "segment": segment}] | ||
|
|
||
| # Start MCP server | ||
| mcp_server_url = "http://test-crm-mcp.local" | ||
| mcp_app = mcp.http_app(path="/mcp") | ||
|
|
||
| async with LifespanManager(mcp_app) as mcp_manager: | ||
| # Route MCP requests through ASGI transport | ||
| handler = create_asgi_request_handler(mcp_manager.app, mcp_server_url) | ||
| respx_mock.route(host="test-crm-mcp.local").mock(side_effect=handler) | ||
|
|
||
| # When: Create agent with MCP tools | ||
| test_agent = agent_factory("customer_service_agent") | ||
| # Set a simple instruction | ||
| test_agent.instruction = "You are a customer service agent." | ||
|
|
||
| tools = [McpTool(name="crm_tools", url=AnyHttpUrl(f"{mcp_server_url}/mcp"), timeout=30)] | ||
|
|
||
| # Create the app which loads the MCP tools | ||
| async with app_factory(test_agent, tools=tools): | ||
| # Access the agent to check its instructions | ||
| # The agent is modified during app startup in agent_factory.load_agent() | ||
| # We need to get the configured agent instance | ||
|
|
||
| # ACTUAL BEHAVIOR (the bug): | ||
| # The agent instructions do NOT contain the MCP tool descriptions | ||
| agent_instructions = test_agent.instruction | ||
|
|
||
| print("\n" + "=" * 80) | ||
| print("AGENT INSTRUCTIONS:") | ||
| print("=" * 80) | ||
| print(agent_instructions) | ||
| print("=" * 80) | ||
|
|
||
| # These assertions document the CURRENT (buggy) behavior: | ||
| # The tool descriptions are NOT present in the instructions | ||
|
|
||
| # Check that the basic instruction is still there | ||
| assert "customer service agent" in agent_instructions.lower() | ||
|
|
||
| # DOCUMENT THE BUG: Tool descriptions are missing | ||
| # If this were working correctly, we would expect to see something like: | ||
| # "Following tools are available: | ||
| # - 'get_customer_crm_data': Retrieve customer CRM data... | ||
| # - 'send_message': Send a personalized message..." | ||
| # But these are NOT present currently. | ||
|
|
||
| assert "get_customer_crm_data" not in agent_instructions, ( | ||
| "Tool names should NOT be in instructions yet (this documents the bug). " | ||
| "If this assertion fails, the issue might be fixed!" | ||
| ) | ||
| assert "Retrieve customer CRM data" not in agent_instructions, ( | ||
| "Tool descriptions should NOT be in instructions yet (this documents the bug). " | ||
| "If this assertion fails, the issue might be fixed!" | ||
| ) | ||
| assert "send_message" not in agent_instructions, ( | ||
| "Tool names should NOT be in instructions yet (this documents the bug)" | ||
| ) | ||
|
|
||
| # When the issue is fixed, the agent instructions should include | ||
| # tool descriptions similar to how sub-agent descriptions are added | ||
| # in agent.py lines 74-80 | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_mcp_tool_descriptions_expected_behavior( | ||
| self, | ||
| app_factory: Any, | ||
| agent_factory: Any, | ||
| respx_mock: respx.MockRouter, | ||
| ) -> None: | ||
| """Test that documents the EXPECTED behavior: MCP tool descriptions should be in instructions. | ||
|
|
||
| This test is currently SKIPPED. It documents what the correct | ||
| behavior should be once the issue is fixed. | ||
|
|
||
| When the bug is fixed, this test should pass, and the agent instructions | ||
| should contain the MCP tool descriptions, similar to how sub-agent | ||
| descriptions are currently handled. | ||
| """ | ||
|
|
||
| # Mark this test as expected to fail - remove this when the issue is fixed | ||
| pytest.skip( | ||
| "This test documents expected behavior and will fail until the issue is fixed. " | ||
| "When MCP tool descriptions are properly added to agent instructions, " | ||
| "remove the pytest.skip() and this test should pass." | ||
| ) | ||
|
|
||
| # Given: MCP server with tools | ||
| mcp = FastMCP("Calculator") | ||
|
|
||
| @mcp.tool() | ||
| def add(a: int, b: int) -> int: | ||
| """Add two numbers together.""" | ||
| return a + b | ||
|
|
||
| @mcp.tool() | ||
| def multiply(x: float, y: float) -> float: | ||
| """Multiply two numbers.""" | ||
| return x * y | ||
|
|
||
| # Start MCP server | ||
| mcp_server_url = "http://test-calc-mcp.local" | ||
| mcp_app = mcp.http_app(path="/mcp") | ||
|
|
||
| async with LifespanManager(mcp_app) as mcp_manager: | ||
| # Route MCP requests through ASGI transport | ||
| handler = create_asgi_request_handler(mcp_manager.app, mcp_server_url) | ||
| respx_mock.route(host="test-calc-mcp.local").mock(side_effect=handler) | ||
|
|
||
| # When: Create agent with MCP tools | ||
| test_agent = agent_factory("calculator_agent") | ||
| test_agent.instruction = "You are a calculator assistant." | ||
|
|
||
| tools = [McpTool(name="calculator", url=AnyHttpUrl(f"{mcp_server_url}/mcp"), timeout=30)] | ||
|
|
||
| async with app_factory(test_agent, tools=tools): | ||
| agent_instructions = test_agent.instruction | ||
|
|
||
| print("\n" + "=" * 80) | ||
| print("EXPECTED AGENT INSTRUCTIONS (when bug is fixed):") | ||
| print("=" * 80) | ||
| print(agent_instructions) | ||
| print("=" * 80) | ||
|
|
||
| # These assertions define the expected behavior: | ||
| assert "add" in agent_instructions, "Tool name 'add' should be in instructions" | ||
| assert "multiply" in agent_instructions, "Tool name 'multiply' should be in instructions" | ||
|
|
||
| # The full descriptions should be present | ||
| assert "Add two numbers together" in agent_instructions, ( | ||
| "Full tool description should be in instructions" | ||
| ) | ||
| assert "Multiply two numbers" in agent_instructions, "Full tool description should be in instructions" | ||
|
|
||
| # The format should be similar to sub-agent descriptions | ||
| # (see agent.py lines 74-80 for reference) | ||
| assert ( | ||
| "tools are available" in agent_instructions.lower() or "functions" in agent_instructions.lower() | ||
| ), "Instructions should mention available tools/functions" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.