@neural-technologies-indonesia/nl2sql 0.0.1 → 0.0.2

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.
@@ -1,4 +1,3 @@
1
- import { MCPClient } from "mcp-use";
2
1
  import { MCPQueryExecutor } from "../adapters/mcpExecutor.js";
3
2
  import { MCPMilvusRetriever } from "../adapters/mcpRetriever.js";
4
3
  import type { MCPMilvusResult } from "../adapters/mcpRetriever.js";
@@ -8,6 +7,8 @@ export type MCPUseClientOptions = {
8
7
  command?: string;
9
8
  args?: string[];
10
9
  env?: Record<string, string>;
10
+ transportType?: "stdio" | "httpStream";
11
+ httpStreamUrl?: string;
11
12
  };
12
13
  export declare const createMcpUseAdapters: (options?: MCPUseClientOptions) => Promise<{
13
14
  executor: MCPQueryExecutor;
@@ -33,7 +34,7 @@ export declare const createMcpUseAdapters: (options?: MCPUseClientOptions) => Pr
33
34
  tablename?: string;
34
35
  }) => Promise<string>;
35
36
  close: () => Promise<void>;
36
- client: MCPClient;
37
- session: import("mcp-use").MCPSession;
37
+ client: unknown;
38
+ session: unknown;
38
39
  }>;
39
40
  //# sourceMappingURL=mcpUseClient.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mcpUseClient.d.ts","sourceRoot":"","sources":["../../src/clients/mcpUseClient.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAC;AACjE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAGnE,MAAM,MAAM,mBAAmB,GAAG;IAChC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,EAAE,CA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,eAAe,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;2BAK9B,MAAM;;;;;;;;;;;;;;;4BAmEL;QACnC,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB;iBA1EY,OAAO,CAAC,IAAI,CAAC;;;EAoL7B,CAAC"}
@@ -1,6 +1,8 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { MCPClient } from "mcp-use";
4
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
5
+ import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
4
6
  import { MCPQueryExecutor } from "../adapters/mcpExecutor.js";
5
7
  import { MCPMilvusRetriever } from "../adapters/mcpRetriever.js";
6
8
  // Accepts a whitespace-delimited env var and turns it into an args array.
