@hashgraphonline/conversational-agent 0.1.206 → 0.1.208
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/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/langchain/ContentAwareAgentExecutor.d.ts +14 -0
- package/dist/esm/index12.js +1 -1
- package/dist/esm/index15.js +7 -172
- package/dist/esm/index15.js.map +1 -1
- package/dist/esm/index16.js +159 -118
- package/dist/esm/index16.js.map +1 -1
- package/dist/esm/index17.js +156 -231
- package/dist/esm/index17.js.map +1 -1
- package/dist/esm/index18.js +225 -84
- package/dist/esm/index18.js.map +1 -1
- package/dist/esm/index19.js +2 -2
- package/dist/esm/index23.js +83 -56
- package/dist/esm/index23.js.map +1 -1
- package/dist/esm/index24.js +62 -32
- package/dist/esm/index24.js.map +1 -1
- package/dist/esm/index25.js +38 -0
- package/dist/esm/index25.js.map +1 -0
- package/dist/esm/index8.js +6 -5
- package/dist/esm/index8.js.map +1 -1
- package/dist/types/langchain/ContentAwareAgentExecutor.d.ts +14 -0
- package/package.json +1 -1
- package/src/langchain/ContentAwareAgentExecutor.ts +20 -0
- package/src/langchain-agent.ts +4 -3
- package/src/mcp/adapters/langchain.ts +32 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AgentExecutor } from 'langchain/agents';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Custom AgentExecutor that intercepts large tool outputs and converts them to content references
|
|
5
|
+
* before they are sent to the LLM to avoid token limit issues.
|
|
6
|
+
*
|
|
7
|
+
* Note: The content reference conversion is already handled in the MCP adapter,
|
|
8
|
+
* so this class currently just extends AgentExecutor without modifications.
|
|
9
|
+
* We keep it as a placeholder for future enhancements.
|
|
10
|
+
*/
|
|
11
|
+
export declare class ContentAwareAgentExecutor extends AgentExecutor {
|
|
12
|
+
private logger;
|
|
13
|
+
constructor(config: any);
|
|
14
|
+
}
|
package/dist/esm/index12.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { AccountBuilder } from "./
|
|
2
|
+
import { AccountBuilder } from "./index23.js";
|
|
3
3
|
import { BaseHederaTransactionTool } from "hedera-agent-kit";
|
|
4
4
|
const HbarTransferInputSchema = z.object({
|
|
5
5
|
accountId: z.string().describe('Account ID for the transfer (e.g., "0.0.xxxx").'),
|
package/dist/esm/index15.js
CHANGED
|
@@ -1,177 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
this.
|
|
7
|
-
this.tools = /* @__PURE__ */ new Map();
|
|
8
|
-
this.logger = logger;
|
|
9
|
-
if (contentStorage) {
|
|
10
|
-
this.contentProcessor = new MCPContentProcessor(contentStorage, logger);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Connect to an MCP server and discover its tools
|
|
15
|
-
*/
|
|
16
|
-
async connectServer(config) {
|
|
17
|
-
try {
|
|
18
|
-
if (this.isServerConnected(config.name)) {
|
|
19
|
-
return {
|
|
20
|
-
serverName: config.name,
|
|
21
|
-
connected: false,
|
|
22
|
-
error: `Server ${config.name} is already connected`,
|
|
23
|
-
tools: []
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
if (config.transport && config.transport !== "stdio") {
|
|
27
|
-
throw new Error(`Transport ${config.transport} not yet supported`);
|
|
28
|
-
}
|
|
29
|
-
const transport = new StdioClientTransport({
|
|
30
|
-
command: config.command,
|
|
31
|
-
args: config.args,
|
|
32
|
-
...config.env && { env: config.env }
|
|
33
|
-
});
|
|
34
|
-
const client = new Client({
|
|
35
|
-
name: `conversational-agent-${config.name}`,
|
|
36
|
-
version: "1.0.0"
|
|
37
|
-
}, {
|
|
38
|
-
capabilities: {}
|
|
39
|
-
});
|
|
40
|
-
await client.connect(transport);
|
|
41
|
-
this.clients.set(config.name, client);
|
|
42
|
-
const toolsResponse = await client.listTools();
|
|
43
|
-
const toolsWithServer = toolsResponse.tools.map((tool) => ({
|
|
44
|
-
...tool,
|
|
45
|
-
serverName: config.name
|
|
46
|
-
}));
|
|
47
|
-
this.tools.set(config.name, toolsWithServer);
|
|
48
|
-
this.logger.info(`Connected to MCP server ${config.name} with ${toolsWithServer.length} tools`);
|
|
49
|
-
return {
|
|
50
|
-
serverName: config.name,
|
|
51
|
-
connected: true,
|
|
52
|
-
tools: toolsWithServer
|
|
53
|
-
};
|
|
54
|
-
} catch (error) {
|
|
55
|
-
this.logger.error(`Failed to connect to MCP server ${config.name}:`, error);
|
|
56
|
-
return {
|
|
57
|
-
serverName: config.name,
|
|
58
|
-
connected: false,
|
|
59
|
-
error: error instanceof Error ? error.message : "Unknown error",
|
|
60
|
-
tools: []
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Execute a tool on a specific MCP server
|
|
66
|
-
*/
|
|
67
|
-
async executeTool(serverName, toolName, args) {
|
|
68
|
-
const client = this.clients.get(serverName);
|
|
69
|
-
if (!client) {
|
|
70
|
-
throw new Error(`MCP server ${serverName} not connected`);
|
|
71
|
-
}
|
|
72
|
-
this.logger.debug(`Executing MCP tool ${toolName} on server ${serverName}`, args);
|
|
73
|
-
try {
|
|
74
|
-
const result = await client.callTool({
|
|
75
|
-
name: toolName,
|
|
76
|
-
arguments: args
|
|
77
|
-
});
|
|
78
|
-
if (this.contentProcessor) {
|
|
79
|
-
const processed = await this.contentProcessor.processResponse(result, serverName, toolName);
|
|
80
|
-
if (processed.wasProcessed) {
|
|
81
|
-
this.logger.debug(
|
|
82
|
-
`Processed MCP response from ${serverName}::${toolName}`,
|
|
83
|
-
{
|
|
84
|
-
referenceCreated: processed.referenceCreated,
|
|
85
|
-
originalSize: processed.originalSize,
|
|
86
|
-
errors: processed.errors
|
|
87
|
-
}
|
|
88
|
-
);
|
|
89
|
-
if (processed.errors && processed.errors.length > 0) {
|
|
90
|
-
this.logger.warn(`Content processing warnings for ${serverName}::${toolName}:`, processed.errors);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
return processed.content;
|
|
94
|
-
}
|
|
95
|
-
return result;
|
|
96
|
-
} catch (error) {
|
|
97
|
-
this.logger.error(`Error executing MCP tool ${toolName}:`, error);
|
|
98
|
-
throw error;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Disconnect all MCP servers
|
|
103
|
-
*/
|
|
104
|
-
async disconnectAll() {
|
|
105
|
-
for (const [name, client] of this.clients) {
|
|
106
|
-
try {
|
|
107
|
-
await client.close();
|
|
108
|
-
this.logger.info(`Disconnected from MCP server ${name}`);
|
|
109
|
-
} catch (error) {
|
|
110
|
-
this.logger.error(`Error disconnecting MCP server ${name}:`, error);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
this.clients.clear();
|
|
114
|
-
this.tools.clear();
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Get all discovered tools from all connected servers
|
|
118
|
-
*/
|
|
119
|
-
getAllTools() {
|
|
120
|
-
const allTools = [];
|
|
121
|
-
for (const tools of this.tools.values()) {
|
|
122
|
-
allTools.push(...tools);
|
|
123
|
-
}
|
|
124
|
-
return allTools;
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Get tools from a specific server
|
|
128
|
-
*/
|
|
129
|
-
getServerTools(serverName) {
|
|
130
|
-
return this.tools.get(serverName) || [];
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Check if a server is connected
|
|
134
|
-
*/
|
|
135
|
-
isServerConnected(serverName) {
|
|
136
|
-
return this.clients.has(serverName);
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Get list of connected server names
|
|
140
|
-
*/
|
|
141
|
-
getConnectedServers() {
|
|
142
|
-
return Array.from(this.clients.keys());
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Enable content processing with content storage
|
|
146
|
-
*/
|
|
147
|
-
enableContentProcessing(contentStorage) {
|
|
148
|
-
this.contentProcessor = new MCPContentProcessor(contentStorage, this.logger);
|
|
149
|
-
this.logger.info("Content processing enabled for MCP responses");
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Disable content processing
|
|
153
|
-
*/
|
|
154
|
-
disableContentProcessing() {
|
|
155
|
-
delete this.contentProcessor;
|
|
156
|
-
this.logger.info("Content processing disabled for MCP responses");
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Check if content processing is enabled
|
|
160
|
-
*/
|
|
161
|
-
isContentProcessingEnabled() {
|
|
162
|
-
return this.contentProcessor !== void 0;
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Analyze a response without processing it (for testing/debugging)
|
|
166
|
-
*/
|
|
167
|
-
analyzeResponseContent(response) {
|
|
168
|
-
if (!this.contentProcessor) {
|
|
169
|
-
throw new Error("Content processing is not enabled");
|
|
170
|
-
}
|
|
171
|
-
return this.contentProcessor.analyzeResponse(response);
|
|
1
|
+
import { AgentExecutor } from "langchain/agents";
|
|
2
|
+
import { Logger } from "@hashgraphonline/standards-sdk";
|
|
3
|
+
class ContentAwareAgentExecutor extends AgentExecutor {
|
|
4
|
+
constructor(config) {
|
|
5
|
+
super(config);
|
|
6
|
+
this.logger = new Logger({ module: "ContentAwareAgentExecutor" });
|
|
172
7
|
}
|
|
173
8
|
}
|
|
174
9
|
export {
|
|
175
|
-
|
|
10
|
+
ContentAwareAgentExecutor
|
|
176
11
|
};
|
|
177
12
|
//# sourceMappingURL=index15.js.map
|
package/dist/esm/index15.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index15.js","sources":["../../src/
|
|
1
|
+
{"version":3,"file":"index15.js","sources":["../../src/langchain/ContentAwareAgentExecutor.ts"],"sourcesContent":["import { AgentExecutor } from 'langchain/agents';\nimport type { ChainValues } from '@langchain/core/utils/types';\nimport { Logger } from '@hashgraphonline/standards-sdk';\n\n/**\n * Custom AgentExecutor that intercepts large tool outputs and converts them to content references\n * before they are sent to the LLM to avoid token limit issues.\n * \n * Note: The content reference conversion is already handled in the MCP adapter,\n * so this class currently just extends AgentExecutor without modifications.\n * We keep it as a placeholder for future enhancements.\n */\nexport class ContentAwareAgentExecutor extends AgentExecutor {\n private logger: Logger;\n\n constructor(config: any) {\n super(config);\n this.logger = new Logger({ module: 'ContentAwareAgentExecutor' });\n }\n}"],"names":[],"mappings":";;AAYO,MAAM,kCAAkC,cAAc;AAAA,EAG3D,YAAY,QAAa;AACvB,UAAM,MAAM;AACZ,SAAK,SAAS,IAAI,OAAO,EAAE,QAAQ,6BAA6B;AAAA,EAClE;AACF;"}
|
package/dist/esm/index16.js
CHANGED
|
@@ -1,136 +1,177 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
${serverConfig.toolDescriptions[tool.name]}`;
|
|
1
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
2
|
+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
3
|
+
import { MCPContentProcessor } from "./index20.js";
|
|
4
|
+
class MCPClientManager {
|
|
5
|
+
constructor(logger, contentStorage) {
|
|
6
|
+
this.clients = /* @__PURE__ */ new Map();
|
|
7
|
+
this.tools = /* @__PURE__ */ new Map();
|
|
8
|
+
this.logger = logger;
|
|
9
|
+
if (contentStorage) {
|
|
10
|
+
this.contentProcessor = new MCPContentProcessor(contentStorage, logger);
|
|
11
|
+
}
|
|
14
12
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Connect to an MCP server and discover its tools
|
|
15
|
+
*/
|
|
16
|
+
async connectServer(config) {
|
|
17
|
+
try {
|
|
18
|
+
if (this.isServerConnected(config.name)) {
|
|
19
|
+
return {
|
|
20
|
+
serverName: config.name,
|
|
21
|
+
connected: false,
|
|
22
|
+
error: `Server ${config.name} is already connected`,
|
|
23
|
+
tools: []
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
if (config.transport && config.transport !== "stdio") {
|
|
27
|
+
throw new Error(`Transport ${config.transport} not yet supported`);
|
|
28
|
+
}
|
|
29
|
+
const transport = new StdioClientTransport({
|
|
30
|
+
command: config.command,
|
|
31
|
+
args: config.args,
|
|
32
|
+
...config.env && { env: config.env }
|
|
33
|
+
});
|
|
34
|
+
const client = new Client({
|
|
35
|
+
name: `conversational-agent-${config.name}`,
|
|
36
|
+
version: "1.0.0"
|
|
37
|
+
}, {
|
|
38
|
+
capabilities: {}
|
|
39
|
+
});
|
|
40
|
+
await client.connect(transport);
|
|
41
|
+
this.clients.set(config.name, client);
|
|
42
|
+
const toolsResponse = await client.listTools();
|
|
43
|
+
const toolsWithServer = toolsResponse.tools.map((tool) => ({
|
|
44
|
+
...tool,
|
|
45
|
+
serverName: config.name
|
|
46
|
+
}));
|
|
47
|
+
this.tools.set(config.name, toolsWithServer);
|
|
48
|
+
this.logger.info(`Connected to MCP server ${config.name} with ${toolsWithServer.length} tools`);
|
|
49
|
+
return {
|
|
50
|
+
serverName: config.name,
|
|
51
|
+
connected: true,
|
|
52
|
+
tools: toolsWithServer
|
|
53
|
+
};
|
|
54
|
+
} catch (error) {
|
|
55
|
+
this.logger.error(`Failed to connect to MCP server ${config.name}:`, error);
|
|
56
|
+
return {
|
|
57
|
+
serverName: config.name,
|
|
58
|
+
connected: false,
|
|
59
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
60
|
+
tools: []
|
|
61
|
+
};
|
|
62
|
+
}
|
|
19
63
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Execute a tool on a specific MCP server
|
|
66
|
+
*/
|
|
67
|
+
async executeTool(serverName, toolName, args) {
|
|
68
|
+
const client = this.clients.get(serverName);
|
|
69
|
+
if (!client) {
|
|
70
|
+
throw new Error(`MCP server ${serverName} not connected`);
|
|
71
|
+
}
|
|
72
|
+
this.logger.debug(`Executing MCP tool ${toolName} on server ${serverName}`, args);
|
|
73
|
+
try {
|
|
74
|
+
const result = await client.callTool({
|
|
75
|
+
name: toolName,
|
|
76
|
+
arguments: args
|
|
77
|
+
});
|
|
78
|
+
if (this.contentProcessor) {
|
|
79
|
+
const processed = await this.contentProcessor.processResponse(result, serverName, toolName);
|
|
80
|
+
if (processed.wasProcessed) {
|
|
81
|
+
this.logger.debug(
|
|
82
|
+
`Processed MCP response from ${serverName}::${toolName}`,
|
|
83
|
+
{
|
|
84
|
+
referenceCreated: processed.referenceCreated,
|
|
85
|
+
originalSize: processed.originalSize,
|
|
86
|
+
errors: processed.errors
|
|
87
|
+
}
|
|
88
|
+
);
|
|
89
|
+
if (processed.errors && processed.errors.length > 0) {
|
|
90
|
+
this.logger.warn(`Content processing warnings for ${serverName}::${toolName}:`, processed.errors);
|
|
43
91
|
}
|
|
44
|
-
} else {
|
|
45
|
-
responseText = JSON.stringify(result);
|
|
46
92
|
}
|
|
47
|
-
return
|
|
93
|
+
return processed.content;
|
|
94
|
+
}
|
|
95
|
+
return result;
|
|
96
|
+
} catch (error) {
|
|
97
|
+
this.logger.error(`Error executing MCP tool ${toolName}:`, error);
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Disconnect all MCP servers
|
|
103
|
+
*/
|
|
104
|
+
async disconnectAll() {
|
|
105
|
+
for (const [name, client] of this.clients) {
|
|
106
|
+
try {
|
|
107
|
+
await client.close();
|
|
108
|
+
this.logger.info(`Disconnected from MCP server ${name}`);
|
|
48
109
|
} catch (error) {
|
|
49
|
-
|
|
50
|
-
return `Error executing MCP tool ${tool.name}: ${errorMessage}`;
|
|
110
|
+
this.logger.error(`Error disconnecting MCP server ${name}:`, error);
|
|
51
111
|
}
|
|
52
112
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
113
|
+
this.clients.clear();
|
|
114
|
+
this.tools.clear();
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Get all discovered tools from all connected servers
|
|
118
|
+
*/
|
|
119
|
+
getAllTools() {
|
|
120
|
+
const allTools = [];
|
|
121
|
+
for (const tools of this.tools.values()) {
|
|
122
|
+
allTools.push(...tools);
|
|
123
|
+
}
|
|
124
|
+
return allTools;
|
|
58
125
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
126
|
+
/**
|
|
127
|
+
* Get tools from a specific server
|
|
128
|
+
*/
|
|
129
|
+
getServerTools(serverName) {
|
|
130
|
+
return this.tools.get(serverName) || [];
|
|
62
131
|
}
|
|
63
|
-
|
|
64
|
-
|
|
132
|
+
/**
|
|
133
|
+
* Check if a server is connected
|
|
134
|
+
*/
|
|
135
|
+
isServerConnected(serverName) {
|
|
136
|
+
return this.clients.has(serverName);
|
|
65
137
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
zodType = zodType.optional();
|
|
72
|
-
}
|
|
73
|
-
shape[key] = zodType;
|
|
138
|
+
/**
|
|
139
|
+
* Get list of connected server names
|
|
140
|
+
*/
|
|
141
|
+
getConnectedServers() {
|
|
142
|
+
return Array.from(this.clients.keys());
|
|
74
143
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
144
|
+
/**
|
|
145
|
+
* Enable content processing with content storage
|
|
146
|
+
*/
|
|
147
|
+
enableContentProcessing(contentStorage) {
|
|
148
|
+
this.contentProcessor = new MCPContentProcessor(contentStorage, this.logger);
|
|
149
|
+
this.logger.info("Content processing enabled for MCP responses");
|
|
80
150
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
zodType = zodType.min(schemaObj.minimum);
|
|
94
|
-
}
|
|
95
|
-
if ("maximum" in schemaObj && typeof schemaObj.maximum === "number") {
|
|
96
|
-
zodType = zodType.max(schemaObj.maximum);
|
|
97
|
-
}
|
|
98
|
-
break;
|
|
99
|
-
case "integer":
|
|
100
|
-
zodType = z.number().int();
|
|
101
|
-
if ("minimum" in schemaObj && typeof schemaObj.minimum === "number") {
|
|
102
|
-
zodType = zodType.min(schemaObj.minimum);
|
|
103
|
-
}
|
|
104
|
-
if ("maximum" in schemaObj && typeof schemaObj.maximum === "number") {
|
|
105
|
-
zodType = zodType.max(schemaObj.maximum);
|
|
106
|
-
}
|
|
107
|
-
break;
|
|
108
|
-
case "boolean":
|
|
109
|
-
zodType = z.boolean();
|
|
110
|
-
break;
|
|
111
|
-
case "array":
|
|
112
|
-
if (schemaObj.items) {
|
|
113
|
-
zodType = z.array(convertType(schemaObj.items));
|
|
114
|
-
} else {
|
|
115
|
-
zodType = z.array(z.unknown());
|
|
116
|
-
}
|
|
117
|
-
break;
|
|
118
|
-
case "object":
|
|
119
|
-
if ("properties" in schemaObj) {
|
|
120
|
-
zodType = jsonSchemaToZod(schemaObj);
|
|
121
|
-
} else {
|
|
122
|
-
zodType = z.object({}).passthrough();
|
|
123
|
-
}
|
|
124
|
-
break;
|
|
125
|
-
default:
|
|
126
|
-
zodType = z.unknown();
|
|
151
|
+
/**
|
|
152
|
+
* Disable content processing
|
|
153
|
+
*/
|
|
154
|
+
disableContentProcessing() {
|
|
155
|
+
delete this.contentProcessor;
|
|
156
|
+
this.logger.info("Content processing disabled for MCP responses");
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Check if content processing is enabled
|
|
160
|
+
*/
|
|
161
|
+
isContentProcessingEnabled() {
|
|
162
|
+
return this.contentProcessor !== void 0;
|
|
127
163
|
}
|
|
128
|
-
|
|
129
|
-
|
|
164
|
+
/**
|
|
165
|
+
* Analyze a response without processing it (for testing/debugging)
|
|
166
|
+
*/
|
|
167
|
+
analyzeResponseContent(response) {
|
|
168
|
+
if (!this.contentProcessor) {
|
|
169
|
+
throw new Error("Content processing is not enabled");
|
|
170
|
+
}
|
|
171
|
+
return this.contentProcessor.analyzeResponse(response);
|
|
130
172
|
}
|
|
131
|
-
return zodType;
|
|
132
173
|
}
|
|
133
174
|
export {
|
|
134
|
-
|
|
175
|
+
MCPClientManager
|
|
135
176
|
};
|
|
136
177
|
//# sourceMappingURL=index16.js.map
|
package/dist/esm/index16.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index16.js","sources":["../../src/mcp/
|
|
1
|
+
{"version":3,"file":"index16.js","sources":["../../src/mcp/MCPClientManager.ts"],"sourcesContent":["import { Client } from '@modelcontextprotocol/sdk/client/index.js';\nimport { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';\nimport type { MCPServerConfig, MCPToolInfo, MCPConnectionStatus } from './types';\nimport { Logger } from '@hashgraphonline/standards-sdk';\nimport type { ContentStorage } from '../memory/ContentStorage';\nimport { MCPContentProcessor, ProcessedResponse } from './ContentProcessor';\n\n/**\n * Manages connections to MCP servers and tool discovery\n */\nexport class MCPClientManager {\n private clients: Map<string, Client> = new Map();\n private tools: Map<string, MCPToolInfo[]> = new Map();\n private logger: Logger;\n private contentProcessor?: MCPContentProcessor;\n\n constructor(logger: Logger, contentStorage?: ContentStorage) {\n this.logger = logger;\n if (contentStorage) {\n this.contentProcessor = new MCPContentProcessor(contentStorage, logger);\n }\n }\n\n /**\n * Connect to an MCP server and discover its tools\n */\n async connectServer(config: MCPServerConfig): Promise<MCPConnectionStatus> {\n try {\n if (this.isServerConnected(config.name)) {\n return {\n serverName: config.name,\n connected: false,\n error: `Server ${config.name} is already connected`,\n tools: [],\n };\n }\n\n if (config.transport && config.transport !== 'stdio') {\n throw new Error(`Transport ${config.transport} not yet supported`);\n }\n\n const transport = new StdioClientTransport({\n command: config.command,\n args: config.args,\n ...(config.env && { env: config.env }),\n });\n\n const client = new Client({\n name: `conversational-agent-${config.name}`,\n version: '1.0.0',\n }, {\n capabilities: {},\n });\n\n await client.connect(transport);\n this.clients.set(config.name, client);\n\n const toolsResponse = await client.listTools();\n const toolsWithServer: MCPToolInfo[] = toolsResponse.tools.map(tool => ({\n ...tool,\n serverName: config.name,\n }));\n\n this.tools.set(config.name, toolsWithServer);\n this.logger.info(`Connected to MCP server ${config.name} with ${toolsWithServer.length} tools`);\n\n return {\n serverName: config.name,\n connected: true,\n tools: toolsWithServer,\n };\n } catch (error) {\n this.logger.error(`Failed to connect to MCP server ${config.name}:`, error);\n return {\n serverName: config.name,\n connected: false,\n error: error instanceof Error ? error.message : 'Unknown error',\n tools: [],\n };\n }\n }\n\n /**\n * Execute a tool on a specific MCP server\n */\n async executeTool(serverName: string, toolName: string, args: Record<string, unknown>): Promise<unknown> {\n const client = this.clients.get(serverName);\n if (!client) {\n throw new Error(`MCP server ${serverName} not connected`);\n }\n\n this.logger.debug(`Executing MCP tool ${toolName} on server ${serverName}`, args);\n\n try {\n const result = await client.callTool({\n name: toolName,\n arguments: args,\n });\n\n if (this.contentProcessor) {\n const processed = await this.contentProcessor.processResponse(result, serverName, toolName);\n \n if (processed.wasProcessed) {\n this.logger.debug(\n `Processed MCP response from ${serverName}::${toolName}`,\n {\n referenceCreated: processed.referenceCreated,\n originalSize: processed.originalSize,\n errors: processed.errors\n }\n );\n\n if (processed.errors && processed.errors.length > 0) {\n this.logger.warn(`Content processing warnings for ${serverName}::${toolName}:`, processed.errors);\n }\n }\n\n return processed.content;\n }\n\n return result;\n } catch (error) {\n this.logger.error(`Error executing MCP tool ${toolName}:`, error);\n throw error;\n }\n }\n\n /**\n * Disconnect all MCP servers\n */\n async disconnectAll(): Promise<void> {\n for (const [name, client] of this.clients) {\n try {\n await client.close();\n this.logger.info(`Disconnected from MCP server ${name}`);\n } catch (error) {\n this.logger.error(`Error disconnecting MCP server ${name}:`, error);\n }\n }\n this.clients.clear();\n this.tools.clear();\n }\n\n /**\n * Get all discovered tools from all connected servers\n */\n getAllTools(): MCPToolInfo[] {\n const allTools: MCPToolInfo[] = [];\n for (const tools of this.tools.values()) {\n allTools.push(...tools);\n }\n return allTools;\n }\n\n /**\n * Get tools from a specific server\n */\n getServerTools(serverName: string): MCPToolInfo[] {\n return this.tools.get(serverName) || [];\n }\n\n /**\n * Check if a server is connected\n */\n isServerConnected(serverName: string): boolean {\n return this.clients.has(serverName);\n }\n\n /**\n * Get list of connected server names\n */\n getConnectedServers(): string[] {\n return Array.from(this.clients.keys());\n }\n\n /**\n * Enable content processing with content storage\n */\n enableContentProcessing(contentStorage: ContentStorage): void {\n this.contentProcessor = new MCPContentProcessor(contentStorage, this.logger);\n this.logger.info('Content processing enabled for MCP responses');\n }\n\n /**\n * Disable content processing\n */\n disableContentProcessing(): void {\n delete this.contentProcessor;\n this.logger.info('Content processing disabled for MCP responses');\n }\n\n /**\n * Check if content processing is enabled\n */\n isContentProcessingEnabled(): boolean {\n return this.contentProcessor !== undefined;\n }\n\n /**\n * Analyze a response without processing it (for testing/debugging)\n */\n analyzeResponseContent(response: unknown): unknown {\n if (!this.contentProcessor) {\n throw new Error('Content processing is not enabled');\n }\n return this.contentProcessor.analyzeResponse(response);\n }\n}"],"names":[],"mappings":";;;AAUO,MAAM,iBAAiB;AAAA,EAM5B,YAAY,QAAgB,gBAAiC;AAL7D,SAAQ,8BAAmC,IAAA;AAC3C,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