{"version":3,"file":"McpLlm.mjs","sources":["../src/McpLlm.ts"],"sourcesContent":["import { OpenApi } from \"./OpenApi\";\nimport { LlmSchemaComposer } from \"./composers/LlmSchemaComposer\";\nimport { OpenApiV3_1Emender } from \"./converters/OpenApiV3_1Emender\";\nimport { ILlmSchema } from \"./structures/ILlmSchema\";\nimport { IMcpLlmApplication } from \"./structures/IMcpLlmApplication\";\nimport { IMcpLlmFunction } from \"./structures/IMcpLlmFunction\";\nimport { IMcpTool } from \"./structures/IMcpTool\";\nimport { IOpenApiSchemaError } from \"./structures/IOpenApiSchemaError\";\nimport { IResult } from \"./structures/IResult\";\nimport { OpenApiTypeChecker } from \"./utils/OpenApiTypeChecker\";\nimport { OpenApiValidator } from \"./utils/OpenApiValidator\";\n\n/**\n * Application of LLM function calling from MCP document.\n *\n * `McpLlm` is a module for composing LLM (Large Language Model) function\n * calling application from MCP (Model Context Protocol) document.\n *\n * The reasons why `@samchon/openapi` recommends to use the function calling\n * feature instead of directly using the\n * [`mcp_servers`](https://openai.github.io/openai-agents-python/mcp/#using-mcp-servers)\n * property of LLM API are:\n *\n * - Model Specification: {@link ILlmSchema}\n * - Validation Feedback: {@link IMcpLlmFunction.validate}\n * - Selector agent for reducing context: [Agentica > Orchestration\n *   Strategy](https://wrtnlabs.io/agentica/docs/concepts/function-calling/#orchestration-strategy)\n *\n * @author Jeongho Nam - https://github.com/samchon\n */\nexport namespace McpLlm {\n  /** Properties for the LLM function calling application composer. */\n  export interface IApplicationProps {\n    /**\n     * List of tools.\n     *\n     * A list of tools defined in the MCP (Model Context Protocol) document.\n     *\n     * It is better to validate the tools using the\n     * [`typia.assert<T>()`](https://typia.io/docs/validate/assert) function for\n     * type safety.\n     */\n    tools: Array<IMcpTool>;\n\n    /** Configuration for the LLM function calling schema conversion. */\n    config?: Partial<IMcpLlmApplication.IConfig>;\n  }\n\n  /**\n   * Convert MCP document to LLM function calling application.\n   *\n   * Converts MCP (Model Context Protocol) to LLM (Large Language Model)\n   * function calling application.\n   *\n   * The reasons why `@samchon/openapi` recommends using the function calling\n   * feature instead of directly using the\n   * [`mcp_servers`](https://openai.github.io/openai-agents-python/mcp/#using-mcp-servers)\n   * property of LLM API are:\n   *\n   * - **Model Specification**: {@link ILlmSchema}\n   * - **Validation Feedback**: {@link IMcpLlmFunction.validate}\n   * - **Selector agent for reducing context**: [Agentica > Orchestration\n   *   Strategy](https://wrtnlabs.io/agentica/docs/concepts/function-calling/#orchestration-strategy)\n   *\n   * @param props Properties for composition\n   * @returns LLM function calling application\n   */\n  export const application = (props: IApplicationProps): IMcpLlmApplication => {\n    const config: IMcpLlmApplication.IConfig = {\n      reference: props.config?.reference ?? true,\n      strict: props.config?.strict ?? false,\n      maxLength: props.config?.maxLength ?? 64,\n      equals: props.config?.equals ?? false,\n    };\n    const functions: IMcpLlmFunction[] = [];\n    const errors: IMcpLlmApplication.IError[] = [];\n\n    props.tools.forEach((tool, i) => {\n      // CONVERT TO EMENDED OPENAPI V3.1 SPECIFICATION\n      const components: OpenApi.IComponents =\n        OpenApiV3_1Emender.convertComponents({\n          schemas: tool.inputSchema.$defs,\n        });\n      const schema: OpenApi.IJsonSchema = OpenApiV3_1Emender.convertSchema({\n        schemas: tool.inputSchema.$defs,\n      })(tool.inputSchema);\n      if (components.schemas) {\n        const visited: Set<string> = new Set<string>();\n        OpenApiTypeChecker.visit({\n          closure: (schema: any) => {\n            if (typeof schema.$ref === \"string\")\n              visited.add(schema.$ref.split(\"/\").pop()!);\n          },\n          components,\n          schema,\n        });\n        components.schemas = Object.fromEntries(\n          Object.entries(components.schemas).filter(([key]) =>\n            visited.has(key),\n          ),\n        );\n      }\n\n      // CONVERT TO LLM PARAMETERS\n      const parameters: IResult<ILlmSchema.IParameters, IOpenApiSchemaError> =\n        LlmSchemaComposer.parameters({\n          config,\n          components,\n          schema: schema as\n            | OpenApi.IJsonSchema.IObject\n            | OpenApi.IJsonSchema.IReference,\n          accessor: `$input.tools[${i}].inputSchema`,\n        });\n      if (parameters.success)\n        functions.push({\n          name: tool.name,\n          parameters: parameters.value,\n          description: tool.description,\n          validate: OpenApiValidator.create({\n            components,\n            schema,\n            required: true,\n            equals: config.equals,\n          }),\n        });\n      else\n        errors.push({\n          name: tool.name,\n          parameters: tool.inputSchema,\n          description: tool.description,\n          messages: parameters.error.reasons.map((r) => {\n            const accessor: string = `$input.tools[${i}].inputSchema`;\n            return `${accessor}: ${r.message}`;\n          }),\n        });\n    });\n    return {\n      functions,\n      config,\n      errors,\n    };\n  };\n}\n"],"names":["McpLlm","application","props","config","reference","strict","maxLength","equals","functions","errors","tools","forEach","tool","i","components","OpenApiV3_1Emender","convertComponents","schemas","inputSchema","$defs","schema","convertSchema","visited","Set","OpenApiTypeChecker","visit","closure","$ref","add","split","pop","Object","fromEntries","entries","filter","key","has","parameters","LlmSchemaComposer","accessor","success","push","name","value","description","validate","OpenApiValidator","create","required","messages","error","reasons","map","r","message"],"mappings":";;;;;;;;AA8BM,IAAWA;;CAAjB,SAAiBA;IAqCFA,OAAAC,cAAeC;QAC1B,MAAMC,SAAqC;YACzCC,WAAWF,MAAMC,QAAQC,aAAa;YACtCC,QAAQH,MAAMC,QAAQE,UAAU;YAChCC,WAAWJ,MAAMC,QAAQG,aAAa;YACtCC,QAAQL,MAAMC,QAAQI,UAAU;;QAElC,MAAMC,YAA+B;QACrC,MAAMC,SAAsC;QAE5CP,MAAMQ,MAAMC,QAAQ,CAACC,MAAMC;YAEzB,MAAMC,aACJC,mBAAmBC,kBAAkB;gBACnCC,SAASL,KAAKM,YAAYC;;YAE9B,MAAMC,SAA8BL,mBAAmBM,cAAc;gBACnEJ,SAASL,KAAKM,YAAYC;cADQJ,CAEjCH,KAAKM;YACR,IAAIJ,WAAWG,SAAS;gBACtB,MAAMK,UAAuB,IAAIC;gBACjCC,mBAAmBC,MAAM;oBACvBC,SAAUN;wBACR,WAAWA,OAAOO,SAAS,UACzBL,QAAQM,IAAIR,OAAOO,KAAKE,MAAM,KAAKC;;oBAEvChB;oBACA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