Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 22:15 UTC
Route preloaded custom tools locally
Dhawal Upadhyay committed
May 6, 2026 at 17:30 UTC
d4f3c90ed5408cdf8a4eefabc60c0ba287996108
4 files changed
+38
-2
python/composio/core/models/tool_router_session.py
+7
@@ -354,6 +354,13 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
354
)
355
356
return result
357
+ entry = find_custom_tool(self._custom_tools_map, slug)
358
+ if entry:
359
+ return execute_custom_tool(
360
+ entry,
361
+ arguments,
362
+ t.cast(SessionContextImpl, self._session_context),
363
+ )
364
# Non-multi-execute meta tools always go to backend
365
return backend_execute(slug, arguments)
366
python/tests/test_tool_router.py
+10
-1
@@ -1232,7 +1232,7 @@ class TestToolRouter:
1232
preload=True,
1233
)
1234
def grep(input: GrepInput, ctx):
1235
- return {"matches": []}
1235
+ return {"matches": [input.pattern]}
1236
1237
mock_response = mock_client.tool_router.session.create.return_value
1238
mock_response.tool_router_tools = ["COMPOSIO_SEARCH_TOOLS"]
@@ -1270,6 +1270,15 @@ class TestToolRouter:
1270
assert wrapped_tools[1].toolkit.slug == "custom"
1271
assert result == "mocked-custom-tools"
1272
1273
+ execute_tool = mock_provider.wrap_tools.call_args.kwargs["execute_tool"]
1274
+ local_result = execute_tool(slug="SERVER_GREP", arguments={"pattern": "needle"})
1275
+ assert local_result == {
1276
+ "data": {"matches": ["needle"]},
1277
+ "error": None,
1278
+ "successful": True,
1279
+ }
1280
+ mock_client.tool_router.session.execute.assert_not_called()
1281
+
1282
@patch("composio.core.models.tools.Tools")
1283
def test_tools_function_with_modifiers(
1284
self,
ts/packages/core/src/models/ToolRouterSession.ts
+4
@@ -125,6 +125,10 @@ export class ToolRouterSession<
125
if (toolSlug === COMPOSIO_MULTI_EXECUTE_TOOL) {
126
return this.routeMultiExecute(input, ToolsModel, sessionTools, modifiers);
127
}
128
+ const customTool = findCustomTool(this.customToolsMap, toolSlug);
129
+ if (customTool) {
130
+ return executeCustomTool(customTool, input, this.sessionContext!);
131
+ }
132
return ToolsModel.executeSessionTool(
133
toolSlug,
134
{ sessionId: this.sessionId, arguments: input },
ts/packages/core/test/models/toolRouter.test.ts
+17
-1
@@ -2437,6 +2437,9 @@ describe('ToolRouter', () => {
2437
});
2438
2439
it('should include custom tools with preload enabled locally', async () => {
2440
+ const executeGrep = vi.fn(async ({ pattern }: { pattern: string }) => ({
2441
+ matches: [pattern],
2442
+ }));
2443
const grepTool = createCustomTool('GREP', {
2444
name: 'Grep',
2445
description: 'Search local text',
@@ -2444,7 +2447,7 @@ describe('ToolRouter', () => {
2447
inputParams: z.object({
2448
pattern: z.string().describe('Pattern to search for'),
2449
}),
2447
- execute: vi.fn(async () => ({ matches: [] })),
2450
+ execute: executeGrep,
2451
});
2452
2453
mockClient.toolRouter.session.create.mockResolvedValueOnce({
@@ -2477,6 +2480,19 @@ describe('ToolRouter', () => {
2480
expect(wrappedTools[1].description).toContain(DIRECT_CUSTOM_TOOL_DESCRIPTION_PREFIX);
2481
expect(wrappedTools[1].toolkit).toEqual({ slug: 'custom', name: 'Custom' });
2482
expect(tools).toBe('mocked-custom-tools');
2483
+
2484
+ const routingExecute = mockProvider.wrapTools.mock.calls[0][1] as (
2485
+ toolSlug: string,
2486
+ input: Record<string, unknown>
2487
+ ) => Promise<unknown>;
2488
+ const result = await routingExecute('SERVER_GREP', { pattern: 'needle' });
2489
+ expect(result).toEqual({
2490
+ data: { matches: ['needle'] },
2491
+ error: null,
2492
+ successful: true,
2493
+ });
2494
+ expect(executeGrep).toHaveBeenCalledWith({ pattern: 'needle' }, expect.anything());
2495
+ expect(mockClient.toolRouter.session.execute).not.toHaveBeenCalled();
2496
});
2497
2498
it('should not expose custom tools unless SDK preload selects them', async () => {