Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Route preloaded custom tools locally

Dhawal Upadhyayd4f3c90ed540
diff --git a/python/composio/core/models/tool_router_session.py b/python/composio/core/models/tool_router_session.py
index 51721319c..448834561 100644
--- a/python/composio/core/models/tool_router_session.py
+++ b/python/composio/core/models/tool_router_session.py
@@ -354,6 +354,13 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
)
return result
+ entry = find_custom_tool(self._custom_tools_map, slug)
+ if entry:
+ return execute_custom_tool(
+ entry,
+ arguments,
+ t.cast(SessionContextImpl, self._session_context),
+ )
# Non-multi-execute meta tools always go to backend
return backend_execute(slug, arguments)
diff --git a/python/tests/test_tool_router.py b/python/tests/test_tool_router.py
index bf6f47d4d..c6a91414d 100644
--- a/python/tests/test_tool_router.py
+++ b/python/tests/test_tool_router.py
@@ -1232,7 +1232,7 @@ class TestToolRouter:
preload=True,
)
def grep(input: GrepInput, ctx):
- return {"matches": []}
+ return {"matches": [input.pattern]}
mock_response = mock_client.tool_router.session.create.return_value
mock_response.tool_router_tools = ["COMPOSIO_SEARCH_TOOLS"]
@@ -1270,6 +1270,15 @@ class TestToolRouter:
assert wrapped_tools[1].toolkit.slug == "custom"
assert result == "mocked-custom-tools"
+ execute_tool = mock_provider.wrap_tools.call_args.kwargs["execute_tool"]
+ local_result = execute_tool(slug="SERVER_GREP", arguments={"pattern": "needle"})
+ assert local_result == {
+ "data": {"matches": ["needle"]},
+ "error": None,
+ "successful": True,
+ }
+ mock_client.tool_router.session.execute.assert_not_called()
+
@patch("composio.core.models.tools.Tools")
def test_tools_function_with_modifiers(
self,
diff --git a/ts/packages/core/src/models/ToolRouterSession.ts b/ts/packages/core/src/models/ToolRouterSession.ts
index 710bf42cb..fbae11581 100644
--- a/ts/packages/core/src/models/ToolRouterSession.ts
+++ b/ts/packages/core/src/models/ToolRouterSession.ts
@@ -125,6 +125,10 @@ export class ToolRouterSession<
if (toolSlug === COMPOSIO_MULTI_EXECUTE_TOOL) {
return this.routeMultiExecute(input, ToolsModel, sessionTools, modifiers);
}
+ const customTool = findCustomTool(this.customToolsMap, toolSlug);
+ if (customTool) {
+ return executeCustomTool(customTool, input, this.sessionContext!);
+ }
return ToolsModel.executeSessionTool(
toolSlug,
{ sessionId: this.sessionId, arguments: input },
diff --git a/ts/packages/core/test/models/toolRouter.test.ts b/ts/packages/core/test/models/toolRouter.test.ts
index e3c8dbc83..1799de267 100644
--- a/ts/packages/core/test/models/toolRouter.test.ts
+++ b/ts/packages/core/test/models/toolRouter.test.ts
@@ -2437,6 +2437,9 @@ describe('ToolRouter', () => {
});
it('should include custom tools with preload enabled locally', async () => {
+ const executeGrep = vi.fn(async ({ pattern }: { pattern: string }) => ({
+ matches: [pattern],
+ }));
const grepTool = createCustomTool('GREP', {
name: 'Grep',
description: 'Search local text',
@@ -2444,7 +2447,7 @@ describe('ToolRouter', () => {
inputParams: z.object({
pattern: z.string().describe('Pattern to search for'),
}),
- execute: vi.fn(async () => ({ matches: [] })),
+ execute: executeGrep,
});
mockClient.toolRouter.session.create.mockResolvedValueOnce({
@@ -2477,6 +2480,19 @@ describe('ToolRouter', () => {
expect(wrappedTools[1].description).toContain(DIRECT_CUSTOM_TOOL_DESCRIPTION_PREFIX);
expect(wrappedTools[1].toolkit).toEqual({ slug: 'custom', name: 'Custom' });
expect(tools).toBe('mocked-custom-tools');
+
+ const routingExecute = mockProvider.wrapTools.mock.calls[0][1] as (
+ toolSlug: string,
+ input: Record<string, unknown>
+ ) => Promise<unknown>;
+ const result = await routingExecute('SERVER_GREP', { pattern: 'needle' });
+ expect(result).toEqual({
+ data: { matches: ['needle'] },
+ error: null,
+ successful: true,
+ });
+ expect(executeGrep).toHaveBeenCalledWith({ pattern: 'needle' }, expect.anything());
+ expect(mockClient.toolRouter.session.execute).not.toHaveBeenCalled();
});
it('should not expose custom tools unless SDK preload selects them', async () => {