From ee1b5494fc76268baeb133e0eb810ef9994e3b5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Est=C3=A9vez?= Date: Mon, 24 Mar 2025 11:11:40 -0400 Subject: [PATCH] fix: Standardize flow name handling with snake_case conversion in MCP API (#7242) * support spaces * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- src/backend/base/langflow/api/v1/mcp.py | 9 +++++---- src/backend/base/langflow/base/mcp/util.py | 5 +++-- .../unit/components/data/test_api_request_component.py | 3 +-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/backend/base/langflow/api/v1/mcp.py b/src/backend/base/langflow/api/v1/mcp.py index a609863b6..3ba0fc3bd 100644 --- a/src/backend/base/langflow/api/v1/mcp.py +++ b/src/backend/base/langflow/api/v1/mcp.py @@ -21,7 +21,7 @@ from starlette.background import BackgroundTasks from langflow.api.v1.chat import build_flow_and_stream from langflow.api.v1.schemas import InputValueRequest -from langflow.base.mcp.util import get_flow +from langflow.base.mcp.util import get_flow_snake_case from langflow.helpers.flow import json_schema_from_flow from langflow.services.auth.utils import get_current_active_user from langflow.services.database.models import Flow, User @@ -186,11 +186,12 @@ async def handle_list_tools(): if flow.user_id is None: continue + flow_name = "_".join(flow.name.lower().split()) tool = types.Tool( - name=flow.name, + name=flow_name, description=f"{flow.id}: {flow.description}" if flow.description - else f"Tool generated from flow: {flow.name}", + else f"Tool generated from flow: {flow_name}", inputSchema=json_schema_from_flow(flow), ) tools.append(tool) @@ -215,7 +216,7 @@ async def handle_call_tool(name: str, arguments: dict) -> list[types.TextContent async def execute_tool(session): # get flow id from name - flow = await get_flow(name, current_user.id, session) + flow = await get_flow_snake_case(name, current_user.id, session) if not flow: msg = f"Flow with name '{name}' not found" raise ValueError(msg) diff --git a/src/backend/base/langflow/base/mcp/util.py b/src/backend/base/langflow/base/mcp/util.py index bb90a2492..95a62d61d 100644 --- a/src/backend/base/langflow/base/mcp/util.py +++ b/src/backend/base/langflow/base/mcp/util.py @@ -60,13 +60,14 @@ def create_tool_func(tool_name: str, arg_schema: type[BaseModel], session) -> Ca return tool_func -async def get_flow(flow_name: str, user_id: str, session) -> Flow | None: +async def get_flow_snake_case(flow_name: str, user_id: str, session) -> Flow | None: uuid_user_id = UUID(user_id) if isinstance(user_id, str) else user_id stmt = select(Flow).where(Flow.user_id == uuid_user_id).where(Flow.is_component == False) # noqa: E712 flows = (await session.exec(stmt)).all() for flow in flows: - if flow.to_data().name == flow_name: + this_flow_name = "_".join(flow.name.lower().split()) + if this_flow_name == flow_name: return flow return None diff --git a/src/backend/tests/unit/components/data/test_api_request_component.py b/src/backend/tests/unit/components/data/test_api_request_component.py index b1a4b07f8..dd7d8210b 100644 --- a/src/backend/tests/unit/components/data/test_api_request_component.py +++ b/src/backend/tests/unit/components/data/test_api_request_component.py @@ -8,7 +8,7 @@ import pytest import respx from httpx import Response from langflow.components.data import APIRequestComponent -from langflow.schema import Data, DataFrame, Message +from langflow.schema import Data, DataFrame from tests.base import ComponentTestBaseWithoutClient @@ -146,7 +146,6 @@ class TestAPIRequestComponent(ComponentTestBaseWithoutClient): assert isinstance(result, Data) assert result.data["source"] == url - @respx.mock async def test_make_request_timeout(self, component): # Test request timeout