@@ -69,6 +71,102 @@ const parseDurationMs = (text) => {
69
71
  // Starts mcp-use against the sibling MCP server and returns MCP-backed adapters.
70
72
  export const createMcpUseAdapters = async (options) => {
71
73
  const serverName = options?.serverName ?? "query-runner";
74
+ const transportType = options?.transportType ??
75
+ (process.env.MCP_SERVER_TRANSPORT === "http-stream"
76
+ ? "httpStream"
77
+ : process.env.MCP_SERVER_TRANSPORT);
78
+ const httpStreamUrl = options?.httpStreamUrl ??
79
+ process.env.MCP_SERVER_URL ??
80
+ undefined;
81
+ const buildAdapters = (callTool, close, client, session) => {
82
+ const runQueryExplain = async (sql) => {
83
+ const content = await callTool("run_query_explain", { raw_query: sql });
84
+ const text = contentToText(content);
85
+ const durationMs = parseDurationMs(text) ?? Number.NaN;
86
+ const jsonStart = text.search(/JSON:\s*/i);
87
+ const explainMarker = text.search(/EXPLAIN ANALYZE:\s*/i);
88
+ let parsed = null;
89
+ if (jsonStart !== -1) {
90
+ const jsonBlock = text
91
+ .slice(jsonStart)
92
+ .replace(/^JSON:\s*/i, "");
93
+ const jsonText = explainMarker !== -1
94
+ ? jsonBlock.slice(0, explainMarker - jsonStart).trim()
95
+ : jsonBlock.trim();
96
+ try {
97
+ parsed = JSON.parse(jsonText);
98
+ }
99
+ catch {
100
+ parsed = null;
101
+ }
102
+ }
103
+ if (parsed) {
104
+ const explainLines = Array.isArray(parsed.explainLines)
105
+ ? parsed.explainLines.map((line) => String(line))
106
+ : undefined;
107
+ return {
108
+ durationMs: typeof parsed.durationMs === "number"
109
+ ? parsed.durationMs
110
+ : durationMs,
111
+ explainLines,
112
+ planningMs: typeof parsed.planningMs === "number"
113
+ ? parsed.planningMs
114
+ : undefined,
115
+ executionMs: typeof parsed.executionMs === "number"
116
+ ? parsed.executionMs
117
+ : undefined,
118
+ raw: text,
119
+ };
120
+ }
121
+ const explainSplit = text.split(/EXPLAIN ANALYZE:\s*/i)[1] ??
122
+ text.split(/EXPLAIN:\s*/i)[1] ??
123
+ "";
124
+ return { durationMs, explain: explainSplit.trim(), raw: text };
125
+ };
126
+ const executor = new MCPQueryExecutor({
127
+ runQuery: async (sql) => {
128
+ const content = await callTool("run_query", { raw_query: sql });
129
+ const text = contentToText(content);
130
+ const durationMs = parseDurationMs(text) ?? Number.NaN; // Keep numeric for typing even if parse fails.
131
+ return { durationMs };
132
+ },
133
+ runQueryExplain: async (sql) => runQueryExplain(sql),
134
+ });
135
+ const retriever = new MCPMilvusRetriever({
136
+ milvusRetrieve: async (query, topK) => callTool("milvus_retrieve", { query, top_k: topK ?? 5 }),
137
+ });
138
+ const listIndexes = async (options) => {
139
+ const content = await callTool("list_indexes", {
140
+ schemaname: options?.schemaname,
141
+ tablename: options?.tablename,
142
+ });
143
+ return contentToText(content);
144
+ };
145
+ return {
146
+ executor,
147
+ retriever,
148
+ callTool,
149
+ runQueryExplain,
150
+ listIndexes,
151
+ close,
152
+ client,
153
+ session,
154
+ };
155
+ };
156
+ if (transportType === "httpStream" || httpStreamUrl) {
157
+ const url = httpStreamUrl ?? "http://localhost:8080/mcp";
158
+ const transport = new StreamableHTTPClientTransport(new URL(url));
159
+ const client = new Client({ name: serverName, version: "1.0.0" });
160
+ await client.connect(transport);
161
+ const callTool = async (name, args) => {
162
+ const result = await client.callTool({ name, arguments: args });
163
+ return toolResultPayload(result);
164
+ };
165
+ const close = async () => {
166
+ await transport.close();
167
+ };
168
+ return buildAdapters(callTool, close, client, null);
169
+ }
72
170
  console.log("mcp server cwd", process.env.MCP_SERVER_CWD);
73
171
  const serverCwd = options?.serverCwd ??
74
172
  process.env.MCP_SERVER_CWD ??
@@ -100,83 +198,11 @@ export const createMcpUseAdapters = async (options) => {
100
198
  const result = await session.callTool(name, args);
101
199
  return toolResultPayload(result);
102
200
  };
103
- const executor = new MCPQueryExecutor({
104
- runQuery: async (sql) => {
105
- const content = await callTool("run_query", { raw_query: sql });
106
- const text = contentToText(content);
107
- const durationMs = parseDurationMs(text) ?? Number.NaN; // Keep numeric for typing even if parse fails.
108
- return { durationMs };
109
- },
110
- runQueryExplain: async (sql) => runQueryExplain(sql),
111
- });
112
- const retriever = new MCPMilvusRetriever({
113
- milvusRetrieve: async (query, topK) => callTool("milvus_retrieve", { query, top_k: topK ?? 5 }),
114
- });
115
- const runQueryExplain = async (sql) => {
116
- const content = await callTool("run_query_explain", { raw_query: sql });
117
- const text = contentToText(content);
118
- const durationMs = parseDurationMs(text) ?? Number.NaN;
119
- const jsonStart = text.search(/JSON:\s*/i);
120
- const explainMarker = text.search(/EXPLAIN ANALYZE:\s*/i);
121
- let parsed = null;
122
- if (jsonStart !== -1) {
123
- const jsonBlock = text
124
- .slice(jsonStart)
125
- .replace(/^JSON:\s*/i, "");
126
- const jsonText = explainMarker !== -1
127
- ? jsonBlock.slice(0, explainMarker - jsonStart).trim()
128
- : jsonBlock.trim();
129
- try {
130
- parsed = JSON.parse(jsonText);
131
- }
132
- catch {
133
- parsed = null;
134
- }
135
- }
136
- if (parsed) {
137
- const explainLines = Array.isArray(parsed.explainLines)
138
- ? parsed.explainLines.map((line) => String(line))
139
- : undefined;
140
- return {
141
- durationMs: typeof parsed.durationMs === "number"
142
- ? parsed.durationMs
143
- : durationMs,
144
- explainLines,
145
- planningMs: typeof parsed.planningMs === "number"
146
- ? parsed.planningMs
147
- : undefined,
148
- executionMs: typeof parsed.executionMs === "number"
149
- ? parsed.executionMs
150
- : undefined,
151
- raw: text,
152
- };
153
- }
154
- const explainSplit = text.split(/EXPLAIN ANALYZE:\s*/i)[1] ??
155
- text.split(/EXPLAIN:\s*/i)[1] ??
156
- "";
157
- return { durationMs, explain: explainSplit.trim(), raw: text };
158
- };
159
- const listIndexes = async (options) => {
160
- const content = await callTool("list_indexes", {
161
- schemaname: options?.schemaname,
162
- tablename: options?.tablename,
163
- });
164
- return contentToText(content);
165
- };
166
201
  const close = async () => {
167
202
  if (typeof client
168
203
  .closeAllSessions === "function") {
169
204
  await client.closeAllSessions();
170
205
  }
171
206
  };
172
- return {
173
- executor,
174
- retriever,
175
- callTool,
176
- runQueryExplain,
177
- listIndexes,
178
- close,
179
- client,
180
- session,
181
- };
207
+ return buildAdapters(callTool, close, client, session);
182
208
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neural-technologies-indonesia/nl2sql",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Core reasoning pipeline for text-to-SQL.",
5
5
  "private": false,
6
6
  "type": "module",
@@ -25,6 +25,7 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "langchain": "^1.2.1",
28
+ "@modelcontextprotocol/sdk": "^1.25.1",
28
29
  "mcp-use": "*",
29
30
  "openai": "^6.14.0"
30
31
  },