@agentica/core 0.17.1 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentica/core",
3
- "version": "0.17.1",
3
+ "version": "0.19.0",
4
4
  "description": "Agentic AI Library specialized in LLM Function Calling",
5
5
  "author": "Wrtn Technologies",
6
6
  "license": "MIT",
@@ -1,11 +1,21 @@
1
1
  import type { IHttpLlmFunction, ILlmFunction, IValidation } from "@samchon/openapi";
2
2
 
3
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
4
+ import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
5
+
3
6
  import type { IAgenticaConfig } from "../../structures/IAgenticaConfig";
4
7
  import type { IAgenticaController } from "../../structures/IAgenticaController";
5
8
  import type { IMcpLlmFunction } from "../../structures/mcp/IMcpLlmFunction";
6
9
 
10
+ import { assertMcpLlmApplication } from "../../functional/assertMcpLlmApplication";
11
+
7
12
  import { compose, divide, getOperations, toClassOperations, toHttpOperations, toMcpOperations } from "./AgenticaOperationComposer";
8
13
 
14
+ const client = new Client({
15
+ name: "calculator",
16
+ version: "1.0.0",
17
+ });
18
+
9
19
  // test helper functions
10
20
  function createMockHttpFunction(name: string, method: "get" | "post" | "patch" | "put" | "delete", path: string): IHttpLlmFunction<any> {
11
21
  return {
@@ -60,23 +70,28 @@ function createMockClassController(name: string, functions: ILlmFunction<any>[])
60
70
  };
61
71
  }
62
72
 
