2024-02-01 18:11:57 +08:00
|
|
|
import io
|
2025-07-10 14:01:34 +08:00
|
|
|
from urllib.parse import urlparse
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2025-07-17 17:18:44 +08:00
|
|
|
from flask import make_response, redirect, request, send_file
|
2025-08-24 14:45:47 +09:00
|
|
|
from flask_restx import (
|
2025-07-17 17:18:44 +08:00
|
|
|
Resource,
|
|
|
|
|
reqparse,
|
|
|
|
|
)
|
2025-10-27 17:07:51 +08:00
|
|
|
from sqlalchemy.orm import Session
|
2024-02-06 13:21:13 +08:00
|
|
|
from werkzeug.exceptions import Forbidden
|
|
|
|
|
|
2024-07-21 02:11:40 +09:00
|
|
|
from configs import dify_config
|
2025-11-13 13:38:45 +09:00
|
|
|
from controllers.console import api, console_ns
|
2025-07-17 17:18:44 +08:00
|
|
|
from controllers.console.wraps import (
|
|
|
|
|
account_initialization_required,
|
|
|
|
|
enterprise_license_required,
|
|
|
|
|
setup_required,
|
|
|
|
|
)
|
2025-10-27 17:07:51 +08:00
|
|
|
from core.entities.mcp_provider import MCPAuthentication, MCPConfiguration
|
2025-07-10 14:01:34 +08:00
|
|
|
from core.mcp.auth.auth_flow import auth, handle_callback
|
2025-10-27 17:07:51 +08:00
|
|
|
from core.mcp.error import MCPAuthError, MCPError, MCPRefreshTokenError
|
2025-07-10 14:01:34 +08:00
|
|
|
from core.mcp.mcp_client import MCPClient
|
2024-04-08 18:51:46 +08:00
|
|
|
from core.model_runtime.utils.encoders import jsonable_encoder
|
2025-11-12 17:59:37 +08:00
|
|
|
from core.plugin.entities.plugin_daemon import CredentialType
|
2025-07-17 17:18:44 +08:00
|
|
|
from core.plugin.impl.oauth import OAuthHandler
|
2025-10-27 17:07:51 +08:00
|
|
|
from extensions.ext_database import db
|
2025-07-17 17:18:44 +08:00
|
|
|
from libs.helper import StrLen, alphanumeric, uuid_value
|
2025-10-14 10:20:37 +09:00
|
|
|
from libs.login import current_account_with_tenant, login_required
|
2025-09-18 12:49:10 +08:00
|
|
|
from models.provider_ids import ToolProviderID
|
2025-11-12 17:59:37 +08:00
|
|
|
|
|
|
|
|
# from models.provider_ids import ToolProviderID
|
2025-07-17 17:18:44 +08:00
|
|
|
from services.plugin.oauth_service import OAuthProxyService
|
2024-05-27 22:01:11 +08:00
|
|
|
from services.tools.api_tools_manage_service import ApiToolManageService
|
|
|
|
|
from services.tools.builtin_tools_manage_service import BuiltinToolManageService
|
2025-10-27 17:07:51 +08:00
|
|
|
from services.tools.mcp_tools_manage_service import MCPToolManageService, OAuthDataType
|
2024-05-27 22:01:11 +08:00
|
|
|
from services.tools.tool_labels_service import ToolLabelsService
|
|
|
|
|
from services.tools.tools_manage_service import ToolCommonService
|
2025-07-10 14:01:34 +08:00
|
|
|
from services.tools.tools_transform_service import ToolTransformService
|
2024-05-27 22:01:11 +08:00
|
|
|
from services.tools.workflow_tools_manage_service import WorkflowToolManageService
|
2023-07-27 13:08:57 +08:00
|
|
|
|
|
|
|
|
|
2025-07-10 14:01:34 +08:00
|
|
|
def is_valid_url(url: str) -> bool:
|
|
|
|
|
if not url:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
parsed = urlparse(url)
|
|
|
|
|
return all([parsed.scheme, parsed.netloc]) and parsed.scheme in ["http", "https"]
|
2025-10-27 17:07:51 +08:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
|
# ValueError: Invalid URL format
|
|
|
|
|
# TypeError: url is not a string
|
2025-07-10 14:01:34 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_tool = reqparse.RequestParser().add_argument(
|
|
|
|
|
"type",
|
|
|
|
|
type=str,
|
|
|
|
|
choices=["builtin", "model", "api", "workflow", "mcp"],
|
|
|
|
|
required=False,
|
|
|
|
|
nullable=True,
|
|
|
|
|
location="args",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-providers")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolProviderListApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_tool)
|
2023-07-27 13:08:57 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
user_id = user.id
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_tool.parse_args()
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return ToolCommonService.list_tool_providers(user_id, tenant_id, args.get("type", None))
|
|
|
|
|
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/builtin/<path:provider>/tools")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolBuiltinProviderListToolsApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self, provider):
|
2025-10-14 10:20:37 +09:00
|
|
|
_, tenant_id = current_account_with_tenant()
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return jsonable_encoder(
|
|
|
|
|
BuiltinToolManageService.list_builtin_tool_provider_tools(
|
|
|
|
|
tenant_id,
|
|
|
|
|
provider,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/builtin/<path:provider>/info")
|
2025-02-17 17:05:13 +08:00
|
|
|
class ToolBuiltinProviderInfoApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self, provider):
|
2025-10-14 10:20:37 +09:00
|
|
|
_, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
2025-07-17 17:18:44 +08:00
|
|
|
return jsonable_encoder(BuiltinToolManageService.get_builtin_tool_provider_info(tenant_id, provider))
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_delete = reqparse.RequestParser().add_argument(
|
|
|
|
|
"credential_id", type=str, required=True, nullable=False, location="json"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/builtin/<path:provider>/delete")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolBuiltinProviderDeleteApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_delete)
|
2023-07-27 13:08:57 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self, provider):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
if not user.is_admin_or_owner:
|
2024-01-23 19:58:23 +08:00
|
|
|
raise Forbidden()
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_delete.parse_args()
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
return BuiltinToolManageService.delete_builtin_tool_provider(
|
2024-01-23 19:58:23 +08:00
|
|
|
tenant_id,
|
|
|
|
|
provider,
|
2025-07-17 17:18:44 +08:00
|
|
|
args["credential_id"],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_add = (
|
|
|
|
|
reqparse.RequestParser()
|
|
|
|
|
.add_argument("credentials", type=dict, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("name", type=StrLen(30), required=False, nullable=False, location="json")
|
|
|
|
|
.add_argument("type", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/builtin/<path:provider>/add")
|
2025-07-17 17:18:44 +08:00
|
|
|
class ToolBuiltinProviderAddApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_add)
|
2025-07-17 17:18:44 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self, provider):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-07-17 17:18:44 +08:00
|
|
|
|
|
|
|
|
user_id = user.id
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_add.parse_args()
|
2025-07-17 17:18:44 +08:00
|
|
|
|
|
|
|
|
if args["type"] not in CredentialType.values():
|
|
|
|
|
raise ValueError(f"Invalid credential type: {args['type']}")
|
|
|
|
|
|
|
|
|
|
return BuiltinToolManageService.add_builtin_tool_provider(
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
provider=provider,
|
|
|
|
|
credentials=args["credentials"],
|
|
|
|
|
name=args["name"],
|
|
|
|
|
api_type=CredentialType.of(args["type"]),
|
2024-01-23 19:58:23 +08:00
|
|
|
)
|
2024-08-26 15:29:10 +08:00
|
|
|
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_update = (
|
|
|
|
|
reqparse.RequestParser()
|
|
|
|
|
.add_argument("credential_id", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("credentials", type=dict, required=False, nullable=True, location="json")
|
|
|
|
|
.add_argument("name", type=StrLen(30), required=False, nullable=True, location="json")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/builtin/<path:provider>/update")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolBuiltinProviderUpdateApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_update)
|
2024-01-23 19:58:23 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self, provider):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
if not user.is_admin_or_owner:
|
2024-01-23 19:58:23 +08:00
|
|
|
raise Forbidden()
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
user_id = user.id
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_update.parse_args()
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2025-07-17 17:18:44 +08:00
|
|
|
result = BuiltinToolManageService.update_builtin_tool_provider(
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
provider=provider,
|
|
|
|
|
credential_id=args["credential_id"],
|
|
|
|
|
credentials=args.get("credentials", None),
|
|
|
|
|
name=args.get("name", ""),
|
|
|
|
|
)
|
2024-12-21 21:21:09 +08:00
|
|
|
return result
|
2024-08-26 15:29:10 +08:00
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/builtin/<path:provider>/credentials")
|
2024-04-08 18:51:46 +08:00
|
|
|
class ToolBuiltinProviderGetCredentialsApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self, provider):
|
2025-10-14 10:20:37 +09:00
|
|
|
_, tenant_id = current_account_with_tenant()
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2025-07-17 17:18:44 +08:00
|
|
|
return jsonable_encoder(
|
|
|
|
|
BuiltinToolManageService.get_builtin_tool_provider_credentials(
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
provider_name=provider,
|
|
|
|
|
)
|
2024-04-08 18:51:46 +08:00
|
|
|
)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/builtin/<path:provider>/icon")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolBuiltinProviderIconApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
def get(self, provider):
|
2024-05-27 22:01:11 +08:00
|
|
|
icon_bytes, mimetype = BuiltinToolManageService.get_builtin_tool_provider_icon(provider)
|
2024-07-21 02:11:40 +09:00
|
|
|
icon_cache_max_age = dify_config.TOOL_ICON_CACHE_MAX_AGE
|
2024-04-08 18:51:46 +08:00
|
|
|
return send_file(io.BytesIO(icon_bytes), mimetype=mimetype, max_age=icon_cache_max_age)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_api_add = (
|
|
|
|
|
reqparse.RequestParser()
|
|
|
|
|
.add_argument("credentials", type=dict, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("schema_type", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("schema", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("provider", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("icon", type=dict, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("privacy_policy", type=str, required=False, nullable=True, location="json")
|
|
|
|
|
.add_argument("labels", type=list[str], required=False, nullable=True, location="json", default=[])
|
|
|
|
|
.add_argument("custom_disclaimer", type=str, required=False, nullable=True, location="json")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/api/add")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolApiProviderAddApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_api_add)
|
2024-01-23 19:58:23 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
if not user.is_admin_or_owner:
|
2024-01-23 19:58:23 +08:00
|
|
|
raise Forbidden()
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
user_id = user.id
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_api_add.parse_args()
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
return ApiToolManageService.create_api_tool_provider(
|
2024-01-23 19:58:23 +08:00
|
|
|
user_id,
|
|
|
|
|
tenant_id,
|
2024-08-26 15:29:10 +08:00
|
|
|
args["provider"],
|
|
|
|
|
args["icon"],
|
|
|
|
|
args["credentials"],
|
|
|
|
|
args["schema_type"],
|
|
|
|
|
args["schema"],
|
|
|
|
|
args.get("privacy_policy", ""),
|
|
|
|
|
args.get("custom_disclaimer", ""),
|
|
|
|
|
args.get("labels", []),
|
2024-01-23 19:58:23 +08:00
|
|
|
)
|
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_remote = reqparse.RequestParser().add_argument("url", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/api/remote")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolApiProviderGetRemoteSchemaApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_remote)
|
2024-01-23 19:58:23 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
user_id = user.id
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_remote.parse_args()
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
return ApiToolManageService.get_api_tool_provider_remote_schema(
|
2025-02-17 17:05:13 +08:00
|
|
|
user_id,
|
|
|
|
|
tenant_id,
|
2024-08-26 15:29:10 +08:00
|
|
|
args["url"],
|
2024-01-23 19:58:23 +08:00
|
|
|
)
|
2024-08-26 15:29:10 +08:00
|
|
|
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_tools = reqparse.RequestParser().add_argument(
|
|
|
|
|
"provider", type=str, required=True, nullable=False, location="args"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/api/tools")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolApiProviderListToolsApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_tools)
|
2024-01-23 19:58:23 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
user_id = user.id
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_tools.parse_args()
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return jsonable_encoder(
|
|
|
|
|
ApiToolManageService.list_api_tool_provider_tools(
|
|
|
|
|
user_id,
|
|
|
|
|
tenant_id,
|
|
|
|
|
args["provider"],
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_api_update = (
|
|
|
|
|
reqparse.RequestParser()
|
|
|
|
|
.add_argument("credentials", type=dict, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("schema_type", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("schema", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("provider", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("original_provider", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("icon", type=dict, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("privacy_policy", type=str, required=True, nullable=True, location="json")
|
|
|
|
|
.add_argument("labels", type=list[str], required=False, nullable=True, location="json")
|
|
|
|
|
.add_argument("custom_disclaimer", type=str, required=True, nullable=True, location="json")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/api/update")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolApiProviderUpdateApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_api_update)
|
2023-07-27 13:08:57 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
2024-01-23 19:58:23 +08:00
|
|
|
def post(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
if not user.is_admin_or_owner:
|
2024-01-23 19:58:23 +08:00
|
|
|
raise Forbidden()
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
user_id = user.id
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_api_update.parse_args()
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
return ApiToolManageService.update_api_tool_provider(
|
2024-01-23 19:58:23 +08:00
|
|
|
user_id,
|
|
|
|
|
tenant_id,
|
2024-08-26 15:29:10 +08:00
|
|
|
args["provider"],
|
|
|
|
|
args["original_provider"],
|
|
|
|
|
args["icon"],
|
|
|
|
|
args["credentials"],
|
|
|
|
|
args["schema_type"],
|
|
|
|
|
args["schema"],
|
|
|
|
|
args["privacy_policy"],
|
|
|
|
|
args["custom_disclaimer"],
|
|
|
|
|
args.get("labels", []),
|
2024-01-23 19:58:23 +08:00
|
|
|
)
|
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_api_delete = reqparse.RequestParser().add_argument(
|
|
|
|
|
"provider", type=str, required=True, nullable=False, location="json"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/api/delete")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolApiProviderDeleteApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_api_delete)
|
2024-01-23 19:58:23 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
if not user.is_admin_or_owner:
|
2024-01-23 19:58:23 +08:00
|
|
|
raise Forbidden()
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
user_id = user.id
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_api_delete.parse_args()
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
return ApiToolManageService.delete_api_tool_provider(
|
2024-01-23 19:58:23 +08:00
|
|
|
user_id,
|
|
|
|
|
tenant_id,
|
2024-08-26 15:29:10 +08:00
|
|
|
args["provider"],
|
2024-01-23 19:58:23 +08:00
|
|
|
)
|
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_get = reqparse.RequestParser().add_argument("provider", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/api/get")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolApiProviderGetApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_get)
|
2024-01-23 19:58:23 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
user_id = user.id
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_get.parse_args()
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
return ApiToolManageService.get_api_tool_provider(
|
2024-01-23 19:58:23 +08:00
|
|
|
user_id,
|
|
|
|
|
tenant_id,
|
2024-08-26 15:29:10 +08:00
|
|
|
args["provider"],
|
2024-01-23 19:58:23 +08:00
|
|
|
)
|
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/builtin/<path:provider>/credential/schema/<path:credential_type>")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolBuiltinProviderCredentialsSchemaApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
2025-07-17 17:18:44 +08:00
|
|
|
def get(self, provider, credential_type):
|
2025-10-14 10:20:37 +09:00
|
|
|
_, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
2025-07-17 17:18:44 +08:00
|
|
|
return jsonable_encoder(
|
|
|
|
|
BuiltinToolManageService.list_builtin_provider_credentials_schema(
|
|
|
|
|
provider, CredentialType.of(credential_type), tenant_id
|
|
|
|
|
)
|
|
|
|
|
)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_schema = reqparse.RequestParser().add_argument(
|
|
|
|
|
"schema", type=str, required=True, nullable=False, location="json"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/api/schema")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolApiProviderSchemaApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_schema)
|
2024-01-23 19:58:23 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self):
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_schema.parse_args()
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
return ApiToolManageService.parser_api_schema(
|
2024-08-26 15:29:10 +08:00
|
|
|
schema=args["schema"],
|
2024-01-23 19:58:23 +08:00
|
|
|
)
|
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_pre = (
|
|
|
|
|
reqparse.RequestParser()
|
|
|
|
|
.add_argument("tool_name", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("provider_name", type=str, required=False, nullable=False, location="json")
|
|
|
|
|
.add_argument("credentials", type=dict, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("parameters", type=dict, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("schema_type", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("schema", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/api/test/pre")
|
2024-01-23 19:58:23 +08:00
|
|
|
class ToolApiProviderPreviousTestApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_pre)
|
2024-01-23 19:58:23 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self):
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_pre.parse_args()
|
2025-10-14 10:20:37 +09:00
|
|
|
_, current_tenant_id = current_account_with_tenant()
|
2024-05-27 22:01:11 +08:00
|
|
|
return ApiToolManageService.test_api_tool_preview(
|
2025-10-14 10:20:37 +09:00
|
|
|
current_tenant_id,
|
2024-09-12 15:50:49 +08:00
|
|
|
args["provider_name"] or "",
|
2024-08-26 15:29:10 +08:00
|
|
|
args["tool_name"],
|
|
|
|
|
args["credentials"],
|
|
|
|
|
args["parameters"],
|
|
|
|
|
args["schema_type"],
|
|
|
|
|
args["schema"],
|
2024-01-23 19:58:23 +08:00
|
|
|
)
|
2023-07-27 13:08:57 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_create = (
|
|
|
|
|
reqparse.RequestParser()
|
|
|
|
|
.add_argument("workflow_app_id", type=uuid_value, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("name", type=alphanumeric, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("label", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("description", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("icon", type=dict, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("parameters", type=list[dict], required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("privacy_policy", type=str, required=False, nullable=True, location="json", default="")
|
|
|
|
|
.add_argument("labels", type=list[str], required=False, nullable=True, location="json")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/workflow/create")
|
2024-05-27 22:01:11 +08:00
|
|
|
class ToolWorkflowProviderCreateApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_create)
|
2024-05-27 22:01:11 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
if not user.is_admin_or_owner:
|
2024-05-27 22:01:11 +08:00
|
|
|
raise Forbidden()
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
user_id = user.id
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_create.parse_args()
|
2024-05-27 22:01:11 +08:00
|
|
|
|
|
|
|
|
return WorkflowToolManageService.create_workflow_tool(
|
2024-10-21 10:43:49 +08:00
|
|
|
user_id=user_id,
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
workflow_app_id=args["workflow_app_id"],
|
|
|
|
|
name=args["name"],
|
|
|
|
|
label=args["label"],
|
|
|
|
|
icon=args["icon"],
|
|
|
|
|
description=args["description"],
|
|
|
|
|
parameters=args["parameters"],
|
|
|
|
|
privacy_policy=args["privacy_policy"],
|
2024-12-09 14:38:02 +08:00
|
|
|
labels=args["labels"],
|
2024-05-27 22:01:11 +08:00
|
|
|
)
|
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_workflow_update = (
|
|
|
|
|
reqparse.RequestParser()
|
|
|
|
|
.add_argument("workflow_tool_id", type=uuid_value, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("name", type=alphanumeric, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("label", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("description", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("icon", type=dict, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("parameters", type=list[dict], required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("privacy_policy", type=str, required=False, nullable=True, location="json", default="")
|
|
|
|
|
.add_argument("labels", type=list[str], required=False, nullable=True, location="json")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/workflow/update")
|
2024-05-27 22:01:11 +08:00
|
|
|
class ToolWorkflowProviderUpdateApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_workflow_update)
|
2024-05-27 22:01:11 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
if not user.is_admin_or_owner:
|
2024-05-27 22:01:11 +08:00
|
|
|
raise Forbidden()
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
user_id = user.id
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_workflow_update.parse_args()
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
if not args["workflow_tool_id"]:
|
|
|
|
|
raise ValueError("incorrect workflow_tool_id")
|
|
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
return WorkflowToolManageService.update_workflow_tool(
|
|
|
|
|
user_id,
|
|
|
|
|
tenant_id,
|
2024-08-26 15:29:10 +08:00
|
|
|
args["workflow_tool_id"],
|
|
|
|
|
args["name"],
|
|
|
|
|
args["label"],
|
|
|
|
|
args["icon"],
|
|
|
|
|
args["description"],
|
|
|
|
|
args["parameters"],
|
|
|
|
|
args["privacy_policy"],
|
|
|
|
|
args.get("labels", []),
|
2024-05-27 22:01:11 +08:00
|
|
|
)
|
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_workflow_delete = reqparse.RequestParser().add_argument(
|
|
|
|
|
"workflow_tool_id", type=uuid_value, required=True, nullable=False, location="json"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/workflow/delete")
|
2024-05-27 22:01:11 +08:00
|
|
|
class ToolWorkflowProviderDeleteApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_workflow_delete)
|
2024-05-27 22:01:11 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
if not user.is_admin_or_owner:
|
2024-05-27 22:01:11 +08:00
|
|
|
raise Forbidden()
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
user_id = user.id
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_workflow_delete.parse_args()
|
2024-05-27 22:01:11 +08:00
|
|
|
|
|
|
|
|
return WorkflowToolManageService.delete_workflow_tool(
|
|
|
|
|
user_id,
|
|
|
|
|
tenant_id,
|
2024-08-26 15:29:10 +08:00
|
|
|
args["workflow_tool_id"],
|
2024-05-27 22:01:11 +08:00
|
|
|
)
|
2024-08-26 15:29:10 +08:00
|
|
|
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_wf_get = (
|
|
|
|
|
reqparse.RequestParser()
|
|
|
|
|
.add_argument("workflow_tool_id", type=uuid_value, required=False, nullable=True, location="args")
|
|
|
|
|
.add_argument("workflow_app_id", type=uuid_value, required=False, nullable=True, location="args")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/workflow/get")
|
2024-05-27 22:01:11 +08:00
|
|
|
class ToolWorkflowProviderGetApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_wf_get)
|
2024-05-27 22:01:11 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
user_id = user.id
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_wf_get.parse_args()
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
if args.get("workflow_tool_id"):
|
2024-05-27 22:01:11 +08:00
|
|
|
tool = WorkflowToolManageService.get_workflow_tool_by_tool_id(
|
|
|
|
|
user_id,
|
|
|
|
|
tenant_id,
|
2024-08-26 15:29:10 +08:00
|
|
|
args["workflow_tool_id"],
|
2024-05-27 22:01:11 +08:00
|
|
|
)
|
2024-08-26 15:29:10 +08:00
|
|
|
elif args.get("workflow_app_id"):
|
2024-05-27 22:01:11 +08:00
|
|
|
tool = WorkflowToolManageService.get_workflow_tool_by_app_id(
|
|
|
|
|
user_id,
|
|
|
|
|
tenant_id,
|
2024-08-26 15:29:10 +08:00
|
|
|
args["workflow_app_id"],
|
2024-05-27 22:01:11 +08:00
|
|
|
)
|
|
|
|
|
else:
|
2024-08-26 15:29:10 +08:00
|
|
|
raise ValueError("incorrect workflow_tool_id or workflow_app_id")
|
2024-05-27 22:01:11 +08:00
|
|
|
|
|
|
|
|
return jsonable_encoder(tool)
|
2024-08-26 15:29:10 +08:00
|
|
|
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_wf_tools = reqparse.RequestParser().add_argument(
|
|
|
|
|
"workflow_tool_id", type=uuid_value, required=True, nullable=False, location="args"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/workflow/tools")
|
2024-05-27 22:01:11 +08:00
|
|
|
class ToolWorkflowProviderListToolApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_wf_tools)
|
2024-05-27 22:01:11 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
user_id = user.id
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_wf_tools.parse_args()
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return jsonable_encoder(
|
|
|
|
|
WorkflowToolManageService.list_single_workflow_tools(
|
|
|
|
|
user_id,
|
|
|
|
|
tenant_id,
|
|
|
|
|
args["workflow_tool_id"],
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tools/builtin")
|
2024-04-08 18:51:46 +08:00
|
|
|
class ToolBuiltinListApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
user_id = user.id
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return jsonable_encoder(
|
|
|
|
|
[
|
|
|
|
|
provider.to_dict()
|
|
|
|
|
for provider in BuiltinToolManageService.list_builtin_tools(
|
|
|
|
|
user_id,
|
|
|
|
|
tenant_id,
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tools/api")
|
2024-04-08 18:51:46 +08:00
|
|
|
class ToolApiListApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
_, tenant_id = current_account_with_tenant()
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return jsonable_encoder(
|
|
|
|
|
[
|
|
|
|
|
provider.to_dict()
|
|
|
|
|
for provider in ApiToolManageService.list_api_tools(
|
|
|
|
|
tenant_id,
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tools/workflow")
|
2024-05-27 22:01:11 +08:00
|
|
|
class ToolWorkflowListApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
|
|
|
|
user_id = user.id
|
2024-04-08 18:51:46 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return jsonable_encoder(
|
|
|
|
|
[
|
|
|
|
|
provider.to_dict()
|
|
|
|
|
for provider in WorkflowToolManageService.list_tenant_workflow_tools(
|
|
|
|
|
user_id,
|
|
|
|
|
tenant_id,
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-labels")
|
2024-05-27 22:01:11 +08:00
|
|
|
class ToolLabelsApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
2024-11-15 17:59:36 +08:00
|
|
|
@enterprise_license_required
|
2024-05-27 22:01:11 +08:00
|
|
|
def get(self):
|
|
|
|
|
return jsonable_encoder(ToolLabelsService.list_tool_labels())
|
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/oauth/plugin/<path:provider>/tool/authorization-url")
|
2025-07-17 17:18:44 +08:00
|
|
|
class ToolPluginOAuthApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self, provider):
|
|
|
|
|
tool_provider = ToolProviderID(provider)
|
|
|
|
|
plugin_id = tool_provider.plugin_id
|
|
|
|
|
provider_name = tool_provider.provider_name
|
|
|
|
|
|
|
|
|
|
# todo check permission
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-07-17 17:18:44 +08:00
|
|
|
|
|
|
|
|
if not user.is_admin_or_owner:
|
|
|
|
|
raise Forbidden()
|
|
|
|
|
|
|
|
|
|
oauth_client_params = BuiltinToolManageService.get_oauth_client(tenant_id=tenant_id, provider=provider)
|
|
|
|
|
if oauth_client_params is None:
|
|
|
|
|
raise Forbidden("no oauth available client config found for this tool provider")
|
|
|
|
|
|
|
|
|
|
oauth_handler = OAuthHandler()
|
|
|
|
|
context_id = OAuthProxyService.create_proxy_context(
|
2025-10-14 10:20:37 +09:00
|
|
|
user_id=user.id, tenant_id=tenant_id, plugin_id=plugin_id, provider=provider_name
|
2025-07-17 17:18:44 +08:00
|
|
|
)
|
|
|
|
|
redirect_uri = f"{dify_config.CONSOLE_API_URL}/console/api/oauth/plugin/{provider}/tool/callback"
|
|
|
|
|
authorization_url_response = oauth_handler.get_authorization_url(
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
user_id=user.id,
|
|
|
|
|
plugin_id=plugin_id,
|
|
|
|
|
provider=provider_name,
|
|
|
|
|
redirect_uri=redirect_uri,
|
|
|
|
|
system_credentials=oauth_client_params,
|
|
|
|
|
)
|
|
|
|
|
response = make_response(jsonable_encoder(authorization_url_response))
|
|
|
|
|
response.set_cookie(
|
|
|
|
|
"context_id",
|
|
|
|
|
context_id,
|
|
|
|
|
httponly=True,
|
|
|
|
|
samesite="Lax",
|
|
|
|
|
max_age=OAuthProxyService.__MAX_AGE__,
|
|
|
|
|
)
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/oauth/plugin/<path:provider>/tool/callback")
|
2025-07-17 17:18:44 +08:00
|
|
|
class ToolOAuthCallback(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
def get(self, provider):
|
|
|
|
|
context_id = request.cookies.get("context_id")
|
|
|
|
|
if not context_id:
|
|
|
|
|
raise Forbidden("context_id not found")
|
|
|
|
|
|
|
|
|
|
context = OAuthProxyService.use_proxy_context(context_id)
|
|
|
|
|
if context is None:
|
|
|
|
|
raise Forbidden("Invalid context_id")
|
|
|
|
|
|
|
|
|
|
tool_provider = ToolProviderID(provider)
|
|
|
|
|
plugin_id = tool_provider.plugin_id
|
|
|
|
|
provider_name = tool_provider.provider_name
|
|
|
|
|
user_id, tenant_id = context.get("user_id"), context.get("tenant_id")
|
|
|
|
|
|
|
|
|
|
oauth_handler = OAuthHandler()
|
|
|
|
|
oauth_client_params = BuiltinToolManageService.get_oauth_client(tenant_id, provider)
|
|
|
|
|
if oauth_client_params is None:
|
|
|
|
|
raise Forbidden("no oauth available client config found for this tool provider")
|
|
|
|
|
|
|
|
|
|
redirect_uri = f"{dify_config.CONSOLE_API_URL}/console/api/oauth/plugin/{provider}/tool/callback"
|
2025-07-23 13:12:39 +08:00
|
|
|
credentials_response = oauth_handler.get_credentials(
|
2025-07-17 17:18:44 +08:00
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
plugin_id=plugin_id,
|
|
|
|
|
provider=provider_name,
|
|
|
|
|
redirect_uri=redirect_uri,
|
|
|
|
|
system_credentials=oauth_client_params,
|
|
|
|
|
request=request,
|
2025-07-23 13:12:39 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
credentials = credentials_response.credentials
|
|
|
|
|
expires_at = credentials_response.expires_at
|
2025-07-17 17:18:44 +08:00
|
|
|
|
|
|
|
|
if not credentials:
|
|
|
|
|
raise Exception("the plugin credentials failed")
|
|
|
|
|
|
|
|
|
|
# add credentials to database
|
|
|
|
|
BuiltinToolManageService.add_builtin_tool_provider(
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
provider=provider,
|
|
|
|
|
credentials=dict(credentials),
|
2025-07-23 13:12:39 +08:00
|
|
|
expires_at=expires_at,
|
2025-07-17 17:18:44 +08:00
|
|
|
api_type=CredentialType.OAUTH2,
|
|
|
|
|
)
|
|
|
|
|
return redirect(f"{dify_config.CONSOLE_WEB_URL}/oauth-callback")
|
|
|
|
|
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_default_cred = reqparse.RequestParser().add_argument(
|
|
|
|
|
"id", type=str, required=True, nullable=False, location="json"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/builtin/<path:provider>/default-credential")
|
2025-07-17 17:18:44 +08:00
|
|
|
class ToolBuiltinProviderSetDefaultApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_default_cred)
|
2025-07-17 17:18:44 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self, provider):
|
2025-10-14 10:20:37 +09:00
|
|
|
current_user, current_tenant_id = current_account_with_tenant()
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_default_cred.parse_args()
|
2025-07-17 17:18:44 +08:00
|
|
|
return BuiltinToolManageService.set_default_provider(
|
2025-10-14 10:20:37 +09:00
|
|
|
tenant_id=current_tenant_id, user_id=current_user.id, provider=provider, id=args["id"]
|
2025-07-17 17:18:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_custom = (
|
|
|
|
|
reqparse.RequestParser()
|
|
|
|
|
.add_argument("client_params", type=dict, required=False, nullable=True, location="json")
|
|
|
|
|
.add_argument("enable_oauth_custom_client", type=bool, required=False, nullable=True, location="json")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/builtin/<path:provider>/oauth/custom-client")
|
2025-07-17 17:18:44 +08:00
|
|
|
class ToolOAuthCustomClient(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_custom)
|
2025-07-17 17:18:44 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self, provider):
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_custom.parse_args()
|
2025-07-17 17:18:44 +08:00
|
|
|
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-07-17 17:18:44 +08:00
|
|
|
|
|
|
|
|
if not user.is_admin_or_owner:
|
|
|
|
|
raise Forbidden()
|
|
|
|
|
|
|
|
|
|
return BuiltinToolManageService.save_custom_oauth_client_params(
|
2025-10-14 10:20:37 +09:00
|
|
|
tenant_id=tenant_id,
|
2025-07-17 17:18:44 +08:00
|
|
|
provider=provider,
|
|
|
|
|
client_params=args.get("client_params", {}),
|
|
|
|
|
enable_oauth_custom_client=args.get("enable_oauth_custom_client", True),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self, provider):
|
2025-10-14 10:20:37 +09:00
|
|
|
_, current_tenant_id = current_account_with_tenant()
|
2025-07-17 17:18:44 +08:00
|
|
|
return jsonable_encoder(
|
2025-10-14 10:20:37 +09:00
|
|
|
BuiltinToolManageService.get_custom_oauth_client_params(tenant_id=current_tenant_id, provider=provider)
|
2025-07-17 17:18:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def delete(self, provider):
|
2025-10-14 10:20:37 +09:00
|
|
|
_, current_tenant_id = current_account_with_tenant()
|
2025-07-17 17:18:44 +08:00
|
|
|
return jsonable_encoder(
|
2025-10-14 10:20:37 +09:00
|
|
|
BuiltinToolManageService.delete_custom_oauth_client_params(tenant_id=current_tenant_id, provider=provider)
|
2025-07-17 17:18:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/builtin/<path:provider>/oauth/client-schema")
|
2025-07-17 17:18:44 +08:00
|
|
|
class ToolBuiltinProviderGetOauthClientSchemaApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self, provider):
|
2025-10-14 10:20:37 +09:00
|
|
|
_, current_tenant_id = current_account_with_tenant()
|
2025-07-17 17:18:44 +08:00
|
|
|
return jsonable_encoder(
|
|
|
|
|
BuiltinToolManageService.get_builtin_tool_provider_oauth_client_schema(
|
2025-10-14 10:20:37 +09:00
|
|
|
tenant_id=current_tenant_id, provider_name=provider
|
2025-07-17 17:18:44 +08:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/builtin/<path:provider>/credential/info")
|
2025-07-17 17:18:44 +08:00
|
|
|
class ToolBuiltinProviderGetCredentialInfoApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self, provider):
|
2025-10-14 10:20:37 +09:00
|
|
|
_, tenant_id = current_account_with_tenant()
|
2025-07-17 17:18:44 +08:00
|
|
|
|
|
|
|
|
return jsonable_encoder(
|
|
|
|
|
BuiltinToolManageService.get_builtin_tool_provider_credential_info(
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
provider=provider,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_mcp = (
|
|
|
|
|
reqparse.RequestParser()
|
|
|
|
|
.add_argument("server_url", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("name", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("icon", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("icon_type", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("icon_background", type=str, required=False, nullable=True, location="json", default="")
|
|
|
|
|
.add_argument("server_identifier", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("configuration", type=dict, required=False, nullable=True, location="json", default={})
|
|
|
|
|
.add_argument("headers", type=dict, required=False, nullable=True, location="json", default={})
|
|
|
|
|
.add_argument("authentication", type=dict, required=False, nullable=True, location="json", default={})
|
|
|
|
|
)
|
|
|
|
|
parser_mcp_put = (
|
|
|
|
|
reqparse.RequestParser()
|
|
|
|
|
.add_argument("server_url", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("name", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("icon", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("icon_type", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("icon_background", type=str, required=False, nullable=True, location="json")
|
|
|
|
|
.add_argument("provider_id", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("server_identifier", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("configuration", type=dict, required=False, nullable=True, location="json", default={})
|
|
|
|
|
.add_argument("headers", type=dict, required=False, nullable=True, location="json", default={})
|
|
|
|
|
.add_argument("authentication", type=dict, required=False, nullable=True, location="json", default={})
|
|
|
|
|
)
|
|
|
|
|
parser_mcp_delete = reqparse.RequestParser().add_argument(
|
|
|
|
|
"provider_id", type=str, required=True, nullable=False, location="json"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/mcp")
|
2025-07-10 14:01:34 +08:00
|
|
|
class ToolProviderMCPApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_mcp)
|
2025-07-10 14:01:34 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self):
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_mcp.parse_args()
|
2025-10-14 10:20:37 +09:00
|
|
|
user, tenant_id = current_account_with_tenant()
|
2025-10-27 17:07:51 +08:00
|
|
|
|
|
|
|
|
# Parse and validate models
|
|
|
|
|
configuration = MCPConfiguration.model_validate(args["configuration"])
|
|
|
|
|
authentication = MCPAuthentication.model_validate(args["authentication"]) if args["authentication"] else None
|
|
|
|
|
|
|
|
|
|
# Create provider
|
|
|
|
|
with Session(db.engine) as session, session.begin():
|
|
|
|
|
service = MCPToolManageService(session=session)
|
|
|
|
|
result = service.create_provider(
|
2025-10-14 10:20:37 +09:00
|
|
|
tenant_id=tenant_id,
|
2025-10-27 17:07:51 +08:00
|
|
|
user_id=user.id,
|
2025-07-10 14:01:34 +08:00
|
|
|
server_url=args["server_url"],
|
|
|
|
|
name=args["name"],
|
|
|
|
|
icon=args["icon"],
|
|
|
|
|
icon_type=args["icon_type"],
|
|
|
|
|
icon_background=args["icon_background"],
|
|
|
|
|
server_identifier=args["server_identifier"],
|
2025-09-08 14:10:55 +08:00
|
|
|
headers=args["headers"],
|
2025-10-27 17:07:51 +08:00
|
|
|
configuration=configuration,
|
|
|
|
|
authentication=authentication,
|
2025-07-10 14:01:34 +08:00
|
|
|
)
|
2025-10-27 17:07:51 +08:00
|
|
|
return jsonable_encoder(result)
|
2025-07-10 14:01:34 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_mcp_put)
|
2025-07-10 14:01:34 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def put(self):
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_mcp_put.parse_args()
|
2025-10-27 17:07:51 +08:00
|
|
|
configuration = MCPConfiguration.model_validate(args["configuration"])
|
|
|
|
|
authentication = MCPAuthentication.model_validate(args["authentication"]) if args["authentication"] else None
|
2025-10-14 10:20:37 +09:00
|
|
|
_, current_tenant_id = current_account_with_tenant()
|
2025-10-27 17:07:51 +08:00
|
|
|
|
|
|
|
|
# Step 1: Validate server URL change if needed (includes URL format validation and network operation)
|
|
|
|
|
validation_result = None
|
|
|
|
|
with Session(db.engine) as session:
|
|
|
|
|
service = MCPToolManageService(session=session)
|
|
|
|
|
validation_result = service.validate_server_url_change(
|
|
|
|
|
tenant_id=current_tenant_id, provider_id=args["provider_id"], new_server_url=args["server_url"]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# No need to check for errors here, exceptions will be raised directly
|
|
|
|
|
|
|
|
|
|
# Step 2: Perform database update in a transaction
|
|
|
|
|
with Session(db.engine) as session, session.begin():
|
|
|
|
|
service = MCPToolManageService(session=session)
|
|
|
|
|
service.update_provider(
|
|
|
|
|
tenant_id=current_tenant_id,
|
|
|
|
|
provider_id=args["provider_id"],
|
|
|
|
|
server_url=args["server_url"],
|
|
|
|
|
name=args["name"],
|
|
|
|
|
icon=args["icon"],
|
|
|
|
|
icon_type=args["icon_type"],
|
|
|
|
|
icon_background=args["icon_background"],
|
|
|
|
|
server_identifier=args["server_identifier"],
|
|
|
|
|
headers=args["headers"],
|
|
|
|
|
configuration=configuration,
|
|
|
|
|
authentication=authentication,
|
|
|
|
|
validation_result=validation_result,
|
|
|
|
|
)
|
|
|
|
|
return {"result": "success"}
|
2025-07-10 14:01:34 +08:00
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_mcp_delete)
|
2025-07-10 14:01:34 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def delete(self):
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_mcp_delete.parse_args()
|
2025-10-14 10:20:37 +09:00
|
|
|
_, current_tenant_id = current_account_with_tenant()
|
2025-10-27 17:07:51 +08:00
|
|
|
|
|
|
|
|
with Session(db.engine) as session, session.begin():
|
|
|
|
|
service = MCPToolManageService(session=session)
|
|
|
|
|
service.delete_provider(tenant_id=current_tenant_id, provider_id=args["provider_id"])
|
|
|
|
|
return {"result": "success"}
|
2025-07-10 14:01:34 +08:00
|
|
|
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_auth = (
|
|
|
|
|
reqparse.RequestParser()
|
|
|
|
|
.add_argument("provider_id", type=str, required=True, nullable=False, location="json")
|
|
|
|
|
.add_argument("authorization_code", type=str, required=False, nullable=True, location="json")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/mcp/auth")
|
2025-07-10 14:01:34 +08:00
|
|
|
class ToolMCPAuthApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_auth)
|
2025-07-10 14:01:34 +08:00
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def post(self):
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_auth.parse_args()
|
2025-07-10 14:01:34 +08:00
|
|
|
provider_id = args["provider_id"]
|
2025-10-14 10:20:37 +09:00
|
|
|
_, tenant_id = current_account_with_tenant()
|
2025-10-27 17:07:51 +08:00
|
|
|
|
|
|
|
|
with Session(db.engine) as session, session.begin():
|
|
|
|
|
service = MCPToolManageService(session=session)
|
|
|
|
|
db_provider = service.get_provider(provider_id=provider_id, tenant_id=tenant_id)
|
|
|
|
|
if not db_provider:
|
|
|
|
|
raise ValueError("provider not found")
|
|
|
|
|
|
|
|
|
|
# Convert to entity
|
|
|
|
|
provider_entity = db_provider.to_entity()
|
|
|
|
|
server_url = provider_entity.decrypt_server_url()
|
|
|
|
|
headers = provider_entity.decrypt_authentication()
|
|
|
|
|
|
|
|
|
|
# Try to connect without active transaction
|
2025-07-10 14:01:34 +08:00
|
|
|
try:
|
2025-10-27 17:07:51 +08:00
|
|
|
# Use MCPClientWithAuthRetry to handle authentication automatically
|
2025-07-10 14:01:34 +08:00
|
|
|
with MCPClient(
|
2025-10-27 17:07:51 +08:00
|
|
|
server_url=server_url,
|
|
|
|
|
headers=headers,
|
|
|
|
|
timeout=provider_entity.timeout,
|
|
|
|
|
sse_read_timeout=provider_entity.sse_read_timeout,
|
2025-07-10 14:01:34 +08:00
|
|
|
):
|
2025-10-27 17:07:51 +08:00
|
|
|
# Update credentials in new transaction
|
|
|
|
|
with Session(db.engine) as session, session.begin():
|
|
|
|
|
service = MCPToolManageService(session=session)
|
|
|
|
|
service.update_provider_credentials(
|
|
|
|
|
provider_id=provider_id,
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
credentials=provider_entity.credentials,
|
|
|
|
|
authed=True,
|
|
|
|
|
)
|
2025-07-10 14:01:34 +08:00
|
|
|
return {"result": "success"}
|
2025-10-27 17:07:51 +08:00
|
|
|
except MCPAuthError as e:
|
|
|
|
|
try:
|
|
|
|
|
auth_result = auth(provider_entity, args.get("authorization_code"))
|
|
|
|
|
with Session(db.engine) as session, session.begin():
|
|
|
|
|
service = MCPToolManageService(session=session)
|
|
|
|
|
response = service.execute_auth_actions(auth_result)
|
|
|
|
|
return response
|
|
|
|
|
except MCPRefreshTokenError as e:
|
|
|
|
|
with Session(db.engine) as session, session.begin():
|
|
|
|
|
service = MCPToolManageService(session=session)
|
|
|
|
|
service.clear_provider_credentials(provider_id=provider_id, tenant_id=tenant_id)
|
|
|
|
|
raise ValueError(f"Failed to refresh token, please try to authorize again: {e}") from e
|
2025-07-10 14:01:34 +08:00
|
|
|
except MCPError as e:
|
2025-10-27 17:07:51 +08:00
|
|
|
with Session(db.engine) as session, session.begin():
|
|
|
|
|
service = MCPToolManageService(session=session)
|
|
|
|
|
service.clear_provider_credentials(provider_id=provider_id, tenant_id=tenant_id)
|
2025-07-10 14:01:34 +08:00
|
|
|
raise ValueError(f"Failed to connect to MCP server: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/mcp/tools/<path:provider_id>")
|
2025-07-10 14:01:34 +08:00
|
|
|
class ToolMCPDetailApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self, provider_id):
|
2025-10-14 10:20:37 +09:00
|
|
|
_, tenant_id = current_account_with_tenant()
|
2025-10-27 17:07:51 +08:00
|
|
|
with Session(db.engine) as session, session.begin():
|
|
|
|
|
service = MCPToolManageService(session=session)
|
|
|
|
|
provider = service.get_provider(provider_id=provider_id, tenant_id=tenant_id)
|
|
|
|
|
return jsonable_encoder(ToolTransformService.mcp_provider_to_user_provider(provider, for_list=True))
|
2025-07-10 14:01:34 +08:00
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tools/mcp")
|
2025-07-10 14:01:34 +08:00
|
|
|
class ToolMCPListAllApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self):
|
2025-10-14 10:20:37 +09:00
|
|
|
_, tenant_id = current_account_with_tenant()
|
2025-07-10 14:01:34 +08:00
|
|
|
|
2025-10-27 17:07:51 +08:00
|
|
|
with Session(db.engine) as session, session.begin():
|
|
|
|
|
service = MCPToolManageService(session=session)
|
|
|
|
|
# Skip sensitive data decryption for list view to improve performance
|
|
|
|
|
tools = service.list_providers(tenant_id=tenant_id, include_sensitive=False)
|
2025-07-10 14:01:34 +08:00
|
|
|
|
2025-10-27 17:07:51 +08:00
|
|
|
return [tool.to_dict() for tool in tools]
|
2025-07-10 14:01:34 +08:00
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/workspaces/current/tool-provider/mcp/update/<path:provider_id>")
|
2025-07-10 14:01:34 +08:00
|
|
|
class ToolMCPUpdateApi(Resource):
|
|
|
|
|
@setup_required
|
|
|
|
|
@login_required
|
|
|
|
|
@account_initialization_required
|
|
|
|
|
def get(self, provider_id):
|
2025-10-14 10:20:37 +09:00
|
|
|
_, tenant_id = current_account_with_tenant()
|
2025-10-27 17:07:51 +08:00
|
|
|
with Session(db.engine) as session, session.begin():
|
|
|
|
|
service = MCPToolManageService(session=session)
|
|
|
|
|
tools = service.list_provider_tools(
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
provider_id=provider_id,
|
|
|
|
|
)
|
|
|
|
|
return jsonable_encoder(tools)
|
2025-07-10 14:01:34 +08:00
|
|
|
|
|
|
|
|
|
2025-11-13 13:38:45 +09:00
|
|
|
parser_cb = (
|
|
|
|
|
reqparse.RequestParser()
|
|
|
|
|
.add_argument("code", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
.add_argument("state", type=str, required=True, nullable=False, location="args")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-10-10 17:23:39 +09:00
|
|
|
@console_ns.route("/mcp/oauth/callback")
|
2025-07-10 14:01:34 +08:00
|
|
|
class ToolMCPCallbackApi(Resource):
|
2025-11-13 13:38:45 +09:00
|
|
|
@api.expect(parser_cb)
|
2025-07-10 14:01:34 +08:00
|
|
|
def get(self):
|
2025-11-13 13:38:45 +09:00
|
|
|
args = parser_cb.parse_args()
|
2025-07-10 14:01:34 +08:00
|
|
|
state_key = args["state"]
|
|
|
|
|
authorization_code = args["code"]
|
2025-10-27 17:07:51 +08:00
|
|
|
|
|
|
|
|
# Create service instance for handle_callback
|
|
|
|
|
with Session(db.engine) as session, session.begin():
|
|
|
|
|
mcp_service = MCPToolManageService(session=session)
|
|
|
|
|
# handle_callback now returns state data and tokens
|
|
|
|
|
state_data, tokens = handle_callback(state_key, authorization_code)
|
|
|
|
|
# Save tokens using the service layer
|
|
|
|
|
mcp_service.save_oauth_data(
|
|
|
|
|
state_data.provider_id, state_data.tenant_id, tokens.model_dump(), OAuthDataType.TOKENS
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-10 14:01:34 +08:00
|
|
|
return redirect(f"{dify_config.CONSOLE_WEB_URL}/oauth-callback")
|