Creating Custom Tools
Define your own functions as tools, extend apps, or use custom authentication
While Composio offers a vast library of pre-built tools, you often need to integrate your own custom logic or interact with APIs in specific ways. This guide covers how to create and use custom tools within the Composio ecosystem.
You can create custom tools to:
- Wrap your existing functions, making them callable by LLMs.
- Extend the functionality of existing Composio-integrated apps by calling their APIs using Composio’s managed authentication.
- Inject and use your own external authentication credentials to execute any Composio tool.
Defining Tools from Your Functions
The most straightforward way to create a custom tool is to wrap an existing function in your codebase. Composio provides decorators (Python) and methods (TypeScript) to make these functions discoverable and executable by LLMs.
Python (@action
)
Use the @action
decorator from composio
to designate a Python function as a tool. Composio automatically infers the tool’s schema and description from the following:
- Function Name: Becomes the default tool name (can be overridden).
- Docstring: Used as the tool’s description for the LLM. Make it clear and concise!
- Type Hints: Define the input parameters and their types for the LLM and for validation.
- Return Type Hint: Informs the expected output structure.
Example:
TypeScript (createAction
)
Use the createAction
method on your ToolSet instance (OpenAIToolSet
, LangchainToolSet
, etc.). You provide the configuration, including a Zod schema for input parameters and an async callback function containing your logic.
Example:
Using Your Custom Function Tools
Once defined (@action
) or registered (createAction
), these tools behave like any other Composio tool:
- Fetch them: Use
get_tools
, referencing the function object (Python) or theactionName
string (Python/TS). - Execute them: Use framework handlers (like Vercel’s
execute
) orexecute_action
.
Extending Composio Toolkits
A powerful feature is creating custom tools that leverage Composio’s managed authentication for an existing app (like GitHub, Slack, etc.). This allows you to call API endpoints for that app without handling credentials yourself.
Example: Get GitHub Repository Topics
Let’s create a tool to fetch topics for a GitHub repo, using Composio’s managed GitHub auth.
This allows you to extend Composio’s capabilities for any integrated app without managing the authentication flow yourself.
Adding Custom Authentication to Tools
You can also execute any Composio tool (pre-built or custom-defined) using your own authentication credentials provided at runtime. This is useful if you manage tokens or API keys separately from Composio’s connection system.
Use the execute_action
method and provide the auth
parameter.
Example: Create GitHub Issue with a Provided Token
This gives you flexibility while still leveraging Composio’s standardized action execution and schema handling. The ParamPlacement
enum (TS) or string values ("header"
, "query"
, "path"
) specify where the custom parameter should be injected into the API request.