AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(backend/copilot): add for_agent_generation flag to find_block (#12787)
Zamil Majdyda18f372f776
diff --git a/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.md b/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.mdindex 35b4a348b9..145354b704 100644--- a/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.md+++ b/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.md@@ -34,9 +34,13 @@ Steps:always inspect the current graph first so you know exactly what to change.Avoid using `include_graph=true` with broad keyword searches, as fetchingmultiple graphs at once is expensive and consumes LLM context budget.-2. **Discover blocks**: Call `find_block(query, include_schemas=true)` to+2. **Discover blocks**: Call `find_block(query, include_schemas=true, for_agent_generation=true)` tosearch for relevant blocks. This returns block IDs, names, descriptions,- and full input/output schemas.+ and full input/output schemas. The `for_agent_generation=true` flag is+ required to surface graph-only blocks such as AgentInputBlock,+ AgentDropdownInputBlock, AgentOutputBlock, OrchestratorBlock,+ and WebhookBlock and MCPToolBlock. (When running MCP tools interactively+ in CoPilot outside agent generation, use `run_mcp_tool` instead.)3. **Find library agents**: Call `find_library_agent` to discover reusableagents that can be composed as sub-agents via `AgentExecutorBlock`.4. **Generate/modify JSON**: Build or modify the agent JSON using block schemas:@@ -177,6 +181,12 @@ To compose agents using other agents as sub-agents:### Using MCP Tools (MCPToolBlock)+> **Agent graph vs CoPilot direct execution**: This section covers embedding MCP+> tools as persistent nodes in an agent graph. When running MCP tools directly in+> CoPilot (outside agent generation), use `run_mcp_tool` instead — it handles+> server discovery and authentication interactively. Use `MCPToolBlock` here only+> when the user wants the MCP call baked into a reusable agent graph.+To use an MCP (Model Context Protocol) tool as a node in the agent:1. The user must specify which MCP server URL and tool name they want2. Create an `MCPToolBlock` node (ID: `a0a4b1c2-d3e4-4f56-a7b8-c9d0e1f2a3b4`)diff --git a/autogpt_platform/backend/backend/copilot/tools/find_block.py b/autogpt_platform/backend/backend/copilot/tools/find_block.pyindex 0cbc3ba047..130e26562b 100644--- a/autogpt_platform/backend/backend/copilot/tools/find_block.py+++ b/autogpt_platform/backend/backend/copilot/tools/find_block.py@@ -74,6 +74,15 @@ class FindBlockTool(BaseTool):"description": "Include full input/output schemas (for agent JSON generation).","default": False,},+ "for_agent_generation": {+ "type": "boolean",+ "description": (+ "Set to true when searching for blocks to use inside an agent graph "+ "(e.g. AgentInputBlock, AgentOutputBlock, OrchestratorBlock). "+ "Bypasses the CoPilot-only filter so graph-only blocks are visible."+ ),+ "default": False,+ },},"required": ["query"],}@@ -88,6 +97,7 @@ class FindBlockTool(BaseTool):session: ChatSession,query: str = "",include_schemas: bool = False,+ for_agent_generation: bool = False,**kwargs,) -> ToolResponseBase:"""Search for blocks matching the query.@@ -97,6 +107,8 @@ class FindBlockTool(BaseTool):session: Chat sessionquery: Search queryinclude_schemas: Whether to include block schemas in results+ for_agent_generation: When True, bypasses the CoPilot exclusion filter+ so graph-only blocks (INPUT, OUTPUT, ORCHESTRATOR, etc.) are visible.Returns:BlockListResponse: List of matching blocks@@ -123,34 +135,36 @@ class FindBlockTool(BaseTool):suggestions=["Search for an alternative block by name"],session_id=session_id,)- if (+ is_excluded = (block.block_type in COPILOT_EXCLUDED_BLOCK_TYPESor block.id in COPILOT_EXCLUDED_BLOCK_IDS- ):- if block.block_type == BlockType.MCP_TOOL:+ )+ if is_excluded:+ # Graph-only blocks (INPUT, OUTPUT, MCP_TOOL, AGENT, etc.) are+ # exposed when building an agent graph so the LLM can inspect+ # their schemas and wire them as nodes. In CoPilot direct use+ # they are not executable — guide the LLM to the right tool.+ if not for_agent_generation:+ if block.block_type == BlockType.MCP_TOOL:+ message = (+ f"Block '{block.name}' (ID: {block.id}) cannot be "+ "run directly in CoPilot. Use run_mcp_tool for "+ "interactive MCP execution, or call find_block with "+ "for_agent_generation=true to embed it in an agent graph."+ )+ else:+ message = (+ f"Block '{block.name}' (ID: {block.id}) is not available "+ "in CoPilot. It can only be used within agent graphs."+ )return NoResultsResponse(- message=(- f"Block '{block.name}' (ID: {block.id}) is not "- "runnable through find_block/run_block. Use "- "run_mcp_tool instead."- ),+ message=message,suggestions=[- "Use run_mcp_tool to discover and run this MCP tool","Search for an alternative block by name",+ "Use this block in an agent graph instead",],session_id=session_id,)- return NoResultsResponse(- message=(- f"Block '{block.name}' (ID: {block.id}) is not available "- "in CoPilot. It can only be used within agent graphs."- ),- suggestions=[- "Search for an alternative block by name",- "Use this block in an agent graph instead",- ],- session_id=session_id,- )# Check block-level permissions — hide denied blocks entirelyperms = get_current_permissions()@@ -221,8 +235,9 @@ class FindBlockTool(BaseTool):if not block or block.disabled:continue- # Skip blocks excluded from CoPilot (graph-only blocks)- if (+ # Graph-only blocks (INPUT, OUTPUT, MCP_TOOL, AGENT, etc.) are+ # skipped in CoPilot direct use but surfaced for agent graph building.+ if not for_agent_generation and (block.block_type in COPILOT_EXCLUDED_BLOCK_TYPESor block.id in COPILOT_EXCLUDED_BLOCK_IDS):diff --git a/autogpt_platform/backend/backend/copilot/tools/find_block_test.py b/autogpt_platform/backend/backend/copilot/tools/find_block_test.pyindex 64a7fe3788..d99672daa2 100644--- a/autogpt_platform/backend/backend/copilot/tools/find_block_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/find_block_test.py@@ -12,7 +12,7 @@ from .find_block import (COPILOT_EXCLUDED_BLOCK_TYPES,FindBlockTool,)-from .models import BlockListResponse+from .models import BlockListResponse, NoResultsResponse_TEST_USER_ID = "test-user-find-block"@@ -166,6 +166,194 @@ class TestFindBlockFiltering:assert len(response.blocks) == 1assert response.blocks[0].id == "normal-block-id"+ @pytest.mark.asyncio(loop_scope="session")+ async def test_for_agent_generation_exposes_excluded_blocks_in_search(self):+ """With for_agent_generation=True, excluded block types appear in search results."""+ session = make_session(user_id=_TEST_USER_ID)++ search_results = [+ {"content_id": "input-block-id", "score": 0.9},+ {"content_id": "output-block-id", "score": 0.8},+ ]+ input_block = make_mock_block("input-block-id", "Agent Input", BlockType.INPUT)+ output_block = make_mock_block(+ "output-block-id", "Agent Output", BlockType.OUTPUT+ )++ def mock_get_block(block_id):+ return {+ "input-block-id": input_block,+ "output-block-id": output_block,+ }.get(block_id)++ mock_search_db = MagicMock()+ mock_search_db.unified_hybrid_search = AsyncMock(+ return_value=(search_results, 2)+ )++ with patch(+ "backend.copilot.tools.find_block.search",+ return_value=mock_search_db,+ ):+ with patch(+ "backend.copilot.tools.find_block.get_block",+ side_effect=mock_get_block,+ ):+ tool = FindBlockTool()+ response = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ query="agent input",+ for_agent_generation=True,+ )++ assert isinstance(response, BlockListResponse)+ assert len(response.blocks) == 2+ block_ids = {b.id for b in response.blocks}+ assert "input-block-id" in block_ids+ assert "output-block-id" in block_ids++ @pytest.mark.asyncio(loop_scope="session")+ async def test_mcp_tool_exposed_with_for_agent_generation_in_search(self):+ """MCP_TOOL blocks appear in search results when for_agent_generation=True."""+ session = make_session(user_id=_TEST_USER_ID)++ search_results = [+ {"content_id": "mcp-block-id", "score": 0.9},+ {"content_id": "standard-block-id", "score": 0.8},+ ]+ mcp_block = make_mock_block("mcp-block-id", "MCP Tool", BlockType.MCP_TOOL)+ standard_block = make_mock_block(+ "standard-block-id", "Normal Block", BlockType.STANDARD+ )++ def mock_get_block(block_id):+ return {+ "mcp-block-id": mcp_block,+ "standard-block-id": standard_block,+ }.get(block_id)++ mock_search_db = MagicMock()+ mock_search_db.unified_hybrid_search = AsyncMock(+ return_value=(search_results, 2)+ )++ with patch(+ "backend.copilot.tools.find_block.search",+ return_value=mock_search_db,+ ):+ with patch(+ "backend.copilot.tools.find_block.get_block",+ side_effect=mock_get_block,+ ):+ tool = FindBlockTool()+ response = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ query="mcp tool",+ for_agent_generation=True,+ )++ assert isinstance(response, BlockListResponse)+ assert len(response.blocks) == 2+ assert any(b.id == "mcp-block-id" for b in response.blocks)+ assert any(b.id == "standard-block-id" for b in response.blocks)++ @pytest.mark.asyncio(loop_scope="session")+ async def test_mcp_tool_excluded_without_for_agent_generation_in_search(self):+ """MCP_TOOL blocks are excluded from search in normal CoPilot mode."""+ session = make_session(user_id=_TEST_USER_ID)++ search_results = [+ {"content_id": "mcp-block-id", "score": 0.9},+ {"content_id": "standard-block-id", "score": 0.8},+ ]+ mcp_block = make_mock_block("mcp-block-id", "MCP Tool", BlockType.MCP_TOOL)+ standard_block = make_mock_block(+ "standard-block-id", "Normal Block", BlockType.STANDARD+ )++ def mock_get_block(block_id):+ return {+ "mcp-block-id": mcp_block,+ "standard-block-id": standard_block,+ }.get(block_id)++ mock_search_db = MagicMock()+ mock_search_db.unified_hybrid_search = AsyncMock(+ return_value=(search_results, 2)+ )++ with patch(+ "backend.copilot.tools.find_block.search",+ return_value=mock_search_db,+ ):+ with patch(+ "backend.copilot.tools.find_block.get_block",+ side_effect=mock_get_block,+ ):+ tool = FindBlockTool()+ response = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ query="mcp tool",+ for_agent_generation=False,+ )++ assert isinstance(response, BlockListResponse)+ assert len(response.blocks) == 1+ assert response.blocks[0].id == "standard-block-id"++ @pytest.mark.asyncio(loop_scope="session")+ async def test_for_agent_generation_exposes_excluded_ids_in_search(self):+ """With for_agent_generation=True, excluded block IDs appear in search results."""+ session = make_session(user_id=_TEST_USER_ID)+ orchestrator_id = next(iter(COPILOT_EXCLUDED_BLOCK_IDS))++ search_results = [+ {"content_id": orchestrator_id, "score": 0.9},+ {"content_id": "normal-block-id", "score": 0.8},+ ]+ orchestrator_block = make_mock_block(+ orchestrator_id, "Orchestrator", BlockType.STANDARD+ )+ normal_block = make_mock_block(+ "normal-block-id", "Normal Block", BlockType.STANDARD+ )++ def mock_get_block(block_id):+ return {+ orchestrator_id: orchestrator_block,+ "normal-block-id": normal_block,+ }.get(block_id)++ mock_search_db = MagicMock()+ mock_search_db.unified_hybrid_search = AsyncMock(+ return_value=(search_results, 2)+ )++ with patch(+ "backend.copilot.tools.find_block.search",+ return_value=mock_search_db,+ ):+ with patch(+ "backend.copilot.tools.find_block.get_block",+ side_effect=mock_get_block,+ ):+ tool = FindBlockTool()+ response = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ query="orchestrator",+ for_agent_generation=True,+ )++ assert isinstance(response, BlockListResponse)+ assert len(response.blocks) == 2+ block_ids = {b.id for b in response.blocks}+ assert orchestrator_id in block_ids+ assert "normal-block-id" in block_ids+@pytest.mark.asyncio(loop_scope="session")async def test_response_size_average_chars_per_block(self):"""Measure average chars per block in the serialized response."""@@ -549,8 +737,6 @@ class TestFindBlockDirectLookup:user_id=_TEST_USER_ID, session=session, query=block_id)- from .models import NoResultsResponse-assert isinstance(response, NoResultsResponse)@pytest.mark.asyncio(loop_scope="session")@@ -571,8 +757,6 @@ class TestFindBlockDirectLookup:user_id=_TEST_USER_ID, session=session, query=block_id)- from .models import NoResultsResponse-assert isinstance(response, NoResultsResponse)assert "disabled" in response.message.lower()@@ -592,8 +776,6 @@ class TestFindBlockDirectLookup:user_id=_TEST_USER_ID, session=session, query=block_id)- from .models import NoResultsResponse-assert isinstance(response, NoResultsResponse)assert "not available" in response.message.lower()@@ -613,7 +795,74 @@ class TestFindBlockDirectLookup:user_id=_TEST_USER_ID, session=session, query=orchestrator_id)- from .models import NoResultsResponse-assert isinstance(response, NoResultsResponse)assert "not available" in response.message.lower()++ @pytest.mark.asyncio(loop_scope="session")+ async def test_uuid_lookup_excluded_block_type_allowed_with_for_agent_generation(+ self,+ ):+ """With for_agent_generation=True, excluded block types (INPUT) are visible."""+ session = make_session(user_id=_TEST_USER_ID)+ block_id = "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"+ block = make_mock_block(block_id, "Agent Input Block", BlockType.INPUT)++ with patch(+ "backend.copilot.tools.find_block.get_block",+ return_value=block,+ ):+ tool = FindBlockTool()+ response = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ query=block_id,+ for_agent_generation=True,+ )++ assert isinstance(response, BlockListResponse)+ assert response.count == 1+ assert response.blocks[0].id == block_id++ @pytest.mark.asyncio(loop_scope="session")+ async def test_uuid_lookup_mcp_tool_exposed_with_for_agent_generation(self):+ """MCP_TOOL blocks are returned by UUID lookup when for_agent_generation=True."""+ session = make_session(user_id=_TEST_USER_ID)+ block_id = "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"+ block = make_mock_block(block_id, "MCP Tool", BlockType.MCP_TOOL)++ with patch(+ "backend.copilot.tools.find_block.get_block",+ return_value=block,+ ):+ tool = FindBlockTool()+ response = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ query=block_id,+ for_agent_generation=True,+ )++ assert isinstance(response, BlockListResponse)+ assert response.blocks[0].id == block_id++ @pytest.mark.asyncio(loop_scope="session")+ async def test_uuid_lookup_mcp_tool_excluded_without_for_agent_generation(self):+ """MCP_TOOL blocks are excluded by UUID lookup in normal CoPilot mode."""+ session = make_session(user_id=_TEST_USER_ID)+ block_id = "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"+ block = make_mock_block(block_id, "MCP Tool", BlockType.MCP_TOOL)++ with patch(+ "backend.copilot.tools.find_block.get_block",+ return_value=block,+ ):+ tool = FindBlockTool()+ response = await tool._execute(+ user_id=_TEST_USER_ID,+ session=session,+ query=block_id,+ for_agent_generation=False,+ )++ assert isinstance(response, NoResultsResponse)+ assert "run_mcp_tool" in response.message