Skip to content

BUG: Remote MCP return string instead of json dict #1596

@rudaoshi

Description

@rudaoshi

Problem
The return value of remote MCP is not a json dict but a string.

Steps to reproduce

  1. Build standard mcp services, which each return a dict
  2. Run following code, if the generated code require to print a filed of the result, error occur saying that cannot index a str using str.
from smolagents import ToolCollection, CodeAgent

from smolagents import OpenAIServerModel

model = OpenAIServerModel(
    model_id=os.getenv("LLM_MODEL"),
    api_base=os.getenv("LLM_BASE_URL"), # Leave this blank to query OpenAI servers.
    api_key=os.getenv("LLM_API_KEY"), # Switch to the API key for the server you're targeting.
)

with ToolCollection.from_mcp({"url": "http://127.0.0.1:8000/stock_info/mcp", "transport": "streamable-http"}, trust_remote_code=True) as stock_info_tools:
    agent = CodeAgent(tools=[*stock_info_tools.tools], 
                      add_base_tools=True, model=model)
    agent.run("Please output stock price of AAPL in the last 30 days")

Actual behavior and error logs

The smolagent produces following code and runs into error mentioned above.

  import datetime                                                                                                          
                                                                                                                           
  # Get today's date and 30 days ago                                                                                       
  today = datetime.datetime.now().date()                                                                                   
  start_date = (today - datetime.timedelta(days=40)).strftime("%Y-%m-%d")  # padding for non-trading days                  
  end_date = today.strftime("%Y-%m-%d")                                                                                    
                                                                                                                           
  # Fetch stock data for AAPL for US market                                                                                
  aapl_data = stock_data(                                                                                                  
      ticker="AAPL",                                                                                                       
      startdate=start_date,                                                                                                
      enddate=end_date,                                                                                                    
      period="daily",                                                                                                      
      market="us",                                                                                                         
      adjust="qfq"                                                                                                         
  )                                                                                                                        
  print("Columns:", aapl_data["columns"])      # <---- error occurs                                                                                
  print("Number of rows:", len(aapl_data["data"]))                                                                         
  print("Sample row:", aapl_data["data"][-1] if aapl_data["data"] else "No data")    

Expected behavior
The remote mcp tool calling should return the exact object defined in function.

Environment:
Please complete the following information:

  • OS: Mac Os
  • Python version: 3.12
  • Package version: 1.20

Add any other context, screenshots, or links about the bug here.


Checklist

  • I have searched the existing issues and have not found a similar bug report.
  • I have provided a minimal, reproducible example.
  • I have provided the full traceback of the error.
  • I have provided my environment details.
  • I am willing to work on this issue and submit a pull request. (optional)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions