polarisagi-knowledge-base 0.1.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/dist/index.js +177 -0
- package/package.json +21 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
20
|
+
var ownKeys = function(o) {
|
|
21
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
22
|
+
var ar = [];
|
|
23
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
24
|
+
return ar;
|
|
25
|
+
};
|
|
26
|
+
return ownKeys(o);
|
|
27
|
+
};
|
|
28
|
+
return function (mod) {
|
|
29
|
+
if (mod && mod.__esModule) return mod;
|
|
30
|
+
var result = {};
|
|
31
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
+
__setModuleDefault(result, mod);
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
})();
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
const readline = __importStar(require("readline"));
|
|
38
|
+
const fs = __importStar(require("fs/promises"));
|
|
39
|
+
const path = __importStar(require("path"));
|
|
40
|
+
// Allowed directory validation
|
|
41
|
+
const allowedDir = process.env.POLARIS_KB_ALLOWED_DIR;
|
|
42
|
+
function isPathAllowed(targetPath) {
|
|
43
|
+
if (!allowedDir) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
const absPath = path.resolve(targetPath);
|
|
47
|
+
const rootPath = path.resolve(allowedDir);
|
|
48
|
+
return absPath === rootPath || absPath.startsWith(rootPath + path.sep);
|
|
49
|
+
}
|
|
50
|
+
// MCP JSON-RPC setup
|
|
51
|
+
const rl = readline.createInterface({
|
|
52
|
+
input: process.stdin,
|
|
53
|
+
output: process.stdout,
|
|
54
|
+
terminal: false
|
|
55
|
+
});
|
|
56
|
+
function sendResult(id, result) {
|
|
57
|
+
const msg = {
|
|
58
|
+
jsonrpc: "2.0",
|
|
59
|
+
id,
|
|
60
|
+
result
|
|
61
|
+
};
|
|
62
|
+
console.log(JSON.stringify(msg));
|
|
63
|
+
}
|
|
64
|
+
function sendError(id, code, message) {
|
|
65
|
+
const msg = {
|
|
66
|
+
jsonrpc: "2.0",
|
|
67
|
+
id,
|
|
68
|
+
error: { code, message }
|
|
69
|
+
};
|
|
70
|
+
console.log(JSON.stringify(msg));
|
|
71
|
+
}
|
|
72
|
+
rl.on('line', async (line) => {
|
|
73
|
+
if (!line.trim())
|
|
74
|
+
return;
|
|
75
|
+
try {
|
|
76
|
+
const req = JSON.parse(line);
|
|
77
|
+
if (req.jsonrpc !== "2.0" || !req.method || req.id === undefined) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const id = req.id;
|
|
81
|
+
if (req.method === "initialize") {
|
|
82
|
+
sendResult(id, {
|
|
83
|
+
protocolVersion: "2024-11-05",
|
|
84
|
+
capabilities: { tools: {} },
|
|
85
|
+
serverInfo: {
|
|
86
|
+
name: "polaris-knowledge-base-mcp",
|
|
87
|
+
version: "0.1.0"
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
else if (req.method === "tools/list") {
|
|
92
|
+
sendResult(id, {
|
|
93
|
+
tools: [
|
|
94
|
+
{
|
|
95
|
+
name: "list_files",
|
|
96
|
+
description: "List files in a given directory path",
|
|
97
|
+
inputSchema: {
|
|
98
|
+
type: "object",
|
|
99
|
+
properties: {
|
|
100
|
+
path: {
|
|
101
|
+
type: "string",
|
|
102
|
+
description: "The absolute path to the directory"
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
required: ["path"]
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
name: "read_content",
|
|
110
|
+
description: "Read the textual content of a specific file",
|
|
111
|
+
inputSchema: {
|
|
112
|
+
type: "object",
|
|
113
|
+
properties: {
|
|
114
|
+
path: {
|
|
115
|
+
type: "string",
|
|
116
|
+
description: "The absolute path to the file"
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
required: ["path"]
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
]
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
else if (req.method === "tools/call") {
|
|
126
|
+
const params = req.params || {};
|
|
127
|
+
const args = params.arguments || {};
|
|
128
|
+
if (params.name === "list_files") {
|
|
129
|
+
const targetPath = args.path;
|
|
130
|
+
if (typeof targetPath !== "string") {
|
|
131
|
+
sendResult(id, { isError: true, content: [{ type: "text", text: "path is required and must be a string" }] });
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (!isPathAllowed(targetPath)) {
|
|
135
|
+
sendResult(id, { isError: true, content: [{ type: "text", text: "path is outside the allowed directory (POLARIS_KB_ALLOWED_DIR)" }] });
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
const entries = await fs.readdir(targetPath, { withFileTypes: true });
|
|
140
|
+
const fileNames = entries.map(e => e.isDirectory() ? `${e.name}/` : e.name);
|
|
141
|
+
const result = `Files in ${targetPath}:\n${fileNames.join('\n')}`;
|
|
142
|
+
sendResult(id, { content: [{ type: "text", text: result }] });
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
sendResult(id, { isError: true, content: [{ type: "text", text: `failed to read dir: ${err.message}` }] });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else if (params.name === "read_content") {
|
|
149
|
+
const targetPath = args.path;
|
|
150
|
+
if (typeof targetPath !== "string") {
|
|
151
|
+
sendResult(id, { isError: true, content: [{ type: "text", text: "path is required and must be a string" }] });
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (!isPathAllowed(targetPath)) {
|
|
155
|
+
sendResult(id, { isError: true, content: [{ type: "text", text: "path is outside the allowed directory (POLARIS_KB_ALLOWED_DIR)" }] });
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
const data = await fs.readFile(targetPath, 'utf8');
|
|
160
|
+
sendResult(id, { content: [{ type: "text", text: data }] });
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
sendResult(id, { isError: true, content: [{ type: "text", text: `failed to read file: ${err.message}` }] });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
sendError(id, -32601, `Tool not found: ${params.name}`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
sendError(id, -32601, "Method not found");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
catch (e) {
|
|
175
|
+
// ignore parse errors or malformed json
|
|
176
|
+
}
|
|
177
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "polarisagi-knowledge-base",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "MCP Plugin for reading local knowledge base files",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"bin": {
|
|
10
|
+
"polarisagi-knowledge-base": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"start": "node dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/node": "^20.0.0",
|
|
19
|
+
"typescript": "^5.0.0"
|
|
20
|
+
}
|
|
21
|
+
}
|