Skip to content

Commit 612ea05

Browse files
committed
refactor: improve readability and structure in SmolAgentsAdapter (ruff format recommendation)
1 parent 1cb42c5 commit 612ea05

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/mcpadapt/smolagents_adapter.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,20 @@ def adapt(
108108

109109
# Extract and resolve outputSchema if present (only if structured_output=True)
110110
output_schema = None
111-
if self.structured_output and hasattr(mcp_tool, 'outputSchema') and mcp_tool.outputSchema:
111+
if (
112+
self.structured_output
113+
and hasattr(mcp_tool, "outputSchema")
114+
and mcp_tool.outputSchema
115+
):
112116
try:
113117
output_schema = jsonref.replace_refs(mcp_tool.outputSchema)
114118
except Exception as e:
115-
logger.warning(f"Failed to resolve outputSchema for tool {mcp_tool.name}: {e}")
116-
output_schema = mcp_tool.outputSchema # Use unresolved schema as fallback
119+
logger.warning(
120+
f"Failed to resolve outputSchema for tool {mcp_tool.name}: {e}"
121+
)
122+
output_schema = (
123+
mcp_tool.outputSchema
124+
) # Use unresolved schema as fallback
117125

118126
# Always use "object" output_type for maximum flexibility
119127
# Smolagents will handle type detection at runtime
@@ -138,7 +146,9 @@ def __init__(
138146
self.is_initialized = True
139147
self.skip_forward_signature_validation = True
140148

141-
def forward(self, *args, **kwargs) -> Union[str, "PILImage", "torch.Tensor", Any]:
149+
def forward(
150+
self, *args, **kwargs
151+
) -> Union[str, "PILImage", "torch.Tensor", Any]:
142152
if len(args) > 0:
143153
if len(args) == 1 and isinstance(args[0], dict) and not kwargs:
144154
mcp_output = func(args[0])
@@ -156,7 +166,10 @@ def forward(self, *args, **kwargs) -> Union[str, "PILImage", "torch.Tensor", Any
156166
# Handle structured features if enabled
157167
if self.structured_output:
158168
# Prioritize structuredContent if available
159-
if hasattr(mcp_output, 'structuredContent') and mcp_output.structuredContent is not None:
169+
if (
170+
hasattr(mcp_output, "structuredContent")
171+
and mcp_output.structuredContent is not None
172+
):
160173
return mcp_output.structuredContent
161174

162175
# Handle multiple content warning (unified for both modes)

tests/test_smolagents_adapter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ def test_audio() -> AudioContent:
261261
audio_content = tools[0]()
262262
assert isinstance(audio_content, Tensor)
263263

264+
264265
def test_structured_output_types():
265266
"""Test that structured output returns correct types for different return annotations."""
266267
server_script = dedent(

0 commit comments

Comments
 (0)