Files

744 lines
28 KiB
Python

"""Test Galaxy AI agents API.
Includes deterministic tests against a local fake provider and static backend.
Live LLM tests are skipped unless GALAXY_TEST_ENABLE_LIVE_LLM=1.
For additional static-backend coverage, see test_static_agent_backend.py.
## Running:
export GALAXY_TEST_AI_API_KEY="your-api-key"
export GALAXY_TEST_AI_MODEL="llama-4-scout"
export GALAXY_TEST_AI_API_BASE_URL="http://localhost:4000/v1/"
export GALAXY_TEST_ENABLE_LIVE_LLM=1
pytest test/integration/test_agents.py -v
"""
import asyncio
import json
import logging
import os
import urllib.parse
from typing import cast
import pytest
import yaml
from fastmcp import (
Client,
FastMCP,
)
from fastmcp.exceptions import ToolError
from galaxy.agents.operations import AgentOperationsManager
from galaxy.managers.context import ProvidesUserContext
from galaxy.util.unittest_utils import pytestmark_live_llm
from galaxy.util.unittest_utils.test_http_server import RecordingHttpServer
from galaxy.webapps.galaxy.api.mcp import get_mcp_app
from galaxy.work.context import SessionRequestContext
from galaxy_test.base.populators import (
DatasetPopulator,
TOOL_WITH_SHELL_COMMAND,
WorkflowPopulator,
)
from galaxy_test.driver.integration_util import (
IntegrationTestCase,
UsesFakeAIProvider,
)
log = logging.getLogger(__name__)
class AgentIntegrationTestCase(IntegrationTestCase):
dataset_populator: DatasetPopulator
workflow_populator: WorkflowPopulator
def setUp(self):
super().setUp()
self.dataset_populator = DatasetPopulator(self.galaxy_interactor)
self.workflow_populator = WorkflowPopulator(self.galaxy_interactor)
@classmethod
def handle_galaxy_config_kwds(cls, config):
# AI/LLM configuration for agent tests
if ai_api_key := os.environ.get("GALAXY_TEST_AI_API_KEY"):
config["ai_api_key"] = ai_api_key
if ai_api_base_url := os.environ.get("GALAXY_TEST_AI_API_BASE_URL"):
config["ai_api_base_url"] = ai_api_base_url
if ai_model := os.environ.get("GALAXY_TEST_AI_MODEL"):
config["ai_model"] = ai_model
class TestAgentsApi(AgentIntegrationTestCase):
"""Test the Galaxy AI agents API endpoints.
These tests verify API structure and static backend responses.
For tests of actual agent logic with mocked LLMs, see test/unit/app/test_agents.py.
"""
def test_list_agents(self):
response = self._get("ai/agents")
self._assert_status_code_is_ok(response)
data = response.json()
assert "agents" in data
agents = data["agents"]
assert len(agents) > 0
agent_types = [a["agent_type"] for a in agents]
assert "router" in agent_types
assert "custom_tool" in agent_types
assert "error_analysis" in agent_types
# ============================================================================
# LIVE LLM TEST SUITE - Requires configured LLM
# ============================================================================
@pytestmark_live_llm
class TestAgentsApiLiveLLM(AgentIntegrationTestCase):
"""Test Galaxy AI agents API with real LLM.
These tests require a configured LLM and will be skipped unless
GALAXY_TEST_ENABLE_LIVE_LLM=1 is set.
"""
@classmethod
def handle_galaxy_config_kwds(cls, config):
super().handle_galaxy_config_kwds(config)
# Talk to the configured LLM, not the test driver's default static agent backend.
config.pop("inference_services", None)
def test_query_agent_auto_routing_live(self):
response = self._post(
"ai/agents/query",
data={
"query": "Create a BWA-MEM tool for paired-end reads",
"agent_type": "auto",
},
json=True,
)
self._assert_status_code_is_ok(response)
data = response.json()
assert "response" in data
assert "agent_type" in data["response"]
# Router should route this to custom_tool
assert data.get("routing_info", {}).get("selected_agent") == "custom_tool"
def test_query_custom_tool_agent_live(self):
response = self._post(
"ai/agents/query",
data={
"query": "Create a simple tool that counts lines in a file",
"agent_type": "custom_tool",
},
json=True,
)
self._assert_status_code_is_ok(response)
data = response.json()
assert "response" in data
agent_response = data["response"]
assert "content" in agent_response
assert "metadata" in agent_response
assert "tool_id" in agent_response["metadata"]
assert "tool_yaml" in agent_response["metadata"]
# Check that it created something sensible
tool_yaml = agent_response["metadata"]["tool_yaml"]
assert "command" in tool_yaml or "shell_command" in tool_yaml
def test_error_analysis_endpoint_live(self):
response = self._post(
"ai/agents/error-analysis",
data={
"query": "My BWA job failed with exit code 137 and stderr shows 'Killed'",
"error_details": {"tool_id": "bwa_mem", "exit_code": 137},
},
json=True,
)
self._assert_status_code_is_ok(response)
data = response.json()
assert "content" in data
assert "confidence" in data
# Should mention memory or OOM since exit code 137 is SIGKILL
content = data["content"].lower()
assert any(word in content for word in ["memory", "kill", "resource", "oom"])
def test_custom_tool_endpoint_live(self):
response = self._post(
"ai/agents/custom-tool",
data={
"query": "Create a tool that counts sequences in a FASTA file",
},
json=True,
)
self._assert_status_code_is_ok(response)
data = response.json()
assert "content" in data
assert "metadata" in data
# Should generate tool YAML
metadata = data["metadata"]
assert "tool_yaml" in metadata or "tool_id" in metadata
def test_chat_endpoint_live(self):
response = self._post(
"chat?query=What%20tools%20are%20available%20for%20RNA-seq%3F&agent_type=auto",
data={},
json=True,
)
self._assert_status_code_is_ok(response)
data = response.json()
assert data["error_code"] == 0, data
assert data["error_message"] == "", data
metadata = data["agent_response"]["metadata"]
assert not metadata.get("fallback"), data
assert not metadata.get("error"), data
# Should return some content about RNA-seq
assert len(data["response"]) > 0
def test_chat_history_endpoint(self):
response = self._get("chat/history?limit=5")
self._assert_status_code_is_ok(response)
data = response.json()
# Should return a list (may be empty)
assert isinstance(data, list)
class TestAgentsApiFakeProvider(UsesFakeAIProvider, AgentIntegrationTestCase):
"""Real agents behind real endpoints, with the LLM replaced by a scripted provider."""
@classmethod
def handle_galaxy_config_kwds(cls, config):
cls._configure_fake_ai_provider(config)
def test_error_analysis_endpoint(self):
# pydantic-ai asks for structured output through its ``final_result`` tool.
self.fake_ai_provider.respond_tool_call(
"final_result",
{
"error_category": "resource_limits",
"error_severity": "high",
"likely_cause": "The job ran out of memory and was killed (exit code 137).",
"solution_steps": ["Rerun with more memory"],
"confidence": "high",
},
)
response = self._post(
"ai/agents/error-analysis",
data={
"query": "My BWA job failed with exit code 137 and stderr shows 'Killed'",
"error_details": {"tool_id": "bwa_mem", "exit_code": 137},
},
json=True,
)
self._assert_status_code_is_ok(response)
data = response.json()
assert "**Error Type**: Resource Limits" in data["content"]
assert "1. Rerun with more memory" in data["content"]
assert data["confidence"] == "high"
sent = self.fake_ai_provider.last_body()
assert sent["model"] == "fake_model"
assert self.fake_ai_provider.last_request().headers["Authorization"] == "Bearer fake_api_key"
assert any("exit code 137" in str(message.get("content")) for message in sent["messages"])
assert [tool["function"]["name"] for tool in sent["tools"]] == ["final_result"]
# ============================================================================
# AgentOperationsManager ID Encoding Tests
# ============================================================================
class TestAgentOperationsManagerEncoding(AgentIntegrationTestCase):
"""Test AgentOperationsManager ID encoding.
These tests verify that the _encode_ids_in_response helper correctly
encodes Galaxy database IDs so agents can use them in subsequent API calls.
"""
def _make_ops(self):
class MinimalTrans(ProvidesUserContext):
def __init__(self, app):
self._app = app
@property
def app(self):
return self._app
@property
def user(self):
return None
@property
def url_builder(self):
return None
@property
def security(self):
return self._app.security
@property
def user_is_admin(self):
return False
# the double only needs security.encode_id for _encode_ids_in_response
trans = cast(SessionRequestContext, MinimalTrans(self._app))
return AgentOperationsManager(app=self._app, trans=trans)
def test_encode_ids_helper_encodes_nested_ids(self):
ops = self._make_ops()
test_data = {
"id": 123,
"name": "test",
"nested": {"id": 456, "history_id": 789},
"list_items": [{"id": 111, "dataset_id": 222}, {"id": 333}],
"implicit_collection_jobs_id": 444,
"unmapped_job": {"implicit_collection_jobs_id": None},
}
result = ops._encode_ids_in_response(test_data)
assert isinstance(result["id"], str)
assert len(result["id"]) >= 16
assert result["name"] == "test"
assert isinstance(result["nested"]["id"], str)
assert isinstance(result["nested"]["history_id"], str)
assert isinstance(result["list_items"][0]["id"], str)
assert isinstance(result["list_items"][0]["dataset_id"], str)
assert isinstance(result["list_items"][1]["id"], str)
assert isinstance(result["implicit_collection_jobs_id"], str)
assert result["unmapped_job"]["implicit_collection_jobs_id"] is None
def test_encode_ids_preserves_non_id_fields(self):
ops = self._make_ops()
test_data = {
"id": 1,
"name": "My History",
"annotation": "Test annotation",
"count": 42,
"empty_list": [],
"tags": ["tag1", "tag2"],
}
result = ops._encode_ids_in_response(test_data)
assert isinstance(result["id"], str)
assert result["name"] == "My History"
assert result["annotation"] == "Test annotation"
assert result["count"] == 42
assert result["empty_list"] == []
assert result["tags"] == ["tag1", "tag2"]
def test_encode_ids_handles_already_encoded_ids(self):
ops = self._make_ops()
test_data = {
"id": "abc123def456",
"history_id": "already_encoded_id",
}
result = ops._encode_ids_in_response(test_data)
assert result["id"] == "abc123def456"
assert result["history_id"] == "already_encoded_id"
# ============================================================================
# MCP Server Smoke Tests
# ============================================================================
class TestMCPServerSmoke(IntegrationTestCase):
"""Smoke tests for the MCP server.
Verifies the server initializes, advertises tools, handles auth,
and can execute basic tool calls. Not exhaustive API testing --
the MCP tools are thin wrappers around AgentOperationsManager.
"""
trs_server: RecordingHttpServer
@classmethod
def setUpClass(cls):
# Stands in for Dockstore; it must be up before Galaxy reads its TRS config.
cls.trs_server = RecordingHttpServer()
cls.trs_server.start()
try:
super().setUpClass()
except BaseException:
cls.trs_server.stop()
raise
@classmethod
def tearDownClass(cls):
try:
super().tearDownClass()
finally:
cls.trs_server.stop()
def setUp(self):
super().setUp()
self.trs_server.clear()
@classmethod
def handle_galaxy_config_kwds(cls, config):
config["enable_mcp_server"] = True
config["enable_beta_tool_formats"] = True
trs_config_file = os.path.join(cls.temp_config_dir("trs"), "trs_config.yml")
os.makedirs(os.path.dirname(trs_config_file))
with open(trs_config_file, "w") as f:
yaml.safe_dump([{"id": "dockstore", "api_url": cls.trs_server.url_for("/api"), "label": "Dockstore"}], f)
config["trs_servers_config_file"] = trs_config_file
def _get_mcp_server(self):
http_app = get_mcp_app(self._app)
return http_app.state.mcp_server
def _get_api_key(self):
_, api_key = self._setup_user_get_key("mcp_test_user@test.com")
return api_key
def _setup_udt_user(self, email: str):
"""Create a user, grant USER_TOOL_EXECUTE, return (user, api_key)."""
user, api_key = self._setup_user_get_key(email)
populator = DatasetPopulator(self.galaxy_interactor)
populator.create_role([user["id"]], role_type="user_tool_execute")
return user, api_key
def _run_async(self, coro):
loop = asyncio.new_event_loop()
try:
return loop.run_until_complete(coro)
finally:
loop.close()
def test_mcp_server_initializes(self):
"""MCP server creates a FastMCP instance when enabled."""
mcp_server = self._get_mcp_server()
assert isinstance(mcp_server, FastMCP)
def test_mcp_tools_registered(self):
"""MCP server advertises all expected tools."""
mcp_server = self._get_mcp_server()
async def _list():
async with Client(mcp_server) as client:
return await client.list_tools()
tools = self._run_async(_list())
tool_names = {t.name for t in tools}
expected = {
"connect",
"search_tools",
"list_histories",
"run_tool",
"get_tool_details",
"get_history_contents",
"get_dataset_details",
"upload_file_from_url",
"invoke_workflow",
"get_job_status",
"list_user_tools",
"create_user_tool",
"delete_user_tool",
"run_user_tool",
"search_iwc_workflows",
"get_iwc_workflow_details",
"import_workflow_from_iwc",
"list_pages",
"get_page",
"create_page",
"update_page",
"list_page_revisions",
"get_page_revision",
"revert_page_revision",
}
assert expected.issubset(tool_names), f"Missing tools: {expected - tool_names}"
def test_mcp_connect_with_valid_key(self):
"""connect() succeeds with a valid API key and returns user + server info."""
mcp_server = self._get_mcp_server()
api_key = self._get_api_key()
async def _connect():
async with Client(mcp_server) as client:
return await client.call_tool("connect", {"api_key": api_key})
result = self._run_async(_connect())
assert not result.is_error
data = result.data
assert "user" in data
assert "server" in data
def test_mcp_connect_with_invalid_key(self):
"""connect() rejects an invalid API key."""
mcp_server = self._get_mcp_server()
async def _connect():
async with Client(mcp_server) as client:
return await client.call_tool("connect", {"api_key": "bogus-key-12345"})
with pytest.raises(ToolError, match="(?i)(invalid|api key)"):
self._run_async(_connect())
def test_mcp_list_histories(self):
"""list_histories() returns a valid response."""
mcp_server = self._get_mcp_server()
api_key = self._get_api_key()
async def _list():
async with Client(mcp_server) as client:
return await client.call_tool("list_histories", {"api_key": api_key})
result = self._run_async(_list())
assert not result.is_error
data = result.data
assert "histories" in data
def test_mcp_search_tools(self):
"""search_tools() executes and returns a well-formed response."""
mcp_server = self._get_mcp_server()
api_key = self._get_api_key()
async def _search():
async with Client(mcp_server) as client:
return await client.call_tool("search_tools", {"api_key": api_key, "query": "sort"})
result = self._run_async(_search())
assert not result.is_error
data = result.data
assert "tools" in data
assert "query" in data
assert "count" in data
assert isinstance(data["tools"], list)
def test_mcp_list_user_tools_empty(self):
"""list_user_tools() returns an empty list for a user with the role and no UDTs."""
mcp_server = self._get_mcp_server()
_, api_key = self._setup_udt_user("udt_list_user@test.com")
async def _list():
async with Client(mcp_server) as client:
return await client.call_tool("list_user_tools", {"api_key": api_key})
result = self._run_async(_list())
assert not result.is_error, result
data = result.data
assert data["tools"] == []
assert data["count"] == 0
def test_mcp_create_user_tool(self):
"""create_user_tool() persists a UDT and returns its uuid."""
mcp_server = self._get_mcp_server()
_, api_key = self._setup_udt_user("udt_create_user@test.com")
async def _create():
async with Client(mcp_server) as client:
return await client.call_tool(
"create_user_tool",
{"api_key": api_key, "representation": TOOL_WITH_SHELL_COMMAND},
)
result = self._run_async(_create())
assert not result.is_error, result
data = result.data
assert "uuid" in data
assert data["representation"]["name"] == TOOL_WITH_SHELL_COMMAND["name"]
def test_mcp_delete_user_tool(self):
"""delete_user_tool() deactivates a UDT so list_user_tools no longer returns it."""
mcp_server = self._get_mcp_server()
_, api_key = self._setup_udt_user("udt_delete_user@test.com")
populator = DatasetPopulator(self._get_interactor(api_key=api_key))
history_id = populator.new_history()
async def _flow():
async with Client(mcp_server) as client:
create = await client.call_tool(
"create_user_tool",
{"api_key": api_key, "representation": TOOL_WITH_SHELL_COMMAND},
)
uuid = create.data["uuid"]
await client.call_tool("delete_user_tool", {"api_key": api_key, "uuid": uuid})
listed = await client.call_tool("list_user_tools", {"api_key": api_key})
deleted_run = await client.call_tool(
"run_user_tool",
{
"api_key": api_key,
"history_id": history_id,
"tool_uuid": uuid,
"inputs": {},
},
raise_on_error=False,
)
return uuid, listed, deleted_run
uuid, listed, deleted_run = self._run_async(_flow())
uuids_after = {t["uuid"] for t in listed.data["tools"]}
assert uuid not in uuids_after
assert deleted_run.is_error
assert "deactivated" in deleted_run.content[0].text
def test_mcp_run_user_tool(self):
"""run_user_tool() executes a UDT against an HDA input and produces an output."""
mcp_server = self._get_mcp_server()
_, api_key = self._setup_udt_user("udt_run_user@test.com")
populator = DatasetPopulator(self._get_interactor(api_key=api_key))
history_id = populator.new_history()
dataset = populator.new_dataset(history_id=history_id, content="abc")
async def _flow():
async with Client(mcp_server) as client:
create = await client.call_tool(
"create_user_tool",
{"api_key": api_key, "representation": TOOL_WITH_SHELL_COMMAND},
)
uuid = create.data["uuid"]
return await client.call_tool(
"run_user_tool",
{
"api_key": api_key,
"history_id": history_id,
"tool_uuid": uuid,
"inputs": {"input": {"src": "hda", "id": dataset["id"]}},
},
)
result = self._run_async(_flow())
assert not result.is_error, result
data = result.data
assert data["jobs"][0]["tool_id"] == TOOL_WITH_SHELL_COMMAND["id"]
assert data["jobs"][0]["history_id"] == history_id
assert data["outputs"][0]["output_name"] == "output"
populator.wait_for_history(history_id, assert_ok=True)
output = populator.get_history_dataset_content(history_id)
assert output == "abc\n"
def test_mcp_import_workflow_from_iwc(self):
"""import_workflow_from_iwc() imports a StoredWorkflow via the shared TRS pipeline."""
mcp_server = self._get_mcp_server()
api_key = self._get_api_key()
# Minimal valid Galaxy workflow definition, served as a Dockstore TRS descriptor.
definition = {
"a_galaxy_workflow": "true",
"format-version": "0.1",
"name": "IWC smoke test workflow",
"steps": {
"0": {
"id": 0,
"type": "data_input",
"label": "input",
"inputs": [],
"outputs": [],
"tool_state": "{}",
"input_connections": {},
"annotation": "",
"position": {"left": 0, "top": 0},
}
},
"tags": [],
"annotation": "",
}
trs_id = "#workflow/github.com/iwc-workflows/smoke/main"
descriptor_path = f"/api/ga4gh/trs/v2/tools/{urllib.parse.quote_plus(trs_id)}/versions/main/GALAXY/descriptor"
self.trs_server.add_json_route(descriptor_path, {"content": json.dumps(definition)})
async def _import():
async with Client(mcp_server) as client:
return await client.call_tool(
"import_workflow_from_iwc",
{"api_key": api_key, "trs_id": trs_id},
)
result = self._run_async(_import())
assert not result.is_error, result
data = result.data
assert "id" in data
assert data["trsID"] == trs_id
assert [request.path for request in self.trs_server.requests] == [descriptor_path]
def test_mcp_page_lifecycle(self):
"""create_page -> get_page -> update_page -> list_page_revisions round-trip."""
mcp_server = self._get_mcp_server()
api_key = self._get_api_key()
populator = DatasetPopulator(self._get_interactor(api_key=api_key))
history_id = populator.new_history()
async def _flow():
async with Client(mcp_server) as client:
created = await client.call_tool(
"create_page",
{"api_key": api_key, "history_id": history_id, "title": "MCP Notebook", "content": "# Intro\n"},
)
page_id = created.data["id"]
got = await client.call_tool("get_page", {"api_key": api_key, "page_id": page_id})
await client.call_tool(
"update_page",
{"api_key": api_key, "page_id": page_id, "content": "# Intro\n\n## Methods\n"},
)
revisions = await client.call_tool("list_page_revisions", {"api_key": api_key, "page_id": page_id})
listed = await client.call_tool("list_pages", {"api_key": api_key, "history_id": history_id})
return created, got, revisions, listed
created, got, revisions, listed = self._run_async(_flow())
assert not created.is_error, created
assert created.data["history_id"] == history_id
# get_page returns the editable content and omits the rendered form by default
assert "content_editor" in got.data
assert "content" not in got.data
# update_page created a second revision tagged with edit_source="agent"
assert revisions.data["count"] == 2
assert revisions.data["revisions"][-1]["edit_source"] == "agent"
# the notebook shows up when listing pages for its history
assert created.data["id"] in {p["id"] for p in listed.data["pages"]}
def test_mcp_page_directive_and_revert(self):
"""Dataset directives survive in encoded-id space; revert restores prior content."""
mcp_server = self._get_mcp_server()
api_key = self._get_api_key()
populator = DatasetPopulator(self._get_interactor(api_key=api_key))
history_id = populator.new_history()
hda_id = populator.new_dataset(history_id)["id"]
directive = f"# Analysis\n\n```galaxy\nhistory_dataset_display(history_dataset_id={hda_id})\n```\n"
async def _flow():
async with Client(mcp_server) as client:
created = await client.call_tool(
"create_page",
{"api_key": api_key, "history_id": history_id, "title": "Directive NB", "content": directive},
)
page_id = created.data["id"]
editor = await client.call_tool("get_page", {"api_key": api_key, "page_id": page_id})
rendered = await client.call_tool(
"get_page", {"api_key": api_key, "page_id": page_id, "include_rendered": True}
)
await client.call_tool(
"update_page", {"api_key": api_key, "page_id": page_id, "content": "# Replaced\n"}
)
revisions = await client.call_tool("list_page_revisions", {"api_key": api_key, "page_id": page_id})
first_rev_id = revisions.data["revisions"][0]["id"]
old_rev = await client.call_tool(
"get_page_revision",
{"api_key": api_key, "page_id": page_id, "revision_id": first_rev_id},
)
reverted = await client.call_tool(
"revert_page_revision",
{"api_key": api_key, "page_id": page_id, "revision_id": first_rev_id},
)
return editor, rendered, old_rev, reverted
editor, rendered, old_rev, reverted = self._run_async(_flow())
# content_editor keeps the directive with the ENCODED dataset id (encoded-id space)
assert hda_id in editor.data["content_editor"]
assert "content" not in editor.data
# include_rendered exposes the expanded render form
assert "content" in rendered.data
# get_page_revision mirrors get_page: editable content_editor by default, rendered omitted
assert hda_id in old_rev.data["content_editor"]
assert "content" not in old_rev.data
# revert appends a new "restore" revision carrying the original directive content
assert reverted.data["edit_source"] == "restore"
assert hda_id in reverted.data["content_editor"]
assert "content" not in reverted.data