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

next
ts 56 lines 1.92 KB
Raw
1 import { NextRequest, NextResponse } from 'next/server';
2 import { z } from 'zod';
3 import { APIError } from '@composio/client';
4 import { instantSchema } from '@/lib/instant';
5 import { createComposioClientFromEnv } from '@/lib/composio-api';
6
7 const toolDetailsSchema = z.object({
8 input_parameters: z.unknown().optional(),
9 parameters: z.unknown().optional(),
10 output_parameters: z.unknown().optional(),
11 response: z.unknown().optional(),
12 instant: instantSchema,
13 });
14
15 // COMPOSIO_BASE_URL selects the API host; production by default.
16 const client = process.env.COMPOSIO_API_KEY
17 ? createComposioClientFromEnv(process.env, { revalidate: 3600 })
18 : null;
19
20 export async function GET(
21 request: NextRequest,
22 { params }: { params: Promise<{ slug: string }> }
23 ) {
24 const { slug } = await params;
25 const version = request.nextUrl.searchParams.get('version') || 'latest';
26
27 if (!client) {
28 return NextResponse.json({ error: 'API key not configured' }, { status: 500 });
29 }
30
31 if (!slug || !/^[A-Z0-9_]+$/i.test(slug)) {
32 return NextResponse.json({ error: 'Invalid tool slug' }, { status: 400 });
33 }
34
35 try {
36 const response = await client.tools.retrieve(slug.toUpperCase(), {
37 version,
38 ...(version === 'latest' ? { include_pricing: true } : {}),
39 });
40 const tool = toolDetailsSchema.parse(response);
41
42 const inputSchema = tool.input_parameters || tool.parameters;
43 const outputSchema = tool.output_parameters || tool.response;
44
45 return NextResponse.json({
46 input_parameters: inputSchema || null,
47 output_parameters: outputSchema || null,
48 ...(version === 'latest' && tool.instant ? { instant: tool.instant } : {}),
49 });
50 } catch (error) {
51 if (error instanceof APIError && error.status !== undefined && error.status < 500) {
52 return NextResponse.json({ error: 'Tool not found' }, { status: error.status });
53 }
54 return NextResponse.json({ error: 'Failed to fetch tool' }, { status: 500 });
55 }
56 }