@agentica/core 0.17.0 → 0.18.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/lib/context/internal/AgenticaOperationComposer.spec.js +40 -20
- package/lib/context/internal/AgenticaOperationComposer.spec.js.map +1 -1
- package/lib/functional/assertHttpLlmApplication.js +44 -44
- package/lib/functional/assertMcpLlmApplication.d.ts +5 -2
- package/lib/functional/assertMcpLlmApplication.js +37 -22
- package/lib/functional/assertMcpLlmApplication.js.map +1 -1
- package/lib/functional/validateHttpLlmApplication.js +36 -36
- package/lib/index.mjs +87 -133
- package/lib/index.mjs.map +1 -1
- package/lib/orchestrate/call.js +5 -58
- package/lib/orchestrate/call.js.map +1 -1
- package/lib/structures/mcp/IMcpLlmApplication.d.ts +2 -8
- package/lib/utils/ChatGptCompletionMessageUtil.js +2 -2
- package/package.json +1 -1
- package/src/context/internal/AgenticaOperationComposer.spec.ts +28 -13
- package/src/functional/assertMcpLlmApplication.ts +9 -25
- package/src/orchestrate/call.ts +9 -28
- package/src/structures/mcp/IMcpLlmApplication.ts +3 -10
package/package.json
CHANGED
|
@@ -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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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,9 +1,6 @@
|
|
|
1
|
-
import { Client } from "@modelcontextprotocol/sdk/client/index.
|
|
2
|
-
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
3
|
-
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
4
|
-
import { ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
1
|
+
import type { Client } from "@modelcontextprotocol/sdk/client/index.d.ts";
|
|
5
2
|
|
|
6
|
-
import type { IMcpLlmApplication
|
|
3
|
+
import type { IMcpLlmApplication } from "../structures/mcp";
|
|
7
4
|
/**
|
|
8
5
|
* Create an MCP LLM application instance with type assertion.
|
|
9
6
|
*
|
|
@@ -17,32 +14,19 @@ import type { IMcpLlmApplication, IMcpLlmTransportProps } from "../structures/mc
|
|
|
17
14
|
* @returns MCP LLM application instance
|
|
18
15
|
* @author Samchon
|
|
19
16
|
*/
|
|
20
|
-
export async function assertMcpLlmApplication(props:
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
});
|
|
17
|
+
export async function assertMcpLlmApplication(props: {
|
|
18
|
+
client: Client;
|
|
19
|
+
}): Promise<IMcpLlmApplication> {
|
|
20
|
+
// for peerDependencies
|
|
21
|
+
const { ListToolsResultSchema } = await import("@modelcontextprotocol/sdk/types.js");
|
|
25
22
|
|
|
26
|
-
const
|
|
27
|
-
switch (props.type) {
|
|
28
|
-
case "sse":
|
|
29
|
-
return new SSEClientTransport(props.url);
|
|
30
|
-
case "stdio":
|
|
31
|
-
return new StdioClientTransport(props);
|
|
32
|
-
default:
|
|
33
|
-
props satisfies never;
|
|
34
|
-
throw new Error("Invalid transport type");
|
|
35
|
-
}
|
|
36
|
-
})();
|
|
37
|
-
await client.connect(transport);
|
|
38
|
-
|
|
39
|
-
const toolList = await client.request({ method: "tools/list" }, ListToolsResultSchema);
|
|
23
|
+
const toolList = await props.client.request({ method: "tools/list" }, ListToolsResultSchema);
|
|
40
24
|
return {
|
|
41
25
|
functions: toolList.tools.map(tool => ({
|
|
42
26
|
name: tool.name,
|
|
43
27
|
description: tool.description,
|
|
44
28
|
parameters: tool.inputSchema,
|
|
45
29
|
})),
|
|
46
|
-
|
|
30
|
+
client: props.client,
|
|
47
31
|
};
|
|
48
32
|
}
|
package/src/orchestrate/call.ts
CHANGED
|
@@ -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(
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
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 {
|
|
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
|
}
|