|
|
|
|
@ -1,7 +1,6 @@
|
|
|
|
|
import hashlib
|
|
|
|
|
import json
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import or_
|
|
|
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
|
@ -18,18 +17,7 @@ from extensions.ext_database import db
|
|
|
|
|
from models.tools import MCPToolProvider
|
|
|
|
|
from services.tools.tools_transform_service import ToolTransformService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def mask_url(url: str, mask_char: str = "*"):
|
|
|
|
|
"""
|
|
|
|
|
mask the url to a simple string
|
|
|
|
|
"""
|
|
|
|
|
parsed = urlparse(url)
|
|
|
|
|
base_url = f"{parsed.scheme}://{parsed.netloc}"
|
|
|
|
|
|
|
|
|
|
if parsed.path and parsed.path != "/":
|
|
|
|
|
return f"{base_url}/{mask_char * 6}"
|
|
|
|
|
else:
|
|
|
|
|
return base_url
|
|
|
|
|
UNCHANGED_SERVER_URL_PLACEHOLDER = "[__HIDDEN__]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MCPToolManageService:
|
|
|
|
|
@ -38,15 +26,26 @@ class MCPToolManageService:
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_mcp_provider_by_provider_id(provider_id: str, tenant_id: str) -> MCPToolProvider | None:
|
|
|
|
|
return (
|
|
|
|
|
def get_mcp_provider_by_provider_id(provider_id: str, tenant_id: str) -> MCPToolProvider:
|
|
|
|
|
res = (
|
|
|
|
|
db.session.query(MCPToolProvider)
|
|
|
|
|
.filter(
|
|
|
|
|
MCPToolProvider.id == provider_id,
|
|
|
|
|
MCPToolProvider.tenant_id == tenant_id,
|
|
|
|
|
)
|
|
|
|
|
.filter(MCPToolProvider.tenant_id == tenant_id, MCPToolProvider.id == provider_id)
|
|
|
|
|
.first()
|
|
|
|
|
)
|
|
|
|
|
if not res:
|
|
|
|
|
raise ValueError("MCP tool not found")
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_mcp_provider_by_server_identifier(server_identifier: str, tenant_id: str) -> MCPToolProvider:
|
|
|
|
|
res = (
|
|
|
|
|
db.session.query(MCPToolProvider)
|
|
|
|
|
.filter(MCPToolProvider.tenant_id == tenant_id, MCPToolProvider.server_identifier == server_identifier)
|
|
|
|
|
.first()
|
|
|
|
|
)
|
|
|
|
|
if not res:
|
|
|
|
|
raise ValueError("MCP tool not found")
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def create_mcp_provider(
|
|
|
|
|
@ -109,11 +108,11 @@ class MCPToolManageService:
|
|
|
|
|
@classmethod
|
|
|
|
|
def list_mcp_tool_from_remote_server(cls, tenant_id: str, provider_id: str):
|
|
|
|
|
mcp_provider = cls.get_mcp_provider_by_provider_id(provider_id, tenant_id)
|
|
|
|
|
server_url = cls.get_mcp_provider_server_url(tenant_id, provider_id)
|
|
|
|
|
if mcp_provider is None:
|
|
|
|
|
raise ValueError("MCP tool not found")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with MCPClient(server_url, provider_id, tenant_id, authed=mcp_provider.authed) as mcp_client:
|
|
|
|
|
with MCPClient(
|
|
|
|
|
mcp_provider.decrypted_server_url, provider_id, tenant_id, authed=mcp_provider.authed, for_list=True
|
|
|
|
|
) as mcp_client:
|
|
|
|
|
tools = mcp_client.list_tools()
|
|
|
|
|
except MCPAuthError as e:
|
|
|
|
|
raise ValueError("Please auth the tool first")
|
|
|
|
|
@ -130,25 +129,17 @@ class MCPToolManageService:
|
|
|
|
|
type=ToolProviderType.MCP,
|
|
|
|
|
icon=mcp_provider.icon,
|
|
|
|
|
author=mcp_provider.user.name if mcp_provider.user else "Anonymous",
|
|
|
|
|
server_url=cls.get_masked_mcp_provider_server_url(tenant_id, provider_id),
|
|
|
|
|
server_url=mcp_provider.masked_server_url,
|
|
|
|
|
updated_at=int(mcp_provider.updated_at.timestamp()),
|
|
|
|
|
description=I18nObject(en_US="", zh_Hans=""),
|
|
|
|
|
label=I18nObject(en_US=mcp_provider.name, zh_Hans=mcp_provider.name),
|
|
|
|
|
plugin_unique_identifier=mcp_provider.server_identifier,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def retrieve_mcp_provider(cls, tenant_id: str, provider_id: str):
|
|
|
|
|
provider = cls.get_mcp_provider_by_provider_id(provider_id, tenant_id)
|
|
|
|
|
if provider is None:
|
|
|
|
|
raise ValueError("MCP tool not found")
|
|
|
|
|
return ToolTransformService.mcp_provider_to_user_provider(provider).to_dict()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def delete_mcp_tool(cls, tenant_id: str, provider_id: str):
|
|
|
|
|
mcp_tool = cls.get_mcp_provider_by_provider_id(provider_id, tenant_id)
|
|
|
|
|
if mcp_tool is None:
|
|
|
|
|
raise ValueError("MCP tool not found")
|
|
|
|
|
|
|
|
|
|
db.session.delete(mcp_tool)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
@ -165,60 +156,38 @@ class MCPToolManageService:
|
|
|
|
|
server_identifier: str,
|
|
|
|
|
):
|
|
|
|
|
mcp_provider = cls.get_mcp_provider_by_provider_id(provider_id, tenant_id)
|
|
|
|
|
if mcp_provider is None:
|
|
|
|
|
raise ValueError("MCP tool not found")
|
|
|
|
|
|
|
|
|
|
mcp_provider.name = name
|
|
|
|
|
mcp_provider.icon = (
|
|
|
|
|
json.dumps({"content": icon, "background": icon_background}) if icon_type == "emoji" else icon
|
|
|
|
|
)
|
|
|
|
|
mcp_provider.server_identifier = server_identifier
|
|
|
|
|
|
|
|
|
|
if "[__HIDDEN__]" in server_url:
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return
|
|
|
|
|
encrypted_server_url = encrypter.encrypt_token(tenant_id, server_url)
|
|
|
|
|
mcp_provider.server_url = encrypted_server_url
|
|
|
|
|
server_url_hash = hashlib.sha256(server_url.encode()).hexdigest()
|
|
|
|
|
# if the server url is changed, we need to re-auth the tool
|
|
|
|
|
try:
|
|
|
|
|
if UNCHANGED_SERVER_URL_PLACEHOLDER not in server_url:
|
|
|
|
|
encrypted_server_url = encrypter.encrypt_token(tenant_id, server_url)
|
|
|
|
|
mcp_provider.server_url = encrypted_server_url
|
|
|
|
|
server_url_hash = hashlib.sha256(server_url.encode()).hexdigest()
|
|
|
|
|
|
|
|
|
|
if server_url_hash != mcp_provider.server_url_hash:
|
|
|
|
|
try:
|
|
|
|
|
with MCPClient(
|
|
|
|
|
server_url,
|
|
|
|
|
provider_id,
|
|
|
|
|
tenant_id,
|
|
|
|
|
authed=False,
|
|
|
|
|
) as mcp_client:
|
|
|
|
|
tools = mcp_client.list_tools()
|
|
|
|
|
mcp_provider.authed = True
|
|
|
|
|
mcp_provider.tools = json.dumps([tool.model_dump() for tool in tools])
|
|
|
|
|
except MCPAuthError:
|
|
|
|
|
mcp_provider.authed = False
|
|
|
|
|
mcp_provider.tools = "[]"
|
|
|
|
|
mcp_provider.encrypted_credentials = "{}"
|
|
|
|
|
cls._re_auth_mcp_provider(mcp_provider, provider_id, tenant_id)
|
|
|
|
|
mcp_provider.server_url_hash = server_url_hash
|
|
|
|
|
try:
|
|
|
|
|
db.session.commit()
|
|
|
|
|
except IntegrityError as e:
|
|
|
|
|
db.session.rollback()
|
|
|
|
|
# Check if the error message contains the constraint name
|
|
|
|
|
if "unique_mcp_provider_name" in str(e.orig):
|
|
|
|
|
# Raise your custom exception
|
|
|
|
|
raise ValueError(f"A provider with name '{name}' already exists.")
|
|
|
|
|
elif "unique_mcp_provider_server_url" in str(e.orig):
|
|
|
|
|
# You can define another custom exception for the other constraint
|
|
|
|
|
raise ValueError(f"A provider for server URL '{server_url}' already exists.")
|
|
|
|
|
error_msg = str(e.orig)
|
|
|
|
|
if "unique_mcp_provider_name" in error_msg:
|
|
|
|
|
raise ValueError(f"MCP tool {name} already exists")
|
|
|
|
|
elif "unique_mcp_provider_server_url" in error_msg:
|
|
|
|
|
raise ValueError(f"MCP tool {server_url} already exists")
|
|
|
|
|
else:
|
|
|
|
|
# Re-raise the original exception if it's not the one you're handling
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def update_mcp_provider_credentials(cls, tenant_id: str, provider_id: str, credentials: dict, authed: bool = False):
|
|
|
|
|
mcp_provider = cls.get_mcp_provider_by_provider_id(provider_id, tenant_id)
|
|
|
|
|
if mcp_provider is None:
|
|
|
|
|
raise ValueError("MCP tool not found")
|
|
|
|
|
def update_mcp_provider_credentials(cls, mcp_provider: MCPToolProvider, credentials: dict, authed: bool = False):
|
|
|
|
|
provider_controller = MCPToolProviderController._from_db(mcp_provider)
|
|
|
|
|
tool_configuration = ProviderConfigEncrypter(
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
tenant_id=mcp_provider.tenant_id,
|
|
|
|
|
config=list(provider_controller.get_credentials_schema()),
|
|
|
|
|
provider_type=provider_controller.provider_type.value,
|
|
|
|
|
provider_identity=provider_controller.provider_id,
|
|
|
|
|
@ -229,27 +198,22 @@ class MCPToolManageService:
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_mcp_provider_decrypted_credentials(cls, tenant_id: str, provider_id: str):
|
|
|
|
|
mcp_provider = cls.get_mcp_provider_by_provider_id(provider_id, tenant_id)
|
|
|
|
|
if mcp_provider is None:
|
|
|
|
|
raise ValueError("MCP tool not found")
|
|
|
|
|
provider_controller = MCPToolProviderController._from_db(mcp_provider)
|
|
|
|
|
tool_configuration = ProviderConfigEncrypter(
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
config=list(provider_controller.get_credentials_schema()),
|
|
|
|
|
provider_type=provider_controller.provider_type.value,
|
|
|
|
|
provider_identity=provider_controller.provider_id,
|
|
|
|
|
)
|
|
|
|
|
return tool_configuration.decrypt(mcp_provider.credentials, use_cache=False)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_mcp_provider_server_url(cls, tenant_id: str, provider_id: str):
|
|
|
|
|
mcp_provider = cls.get_mcp_provider_by_provider_id(provider_id, tenant_id)
|
|
|
|
|
if mcp_provider is None:
|
|
|
|
|
raise ValueError("MCP tool not found")
|
|
|
|
|
return encrypter.decrypt_token(tenant_id, mcp_provider.server_url)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_masked_mcp_provider_server_url(cls, tenant_id: str, provider_id: str):
|
|
|
|
|
server_url = cls.get_mcp_provider_server_url(tenant_id, provider_id)
|
|
|
|
|
return mask_url(server_url)
|
|
|
|
|
def _re_auth_mcp_provider(cls, mcp_provider: MCPToolProvider, provider_id: str, tenant_id: str):
|
|
|
|
|
"""re-auth mcp provider"""
|
|
|
|
|
try:
|
|
|
|
|
with MCPClient(
|
|
|
|
|
mcp_provider.decrypted_server_url,
|
|
|
|
|
provider_id,
|
|
|
|
|
tenant_id,
|
|
|
|
|
authed=False,
|
|
|
|
|
for_list=True,
|
|
|
|
|
) as mcp_client:
|
|
|
|
|
tools = mcp_client.list_tools()
|
|
|
|
|
mcp_provider.authed = True
|
|
|
|
|
mcp_provider.tools = json.dumps([tool.model_dump() for tool in tools])
|
|
|
|
|
except MCPAuthError:
|
|
|
|
|
mcp_provider.authed = False
|
|
|
|
|
mcp_provider.tools = "[]"
|
|
|
|
|
|
|
|
|
|
# reset credentials
|
|
|
|
|
mcp_provider.encrypted_credentials = "{}"
|
|
|
|
|
|