Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(ts/e2e): stabilize Tool Router e2e tests (#3605)
Alberto Schiabeld7d5d46f4d54
diff --git a/ts/e2e-tests/runtimes/cloudflare/cf-workers-basic/test/platform.spec.ts b/ts/e2e-tests/runtimes/cloudflare/cf-workers-basic/test/platform.spec.tsindex 3b57d8050..a82da69af 100644--- a/ts/e2e-tests/runtimes/cloudflare/cf-workers-basic/test/platform.spec.ts+++ b/ts/e2e-tests/runtimes/cloudflare/cf-workers-basic/test/platform.spec.ts@@ -3,6 +3,7 @@ import { describe, it, expect } from 'vitest';import app from '../src/index';const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;+const LIVE_API_TEST_TIMEOUT_MS = 30_000;describe('@composio/core Cloudflare Workers compatibility', () => {it('should list the available endpoints', async () => {@@ -89,29 +90,33 @@ describe('@composio/core Cloudflare Workers compatibility', () => {expect(body.error).toContain('not supported in Cloudflare Workers');});- it('should fetch HackerNews user pg successfully', async () => {- const request = new IncomingRequest('http://localhost/test/hackernews');- const ctx = createExecutionContext();- const response = await app.fetch(request, env, ctx);- await waitOnExecutionContext(ctx);-- const body = (await response.json()) as {- success: boolean;- message?: string;- error?: string;- data?: {- username: string;- karma: number;- about?: string;+ it(+ 'should fetch HackerNews user pg successfully',+ async () => {+ const request = new IncomingRequest('http://localhost/test/hackernews');+ const ctx = createExecutionContext();+ const response = await app.fetch(request, env, ctx);+ await waitOnExecutionContext(ctx);++ const body = (await response.json()) as {+ success: boolean;+ message?: string;+ error?: string;+ data?: {+ username: string;+ karma: number;+ about?: string;+ };};- };- console.log('HackerNews response:', JSON.stringify(body, null, 2));+ console.log('HackerNews response:', JSON.stringify(body, null, 2));- expect(response.status).toBe(200);- expect(body.success).toBe(true);- expect(body.message).toContain('pg');- expect(body.data?.username).toBe('pg');- expect(body.data?.karma).toBeGreaterThan(0);- });+ expect(response.status).toBe(200);+ expect(body.success).toBe(true);+ expect(body.message).toContain('pg');+ expect(body.data?.username).toBe('pg');+ expect(body.data?.karma).toBeGreaterThan(0);+ },+ LIVE_API_TEST_TIMEOUT_MS+ );});diff --git a/ts/e2e-tests/runtimes/cloudflare/cf-workers-tool-router-ai/src/index.ts b/ts/e2e-tests/runtimes/cloudflare/cf-workers-tool-router-ai/src/index.tsindex d1b2c57fa..c12e5ae81 100644--- a/ts/e2e-tests/runtimes/cloudflare/cf-workers-tool-router-ai/src/index.ts+++ b/ts/e2e-tests/runtimes/cloudflare/cf-workers-tool-router-ai/src/index.ts@@ -19,16 +19,28 @@ type Bindings = {const app = new Hono<{ Bindings: Bindings }>();+const hackerNewsUserOutputSchema = z+ .union([+ z.object({+ username: z.string().optional(),+ karma: z.number(),+ }),+ z.object({+ data: z.object({+ username: z.string().optional(),+ karma: z.number(),+ }),+ }),+ ])+ .transform(output => ('data' in output ? output.data : output));+/*** Default route - lists available test endpoints*/app.get('/', c => {return c.json({message: 'Tool Router AI E2E Test Worker',- endpoints: [- '/test/mcp-client',- '/test/agent',- ],+ endpoints: ['/test/mcp-client', '/test/agent'],});});@@ -55,15 +67,15 @@ app.get('/test/mcp-client', async c => {const { mcp } = session;- const mcpClient = await createMCPClient({+ await createMCPClient({transport: {type: 'http',url: mcp.url,headers: mcp.headers,},});-- c.executionCtx.waitUntil(mcpClient.close());+ // Intentionally do not close the HTTP MCP client here: in workerd, @ai-sdk/mcp+ // aborts the pending stream and Vitest reports it as an unhandled rejection.return c.json({message: 'MCP client connected successfully',@@ -74,7 +86,7 @@ app.get('/test/mcp-client', async c => {/*** Test: Agent Execution* Tests the full workflow: create session, get tools, run agent with generateText.- *+ ** Note: this takes ~40s locally.*/app.get('/test/agent', async c => {@@ -87,6 +99,7 @@ app.get('/test/agent', async c => {const session = await composio.create('default', {toolkits: ['hackernews'],manageConnections: true,+ preload: { tools: ['HACKERNEWS_GET_USER'] },tools: {hackernews: {enable: ['HACKERNEWS_GET_USER'],@@ -103,37 +116,51 @@ app.get('/test/agent', async c => {headers: mcp.headers,},});-- try {- const tools = await mcpClient.tools();- const openai = createOpenAI({ apiKey: c.env.OPENAI_API_KEY });-- const result = await generateText({- model: openai('gpt-5.1-codex'),- prompt: `Look up the HackerNews user "pg", and tell me their karma score.`,- output: Output.object({- schema: z.object({- karma: z.number(),+ // Intentionally do not close the HTTP MCP client here: in workerd, @ai-sdk/mcp+ // aborts the pending stream and Vitest reports it as an unhandled rejection.++ const tools = await mcpClient.tools({+ schemas: {+ HACKERNEWS_GET_USER: {+ inputSchema: z.object({+ username: z.string(),}),+ outputSchema: hackerNewsUserOutputSchema,+ },+ },+ });+ const openai = createOpenAI({ apiKey: c.env.OPENAI_API_KEY });++ const result = await generateText({+ model: openai('gpt-5.1-codex'),+ prompt: `Look up the HackerNews user "pg" with HACKERNEWS_GET_USER, then return the exact karma value from that tool result.`,+ output: Output.object({+ schema: z.object({+ karma: z.number(),}),- stopWhen: stepCountIs(10),- tools,- });-- const toolCalls = result.steps.flatMap(step =>- step.toolCalls.map(tc => ({ toolName: tc.toolName }))- );-- return c.json({- message: 'Agent executed successfully',- sessionId,- toolCount: Object.keys(tools).length,- toolCalls,- response: result.output,- });- } finally {- c.executionCtx.waitUntil(mcpClient.close());- }+ }),+ stopWhen: stepCountIs(10),+ tools,+ });++ const toolCalls = result.steps.flatMap(step =>+ step.toolCalls.map(toolCall => ({ toolName: toolCall.toolName }))+ );+ const toolResults = result.steps.flatMap(step =>+ step.toolResults.map(toolResult => ({+ toolName: toolResult.toolName,+ output: toolResult.output,+ }))+ );++ return c.json({+ message: 'Agent executed successfully',+ sessionId,+ toolCount: Object.keys(tools).length,+ toolCalls,+ toolResults,+ response: result.output,+ });});export default app;diff --git a/ts/e2e-tests/runtimes/cloudflare/cf-workers-tool-router-ai/test/tool-router-ai.spec.ts b/ts/e2e-tests/runtimes/cloudflare/cf-workers-tool-router-ai/test/tool-router-ai.spec.tsindex c7536407f..fc6dbb57a 100644--- a/ts/e2e-tests/runtimes/cloudflare/cf-workers-tool-router-ai/test/tool-router-ai.spec.ts+++ b/ts/e2e-tests/runtimes/cloudflare/cf-workers-tool-router-ai/test/tool-router-ai.spec.ts@@ -4,6 +4,11 @@ import app from '../src/index';const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;+type HackerNewsUserOutput = {+ username?: string;+ karma: number;+};+describe('Tool Router AI - Cloudflare Workers compatibility', () => {it('should list the available endpoints', async () => {const request = new IncomingRequest('http://localhost/');@@ -58,6 +63,7 @@ describe('Tool Router AI - Cloudflare Workers compatibility', () => {sessionId?: string;toolCount?: number;toolCalls?: Array<{ toolName: string }>;+ toolResults?: Array<{ toolName: string; output: HackerNewsUserOutput }>;response: {karma: number;};@@ -73,6 +79,13 @@ describe('Tool Router AI - Cloudflare Workers compatibility', () => {expect(typeof body.response.karma).toEqual('number');expect(body.response.karma).toBeGreaterThan(150_000);- expect(body.toolCalls).toContainEqual({ toolName: 'COMPOSIO_SEARCH_TOOLS' });+ expect(body.toolCalls).toContainEqual({ toolName: 'HACKERNEWS_GET_USER' });++ const hackerNewsToolResult = body.toolResults?.find(+ toolResult => toolResult.toolName === 'HACKERNEWS_GET_USER'+ );++ expect(hackerNewsToolResult).toBeDefined();+ expect(hackerNewsToolResult?.output.karma).toBe(body.response.karma);});});diff --git a/ts/e2e-tests/runtimes/node/mastra-tool-router-zod-v3/e2e.test.ts b/ts/e2e-tests/runtimes/node/mastra-tool-router-zod-v3/e2e.test.tsindex 3fabea90f..e9b241f8d 100644--- a/ts/e2e-tests/runtimes/node/mastra-tool-router-zod-v3/e2e.test.ts+++ b/ts/e2e-tests/runtimes/node/mastra-tool-router-zod-v3/e2e.test.ts@@ -2,7 +2,7 @@* @composio/mastra + openai + zod 3** Verifies that @composio/mastra works correctly with zod@3.- *+ ** See: https://github.com/ComposioHQ/composio/issues/2109.*/@@ -17,6 +17,8 @@ import { createOpenAI } from '@ai-sdk/openai';import { stepCountIs } from 'ai';import { z } from 'zod';+const MCP_CONNECT_TIMEOUT_MS = 15_000;+declare module 'bun' {interface Env {COMPOSIO_API_KEY: string;@@ -31,74 +33,80 @@ e2e(import.meta.url, {OPENAI_API_KEY: Bun.env.OPENAI_API_KEY,},defineTests: () => {- describe('@composio/mastra + openai + zod 4', () => {-+ describe('@composio/mastra + openai + zod 3', () => {const composio = new Composio({apiKey: Bun.env.COMPOSIO_API_KEY,provider: new MastraProvider(),});-- it('should work with Tool Router', async () => {- const session = await composio.create('default', {- toolkits: ['hackernews'],- manageConnections: true,- tools: {- hackernews: {- enable: ['HACKERNEWS_GET_USER'],++ it(+ 'should work with Tool Router',+ async () => {+ const session = await composio.create('default', {+ toolkits: ['hackernews'],+ manageConnections: true,+ tools: {+ hackernews: {+ enable: ['HACKERNEWS_GET_USER'],+ },},- },- });+ });- const { mcp, sessionId } = session;- expect(sessionId).toBeDefined();+ const { mcp, sessionId } = session;+ expect(sessionId).toBeDefined();- // Create a client with an HTTP server (tries Streamable HTTP, falls back to SSE)- const mcpClient = new MCPClient({- servers: {- myHttpClient: {- url: new URL(mcp.url),- requestInit: {- headers: mcp.headers,+ // Create a client with an HTTP server (tries Streamable HTTP, falls back to SSE)+ const mcpClient = new MCPClient({+ timeout: TIMEOUTS.LLM_LONG,+ servers: {+ myHttpClient: {+ url: new URL(mcp.url),+ connectTimeout: MCP_CONNECT_TIMEOUT_MS,+ requestInit: {+ headers: mcp.headers,+ },},},- },- });+ });- const tools = await mcpClient.listTools();- const openai = createOpenAI({ apiKey: Bun.env.OPENAI_API_KEY });+ const tools = await mcpClient.listTools();+ const openai = createOpenAI({ apiKey: Bun.env.OPENAI_API_KEY });- const hackernewsAgent = new Agent({- id: 'test',- name: 'test',- instructions: `You're a helpful Hackernews agent, able to finding information about users.`,- model: openai('gpt-5.1'),- tools,- });+ const hackernewsAgent = new Agent({+ id: 'test',+ name: 'test',+ instructions: `You're a helpful Hackernews agent, able to finding information about users.`,+ model: openai('gpt-5.1'),+ tools,+ });- const result = await hackernewsAgent.generate('Look up user "pg", and tell me their karma score.', {- structuredOutput: {- schema: z.object({- karma: z.number(),- }),- },- stopWhen: stepCountIs(10),- });+ const result = await hackernewsAgent.generate(+ 'Look up user "pg", and tell me their karma score.',+ {+ structuredOutput: {+ schema: z.object({+ karma: z.number(),+ }),+ },+ stopWhen: stepCountIs(10),+ }+ );- const toolCalls = result.toolCalls.flatMap(toolCall =>- toolCall.payload.toolName- );- const toolCount = Object.keys(tools).length;+ const toolCalls = result.toolCalls.flatMap(toolCall => toolCall.payload.toolName);+ const toolCount = Object.keys(tools).length;- expect(toolCount).toBeGreaterThan(0);- expect(toolCalls).toBeDefined();- expect(result.error).toBeUndefined();- expect(result.object).toBeDefined();- expect(result.object.karma).toBeGreaterThan(0);-- await mcpClient.disconnect();- }, {- timeout: TIMEOUTS.LLM_SHORT,- });+ expect(toolCount).toBeGreaterThan(0);+ expect(toolCalls).toBeDefined();+ expect(result.error).toBeUndefined();+ expect(result.object).toBeDefined();+ expect(result.object.karma).toBeGreaterThan(0);++ await mcpClient.disconnect();+ },+ {+ timeout: TIMEOUTS.LLM_LONG,+ }+ );});- }+ },});diff --git a/ts/e2e-tests/runtimes/node/mastra-tool-router-zod-v4/e2e.test.ts b/ts/e2e-tests/runtimes/node/mastra-tool-router-zod-v4/e2e.test.tsindex 9ae3489a2..d4e784798 100644--- a/ts/e2e-tests/runtimes/node/mastra-tool-router-zod-v4/e2e.test.ts+++ b/ts/e2e-tests/runtimes/node/mastra-tool-router-zod-v4/e2e.test.ts@@ -2,7 +2,7 @@* @composio/mastra + openai + zod 4** Verifies that @composio/mastra works correctly with zod@4.- *+ ** See: https://github.com/ComposioHQ/composio/issues/2109.*/@@ -17,6 +17,8 @@ import { createOpenAI } from '@ai-sdk/openai';import { stepCountIs } from 'ai';import { z } from 'zod';+const MCP_CONNECT_TIMEOUT_MS = 15_000;+declare module 'bun' {interface Env {COMPOSIO_API_KEY: string;@@ -32,73 +34,79 @@ e2e(import.meta.url, {},defineTests: () => {describe('@composio/mastra + openai + zod 4', () => {-const composio = new Composio({apiKey: Bun.env.COMPOSIO_API_KEY,provider: new MastraProvider(),});-- it('should work with Tool Router', async () => {- const session = await composio.create('default', {- toolkits: ['hackernews'],- manageConnections: true,- tools: {- hackernews: {- enable: ['HACKERNEWS_GET_USER'],++ it(+ 'should work with Tool Router',+ async () => {+ const session = await composio.create('default', {+ toolkits: ['hackernews'],+ manageConnections: true,+ tools: {+ hackernews: {+ enable: ['HACKERNEWS_GET_USER'],+ },},- },- });+ });- const { mcp, sessionId } = session;- expect(sessionId).toBeDefined();+ const { mcp, sessionId } = session;+ expect(sessionId).toBeDefined();- // Create a client with an HTTP server (tries Streamable HTTP, falls back to SSE)- const mcpClient = new MCPClient({- servers: {- myHttpClient: {- url: new URL(mcp.url),- requestInit: {- headers: mcp.headers,+ // Create a client with an HTTP server (tries Streamable HTTP, falls back to SSE)+ const mcpClient = new MCPClient({+ timeout: TIMEOUTS.LLM_LONG,+ servers: {+ myHttpClient: {+ url: new URL(mcp.url),+ connectTimeout: MCP_CONNECT_TIMEOUT_MS,+ requestInit: {+ headers: mcp.headers,+ },},},- },- });+ });- const tools = await mcpClient.listTools();- const openai = createOpenAI({ apiKey: Bun.env.OPENAI_API_KEY });+ const tools = await mcpClient.listTools();+ const openai = createOpenAI({ apiKey: Bun.env.OPENAI_API_KEY });- const hackernewsAgent = new Agent({- id: 'test',- name: 'test',- instructions: `You're a helpful Hackernews agent, able to finding information about users.`,- model: openai('gpt-5.1'),- tools,- });+ const hackernewsAgent = new Agent({+ id: 'test',+ name: 'test',+ instructions: `You're a helpful Hackernews agent, able to finding information about users.`,+ model: openai('gpt-5.1'),+ tools,+ });- const result = await hackernewsAgent.generate('Look up user "pg", and tell me their karma score.', {- structuredOutput: {- schema: z.object({- karma: z.number(),- }),- },- stopWhen: stepCountIs(10),- });+ const result = await hackernewsAgent.generate(+ 'Look up user "pg", and tell me their karma score.',+ {+ structuredOutput: {+ schema: z.object({+ karma: z.number(),+ }),+ },+ stopWhen: stepCountIs(10),+ }+ );- const toolCalls = result.toolCalls.flatMap(toolCall =>- toolCall.payload.toolName- );- const toolCount = Object.keys(tools).length;+ const toolCalls = result.toolCalls.flatMap(toolCall => toolCall.payload.toolName);+ const toolCount = Object.keys(tools).length;- expect(toolCount).toBeGreaterThan(0);- expect(toolCalls).toBeDefined();- expect(result.error).toBeUndefined();- expect(result.object).toBeDefined();- expect(result.object.karma).toBeGreaterThanOrEqual(0);-- await mcpClient.disconnect();- }, {- timeout: TIMEOUTS.LLM_SHORT,- });+ expect(toolCount).toBeGreaterThan(0);+ expect(toolCalls).toBeDefined();+ expect(result.error).toBeUndefined();+ expect(result.object).toBeDefined();+ expect(result.object.karma).toBeGreaterThanOrEqual(0);++ await mcpClient.disconnect();+ },+ {+ timeout: TIMEOUTS.LLM_LONG,+ }+ );});- }+ },});