Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | /** |
| 2 | * Llamaindex Provider |
| 3 | * |
| 4 | * This provider provides a set of tools for interacting with Llamaindex. |
| 5 | * |
| 6 | * @packageDocumentation |
| 7 | * @module providers/llamaindex |
| 8 | */ |
| 9 | import { |
| 10 | BaseAgenticProvider, |
| 11 | Tool, |
| 12 | ExecuteToolFn, |
| 13 | McpServerGetResponse, |
| 14 | McpUrlResponse, |
| 15 | deduplicateJsonSchemaRequiredArrays, |
| 16 | dereferenceJsonSchema, |
| 17 | jsonSchemaToZodSchema, |
| 18 | normalizeToolArguments, |
| 19 | } from '@composio/core'; |
| 20 | import { FunctionTool, type tool as createLlamaindexTool } from 'llamaindex'; |
| 21 | |
| 22 | export type LlamaindexTool = ReturnType<typeof createLlamaindexTool>; |
| 23 | type LlamaindexToolParameters = ConstructorParameters< |
| 24 | typeof FunctionTool<Record<string, unknown>, Promise<string>> |
| 25 | >[1]['parameters']; |
| 26 | export type LlamaindexToolCollection = Array<LlamaindexTool>; |
| 27 | |
| 28 | export class LlamaindexProvider extends BaseAgenticProvider< |
| 29 | Array<LlamaindexTool>, |
| 30 | LlamaindexTool, |
| 31 | McpServerGetResponse |
| 32 | > { |
| 33 | readonly name = 'llamaindex'; |
| 34 | |
| 35 | /** |
| 36 | * Wrap a tool in the llamaindex format. |
| 37 | * @param tool - The tool to wrap. |
| 38 | * @returns The wrapped tool. |
| 39 | */ |
| 40 | wrapTool(tool: Tool, executeTool: ExecuteToolFn): LlamaindexTool { |
| 41 | // The model sees the tool's JSON Schema as is, so keywords a Zod schema |
| 42 | // cannot carry (`format`, `contentEncoding`, `contentMediaType`, `default`, |
| 43 | // ...) reach it; LlamaIndex validates the arguments with the Zod schema |
| 44 | // built from the same JSON Schema. The Zod converter does not follow $ref, |
| 45 | // so inline the definitions first, then canonicalize the required arrays |
| 46 | // the inlined definitions brought along. |
| 47 | const parameters = deduplicateJsonSchemaRequiredArrays( |
| 48 | dereferenceJsonSchema(tool.inputParameters ?? {}, { onUnresolved: 'sentinel' }) |
| 49 | ); |
| 50 | const execute = async (input: Record<string, unknown>) => { |
| 51 | // Models occasionally emit tool input as a JSON string rather than an object (issue #2406). |
| 52 | const result = await executeTool(tool.slug, normalizeToolArguments(input, tool.slug)); |
| 53 | return JSON.stringify(result); |
| 54 | }; |
| 55 | return new FunctionTool( |
| 56 | execute, |
| 57 | { |
| 58 | name: tool.slug, |
| 59 | description: tool.description ?? tool.name ?? '', |
| 60 | parameters: parameters as LlamaindexToolParameters, |
| 61 | }, |
| 62 | jsonSchemaToZodSchema(parameters) |
| 63 | ) as unknown as LlamaindexTool; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Wrap a list of tools in the llamaindex format. |
| 68 | * @param tools - The tools to wrap. |
| 69 | * @returns The wrapped tools. |
| 70 | */ |
| 71 | wrapTools(tools: Tool[], executeTool: ExecuteToolFn): LlamaindexToolCollection { |
| 72 | return tools.map(tool => this.wrapTool(tool, executeTool)); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Transform an MCP URL response into the LlamaIndex provider's format. |
| 77 | * LlamaIndex uses the standard MCP server response shape, mapping each |
| 78 | * entry to an object with a `URL` instance and its name. |
| 79 | * |
| 80 | * @param data - The MCP URL response data |
| 81 | * @returns Standard MCP server response format |
| 82 | */ |
| 83 | wrapMcpServerResponse(data: McpUrlResponse): McpServerGetResponse { |
| 84 | // LlamaIndex uses the standard format |
| 85 | return data.map(item => ({ |
| 86 | url: new URL(item.url), |
| 87 | name: item.name, |
| 88 | })) as McpServerGetResponse; |
| 89 | } |
| 90 | } |