-
Notifications
You must be signed in to change notification settings - Fork 47
Smolagents: support ImageContent and AudioContent #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
bab3350
d48f553
c1fbdba
1ea11ac
c4f1619
91c7d01
d6855a3
be83e12
8ee0e32
4090e44
06a42a0
39e0d3e
21670f7
bf3d63c
53fe4b2
ef3559f
cbbfd3b
626b39a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ requires-python = ">=3.10" | |
| license = "MIT" | ||
| authors = [{ name = "Guillaume Raille", email = "[email protected]" }] | ||
| dependencies = [ | ||
| "mcp>=1.9.0", | ||
| "mcp>=1.9.4", | ||
| "jsonref>=1.1.0", | ||
| "python-dotenv>=1.0.1", | ||
| "pydantic>=2.10.6", | ||
|
|
@@ -55,6 +55,9 @@ crewai = [ | |
| google-genai = [ | ||
| "google-genai>=1.2.0", | ||
| ] | ||
| audio = [ | ||
| "torchaudio>=2.7.1", | ||
| ] | ||
|
|
||
| [tool.hatch.version] | ||
| path = "src/mcpadapt/__init__.py" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,14 +8,17 @@ | |
| >>> print(tools) | ||
| """ | ||
|
|
||
| import logging | ||
| import base64 | ||
| import keyword | ||
| import logging | ||
| import re | ||
| from io import BytesIO | ||
| from typing import Any, Callable, Coroutine | ||
|
|
||
| import jsonref # type: ignore | ||
| import mcp | ||
| import smolagents # type: ignore | ||
| from smolagents.utils import _is_package_available | ||
|
|
||
| from mcpadapt.core import ToolAdapter | ||
|
|
||
|
|
@@ -85,7 +88,7 @@ def __init__( | |
| self.is_initialized = True | ||
| self.skip_forward_signature_validation = True | ||
|
|
||
| def forward(self, *args, **kwargs) -> str: | ||
| def forward(self, *args, **kwargs): | ||
|
||
| if len(args) > 0: | ||
| if len(args) == 1 and isinstance(args[0], dict) and not kwargs: | ||
| mcp_output = func(args[0]) | ||
|
|
@@ -104,12 +107,35 @@ def forward(self, *args, **kwargs) -> str: | |
| f"tool {self.name} returned multiple content, using the first one" | ||
| ) | ||
|
|
||
| if not isinstance(mcp_output.content[0], mcp.types.TextContent): | ||
| raise ValueError( | ||
| f"tool {self.name} returned a non-text content: `{type(mcp_output.content[0])}`" | ||
| ) | ||
| content = mcp_output.content[0] | ||
|
|
||
| if isinstance(content, mcp.types.TextContent): | ||
| return content.text | ||
|
|
||
| if isinstance(content, mcp.types.ImageContent): | ||
HSGamer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from PIL import Image | ||
|
|
||
| image_data = base64.b64decode(content.data) | ||
| image = Image.open(BytesIO(image_data)) | ||
| return image | ||
|
|
||
| if isinstance(content, mcp.types.AudioContent): | ||
HSGamer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if not _is_package_available("torchaudio"): | ||
| raise ValueError( | ||
| "Audio content requires the torchaudio package to be installed. " | ||
| "Please install it with `pip install torchaudio`.", | ||
HSGamer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| else: | ||
| import torchaudio | ||
|
|
||
| audio_data = base64.b64decode(content.data) | ||
| audio_io = BytesIO(audio_data) | ||
| audio_tensor, _ = torchaudio.load(audio_io) | ||
| return audio_tensor | ||
|
|
||
| return mcp_output.content[0].text # type: ignore | ||
| raise ValueError( | ||
| f"tool {self.name} returned an unsupported content type: {type(content)}" | ||
| ) | ||
|
|
||
| # make sure jsonref are resolved | ||
| input_schema = { | ||
|
|
@@ -129,7 +155,7 @@ def forward(self, *args, **kwargs) -> str: | |
| name=mcp_tool.name, | ||
| description=mcp_tool.description or "", | ||
| inputs=input_schema["properties"], | ||
| output_type="string", | ||
| output_type="object", | ||
| ) | ||
|
|
||
| return tool | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.