Skip to content

MCP tool is returning a string representation of a dictionary rather than an actual dictionary object #100

@bayesways

Description

@bayesways

Summary:
The code example app.py in Unit 2: Gradio Server does not return json as intended. This causes an error when we run the agent in the following chapter.

Details:
The MCP tool presented in app.py is a sentiment analysis tool. Running the agent from Unit 2: Gradio Client with the prompt "Analyze the sentiment of the following text "This is awesome"" gives the following results

* Running on local URL:  http://127.0.0.1:7861
* To create a public link, set `share=True` in `launch()`.
╭────────────────────────────────── New run ───────────────────────────────────╮
│                                                                              │
│ Analyze the sentiment of the following text "This is awesome"
│                                                                              │
╰─ InferenceClientModel - Qwen/Qwen2.5-Coder-32B-Instruct ─────────────────────╯
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 1 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing parsed code: ─────────────────────────────────────────────────────
  sentiment_result = sentiment_analysis(text="This is awesome")
  print(sentiment_result)
 ──────────────────────────────────────────────────────────────────────────────
Execution logs:
root={'polarity': 1.0, 'subjectivity': 1.0, 'assessment': 'positive'}

Out: None
[Step 1: Duration 3.17 seconds| Input tokens: 2,046 | Output tokens: 51]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 2 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Error in code parsing:
Your code snippet is invalid, because the regex pattern
```(?:py|python)?\s*\n(.*?)\n``` was not found in it.
Here is your code snippet:
Observation:

Make sure to include code with the correct pattern, for instance:
Thoughts: Your thoughts
Code:
```py
# Your python code here
```<end_code>
Make sure to provide correct code blobs.
[Step 2: Duration 0.94 seconds| Input tokens: 4,245 | Output tokens: 54]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 3 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing parsed code: ─────────────────────────────────────────────────────
  sentiment_result = sentiment_analysis(text="This is awesome")
  print(sentiment_result)
 ──────────────────────────────────────────────────────────────────────────────
Execution logs:
root={'polarity': 1.0, 'subjectivity': 1.0, 'assessment': 'positive'}

Out: None
[Step 3: Duration 3.07 seconds| Input tokens: 6,574 | Output tokens: 115]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 4 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing parsed code: ─────────────────────────────────────────────────────
  final_answer(sentiment_result['assessment'])
 ──────────────────────────────────────────────────────────────────────────────
Code execution failed at line 'final_answer(sentiment_result['assessment'\])'
due to: InterpreterError: Could not index root={'polarity': 1.0, 'subjectivity':
1.0, 'assessment': 'positive'} with 'assessment': TypeError: string indices must
be integers, not 'str'
[Step 4: Duration 4.00 seconds| Input tokens: 9,066 | Output tokens: 192]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 5 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing parsed code: ─────────────────────────────────────────────────────
  print(type(sentiment_result))
  print(sentiment_result)
  final_answer(sentiment_result['assessment'])
 ──────────────────────────────────────────────────────────────────────────────
Execution logs:
<class 'str'>
root={'polarity': 1.0, 'subjectivity': 1.0, 'assessment': 'positive'}

Code execution failed at line 'final_answer(sentiment_result['assessment'\])'
due to: InterpreterError: Could not index root={'polarity': 1.0, 'subjectivity':
1.0, 'assessment': 'positive'} with 'assessment': TypeError: string indices must
be integers, not 'str'
[Step 5: Duration 4.26 seconds| Input tokens: 11,789 | Output tokens: 276]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 6 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing parsed code: ─────────────────────────────────────────────────────
  import re

  # Parse the string to extract the sentiment assessment
  sentiment_dict = eval(sentiment_result)
  final_answer(sentiment_dict['assessment'])
 ──────────────────────────────────────────────────────────────────────────────
Code execution failed at line 'sentiment_dict = eval(sentiment_result)' due to:
InterpreterError: Forbidden function evaluation: 'eval' is not among the
explicitly allowed tools or defined/imported in the preceding code
[Step 6: Duration 3.83 seconds| Input tokens: 14,799 | Output tokens: 348]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 7 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing parsed code: ─────────────────────────────────────────────────────
  import re

  # Use regular expression to extract the sentiment assessment
  match = re.search(r"'assessment':\s*'([^']*)'", sentiment_result)
  if match:
      assessment = match.group(1)
      final_answer(assessment)
  else:
      final_answer("Unable to extract assessment")
 ──────────────────────────────────────────────────────────────────────────────
Out - Final answer: positive
[Step 7: Duration 5.12 seconds| Input tokens: 18,038 | Output tokens: 442]
Error in sse_reader: peer closed connection without sending complete message body (incomplete chunked read)

This is taken from the log of the agent when I tried to run it
The sentiment_analysis tool is returning a string that looks like a dictionary ("root={'polarity': 1.0, 'subjectivity': 1.0, 'assessment': 'positive'}") instead of returning an actual Python dictionary object. See for example the following excerpt from the logs

<class 'str'>
root={'polarity': 1.0, 'subjectivity': 1.0, 'assessment': 'positive'}

The agent tries to use dictionary indexing on a string and fails.

I opened this PR to suggest a fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions