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 google drive (#2716)

Tao Sun0bcccfcf8e26
diff --git a/camel/memories/context_creators/score_based.py b/camel/memories/context_creators/score_based.py
index fa6aeb042..6d2b9ea34 100644
--- a/camel/memories/context_creators/score_based.py
+++ b/camel/memories/context_creators/score_based.py
@@ -155,16 +155,21 @@ class ScoreBasedContextCreator(BaseContextCreator):
# ======================
# 6. Truncation Logic with Tool Call Awareness
# ======================
- logger.warning(
- f"Context truncation required "
- f"({total_tokens} > {self.token_limit}), "
- f"pruning low-score messages."
- )
-
remaining_units = self._truncate_with_tool_call_awareness(
regular_units, tool_call_groups, system_tokens
)
+ # Log only after truncation is actually performed so that both
+ # the original and the final token counts are visible.
+ tokens_after = system_tokens + sum(
+ u.num_tokens for u in remaining_units
+ )
+ logger.warning(
+ "Context truncation performed: "
+ f"before={total_tokens}, after={tokens_after}, "
+ f"limit={self.token_limit}"
+ )
+
# ======================
# 7. Output Assembly
# ======================
diff --git a/camel/toolkits/__init__.py b/camel/toolkits/__init__.py
index 486023d57..3fdb08cc6 100644
--- a/camel/toolkits/__init__.py
+++ b/camel/toolkits/__init__.py
@@ -79,10 +79,10 @@ from .wolfram_alpha_toolkit import WolframAlphaToolkit
from .task_planning_toolkit import TaskPlanningToolkit
from .non_visual_browser_toolkit import BrowserNonVisualToolkit
from .edgeone_pages_mcp_toolkit import EdgeOnePagesMCPToolkit
+from .google_drive_mcp_toolkit import GoogleDriveMCPToolkit
from .craw4ai_toolkit import Crawl4AIToolkit
from .markitdown_toolkit import MarkItDownToolkit
-
__all__ = [
'BaseToolkit',
'FunctionTool',
@@ -148,6 +148,7 @@ __all__ = [
'TaskPlanningToolkit',
'BrowserNonVisualToolkit',
'EdgeOnePagesMCPToolkit',
+ 'GoogleDriveMCPToolkit',
'Crawl4AIToolkit',
'MarkItDownToolkit',
]
diff --git a/camel/toolkits/google_drive_mcp_toolkit.py b/camel/toolkits/google_drive_mcp_toolkit.py
new file mode 100644
index 000000000..797200a9b
--- /dev/null
+++ b/camel/toolkits/google_drive_mcp_toolkit.py
@@ -0,0 +1,73 @@
+# ========= 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 GoogleDriveMCPToolkit(BaseToolkit):
+ r"""GoogleDriveMCPToolkit provides an interface for interacting with
+ Google Drive using the Google Drive MCP server.
+
+ Attributes:
+ timeout (Optional[float]): Connection timeout in seconds.
+ (default: :obj:`None`)
+ """
+
+ def __init__(
+ self,
+ timeout: Optional[float] = None,
+ credentials_path: Optional[str] = None,
+ ) -> None:
+ r"""Initializes the GoogleDriveMCPToolkit.
+
+ Args:
+ timeout (Optional[float]): Connection timeout in seconds.
+ (default: :obj:`None`)
+ credentials_path (Optional[str]): Path to the Google Drive
+ credentials file. (default: :obj:`None`)
+ """
+ super().__init__(timeout=timeout)
+
+ self._mcp_toolkit = MCPToolkit(
+ config_dict={
+ "mcpServers": {
+ "gdrive": {
+ "command": "npx",
+ "args": ["-y", "@modelcontextprotocol/server-gdrive"],
+ "env": {"GDRIVE_CREDENTIALS_PATH": credentials_path},
+ }
+ }
+ },
+ timeout=timeout,
+ )
+
+ async def connect(self):
+ r"""Explicitly connect to the Google Drive MCP server."""
+ await self._mcp_toolkit.connect()
+
+ async def disconnect(self):
+ r"""Explicitly disconnect from the Google Drive MCP server."""
+ await self._mcp_toolkit.disconnect()
+
+ def get_tools(self) -> List[FunctionTool]:
+ r"""Returns a list of tools provided by the GoogleDriveMCPToolkit.
+
+ Returns:
+ List[FunctionTool]: List of available tools.
+ """
+ return self._mcp_toolkit.get_tools()
diff --git a/examples/toolkits/goole_drive_mcp_toolkit.py b/examples/toolkits/goole_drive_mcp_toolkit.py
new file mode 100644
index 000000000..060d168a2
--- /dev/null
+++ b/examples/toolkits/goole_drive_mcp_toolkit.py
@@ -0,0 +1,51 @@
+# ========= 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 GoogleDriveMCPToolkit
+from camel.types import ModelPlatformType, ModelType
+
+
+async def main():
+ google_drive_mcp_toolkit = GoogleDriveMCPToolkit(
+ credentials_path="path/to/credentials.json"
+ )
+
+ # connect to google drive mcp
+ await google_drive_mcp_toolkit.connect()
+
+ model = ModelFactory.create(
+ model_platform=ModelPlatformType.DEFAULT,
+ model_type=ModelType.DEFAULT,
+ )
+
+ chat_agent = ChatAgent(
+ model=model,
+ tools=google_drive_mcp_toolkit.get_tools(),
+ )
+
+ response = await chat_agent.astep(
+ "what is the name of the file in the root of my drive?",
+ )
+ print(response.msg.content)
+
+ # disconnect from google drive mcp
+ await google_drive_mcp_toolkit.disconnect()
+
+
+# Run the async main function
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/examples/workforce/eigent.py b/examples/workforce/eigent.py
index 3a8553d0c..1b4014f6c 100644
--- a/examples/workforce/eigent.py
+++ b/examples/workforce/eigent.py
@@ -28,6 +28,7 @@ from camel.toolkits import (
EdgeOnePagesMCPToolkit,
FileWriteToolkit,
FunctionTool,
+ GoogleDriveMCPToolkit,
HumanToolkit,
ImageAnalysisToolkit,
LinkedInToolkit,
@@ -168,12 +169,17 @@ Here are some tips that help you perform web search:
)
-def document_agent_factory(model: BaseModelBackend, task_id: str):
+def document_agent_factory(
+ model: BaseModelBackend,
+ task_id: str,
+ google_drive_mcp_toolkit: GoogleDriveMCPToolkit,
+):
r"""Factory for creating a document agent, based on user-provided code
structure."""
tools = [
*FileWriteToolkit().get_tools(),
*PPTXToolkit().get_tools(),
+ *google_drive_mcp_toolkit.get_tools(),
# *RetrievalToolkit().get_tools(),
HumanToolkit().ask_human_via_console,
*MarkItDownToolkit().get_tools(),
@@ -347,8 +353,12 @@ operations.
async def main():
edgeone_pages_mcp_toolkit = EdgeOnePagesMCPToolkit()
+ google_drive_mcp_toolkit = GoogleDriveMCPToolkit(
+ credentials_path="path/to/credentials.json"
+ )
try:
await edgeone_pages_mcp_toolkit.connect()
+ await google_drive_mcp_toolkit.connect()
# Create a single model backend for all agents
model_backend = ModelFactory.create(
@@ -379,7 +389,9 @@ async def main():
developer_agent = developer_agent_factory(
model_backend, task_id, edgeone_pages_mcp_toolkit
)
- document_agent = document_agent_factory(model_backend, task_id)
+ document_agent = document_agent_factory(
+ model_backend, task_id, google_drive_mcp_toolkit
+ )
multi_modal_agent = multi_modal_agent_factory(model_backend, task_id)
# Configure kwargs for all agents to use the same model_backend
@@ -451,6 +463,7 @@ slides should be very comprehensive and professional.
finally:
await edgeone_pages_mcp_toolkit.disconnect()
+ await google_drive_mcp_toolkit.disconnect()
if __name__ == "__main__":