@fractal-solutions/qflow 1.0.84 → 2.0.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/CHANGELOG.md +119 -0
- package/LICENSE +21 -504
- package/MIGRATION.md +1130 -0
- package/README.md +1432 -823
- package/dist/agent/agent.d.ts +4 -22
- package/dist/agent/agent.js +4 -364
- package/dist/agent/base_agent.d.ts +8 -20
- package/dist/agent/base_agent.js +8 -205
- package/dist/agent/control_nodes.d.ts +61 -0
- package/dist/agent/control_nodes.js +164 -0
- package/dist/agent/engine.d.ts +45 -0
- package/dist/agent/engine.js +415 -0
- package/dist/agent/flow.d.ts +77 -0
- package/dist/agent/flow.js +148 -0
- package/dist/agent/index.d.ts +12 -0
- package/dist/agent/index.js +12 -0
- package/dist/agent/lpavrl_agent.d.ts +1 -2
- package/dist/agent/lpavrl_agent.js +2 -175
- package/dist/agent/ooda_agent.d.ts +1 -2
- package/dist/agent/ooda_agent.js +2 -164
- package/dist/agent/pema_agent.d.ts +1 -2
- package/dist/agent/pema_agent.js +2 -168
- package/dist/agent/raor_agent.d.ts +1 -2
- package/dist/agent/raor_agent.js +2 -161
- package/dist/agent/strategies.d.ts +126 -0
- package/dist/agent/strategies.js +159 -0
- package/dist/agent/tools.d.ts +1 -0
- package/dist/agent/tools.js +42 -24
- package/dist/approval.d.ts +42 -0
- package/dist/approval.js +86 -0
- package/dist/budget.d.ts +103 -0
- package/dist/budget.js +133 -0
- package/dist/bun_nodes.d.ts +119 -0
- package/dist/bun_nodes.js +331 -0
- package/dist/contracts.d.ts +30 -0
- package/dist/contracts.js +103 -0
- package/dist/durable.d.ts +278 -0
- package/dist/durable.js +730 -0
- package/dist/eval.d.ts +165 -0
- package/dist/eval.js +263 -0
- package/dist/graph.d.ts +157 -0
- package/dist/graph.js +330 -0
- package/dist/llm/index.d.ts +359 -0
- package/dist/llm/index.js +924 -0
- package/dist/logger.js +7 -3
- package/dist/mcp/client.d.ts +55 -0
- package/dist/mcp/client.js +145 -0
- package/dist/mcp/index.d.ts +2 -0
- package/dist/mcp/index.js +2 -0
- package/dist/mcp/server.d.ts +106 -0
- package/dist/mcp/server.js +154 -0
- package/dist/mcp/stdio.d.ts +2 -0
- package/dist/mcp/stdio.js +3 -0
- package/dist/memory.d.ts +101 -0
- package/dist/memory.js +217 -0
- package/dist/nodes/browser_control.js +5 -4
- package/dist/nodes/data_extractor.js +2 -1
- package/dist/nodes/data_validation.d.ts +3 -3
- package/dist/nodes/data_validation.js +19 -5
- package/dist/nodes/database.d.ts +2 -1
- package/dist/nodes/database.js +71 -58
- package/dist/nodes/gis.js +1 -1
- package/dist/nodes/git.d.ts +1 -1
- package/dist/nodes/git.js +65 -51
- package/dist/nodes/github.d.ts +1 -0
- package/dist/nodes/hardware_interaction.d.ts +2 -4
- package/dist/nodes/hardware_interaction.js +4 -3
- package/dist/nodes/http.d.ts +1 -7
- package/dist/nodes/http.js +46 -33
- package/dist/nodes/image_gallery.js +45 -45
- package/dist/nodes/index.d.ts +8 -0
- package/dist/nodes/index.js +8 -0
- package/dist/nodes/interactive_webview.d.ts +1 -2
- package/dist/nodes/interactive_webview.js +217 -216
- package/dist/nodes/multimedia_processing.js +1 -1
- package/dist/nodes/navigator.d.ts +7 -0
- package/dist/nodes/navigator.js +48 -8
- package/dist/nodes/pdf_processor.js +2 -1
- package/dist/nodes/remote_execution.js +2 -2
- package/dist/nodes/s3.d.ts +1 -0
- package/dist/nodes/scheduler.js +2 -2
- package/dist/nodes/search.d.ts +1 -1
- package/dist/nodes/search.js +3 -2
- package/dist/nodes/speech_synthesis.js +2 -1
- package/dist/nodes/spreadsheet.d.ts +1 -7
- package/dist/nodes/spreadsheet.js +2 -1
- package/dist/nodes/streaming_llm.d.ts +51 -0
- package/dist/nodes/streaming_llm.js +120 -0
- package/dist/nodes/webhook.d.ts +1 -5
- package/dist/nodes/webhook.js +106 -93
- package/dist/nodes/websockets.js +5 -3
- package/dist/nodes/webview.js +3 -2
- package/dist/optional.d.ts +32 -0
- package/dist/optional.js +61 -0
- package/dist/qflow.d.ts +25 -14
- package/dist/qflow.js +140 -49
- package/dist/schedule.d.ts +41 -0
- package/dist/schedule.js +149 -0
- package/dist/side_effects.d.ts +50 -0
- package/dist/side_effects.js +94 -0
- package/dist/streaming.d.ts +35 -0
- package/dist/streaming.js +199 -0
- package/dist/testing.d.ts +47 -0
- package/dist/testing.js +130 -0
- package/dist/tool_spec.d.ts +76 -0
- package/dist/tool_spec.js +321 -0
- package/dist/trace.d.ts +28 -0
- package/dist/trace.js +190 -0
- package/dist/util.d.ts +57 -0
- package/dist/util.js +122 -0
- package/package.json +153 -67
- package/qflow.config.js +18 -18
- package/scripts/postinstall.js +50 -0
- package/scripts/verify-package.js +114 -0
package/dist/logger.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
3
|
export const LogLevel = {
|
|
4
4
|
DEBUG: 0,
|
|
5
5
|
INFO: 1,
|
|
@@ -68,7 +68,11 @@ class RemoteLogHandler {
|
|
|
68
68
|
export function log(message, nodeLoggingConfig, defaultOptions = {}) {
|
|
69
69
|
const options = { ...defaultOptions, ...nodeLoggingConfig };
|
|
70
70
|
const { type = 'info', method = 'console', params = {} } = options;
|
|
71
|
-
const
|
|
71
|
+
const levelName = String(type).toUpperCase();
|
|
72
|
+
if (levelName === 'NONE' || method === 'none') {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const level = LogLevel[levelName];
|
|
72
76
|
if (level === undefined || level < currentLogLevel) {
|
|
73
77
|
return;
|
|
74
78
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calls a tool on an external MCP server, launched as a child process.
|
|
3
|
+
* Bun-only: uses Bun.spawn and stream-based stdio.
|
|
4
|
+
*/
|
|
5
|
+
export class MCPClientNode extends AsyncNode {
|
|
6
|
+
static getToolDefinition(): {
|
|
7
|
+
name: string;
|
|
8
|
+
description: string;
|
|
9
|
+
parameters: {
|
|
10
|
+
type: string;
|
|
11
|
+
properties: {
|
|
12
|
+
command: {
|
|
13
|
+
type: string;
|
|
14
|
+
description: string;
|
|
15
|
+
};
|
|
16
|
+
args: {
|
|
17
|
+
type: string;
|
|
18
|
+
items: {
|
|
19
|
+
type: string;
|
|
20
|
+
};
|
|
21
|
+
description: string;
|
|
22
|
+
};
|
|
23
|
+
tool: {
|
|
24
|
+
type: string;
|
|
25
|
+
description: string;
|
|
26
|
+
};
|
|
27
|
+
toolArguments: {
|
|
28
|
+
type: string;
|
|
29
|
+
description: string;
|
|
30
|
+
};
|
|
31
|
+
env: {
|
|
32
|
+
type: string;
|
|
33
|
+
description: string;
|
|
34
|
+
};
|
|
35
|
+
cwd: {
|
|
36
|
+
type: string;
|
|
37
|
+
description: string;
|
|
38
|
+
};
|
|
39
|
+
timeout: {
|
|
40
|
+
type: string;
|
|
41
|
+
description: string;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
required: string[];
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
execAsync(): Promise<{
|
|
48
|
+
content: any;
|
|
49
|
+
text: any;
|
|
50
|
+
data: any;
|
|
51
|
+
isError: boolean;
|
|
52
|
+
}>;
|
|
53
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<string>;
|
|
54
|
+
}
|
|
55
|
+
import { AsyncNode } from '../qflow.js';
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { AsyncNode } from '../qflow.js';
|
|
2
|
+
const encoder = new TextEncoder();
|
|
3
|
+
/**
|
|
4
|
+
* Calls a tool on an external MCP server, launched as a child process.
|
|
5
|
+
* Bun-only: uses Bun.spawn and stream-based stdio.
|
|
6
|
+
*/
|
|
7
|
+
export class MCPClientNode extends AsyncNode {
|
|
8
|
+
static getToolDefinition() {
|
|
9
|
+
return {
|
|
10
|
+
name: 'mcp_tool',
|
|
11
|
+
description: 'Calls a tool on an external MCP server.',
|
|
12
|
+
parameters: {
|
|
13
|
+
type: 'object',
|
|
14
|
+
properties: {
|
|
15
|
+
command: { type: 'string', description: 'Executable that starts the MCP server.' },
|
|
16
|
+
args: { type: 'array', items: { type: 'string' }, description: 'Arguments passed to the MCP server command.' },
|
|
17
|
+
tool: { type: 'string', description: 'Name of the MCP tool to call.' },
|
|
18
|
+
toolArguments: { type: 'object', description: 'Arguments for the MCP tool.' },
|
|
19
|
+
env: { type: 'object', description: 'Extra environment variables for the MCP server.' },
|
|
20
|
+
cwd: { type: 'string', description: 'Working directory for the MCP server.' },
|
|
21
|
+
timeout: { type: 'number', description: 'Per-request timeout in milliseconds. Defaults to 60000.' },
|
|
22
|
+
},
|
|
23
|
+
required: ['command', 'tool'],
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
async execAsync() {
|
|
28
|
+
const { command, args = [], tool, toolArguments = {}, env = {}, cwd = process.cwd(), timeout = 60000, } = this.params;
|
|
29
|
+
if (!command)
|
|
30
|
+
throw new Error('MCPClientNode requires a `command`.');
|
|
31
|
+
if (!tool)
|
|
32
|
+
throw new Error('MCPClientNode requires a `tool`.');
|
|
33
|
+
const child = Bun.spawn([command, ...args], {
|
|
34
|
+
stdin: 'pipe',
|
|
35
|
+
stdout: 'pipe',
|
|
36
|
+
stderr: 'pipe',
|
|
37
|
+
cwd,
|
|
38
|
+
env: { ...process.env, ...env },
|
|
39
|
+
});
|
|
40
|
+
const pending = new Map();
|
|
41
|
+
const decoder = new TextDecoder();
|
|
42
|
+
let nextId = 1;
|
|
43
|
+
let buffer = '';
|
|
44
|
+
let streamError = null;
|
|
45
|
+
const readLoop = (async () => {
|
|
46
|
+
const reader = child.stdout.getReader();
|
|
47
|
+
try {
|
|
48
|
+
while (true) {
|
|
49
|
+
const { value, done } = await reader.read();
|
|
50
|
+
if (done)
|
|
51
|
+
break;
|
|
52
|
+
buffer += decoder.decode(value, { stream: true });
|
|
53
|
+
let index;
|
|
54
|
+
while ((index = buffer.indexOf('\n')) !== -1) {
|
|
55
|
+
const line = buffer.slice(0, index).trim();
|
|
56
|
+
buffer = buffer.slice(index + 1);
|
|
57
|
+
if (!line)
|
|
58
|
+
continue;
|
|
59
|
+
let message;
|
|
60
|
+
try {
|
|
61
|
+
message = JSON.parse(line);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (message.id == null)
|
|
67
|
+
continue;
|
|
68
|
+
const entry = pending.get(message.id);
|
|
69
|
+
if (!entry)
|
|
70
|
+
continue;
|
|
71
|
+
clearTimeout(entry.timer);
|
|
72
|
+
pending.delete(message.id);
|
|
73
|
+
if (message.error)
|
|
74
|
+
entry.reject(new Error(message.error.message ?? 'MCP error'));
|
|
75
|
+
else
|
|
76
|
+
entry.resolve(message.result);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
streamError = error;
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
for (const entry of pending.values()) {
|
|
85
|
+
clearTimeout(entry.timer);
|
|
86
|
+
entry.reject(streamError ?? new Error('MCP server closed the connection'));
|
|
87
|
+
}
|
|
88
|
+
pending.clear();
|
|
89
|
+
}
|
|
90
|
+
})();
|
|
91
|
+
const stdin = child.stdin;
|
|
92
|
+
const writeLine = message => stdin.write(encoder.encode(JSON.stringify(message) + '\n'));
|
|
93
|
+
const send = (method, params) => new Promise((resolve, reject) => {
|
|
94
|
+
const id = nextId++;
|
|
95
|
+
const timer = setTimeout(() => {
|
|
96
|
+
pending.delete(id);
|
|
97
|
+
reject(new Error(`MCP request '${method}' timed out after ${timeout}ms`));
|
|
98
|
+
}, timeout);
|
|
99
|
+
pending.set(id, { resolve, reject, timer });
|
|
100
|
+
try {
|
|
101
|
+
writeLine({ jsonrpc: '2.0', id, method, params });
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
pending.delete(id);
|
|
106
|
+
reject(error);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
const notify = (method, params) => writeLine({ jsonrpc: '2.0', method, params });
|
|
110
|
+
try {
|
|
111
|
+
await send('initialize', {
|
|
112
|
+
protocolVersion: '2024-11-05',
|
|
113
|
+
capabilities: {},
|
|
114
|
+
clientInfo: { name: 'qflow', version: '2.0.0' },
|
|
115
|
+
});
|
|
116
|
+
await notify('notifications/initialized', {});
|
|
117
|
+
const result = await send('tools/call', { name: tool, arguments: toolArguments });
|
|
118
|
+
const content = result?.content ?? [];
|
|
119
|
+
const text = content.filter(item => item.type === 'text').map(item => item.text).join('\n');
|
|
120
|
+
let data = text;
|
|
121
|
+
try {
|
|
122
|
+
data = JSON.parse(text);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
// Non-JSON tool output is still returned as text.
|
|
126
|
+
}
|
|
127
|
+
return { content, text, data, isError: Boolean(result?.isError) };
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
try {
|
|
131
|
+
stdin.end();
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
// The child may already have closed its stdin.
|
|
135
|
+
}
|
|
136
|
+
child.kill();
|
|
137
|
+
await readLoop.catch(() => { });
|
|
138
|
+
await child.exited.catch(() => { });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async postAsync(shared, prepRes, execRes) {
|
|
142
|
+
shared.mcpResult = execRes;
|
|
143
|
+
return 'default';
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
export function createMCPServer(options: any): QFlowMCPServer;
|
|
2
|
+
/**
|
|
3
|
+
* Exposes qflow's registered nodes as MCP tools over newline-delimited JSON-RPC
|
|
4
|
+
* on stdin/stdout. Intended to be launched by an MCP client as a child process.
|
|
5
|
+
*/
|
|
6
|
+
export class QFlowMCPServer {
|
|
7
|
+
constructor(options?: {});
|
|
8
|
+
name: any;
|
|
9
|
+
version: any;
|
|
10
|
+
toolRegistry: any;
|
|
11
|
+
toolClasses: any;
|
|
12
|
+
definitions: any;
|
|
13
|
+
classByName: Map<any, any>;
|
|
14
|
+
callTool({ name, arguments: args }: {
|
|
15
|
+
name: any;
|
|
16
|
+
arguments?: {} | undefined;
|
|
17
|
+
}): Promise<{
|
|
18
|
+
content: {
|
|
19
|
+
type: string;
|
|
20
|
+
text: string;
|
|
21
|
+
}[];
|
|
22
|
+
isError?: undefined;
|
|
23
|
+
} | {
|
|
24
|
+
content: {
|
|
25
|
+
type: string;
|
|
26
|
+
text: string;
|
|
27
|
+
}[];
|
|
28
|
+
isError: boolean;
|
|
29
|
+
}>;
|
|
30
|
+
handleMessage(message: any): Promise<{
|
|
31
|
+
jsonrpc: string;
|
|
32
|
+
id: any;
|
|
33
|
+
result: {
|
|
34
|
+
content: {
|
|
35
|
+
type: string;
|
|
36
|
+
text: string;
|
|
37
|
+
}[];
|
|
38
|
+
isError?: undefined;
|
|
39
|
+
} | {
|
|
40
|
+
content: {
|
|
41
|
+
type: string;
|
|
42
|
+
text: string;
|
|
43
|
+
}[];
|
|
44
|
+
isError: boolean;
|
|
45
|
+
} | {
|
|
46
|
+
protocolVersion: string;
|
|
47
|
+
capabilities: {
|
|
48
|
+
tools: {
|
|
49
|
+
listChanged: boolean;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
serverInfo: {
|
|
53
|
+
name: any;
|
|
54
|
+
version: any;
|
|
55
|
+
};
|
|
56
|
+
tools?: undefined;
|
|
57
|
+
resources?: undefined;
|
|
58
|
+
prompts?: undefined;
|
|
59
|
+
} | {
|
|
60
|
+
protocolVersion?: undefined;
|
|
61
|
+
capabilities?: undefined;
|
|
62
|
+
serverInfo?: undefined;
|
|
63
|
+
tools?: undefined;
|
|
64
|
+
resources?: undefined;
|
|
65
|
+
prompts?: undefined;
|
|
66
|
+
} | {
|
|
67
|
+
tools: any;
|
|
68
|
+
protocolVersion?: undefined;
|
|
69
|
+
capabilities?: undefined;
|
|
70
|
+
serverInfo?: undefined;
|
|
71
|
+
resources?: undefined;
|
|
72
|
+
prompts?: undefined;
|
|
73
|
+
} | {
|
|
74
|
+
resources: never[];
|
|
75
|
+
protocolVersion?: undefined;
|
|
76
|
+
capabilities?: undefined;
|
|
77
|
+
serverInfo?: undefined;
|
|
78
|
+
tools?: undefined;
|
|
79
|
+
prompts?: undefined;
|
|
80
|
+
} | {
|
|
81
|
+
prompts: never[];
|
|
82
|
+
protocolVersion?: undefined;
|
|
83
|
+
capabilities?: undefined;
|
|
84
|
+
serverInfo?: undefined;
|
|
85
|
+
tools?: undefined;
|
|
86
|
+
resources?: undefined;
|
|
87
|
+
};
|
|
88
|
+
error?: undefined;
|
|
89
|
+
} | {
|
|
90
|
+
jsonrpc: string;
|
|
91
|
+
id: any;
|
|
92
|
+
error: {
|
|
93
|
+
code: any;
|
|
94
|
+
message: any;
|
|
95
|
+
};
|
|
96
|
+
result?: undefined;
|
|
97
|
+
} | null>;
|
|
98
|
+
start({ input, output }?: {
|
|
99
|
+
input?: (NodeJS.ReadStream & {
|
|
100
|
+
fd: 0;
|
|
101
|
+
}) | undefined;
|
|
102
|
+
output?: (NodeJS.WriteStream & {
|
|
103
|
+
fd: 1;
|
|
104
|
+
}) | undefined;
|
|
105
|
+
}): Promise<void>;
|
|
106
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import readline from 'node:readline';
|
|
2
|
+
import { getToolClasses, getToolDefinitions } from '../agent/tools.js';
|
|
3
|
+
import { AsyncFlow } from '../qflow.js';
|
|
4
|
+
const PROTOCOL_VERSION = '2024-11-05';
|
|
5
|
+
class RpcError extends Error {
|
|
6
|
+
constructor(code, message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.code = code;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function toMCPTool(definition) {
|
|
12
|
+
return {
|
|
13
|
+
name: definition.name,
|
|
14
|
+
description: definition.description,
|
|
15
|
+
inputSchema: definition.parameters ?? { type: 'object', properties: {} },
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Exposes qflow's registered nodes as MCP tools over newline-delimited JSON-RPC
|
|
20
|
+
* on stdin/stdout. Intended to be launched by an MCP client as a child process.
|
|
21
|
+
*/
|
|
22
|
+
export class QFlowMCPServer {
|
|
23
|
+
constructor(options = {}) {
|
|
24
|
+
const { name = 'qflow', version = '2.0.0', toolClasses, toolRegistry } = options;
|
|
25
|
+
this.name = name;
|
|
26
|
+
this.version = version;
|
|
27
|
+
this.toolRegistry = toolRegistry ?? null;
|
|
28
|
+
this.toolClasses = toolClasses ?? getToolClasses();
|
|
29
|
+
if (this.toolRegistry) {
|
|
30
|
+
this.definitions = this.toolRegistry.definitions();
|
|
31
|
+
this.classByName = new Map();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
this.definitions = toolClasses
|
|
35
|
+
? this.toolClasses.filter(c => typeof c.getToolDefinition === 'function').map(c => c.getToolDefinition())
|
|
36
|
+
: getToolDefinitions();
|
|
37
|
+
this.classByName = new Map();
|
|
38
|
+
for (const ToolClass of this.toolClasses) {
|
|
39
|
+
if (typeof ToolClass.getToolDefinition !== 'function')
|
|
40
|
+
continue;
|
|
41
|
+
this.classByName.set(ToolClass.getToolDefinition().name, ToolClass);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async callTool({ name, arguments: args = {} }) {
|
|
45
|
+
if (this.toolRegistry) {
|
|
46
|
+
if (name === 'finish' && !this.toolRegistry.get(name)) {
|
|
47
|
+
return { content: [{ type: 'text', text: String(args?.output ?? '') }] };
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
const outcome = await this.toolRegistry.execute(name, args ?? {});
|
|
51
|
+
const output = outcome.result;
|
|
52
|
+
const text = typeof output === 'string' ? output : JSON.stringify(output ?? null, null, 2);
|
|
53
|
+
return { content: [{ type: 'text', text: text ?? '' }] };
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
return {
|
|
57
|
+
content: [{ type: 'text', text: `Tool '${name}' failed: ${error.message}` }],
|
|
58
|
+
isError: true,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const ToolClass = this.classByName.get(name);
|
|
63
|
+
if (!ToolClass) {
|
|
64
|
+
if (name === 'finish') {
|
|
65
|
+
return { content: [{ type: 'text', text: String(args?.output ?? '') }] };
|
|
66
|
+
}
|
|
67
|
+
return { content: [{ type: 'text', text: `Unknown tool: ${name}` }], isError: true };
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const node = new ToolClass();
|
|
71
|
+
node.setParams(args ?? {});
|
|
72
|
+
const result = await new AsyncFlow(node).runAsync({});
|
|
73
|
+
const text = typeof result === 'string' ? result : JSON.stringify(result ?? null, null, 2);
|
|
74
|
+
return { content: [{ type: 'text', text: text ?? '' }] };
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
return {
|
|
78
|
+
content: [{ type: 'text', text: `Tool '${name}' failed: ${error.message}` }],
|
|
79
|
+
isError: true,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async handleMessage(message) {
|
|
84
|
+
const { id, method, params = {} } = message;
|
|
85
|
+
const isNotification = id === undefined;
|
|
86
|
+
try {
|
|
87
|
+
let result;
|
|
88
|
+
switch (method) {
|
|
89
|
+
case 'initialize':
|
|
90
|
+
result = {
|
|
91
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
92
|
+
capabilities: { tools: { listChanged: false } },
|
|
93
|
+
serverInfo: { name: this.name, version: this.version },
|
|
94
|
+
};
|
|
95
|
+
break;
|
|
96
|
+
case 'notifications/initialized':
|
|
97
|
+
case 'notifications/cancelled':
|
|
98
|
+
return null;
|
|
99
|
+
case 'ping':
|
|
100
|
+
result = {};
|
|
101
|
+
break;
|
|
102
|
+
case 'tools/list':
|
|
103
|
+
result = { tools: this.definitions.map(toMCPTool) };
|
|
104
|
+
break;
|
|
105
|
+
case 'tools/call':
|
|
106
|
+
result = await this.callTool(params);
|
|
107
|
+
break;
|
|
108
|
+
case 'resources/list':
|
|
109
|
+
result = { resources: [] };
|
|
110
|
+
break;
|
|
111
|
+
case 'prompts/list':
|
|
112
|
+
result = { prompts: [] };
|
|
113
|
+
break;
|
|
114
|
+
default:
|
|
115
|
+
throw new RpcError(-32601, `Method not found: ${method}`);
|
|
116
|
+
}
|
|
117
|
+
return isNotification ? null : { jsonrpc: '2.0', id, result };
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
if (isNotification)
|
|
121
|
+
return null;
|
|
122
|
+
return {
|
|
123
|
+
jsonrpc: '2.0',
|
|
124
|
+
id,
|
|
125
|
+
error: { code: error instanceof RpcError ? error.code : -32603, message: error.message },
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async start({ input = process.stdin, output = process.stdout } = {}) {
|
|
130
|
+
const lines = readline.createInterface({ input, crlfDelay: Infinity });
|
|
131
|
+
for await (const line of lines) {
|
|
132
|
+
if (!line.trim())
|
|
133
|
+
continue;
|
|
134
|
+
let message;
|
|
135
|
+
try {
|
|
136
|
+
message = JSON.parse(line);
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
output.write(JSON.stringify({
|
|
140
|
+
jsonrpc: '2.0',
|
|
141
|
+
id: null,
|
|
142
|
+
error: { code: -32700, message: 'Parse error' },
|
|
143
|
+
}) + '\n');
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const response = await this.handleMessage(message);
|
|
147
|
+
if (response)
|
|
148
|
+
output.write(JSON.stringify(response) + '\n');
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export function createMCPServer(options) {
|
|
153
|
+
return new QFlowMCPServer(options);
|
|
154
|
+
}
|
package/dist/memory.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
export function tokenize(text: any): string[];
|
|
2
|
+
/** Lightweight lexical score used by both in-memory and SQLite stores. */
|
|
3
|
+
export function scoreMemory(record: any, tokens: any, tags?: any[]): number;
|
|
4
|
+
export const MEMORY_ADD_EVENT: "memory:add";
|
|
5
|
+
export const MEMORY_SEARCH_EVENT: "memory:search";
|
|
6
|
+
/** Common interface: add / search / get / delete / list / clear. */
|
|
7
|
+
export class MemoryStore {
|
|
8
|
+
add(): Promise<void>;
|
|
9
|
+
search(): Promise<void>;
|
|
10
|
+
get(): Promise<void>;
|
|
11
|
+
delete(): Promise<void>;
|
|
12
|
+
list(): Promise<void>;
|
|
13
|
+
clear(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export class InMemoryMemoryStore extends MemoryStore {
|
|
16
|
+
items: Map<any, any>;
|
|
17
|
+
add({ id, text, tags, metadata }?: {
|
|
18
|
+
tags?: never[] | undefined;
|
|
19
|
+
metadata?: {} | undefined;
|
|
20
|
+
}): Promise<{
|
|
21
|
+
id: any;
|
|
22
|
+
text: string;
|
|
23
|
+
tags: string[];
|
|
24
|
+
metadata: {};
|
|
25
|
+
createdAt: string;
|
|
26
|
+
}>;
|
|
27
|
+
search(query: any, options?: {}): Promise<any>;
|
|
28
|
+
get(id: any): Promise<any>;
|
|
29
|
+
delete(id: any): Promise<boolean>;
|
|
30
|
+
list(): Promise<any[]>;
|
|
31
|
+
clear(): Promise<number>;
|
|
32
|
+
}
|
|
33
|
+
export class SQLiteMemoryStore extends MemoryStore {
|
|
34
|
+
constructor(filename?: string);
|
|
35
|
+
filename: string;
|
|
36
|
+
db: Database;
|
|
37
|
+
add({ id, text, tags, metadata }?: {
|
|
38
|
+
tags?: never[] | undefined;
|
|
39
|
+
metadata?: {} | undefined;
|
|
40
|
+
}): Promise<{
|
|
41
|
+
id: any;
|
|
42
|
+
text: string;
|
|
43
|
+
tags: string[];
|
|
44
|
+
metadata: {};
|
|
45
|
+
createdAt: string;
|
|
46
|
+
}>;
|
|
47
|
+
search(query: any, options?: {}): Promise<any>;
|
|
48
|
+
listSync(): ({
|
|
49
|
+
id: any;
|
|
50
|
+
text: any;
|
|
51
|
+
tags: any;
|
|
52
|
+
metadata: any;
|
|
53
|
+
createdAt: any;
|
|
54
|
+
} | null)[];
|
|
55
|
+
get(id: any): Promise<{
|
|
56
|
+
id: any;
|
|
57
|
+
text: any;
|
|
58
|
+
tags: any;
|
|
59
|
+
metadata: any;
|
|
60
|
+
createdAt: any;
|
|
61
|
+
} | null>;
|
|
62
|
+
delete(id: any): Promise<boolean>;
|
|
63
|
+
list(): Promise<({
|
|
64
|
+
id: any;
|
|
65
|
+
text: any;
|
|
66
|
+
tags: any;
|
|
67
|
+
metadata: any;
|
|
68
|
+
createdAt: any;
|
|
69
|
+
} | null)[]>;
|
|
70
|
+
clear(): Promise<number>;
|
|
71
|
+
close(): void;
|
|
72
|
+
#private;
|
|
73
|
+
}
|
|
74
|
+
/** Flow-facing wrapper around any MemoryStore implementation. */
|
|
75
|
+
export class MemoryStoreNode extends AsyncNode {
|
|
76
|
+
constructor({ store, operation, inputKey, outputKey, storeKey, database, limit, }?: {
|
|
77
|
+
store?: null | undefined;
|
|
78
|
+
operation?: string | undefined;
|
|
79
|
+
inputKey?: string | undefined;
|
|
80
|
+
outputKey?: string | undefined;
|
|
81
|
+
storeKey?: string | undefined;
|
|
82
|
+
database?: null | undefined;
|
|
83
|
+
limit?: number | undefined;
|
|
84
|
+
});
|
|
85
|
+
store: any;
|
|
86
|
+
operation: string;
|
|
87
|
+
inputKey: string;
|
|
88
|
+
outputKey: string;
|
|
89
|
+
storeKey: string;
|
|
90
|
+
database: any;
|
|
91
|
+
limit: number;
|
|
92
|
+
prepAsync(shared?: {}): Promise<{
|
|
93
|
+
store: any;
|
|
94
|
+
operation: any;
|
|
95
|
+
shared: {};
|
|
96
|
+
}>;
|
|
97
|
+
execAsync(prepRes: any, shared?: {}, observer?: {}): Promise<any>;
|
|
98
|
+
postAsync(shared: any, _prepRes: any, execRes: any): Promise<any>;
|
|
99
|
+
}
|
|
100
|
+
import { Database } from 'bun:sqlite';
|
|
101
|
+
import { AsyncNode } from './qflow.js';
|