mirror of
https://github.com/langgenius/dify.git
synced 2026-04-27 02:36:29 +08:00
feat: Add Audio Content Support for MCP Tools (#27979)
This commit is contained in:
parent
becabd84a9
commit
ab39f39db0
@ -1,16 +1,19 @@
|
|||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from core.mcp.auth_client import MCPClientWithAuthRetry
|
from core.mcp.auth_client import MCPClientWithAuthRetry
|
||||||
from core.mcp.error import MCPConnectionError
|
from core.mcp.error import MCPConnectionError
|
||||||
from core.mcp.types import CallToolResult, ImageContent, TextContent
|
from core.mcp.types import AudioContent, CallToolResult, ImageContent, TextContent
|
||||||
from core.tools.__base.tool import Tool
|
from core.tools.__base.tool import Tool
|
||||||
from core.tools.__base.tool_runtime import ToolRuntime
|
from core.tools.__base.tool_runtime import ToolRuntime
|
||||||
from core.tools.entities.tool_entities import ToolEntity, ToolInvokeMessage, ToolProviderType
|
from core.tools.entities.tool_entities import ToolEntity, ToolInvokeMessage, ToolProviderType
|
||||||
from core.tools.errors import ToolInvokeError
|
from core.tools.errors import ToolInvokeError
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class MCPTool(Tool):
|
class MCPTool(Tool):
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -52,6 +55,11 @@ class MCPTool(Tool):
|
|||||||
yield from self._process_text_content(content)
|
yield from self._process_text_content(content)
|
||||||
elif isinstance(content, ImageContent):
|
elif isinstance(content, ImageContent):
|
||||||
yield self._process_image_content(content)
|
yield self._process_image_content(content)
|
||||||
|
elif isinstance(content, AudioContent):
|
||||||
|
yield self._process_audio_content(content)
|
||||||
|
else:
|
||||||
|
logger.warning("Unsupported content type=%s", type(content))
|
||||||
|
|
||||||
# handle MCP structured output
|
# handle MCP structured output
|
||||||
if self.entity.output_schema and result.structuredContent:
|
if self.entity.output_schema and result.structuredContent:
|
||||||
for k, v in result.structuredContent.items():
|
for k, v in result.structuredContent.items():
|
||||||
@ -97,6 +105,10 @@ class MCPTool(Tool):
|
|||||||
"""Process image content and return a blob message."""
|
"""Process image content and return a blob message."""
|
||||||
return self.create_blob_message(blob=base64.b64decode(content.data), meta={"mime_type": content.mimeType})
|
return self.create_blob_message(blob=base64.b64decode(content.data), meta={"mime_type": content.mimeType})
|
||||||
|
|
||||||
|
def _process_audio_content(self, content: AudioContent) -> ToolInvokeMessage:
|
||||||
|
"""Process audio content and return a blob message."""
|
||||||
|
return self.create_blob_message(blob=base64.b64decode(content.data), meta={"mime_type": content.mimeType})
|
||||||
|
|
||||||
def fork_tool_runtime(self, runtime: ToolRuntime) -> "MCPTool":
|
def fork_tool_runtime(self, runtime: ToolRuntime) -> "MCPTool":
|
||||||
return MCPTool(
|
return MCPTool(
|
||||||
entity=self.entity,
|
entity=self.entity,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user