63
- function createMockMcpController(name: string, functions: IMcpLlmFunction[]): IAgenticaController.IMcp {
73
+ async function createMockMcpController(name: string, functions: IMcpLlmFunction[]): Promise<IAgenticaController.IMcp> {
64
74
  return {
65
75
  name,
66
76
  protocol: "mcp",
67
- application: {
68
- transport: {
69
- type: "sse",
70
- url: new URL("https://example.com"),
71
- },
77
+ application: await assertMcpLlmApplication({
78
+ client,
79
+ }).then(v => ({
80
+ ...v,
72
81
  functions,
73
- },
82
+ })),
74
83
  };
75
84
  }
76
85
 
77
86
  describe("a AgenticaOperationComposer", () => {
87
+ beforeAll(async () => {
88
+ await client.connect(new StdioClientTransport({
89
+ command: "npx",
90
+ args: ["-y", "@wrtnlabs/calculator-mcp"],
91
+ }));
92
+ });
78
93
  describe("compose", () => {
79
- it("should compose operations from controllers", () => {
94
+ it("should compose operations from controllers", async () => {
80
95
  // Mock controllers
81
96
  const mockHttpController = createMockHttpController("httpController", [
82
97
  createMockHttpFunction("function1", "get", "/api/function1"),
@@ -92,7 +107,7 @@ describe("a AgenticaOperationComposer", () => {
92
107
  },
93
108
  ]);
94
109
 
95
- const mockMcpController = createMockMcpController("mcpController", [
110
+ const mockMcpController = await createMockMcpController("mcpController", [
96
111
  {
97
112
  name: "function4",
98
113
  parameters: {},
@@ -163,8 +178,8 @@ describe("a AgenticaOperationComposer", () => {
163
178
  expect(result[0]?.name).toBe("_0_function1");
164
179
  });
165
180
 
166
- it("should get operations from mcp controllers", () => {
167
- const mockController = createMockMcpController("mcpController", [
181
+ it("should get operations from mcp controllers", async () => {
182
+ const mockController = await createMockMcpController("mcpController", [
168
183
  {
169
184
  name: "function1",
170
185
  parameters: {},
@@ -226,8 +241,8 @@ describe("a AgenticaOperationComposer", () => {
226
241
  });
227
242
 
228
243
  describe("toMcpOperations", () => {
229
- it("should convert mcp controller to operations", () => {
230
- const mockController = createMockMcpController("mcpController", [
244
+ it("should convert mcp controller to operations", async () => {
245
+ const mockController = await createMockMcpController("mcpController", [
231
246
  {
232
247
  name: "function1",
233
248
  parameters: {},
@@ -1,4 +1,6 @@
1
- import type { IMcpLlmApplication, IMcpLlmTransportProps } from "../structures/mcp";
1
+ import type { Client } from "@modelcontextprotocol/sdk/client/index.d.ts";
2
+
3
+ import type { IMcpLlmApplication } from "../structures/mcp";
2
4
  /**
3
5
  * Create an MCP LLM application instance with type assertion.
4
6
  *
@@ -12,38 +14,19 @@ import type { IMcpLlmApplication, IMcpLlmTransportProps } from "../structures/mc
12
14
  * @returns MCP LLM application instance
13
15
  * @author Samchon
14
16
  */
15
- export async function assertMcpLlmApplication(props: IMcpLlmTransportProps): Promise<IMcpLlmApplication> {
17
+ export async function assertMcpLlmApplication(props: {
18
+ client: Client;
19
+ }): Promise<IMcpLlmApplication> {
16
20
  // for peerDependencies
17
- const { Client } = await import("@modelcontextprotocol/sdk/client/index.js");
18
- const { SSEClientTransport } = await import("@modelcontextprotocol/sdk/client/sse.js");
19
- const { StdioClientTransport } = await import("@modelcontextprotocol/sdk/client/stdio.js");
20
21
  const { ListToolsResultSchema } = await import("@modelcontextprotocol/sdk/types.js");
21
22
 
22
- const client = new Client({
23
- name: "get_tool_list",
24
- version: "1.0.0",
25
- });
26
-
27
- const transport = (() => {
28
- switch (props.type) {
29
- case "sse":
30
- return new SSEClientTransport(props.url);
31
- case "stdio":
32
- return new StdioClientTransport(props);
33
- default:
34
- props satisfies never;
35
- throw new Error("Invalid transport type");
36
- }
37
- })();
38
- await client.connect(transport);
39
-
40
- const toolList = await client.request({ method: "tools/list" }, ListToolsResultSchema);
23
+ const toolList = await props.client.request({ method: "tools/list" }, ListToolsResultSchema);
41
24
  return {
42
25
  functions: toolList.tools.map(tool => ({
43
26
  name: tool.name,
44
27
  description: tool.description,
45
28
  parameters: tool.inputSchema,
46
29
  })),
47
- transport: props,
30
+ client: props.client,
48
31
  };
49
32
  }
@@ -435,34 +435,15 @@ async function executeClassOperation<Model extends ILlmSchema.Model>(operation:
435
435
  return ((execute as Record<string, unknown>)[operation.function.name] as (...args: unknown[]) => Promise<unknown>)(operationArguments);
436
436
  }
437
437
 
438
- async function executeMcpOperation(operation: AgenticaOperation.Mcp, operationArguments: Record<string, unknown>): Promise<unknown> {
439
- // for peerDependencies
440
- const { Client } = await import("@modelcontextprotocol/sdk/client/index.js");
441
- const { SSEClientTransport } = await import("@modelcontextprotocol/sdk/client/sse.js");
442
- const { StdioClientTransport } = await import("@modelcontextprotocol/sdk/client/stdio.js");
443
-
444
- const client = new Client({
445
- name: operation.name,
446
- version: "1.0.0",
447
- });
448
-
449
- const transport = (() => {
450
- switch (operation.controller.application.transport.type) {
451
- case "sse":
452
- return new SSEClientTransport(operation.controller.application.transport.url);
453
- case "stdio":
454
- // @TODO: implement StdioClientTransport cache
455
- // StdioClientTransport and connects a new child process every time it is initialized and connected.
456
- // This results in significant latency and resource waste.
457
- return new StdioClientTransport(operation.controller.application.transport);
458
- default:
459
- operation.controller.application.transport satisfies never;
460
- throw new Error("Unsupported transport type");
461
- }
462
- })();
463
- await client.connect(transport);
464
- const result = await client.callTool({ method: operation.function.name, name: operation.function.name, arguments: operationArguments });
465
- return result.content;
438
+ async function executeMcpOperation(
439
+ operation: AgenticaOperation.Mcp,
440
+ operationArguments: Record<string, unknown>,
441
+ ): Promise<unknown> {
442
+ return operation.controller.application.client.callTool({
443
+ method: operation.function.name,
444
+ name: operation.function.name,
445
+ arguments: operationArguments,
446
+ }).then(v => v.content);
466
447
  }
467
448
 
468
449
  async function correct<Model extends ILlmSchema.Model>(
@@ -1,17 +1,10 @@
1
- import type { IMcpLlmFunction } from "./IMcpLlmFunction";
2
- import type { IMcpLlmTransportProps } from "./IMcpLlmTransportProps";
1
+ import type { Client } from "@modelcontextprotocol/sdk/client/index.d.ts";
3
2
 
3
+ import type { IMcpLlmFunction } from "./IMcpLlmFunction";
4
4
  /**
5
5
  * MCP LLM application.
6
6
  */
7
7
  export interface IMcpLlmApplication {
8
- /**
9
- * Functions of the MCP server.
10
- */
8
+ client: Client;
11
9
  functions: IMcpLlmFunction[];
12
-
13
- /**
14
- * Transport properties of the MCP server.
15
- */
16
- transport: IMcpLlmTransportProps;
17
10
  }