Skip to content

Commit c81ff64

Browse files
authored
fix: fix the system message in simple-chatbot example (#1394)
1 parent 8dd15ac commit c81ff64

File tree

1 file changed

+8
-1
lines changed
  • examples/clients/simple-chatbot/mcp_simple_chatbot

1 file changed

+8
-1
lines changed

examples/clients/simple-chatbot/mcp_simple_chatbot/main.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,15 @@ async def process_llm_response(self, llm_response: str) -> str:
291291
"""
292292
import json
293293

294+
def _clean_json_string(json_string: str) -> str:
295+
"""Remove ```json ... ``` or ``` ... ``` wrappers if the LLM response is fenced."""
296+
import re
297+
298+
pattern = r"^```(?:\s*json)?\s*(.*?)\s*```$"
299+
return re.sub(pattern, r"\1", json_string, flags=re.DOTALL | re.IGNORECASE).strip()
300+
294301
try:
295-
tool_call = json.loads(llm_response)
302+
tool_call = json.loads(_clean_json_string(llm_response))
296303
if "tool" in tool_call and "arguments" in tool_call:
297304
logging.info(f"Executing tool: {tool_call['tool']}")
298305
logging.info(f"With arguments: {tool_call['arguments']}")

0 commit comments

Comments
 (0)