From c81ff64e94469c841010c1fef43cb298c6dc78d3 Mon Sep 17 00:00:00 2001 From: Yukuan Jia Date: Tue, 30 Sep 2025 19:22:33 +0800 Subject: [PATCH] fix: fix the system message in simple-chatbot example (#1394) --- .../clients/simple-chatbot/mcp_simple_chatbot/main.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/examples/clients/simple-chatbot/mcp_simple_chatbot/main.py b/examples/clients/simple-chatbot/mcp_simple_chatbot/main.py index 65e0dde0..1a30578b 100644 --- a/examples/clients/simple-chatbot/mcp_simple_chatbot/main.py +++ b/examples/clients/simple-chatbot/mcp_simple_chatbot/main.py @@ -291,8 +291,15 @@ class ChatSession: """ import json + def _clean_json_string(json_string: str) -> str: + """Remove ```json ... ``` or ``` ... ``` wrappers if the LLM response is fenced.""" + import re + + pattern = r"^```(?:\s*json)?\s*(.*?)\s*```$" + return re.sub(pattern, r"\1", json_string, flags=re.DOTALL | re.IGNORECASE).strip() + try: - tool_call = json.loads(llm_response) + tool_call = json.loads(_clean_json_string(llm_response)) if "tool" in tool_call and "arguments" in tool_call: logging.info(f"Executing tool: {tool_call['tool']}") logging.info(f"With arguments: {tool_call['arguments']}")