Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: Add playwright mcp toolkit (#2353)

Wendong-Fand7805705efe1
diff --git a/camel/toolkits/__init__.py b/camel/toolkits/__init__.py
index 080e527e7..377c450c2 100644
--- a/camel/toolkits/__init__.py
+++ b/camel/toolkits/__init__.py
@@ -71,6 +71,7 @@ from .jina_reranker_toolkit import JinaRerankerToolkit
from .pulse_mcp_search_toolkit import PulseMCPSearchToolkit
from .klavis_toolkit import KlavisToolkit
from .aci_toolkit import ACIToolkit
+from .playwright_mcp_toolkit import PlaywrightMCPToolkit
__all__ = [
@@ -130,4 +131,5 @@ __all__ = [
'PulseMCPSearchToolkit',
'KlavisToolkit',
'ACIToolkit',
+ 'PlaywrightMCPToolkit',
]
diff --git a/camel/toolkits/playwright_mcp_toolkit.py b/camel/toolkits/playwright_mcp_toolkit.py
new file mode 100644
index 000000000..d193bbff1
--- /dev/null
+++ b/camel/toolkits/playwright_mcp_toolkit.py
@@ -0,0 +1,69 @@
+# ========= 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, Optional
+
+from camel.toolkits import BaseToolkit, FunctionTool
+
+from .mcp_toolkit import MCPToolkit
+
+
+class PlaywrightMCPToolkit(BaseToolkit):
+ r"""PlaywrightMCPToolkit provides an interface for interacting with web
+ browsers using the Playwright automation library through the Model Context
+ Protocol (MCP).
+
+ Attributes:
+ timeout (Optional[float]): Connection timeout in seconds.
+ (default: :obj:`None`)
+
+ Note:
+ Currently only supports asynchronous operation mode.
+ """
+
+ def __init__(self, timeout: Optional[float] = None) -> None:
+ r"""Initializes the PlaywrightMCPToolkit with the specified timeout.
+
+ Args:
+ timeout (Optional[float]): Connection timeout in seconds.
+ (default: :obj:`None`)
+ """
+ super().__init__(timeout=timeout)
+
+ self._mcp_toolkit = MCPToolkit(
+ config_dict={
+ "mcpServers": {
+ "playwright": {
+ "command": "npx",
+ "args": ["@playwright/mcp@latest"],
+ }
+ }
+ }
+ )
+
+ async def connect(self):
+ r"""Explicitly connect to the Playwright MCP server."""
+ await self._mcp_toolkit.connect()
+
+ async def disconnect(self):
+ r"""Explicitly disconnect from the Playwright MCP server."""
+ await self._mcp_toolkit.disconnect()
+
+ def get_tools(self) -> List[FunctionTool]:
+ r"""Returns a list of tools provided by the PlaywrightMCPToolkit.
+
+ Returns:
+ List[FunctionTool]: List of available tools.
+ """
+ return self._mcp_toolkit.get_tools()
diff --git a/examples/toolkits/openai_agent_toolkit_example.py b/examples/toolkits/openai_agent_toolkit.py
similarity index 100%
rename from examples/toolkits/openai_agent_toolkit_example.py
rename to examples/toolkits/openai_agent_toolkit.py
diff --git a/examples/toolkits/playwright_mcp_toolkit.py b/examples/toolkits/playwright_mcp_toolkit.py
new file mode 100644
index 000000000..f9581e566
--- /dev/null
+++ b/examples/toolkits/playwright_mcp_toolkit.py
@@ -0,0 +1,56 @@
+# ========= 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. =========
+import asyncio
+
+from camel.agents import ChatAgent
+from camel.models import ModelFactory
+from camel.toolkits import PlaywrightMCPToolkit
+from camel.types import ModelPlatformType, ModelType
+
+
+async def main():
+ playwright_mcp_toolkit = PlaywrightMCPToolkit()
+
+ # connect to playwright mcp
+ await playwright_mcp_toolkit.connect()
+
+ model = ModelFactory.create(
+ model_platform=ModelPlatformType.DEFAULT,
+ model_type=ModelType.DEFAULT,
+ )
+
+ chat_agent = ChatAgent(
+ model=model,
+ tools=playwright_mcp_toolkit.get_tools(),
+ )
+
+ response = await chat_agent.astep(
+ "search for camel-ai using google search, how many stars does it have?"
+ )
+ print(response.msg.content)
+
+ # disconnect from playwright mcp
+ await playwright_mcp_toolkit.disconnect()
+
+
+# Run the async main function
+if __name__ == "__main__":
+ asyncio.run(main())
+
+"""
+==============================================================================
+The GitHub repository for **camel-ai** has **12,338 stars**. If you need more
+information or details about the project, feel free to ask!
+==============================================================================
+"""
diff --git a/examples/toolkits/searxng_toolkit_example.py b/examples/toolkits/searxng_toolkit.py
similarity index 100%
rename from examples/toolkits/searxng_toolkit_example.py
rename to examples/toolkits/searxng_toolkit.py