🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
enhancement: improve mcp agent parse json (#3207)
Xiaotian Jin66d5b24ae449
diff --git a/camel/agents/mcp_agent.py b/camel/agents/mcp_agent.pyindex c1d13c45c..309087534 100644--- a/camel/agents/mcp_agent.py+++ b/camel/agents/mcp_agent.py@@ -15,16 +15,25 @@import asyncioimport jsonimport platform-import re-from typing import Any, Callable, Dict, List, Optional, Union, cast+from typing import (+ TYPE_CHECKING,+ Any,+ Callable,+ Dict,+ List,+ Optional,+ Union,+ cast,+)-from camel.agents import ChatAgent+from camel.agents.chat_agent import ChatAgentfrom camel.logger import get_loggerfrom camel.messages import BaseMessage-from camel.models import BaseModelBackend, ModelFactory+from camel.models.base_model import BaseModelBackend+from camel.models.model_factory import ModelFactoryfrom camel.prompts import TextPromptfrom camel.responses import ChatAgentResponse-from camel.toolkits import FunctionTool, MCPToolkit+from camel.toolkits.function_tool import FunctionToolfrom camel.types import (BaseMCPRegistryConfig,MCPRegistryType,@@ -33,6 +42,9 @@ from camel.types import (RoleType,)+if TYPE_CHECKING:+ from camel.toolkits.mcp_toolkit import MCPToolkit+# AgentOps decorator settingtry:import os@@ -44,6 +56,8 @@ try:except (ImportError, AttributeError):from camel.utils import track_agent+from camel.parsers.mcp_tool_call_parser import extract_tool_calls_from_text+logger = get_logger(__name__)@@ -168,8 +182,10 @@ class MCPAgent(ChatAgent):**kwargs,)- def _initialize_mcp_toolkit(self) -> MCPToolkit:+ def _initialize_mcp_toolkit(self) -> "MCPToolkit":r"""Initialize the MCP toolkit from the provided configuration."""+ from camel.toolkits.mcp_toolkit import MCPToolkit+config_dict = {}for registry_config in self.registry_configs:config_dict.update(registry_config.get_config())@@ -334,27 +350,14 @@ class MCPAgent(ChatAgent):task = f"## Task:\n {input_message}"input_message = str(self._text_tools) + taskresponse = await super().astep(input_message, *args, **kwargs)- content = response.msgs[0].content.lower()-- tool_calls = []- while "```json" in content:- json_match = re.search(r'```json', content)- if not json_match:- break- json_start = json_match.span()[1]-- end_match = re.search(r'```', content[json_start:])- if not end_match:- break- json_end = end_match.span()[0] + json_start-- tool_json = content[json_start:json_end].strip('\n')- try:- tool_calls.append(json.loads(tool_json))- except json.JSONDecodeError:- logger.warning(f"Failed to parse JSON: {tool_json}")- continue- content = content[json_end:]+ raw_content = response.msgs[0].content if response.msgs else ""+ content = (+ raw_content+ if isinstance(raw_content, str)+ else str(raw_content)+ )++ tool_calls = extract_tool_calls_from_text(content)if not tool_calls:return responsediff --git a/camel/parsers/__init__.py b/camel/parsers/__init__.pynew file mode 100644index 000000000..0393a171d--- /dev/null+++ b/camel/parsers/__init__.py@@ -0,0 +1,18 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+"""Helper parsers used across the CAMEL project."""++from .mcp_tool_call_parser import extract_tool_calls_from_text++__all__ = ["extract_tool_calls_from_text"]diff --git a/camel/parsers/mcp_tool_call_parser.py b/camel/parsers/mcp_tool_call_parser.pynew file mode 100644index 000000000..ea592435c--- /dev/null+++ b/camel/parsers/mcp_tool_call_parser.py@@ -0,0 +1,176 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+"""Utility functions for parsing MCP tool calls from model output."""++import ast+import json+import logging+import re+from typing import Any, Dict, List, Optional++try: # pragma: no cover - optional dependency+ import yaml+except ImportError: # pragma: no cover+ yaml = None # type: ignore[assignment]+++CODE_BLOCK_PATTERN = re.compile(+ r"```(?:[a-z0-9_-]+)?\s*([\s\S]+?)\s*```",+ re.IGNORECASE,+)++JSON_START_PATTERN = re.compile(r"[{\[]")+JSON_TOKEN_PATTERN = re.compile(+ r"""+ (?P<double>"(?:\\.|[^"\\])*")+ |+ (?P<single>'(?:\\.|[^'\\])*')+ |+ (?P<brace>[{}\[\]])+ """,+ re.VERBOSE,+)++logger = logging.getLogger(__name__)+++def extract_tool_calls_from_text(content: str) -> List[Dict[str, Any]]:+ """Extract tool call dictionaries from raw text output."""++ if not content:+ return []++ tool_calls: List[Dict[str, Any]] = []+ seen_ranges: List[tuple[int, int]] = []++ for match in CODE_BLOCK_PATTERN.finditer(content):+ snippet = match.group(1).strip()+ if not snippet:+ continue++ parsed = _try_parse_json_like(snippet)+ if parsed is None:+ logger.warning(+ "Failed to parse JSON payload from fenced block: %s",+ snippet,+ )+ continue++ _collect_tool_calls(parsed, tool_calls)+ seen_ranges.append((match.start(1), match.end(1)))++ for start_match in JSON_START_PATTERN.finditer(content):+ start_idx = start_match.start()++ if any(start <= start_idx < stop for start, stop in seen_ranges):+ continue++ segment = _find_json_candidate(content, start_idx)+ if segment is None:+ continue++ end_idx = start_idx + len(segment)+ if any(start <= start_idx < stop for start, stop in seen_ranges):+ continue++ parsed = _try_parse_json_like(segment)+ if parsed is None:+ logger.debug(+ "Unable to parse JSON-like candidate: %s",+ _truncate_snippet(segment),+ )+ continue++ _collect_tool_calls(parsed, tool_calls)+ seen_ranges.append((start_idx, end_idx))++ return tool_calls+++def _collect_tool_calls(+ payload: Any, accumulator: List[Dict[str, Any]]+) -> None:+ """Collect valid tool call dictionaries from parsed payloads."""++ if isinstance(payload, dict):+ if payload.get("tool_name") is None:+ return+ accumulator.append(payload)+ elif isinstance(payload, list):+ for item in payload:+ _collect_tool_calls(item, accumulator)+++def _try_parse_json_like(snippet: str) -> Optional[Any]:+ """Parse a JSON or JSON-like snippet into Python data."""++ try:+ return json.loads(snippet)+ except json.JSONDecodeError as exc:+ logger.debug(+ "json.loads failed: %s | snippet=%s",+ exc,+ _truncate_snippet(snippet),+ )++ if yaml is not None:+ try:+ return yaml.safe_load(snippet)+ except yaml.YAMLError:+ pass++ try:+ return ast.literal_eval(snippet)+ except (ValueError, SyntaxError):+ return None+++def _find_json_candidate(content: str, start_idx: int) -> Optional[str]:+ """Locate a balanced JSON-like segment starting at ``start_idx``."""++ opening = content[start_idx]+ if opening not in "{[":+ return None++ stack = ["}" if opening == "{" else "]"]++ for token in JSON_TOKEN_PATTERN.finditer(content, start_idx + 1):+ if token.lastgroup in {"double", "single"}:+ continue++ brace = token.group("brace")+ if brace in "{[":+ stack.append("}" if brace == "{" else "]")+ continue++ if not stack:+ return None++ expected = stack.pop()+ if brace != expected:+ return None++ if not stack:+ return content[start_idx : token.end()]++ return None+++def _truncate_snippet(snippet: str, limit: int = 120) -> str:+ """Return a truncated representation suitable for logging."""++ compact = " ".join(snippet.strip().split())+ if len(compact) <= limit:+ return compact+ return f"{compact[: limit - 3]}..."diff --git a/camel/toolkits/mcp_toolkit.py b/camel/toolkits/mcp_toolkit.pyindex 803362498..800d6be8d 100644--- a/camel/toolkits/mcp_toolkit.py+++ b/camel/toolkits/mcp_toolkit.py@@ -21,7 +21,8 @@ from typing import Any, Dict, List, Optionalfrom typing_extensions import TypeGuardfrom camel.logger import get_logger-from camel.toolkits import BaseToolkit, FunctionTool+from camel.toolkits.base import BaseToolkit+from camel.toolkits.function_tool import FunctionToolfrom camel.utils.commons import run_asyncfrom camel.utils.mcp_client import MCPClient, create_mcp_clientdiff --git a/test/agents/test_mcp_agent.py b/test/agents/test_mcp_agent.pynew file mode 100644index 000000000..2d3a1df79--- /dev/null+++ b/test/agents/test_mcp_agent.py@@ -0,0 +1,117 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========++from typing import List++import pytest++from camel.parsers.mcp_tool_call_parser import extract_tool_calls_from_text+++ "content,expected",+ [+ (+ """Here is the call:+```json+{+ \"server_idx\": 0,+ \"tool_name\": \"search_tool\",+ \"tool_args\": {\"query\": \"hello\"}+}+```+""",+ [+ {+ "server_idx": 0,+ "tool_name": "search_tool",+ "tool_args": {"query": "hello"},+ }+ ],+ ),+ (+ """Please run this: {\"server_idx\": 1, \"tool_name\": \"math\",+ \"tool_args\": {\"a\": 1, \"b\": 2}}""",+ [+ {+ "server_idx": 1,+ "tool_name": "math",+ "tool_args": {"a": 1, "b": 2},+ }+ ],+ ),+ (+ """Tool candidate {'server_idx': 0, 'tool_name': 'search',+ 'tool_args': {'query': 'plan 2'}}""",+ [+ {+ "server_idx": 0,+ "tool_name": "search",+ "tool_args": {"query": "plan 2"},+ }+ ],+ ),+ (+ """Multiple calls: [{\"server_idx\": 0, \"tool_name\": \"alpha\",+ \"tool_args\": {}}, {\"server_idx\": 1, \"tool_name\": \"beta\",+ \"tool_args\": {\"q\": \"x\"}}]""",+ [+ {"server_idx": 0, "tool_name": "alpha", "tool_args": {}},+ {+ "server_idx": 1,+ "tool_name": "beta",+ "tool_args": {"q": "x"},+ },+ ],+ ),+ ],+)+def test_extract_tool_calls_from_text(+ content: str, expected: List[dict]+) -> None:+ tool_calls = extract_tool_calls_from_text(content)++ assert len(tool_calls) == len(expected)+ for idx, tool_call in enumerate(tool_calls):+ for key, value in expected[idx].items():+ assert tool_call[key] == value+++def test_extract_tool_calls_without_payload() -> None:+ assert extract_tool_calls_from_text("No calls available") == []+++def test_extract_tool_calls_from_yaml_block() -> None:+ pytest.importorskip("yaml")++ content = (+ "Here is the YAML call:\n"+ "```yaml\n"+ "server_idx: 2\n"+ "tool_name: yaml_tool\n"+ "tool_args:\n"+ " prompt: Hello\n"+ "```\n"+ )++ expected = {+ "server_idx": 2,+ "tool_name": "yaml_tool",+ "tool_args": {"prompt": "Hello"},+ }++ tool_calls = extract_tool_calls_from_text(content)++ assert len(tool_calls) == 1+ assert tool_calls[0] == expected