Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: eagerly load EndUser attributes to prevent DetachedInstanceError (#27162)

Guangdong Liu82219c1162de
diff --git a/api/controllers/mcp/mcp.py b/api/controllers/mcp/mcp.py
index 85b7df229fa..8d8fe6b3a81 100644
--- a/api/controllers/mcp/mcp.py
+++ b/api/controllers/mcp/mcp.py
@@ -193,15 +193,16 @@ class MCPAppApi(Resource):
except ValidationError as e:
raise MCPRequestError(mcp_types.INVALID_PARAMS, f"Invalid MCP request: {str(e)}")
- def _retrieve_end_user(self, tenant_id: str, mcp_server_id: str, session: Session) -> EndUser | None:
- """Get end user from existing session - optimized query"""
- return (
- session.query(EndUser)
- .where(EndUser.tenant_id == tenant_id)
- .where(EndUser.session_id == mcp_server_id)
- .where(EndUser.type == "mcp")
- .first()
- )
+ def _retrieve_end_user(self, tenant_id: str, mcp_server_id: str) -> EndUser | None:
+ """Get end user - manages its own database session"""
+ with Session(db.engine, expire_on_commit=False) as session, session.begin():
+ return (
+ session.query(EndUser)
+ .where(EndUser.tenant_id == tenant_id)
+ .where(EndUser.session_id == mcp_server_id)
+ .where(EndUser.type == "mcp")
+ .first()
+ )
def _create_end_user(
self, client_name: str, tenant_id: str, app_id: str, mcp_server_id: str, session: Session
@@ -229,7 +230,7 @@ class MCPAppApi(Resource):
request_id: Union[int, str],
) -> mcp_types.JSONRPCResponse | mcp_types.JSONRPCError | None:
"""Handle MCP request and return response"""
- end_user = self._retrieve_end_user(mcp_server.tenant_id, mcp_server.id, session)
+ end_user = self._retrieve_end_user(mcp_server.tenant_id, mcp_server.id)
if not end_user and isinstance(mcp_request.root, mcp_types.InitializeRequest):
client_info = mcp_request.root.params.clientInfo