Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import typing as t |
| 2 | |
| 3 | import pydantic |
| 4 | from crewai.tools import BaseTool |
| 5 | |
| 6 | from composio.core.provider import AgenticProvider, AgenticProviderExecuteFn |
| 7 | from composio.types import Tool |
| 8 | from composio.utils.json_schema import dereference_json_schema |
| 9 | from composio.utils.pydantic import parse_pydantic_error |
| 10 | from composio.utils.shared import ( |
| 11 | json_schema_to_model, |
| 12 | normalize_tool_arguments, |
| 13 | validate_and_serialize_tool_arguments, |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | class CrewAIProvider(AgenticProvider[BaseTool, list[BaseTool]], name="crewai"): |
| 18 | """ |
| 19 | Composio toolset for CrewiAI framework. |
| 20 | """ |
| 21 | |
| 22 | def wrap_tool( |
| 23 | self, |
| 24 | tool: Tool, |
| 25 | execute_tool: AgenticProviderExecuteFn, |
| 26 | ) -> BaseTool: |
| 27 | """Wrap a tool as a CrewAI tool.""" |
| 28 | |
| 29 | class Wrapper(BaseTool): |
| 30 | def _validate_kwargs( |
| 31 | self, kwargs: t.Dict[str, t.Any] |
| 32 | ) -> t.Dict[str, t.Any]: |
| 33 | """Validate zero-field schemas and preserve argument presence.""" |
| 34 | if self.args_schema is None: |
| 35 | return kwargs |
| 36 | return validate_and_serialize_tool_arguments(self.args_schema, kwargs) |
| 37 | |
| 38 | def run(self, *args, **kwargs): |
| 39 | try: |
| 40 | return super().run(*args, **kwargs) |
| 41 | except pydantic.ValidationError as e: |
| 42 | return { |
| 43 | "successful": False, |
| 44 | "error": parse_pydantic_error(e), |
| 45 | "data": None, |
| 46 | } |
| 47 | |
| 48 | def _run(self, **kwargs): |
| 49 | try: |
| 50 | # Normalize defensively so a stringified payload is coerced to a dict (issue #2406). |
| 51 | return execute_tool( |
| 52 | slug=tool.slug, arguments=normalize_tool_arguments(kwargs) |
| 53 | ) |
| 54 | except pydantic.ValidationError as e: |
| 55 | return { |
| 56 | "successful": False, |
| 57 | "error": parse_pydantic_error(e), |
| 58 | "data": None, |
| 59 | } |
| 60 | |
| 61 | # Inline internal $ref/$defs before building the Pydantic model. The |
| 62 | # converter types a referenced property as Any, so CrewAI would show the |
| 63 | # model an untyped argument. Dangling references degrade to a permissive |
| 64 | # object instead of raising, matching the other providers. |
| 65 | input_parameters = dereference_json_schema( |
| 66 | tool.input_parameters, |
| 67 | on_unresolved="sentinel", |
| 68 | ) |
| 69 | return Wrapper( |
| 70 | name=tool.slug, |
| 71 | description=tool.description, |
| 72 | args_schema=json_schema_to_model( |
| 73 | json_schema=input_parameters, |
| 74 | skip_default=self.skip_default, |
| 75 | ), |
| 76 | ) |
| 77 | |
| 78 | def wrap_tools( |
| 79 | self, |
| 80 | tools: t.Sequence[Tool], |
| 81 | execute_tool: AgenticProviderExecuteFn, |
| 82 | ) -> list[BaseTool]: |
| 83 | """Wrap a list of tools as a list of CrewAI tools.""" |
| 84 | return [self.wrap_tool(tool, execute_tool) for tool in tools] |